api.c 1.9 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 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. /* Test result constants and macros. */
  357. /* Test succeeded. */
  358. #define TEST_SUCCESS (1)
  359. /* Test failed. */
  360. #define TEST_FAIL (0)
  361. /* Test skipped - not run. */
  362. #define TEST_SKIPPED (-1)
  363. /* Returns the result based on whether check is true.
  364. *
  365. * @param [in] check Condition for success.
  366. * @return When condition is true: TEST_SUCCESS.
  367. * @return When condition is false: TEST_FAIL.
  368. */
  369. #ifdef DEBUG_WOLFSSL_VERBOSE
  370. #define XSTRINGIFY(s) STRINGIFY(s)
  371. #define STRINGIFY(s) #s
  372. #define TEST_RES_CHECK(check) ({ \
  373. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  374. if (_ret == TEST_FAIL) { \
  375. fprintf(stderr, " check \"%s\" at %d ", \
  376. XSTRINGIFY(check), __LINE__); \
  377. } \
  378. _ret; })
  379. #else
  380. #define TEST_RES_CHECK(check) \
  381. ((check) ? TEST_SUCCESS : TEST_FAIL)
  382. #endif /* DEBUG_WOLFSSL_VERBOSE */
  383. #define TEST_STRING "Everyone gets Friday off."
  384. #define TEST_STRING_SZ 25
  385. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  386. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  387. #define TEST_RSA_BITS 1024
  388. #else
  389. #define TEST_RSA_BITS 2048
  390. #endif
  391. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  392. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  393. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  394. static const char* bogusFile =
  395. #ifdef _WIN32
  396. "NUL"
  397. #else
  398. "/dev/null"
  399. #endif
  400. ;
  401. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  402. enum {
  403. TESTING_RSA = 1,
  404. TESTING_ECC = 2
  405. };
  406. #ifdef WOLFSSL_QNX_CAAM
  407. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  408. static int testDevId = WOLFSSL_CAAM_DEVID;
  409. #else
  410. static int testDevId = INVALID_DEVID;
  411. #endif
  412. /*----------------------------------------------------------------------------*
  413. | Setup
  414. *----------------------------------------------------------------------------*/
  415. static int test_wolfSSL_Init(void)
  416. {
  417. int result;
  418. result = wolfSSL_Init();
  419. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  420. return result;
  421. }
  422. static int test_wolfSSL_Cleanup(void)
  423. {
  424. int result;
  425. result = wolfSSL_Cleanup();
  426. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  427. return result;
  428. }
  429. /* Initialize the wolfCrypt state.
  430. * POST: 0 success.
  431. */
  432. static int test_wolfCrypt_Init(void)
  433. {
  434. int result;
  435. result = wolfCrypt_Init();
  436. result = TEST_RES_CHECK(result == 0);
  437. return result;
  438. } /* END test_wolfCrypt_Init */
  439. static int test_wolfCrypt_Cleanup(void)
  440. {
  441. int result;
  442. result = wolfCrypt_Cleanup();
  443. result = TEST_RES_CHECK(result == 0);
  444. return result;
  445. }
  446. /*----------------------------------------------------------------------------*
  447. | Platform dependent function test
  448. *----------------------------------------------------------------------------*/
  449. static int test_fileAccess(void)
  450. {
  451. int res = TEST_SKIPPED;
  452. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  453. const char *fname[] = {
  454. svrCertFile, svrKeyFile, caCertFile,
  455. eccCertFile, eccKeyFile, eccRsaCertFile,
  456. cliCertFile, cliCertDerFile, cliKeyFile,
  457. dhParamFile,
  458. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  459. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  460. NULL
  461. };
  462. const char derfile[] = "./certs/server-cert.der";
  463. XFILE f;
  464. size_t sz;
  465. byte *buff;
  466. int i;
  467. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  468. for (i=0; fname[i] != NULL ; i++) {
  469. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  470. XFCLOSE(f);
  471. }
  472. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  473. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  474. sz = (size_t) XFTELL(f);
  475. XREWIND(f);
  476. AssertTrue(sz == sizeof_server_cert_der_2048);
  477. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  478. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  479. XMEMCMP(server_cert_der_2048, buff, sz);
  480. res = TEST_RES_CHECK(1);
  481. #endif
  482. return res;
  483. }
  484. /*----------------------------------------------------------------------------*
  485. | Method Allocators
  486. *----------------------------------------------------------------------------*/
  487. static int test_wolfSSL_Method_Allocators(void)
  488. {
  489. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  490. do { \
  491. WOLFSSL_METHOD *method; \
  492. condition(method = allocator()); \
  493. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  494. } while(0)
  495. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  496. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  497. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  498. TEST_METHOD_ALLOCATOR(a, AssertNull)
  499. #ifndef NO_OLD_TLS
  500. #ifdef WOLFSSL_ALLOW_SSLV3
  501. #ifndef NO_WOLFSSL_SERVER
  502. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  503. #endif
  504. #ifndef NO_WOLFSSL_CLIENT
  505. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  506. #endif
  507. #endif
  508. #ifdef WOLFSSL_ALLOW_TLSV10
  509. #ifndef NO_WOLFSSL_SERVER
  510. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  511. #endif
  512. #ifndef NO_WOLFSSL_CLIENT
  513. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  514. #endif
  515. #endif
  516. #ifndef NO_WOLFSSL_SERVER
  517. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  518. #endif
  519. #ifndef NO_WOLFSSL_CLIENT
  520. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  521. #endif
  522. #endif /* !NO_OLD_TLS */
  523. #ifndef WOLFSSL_NO_TLS12
  524. #ifndef NO_WOLFSSL_SERVER
  525. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  526. #endif
  527. #ifndef NO_WOLFSSL_CLIENT
  528. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  529. #endif
  530. #endif /* !WOLFSSL_NO_TLS12 */
  531. #ifdef WOLFSSL_TLS13
  532. #ifndef NO_WOLFSSL_SERVER
  533. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  534. #endif
  535. #ifndef NO_WOLFSSL_CLIENT
  536. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  537. #endif
  538. #endif /* WOLFSSL_TLS13 */
  539. #ifndef NO_WOLFSSL_SERVER
  540. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  541. #endif
  542. #ifndef NO_WOLFSSL_CLIENT
  543. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  544. #endif
  545. #ifdef WOLFSSL_DTLS
  546. #ifndef NO_OLD_TLS
  547. #ifndef NO_WOLFSSL_SERVER
  548. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  549. #endif
  550. #ifndef NO_WOLFSSL_CLIENT
  551. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  552. #endif
  553. #endif
  554. #ifndef WOLFSSL_NO_TLS12
  555. #ifndef NO_WOLFSSL_SERVER
  556. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  557. #endif
  558. #ifndef NO_WOLFSSL_CLIENT
  559. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  560. #endif
  561. #endif
  562. #endif /* WOLFSSL_DTLS */
  563. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  564. /* Stubs */
  565. #ifndef NO_WOLFSSL_SERVER
  566. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  567. #endif
  568. #ifndef NO_WOLFSSL_CLIENT
  569. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  570. #endif
  571. #endif
  572. /* Test Either Method (client or server) */
  573. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  574. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  575. #ifndef NO_OLD_TLS
  576. #ifdef WOLFSSL_ALLOW_TLSV10
  577. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  578. #endif
  579. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  580. #endif /* !NO_OLD_TLS */
  581. #ifndef WOLFSSL_NO_TLS12
  582. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  583. #endif /* !WOLFSSL_NO_TLS12 */
  584. #ifdef WOLFSSL_TLS13
  585. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  586. #endif /* WOLFSSL_TLS13 */
  587. #ifdef WOLFSSL_DTLS
  588. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  589. #ifndef NO_OLD_TLS
  590. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  591. #endif /* !NO_OLD_TLS */
  592. #ifndef WOLFSSL_NO_TLS12
  593. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  594. #endif /* !WOLFSSL_NO_TLS12 */
  595. #endif /* WOLFSSL_DTLS */
  596. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  597. return TEST_SUCCESS;
  598. }
  599. /*----------------------------------------------------------------------------*
  600. | Context
  601. *----------------------------------------------------------------------------*/
  602. #ifndef NO_WOLFSSL_SERVER
  603. static int test_wolfSSL_CTX_new(void)
  604. {
  605. WOLFSSL_CTX *ctx;
  606. WOLFSSL_METHOD* method;
  607. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  608. AssertNotNull(method = wolfSSLv23_server_method());
  609. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  610. wolfSSL_CTX_free(ctx);
  611. return TEST_RES_CHECK(1);
  612. }
  613. #endif
  614. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  615. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  616. static int test_for_double_Free(void)
  617. {
  618. WOLFSSL_CTX* ctx;
  619. WOLFSSL* ssl;
  620. int skipTest = 0;
  621. const char* testCertFile;
  622. const char* testKeyFile;
  623. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  624. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  625. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  626. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  627. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  628. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  629. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  630. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  631. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  632. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  633. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  634. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  635. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  636. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  637. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  638. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  639. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  640. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  641. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  642. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  643. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  644. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  645. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  646. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  647. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  648. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  649. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  650. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  651. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  652. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  653. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  654. #ifdef OPENSSL_EXTRA
  655. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  656. "!SRP:!kRSA:!aNULL:!eNULL";
  657. #endif
  658. #ifndef NO_RSA
  659. testCertFile = svrCertFile;
  660. testKeyFile = svrKeyFile;
  661. #elif defined(HAVE_ECC)
  662. testCertFile = eccCertFile;
  663. testKeyFile = eccKeyFile;
  664. #else
  665. skipTest = 1;
  666. #endif
  667. if (skipTest != 1) {
  668. #ifndef NO_WOLFSSL_SERVER
  669. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  670. AssertNotNull(ctx);
  671. #else
  672. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  673. AssertNotNull(ctx);
  674. #endif
  675. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  676. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  677. ssl = wolfSSL_new(ctx);
  678. AssertNotNull(ssl);
  679. /* First test freeing SSL, then CTX */
  680. wolfSSL_free(ssl);
  681. wolfSSL_CTX_free(ctx);
  682. #ifndef NO_WOLFSSL_CLIENT
  683. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  684. AssertNotNull(ctx);
  685. #else
  686. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  687. AssertNotNull(ctx);
  688. #endif
  689. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  690. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  691. ssl = wolfSSL_new(ctx);
  692. AssertNotNull(ssl);
  693. /* Next test freeing CTX then SSL */
  694. wolfSSL_CTX_free(ctx);
  695. wolfSSL_free(ssl);
  696. #ifndef NO_WOLFSSL_SERVER
  697. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  698. AssertNotNull(ctx);
  699. #else
  700. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  701. AssertNotNull(ctx);
  702. #endif
  703. /* Test setting ciphers at ctx level */
  704. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  705. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  706. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  707. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  708. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  709. /* only update TLSv13 suites */
  710. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  711. #endif
  712. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  713. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  714. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  715. /* only update pre-TLSv13 suites */
  716. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  717. #endif
  718. #ifdef OPENSSL_EXTRA
  719. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  720. #endif
  721. AssertNotNull(ssl = wolfSSL_new(ctx));
  722. wolfSSL_CTX_free(ctx);
  723. wolfSSL_free(ssl);
  724. #ifndef NO_WOLFSSL_CLIENT
  725. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  726. AssertNotNull(ctx);
  727. #else
  728. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  729. AssertNotNull(ctx);
  730. #endif
  731. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  732. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  733. ssl = wolfSSL_new(ctx);
  734. AssertNotNull(ssl);
  735. /* test setting ciphers at SSL level */
  736. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  737. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  738. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  739. /* only update TLSv13 suites */
  740. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  741. #endif
  742. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  743. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  744. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  745. /* only update pre-TLSv13 suites */
  746. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  747. #endif
  748. wolfSSL_CTX_free(ctx);
  749. wolfSSL_free(ssl);
  750. }
  751. return TEST_RES_CHECK(1);
  752. }
  753. #endif
  754. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  755. {
  756. int res = TEST_SKIPPED;
  757. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  758. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  759. (!defined(NO_RSA) || defined(HAVE_ECC))
  760. const char* testCertFile;
  761. const char* testKeyFile;
  762. WOLFSSL_CTX* ctx;
  763. WOLFSSL* ssl;
  764. const byte cipherList[] =
  765. {
  766. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  767. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  768. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  769. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  770. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  771. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  772. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  773. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  774. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  775. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  776. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  777. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  778. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  779. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  780. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  781. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  782. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  783. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  784. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  785. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  786. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  787. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  788. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  789. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  790. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  791. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  792. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  793. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  794. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  795. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  796. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  797. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  798. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  799. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  800. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  801. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  802. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  803. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  804. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  805. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  806. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  807. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  808. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  809. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  810. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  811. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  812. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  813. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  814. /* SHA256 */
  815. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  816. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  817. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  818. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  819. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  820. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  821. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  822. /* SHA384 */
  823. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  824. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  825. /* AES-GCM */
  826. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  827. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  828. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  829. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  830. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  831. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  832. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  833. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  834. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  835. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  836. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  837. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  838. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  839. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  840. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  841. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  842. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  843. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  844. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  845. * also, in some of the other AES-CCM suites
  846. * there will be second byte number conflicts
  847. * with non-ECC AES-GCM */
  848. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  849. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  850. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  851. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  852. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  853. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  854. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  855. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  856. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  857. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  858. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  859. /* Camellia */
  860. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  861. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  862. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  863. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  864. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  865. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  866. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  867. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  868. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  869. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  870. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  871. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  872. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  873. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  874. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  875. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  876. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  877. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  878. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  879. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  880. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  881. /* TLS v1.3 cipher suites */
  882. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  883. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  884. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  885. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  886. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  887. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  888. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  889. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  890. };
  891. #ifndef NO_RSA
  892. testCertFile = svrCertFile;
  893. testKeyFile = svrKeyFile;
  894. #elif defined(HAVE_ECC)
  895. testCertFile = eccCertFile;
  896. testKeyFile = eccKeyFile;
  897. #endif
  898. #ifndef NO_WOLFSSL_SERVER
  899. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  900. AssertNotNull(ctx);
  901. #else
  902. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  903. AssertNotNull(ctx);
  904. #endif
  905. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  906. sizeof(cipherList)));
  907. wolfSSL_CTX_free(ctx);
  908. #ifndef NO_WOLFSSL_SERVER
  909. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  910. AssertNotNull(ctx);
  911. #else
  912. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  913. AssertNotNull(ctx);
  914. #endif
  915. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  916. WOLFSSL_FILETYPE_PEM));
  917. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  918. WOLFSSL_FILETYPE_PEM));
  919. ssl = wolfSSL_new(ctx);
  920. AssertNotNull(ssl);
  921. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  922. sizeof(cipherList)));
  923. wolfSSL_free(ssl);
  924. wolfSSL_CTX_free(ctx);
  925. res = TEST_RES_CHECK(1);
  926. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  927. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  928. return res;
  929. }
  930. static int test_wolfSSL_CTX_use_certificate_file(void)
  931. {
  932. int res = TEST_SKIPPED;
  933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  934. WOLFSSL_CTX *ctx;
  935. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  936. /* invalid context */
  937. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  938. WOLFSSL_FILETYPE_PEM));
  939. /* invalid cert file */
  940. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  941. WOLFSSL_FILETYPE_PEM));
  942. /* invalid cert type */
  943. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  944. #ifdef NO_RSA
  945. /* rsa needed */
  946. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  947. #else
  948. /* success */
  949. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  950. #endif
  951. wolfSSL_CTX_free(ctx);
  952. res = TEST_RES_CHECK(1);
  953. #endif
  954. return res;
  955. }
  956. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  957. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  958. {
  959. int res = TEST_SKIPPED;
  960. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  961. WOLFSSL_CTX* ctx;
  962. int ret;
  963. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  964. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  965. server_cert_der_2048);
  966. wolfSSL_CTX_free(ctx);
  967. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  968. #endif
  969. return res;
  970. }
  971. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  972. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  973. * context using buffer.
  974. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  975. * --enable-testcert flag.
  976. */
  977. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  978. {
  979. int res = TEST_SKIPPED;
  980. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  981. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  982. WOLFSSL_CTX* ctx;
  983. int ret;
  984. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  985. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  986. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  987. wolfSSL_CTX_free(ctx);
  988. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  989. #endif
  990. return res;
  991. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  992. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  993. {
  994. int res = TEST_SKIPPED;
  995. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  996. WOLFSSL_CTX *ctx;
  997. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  998. /* invalid context */
  999. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1000. WOLFSSL_FILETYPE_PEM));
  1001. /* invalid key file */
  1002. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1003. WOLFSSL_FILETYPE_PEM));
  1004. /* invalid key type */
  1005. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1006. /* success */
  1007. #ifdef NO_RSA
  1008. /* rsa needed */
  1009. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1010. #else
  1011. /* success */
  1012. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1013. #endif
  1014. wolfSSL_CTX_free(ctx);
  1015. res = TEST_RES_CHECK(1);
  1016. #endif
  1017. return res;
  1018. }
  1019. /* test both file and buffer versions along with unloading trusted peer certs */
  1020. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1021. {
  1022. int res = TEST_SKIPPED;
  1023. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1024. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1025. WOLFSSL_CTX *ctx;
  1026. WOLFSSL* ssl;
  1027. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1028. AssertNotNull(ssl = wolfSSL_new(ctx));
  1029. #if !defined(NO_FILESYSTEM)
  1030. /* invalid file */
  1031. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1032. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1033. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1034. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1035. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1036. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1037. /* success */
  1038. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1039. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1040. /* unload cert */
  1041. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1042. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1043. /* invalid file */
  1044. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1046. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1047. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1048. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1049. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1050. /* success */
  1051. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1052. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1053. #ifdef WOLFSSL_LOCAL_X509_STORE
  1054. /* unload cert */
  1055. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1056. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1057. #endif
  1058. #endif
  1059. /* Test of loading certs from buffers */
  1060. /* invalid buffer */
  1061. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1062. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1063. /* success */
  1064. #ifdef USE_CERT_BUFFERS_1024
  1065. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1066. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1067. #endif
  1068. #ifdef USE_CERT_BUFFERS_2048
  1069. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1070. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1071. #endif
  1072. /* unload cert */
  1073. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1074. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1075. wolfSSL_free(ssl);
  1076. wolfSSL_CTX_free(ctx);
  1077. res = TEST_RES_CHECK(1);
  1078. #endif
  1079. return res;
  1080. }
  1081. static int test_wolfSSL_CTX_load_verify_locations(void)
  1082. {
  1083. int res = TEST_SKIPPED;
  1084. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1085. WOLFSSL_CTX *ctx;
  1086. #ifndef NO_RSA
  1087. WOLFSSL_CERT_MANAGER* cm;
  1088. #ifdef PERSIST_CERT_CACHE
  1089. int cacheSz;
  1090. #endif
  1091. #endif
  1092. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1093. const char* load_certs_path = "./certs/external";
  1094. const char* load_no_certs_path = "./examples";
  1095. const char* load_expired_path = "./certs/test/expired";
  1096. #endif
  1097. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1098. /* invalid arguments */
  1099. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1100. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1101. /* invalid ca file */
  1102. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1103. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1104. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1105. (defined(WOLFSSL_QT) && \
  1106. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1107. /* invalid path */
  1108. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1109. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1110. #endif
  1111. /* load ca cert */
  1112. #ifdef NO_RSA
  1113. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1114. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1115. #else /* Skip the following test without RSA certs. */
  1116. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1117. #ifdef PERSIST_CERT_CACHE
  1118. /* Get cert cache size */
  1119. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1120. #endif
  1121. /* Test unloading CA's */
  1122. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), 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. /* load ca cert again */
  1128. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1129. /* Test getting CERT_MANAGER */
  1130. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1131. /* Test unloading CA's using CM */
  1132. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1133. #ifdef PERSIST_CERT_CACHE
  1134. /* Verify no certs (result is less than cacheSz) */
  1135. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1136. #endif
  1137. #endif
  1138. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1139. /* Test loading CA certificates using a path */
  1140. #ifdef NO_RSA
  1141. /* failure here okay since certs in external directory are RSA */
  1142. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1143. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1144. #else
  1145. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1146. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1147. #endif
  1148. /* Test loading path with no files */
  1149. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1150. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1151. /* Test loading expired CA certificates */
  1152. #ifdef NO_RSA
  1153. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1154. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1155. WOLFSSL_SUCCESS);
  1156. #else
  1157. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1158. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1159. WOLFSSL_SUCCESS);
  1160. #endif
  1161. /* Test loading CA certificates and ignoring all errors */
  1162. #ifdef NO_RSA
  1163. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1164. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1165. #else
  1166. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1167. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1168. #endif
  1169. #endif
  1170. wolfSSL_CTX_free(ctx);
  1171. res = TEST_RES_CHECK(1);
  1172. #endif
  1173. return res;
  1174. }
  1175. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1176. {
  1177. int res = TEST_SKIPPED;
  1178. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1179. (!defined(NO_RSA) || defined(HAVE_ECC))
  1180. WOLFSSL_CTX* ctx;
  1181. byte dirValid = 0;
  1182. int ret = 0;
  1183. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1184. if (ctx == NULL) {
  1185. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1186. ret = -1;
  1187. }
  1188. if (ret == 0) {
  1189. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1190. dirValid = 1;
  1191. #else
  1192. word32 numDirs;
  1193. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1194. if (caDirs == NULL || numDirs == 0) {
  1195. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1196. ret = -1;
  1197. }
  1198. else {
  1199. ReadDirCtx dirCtx;
  1200. word32 i;
  1201. for (i = 0; i < numDirs; ++i) {
  1202. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1203. /* Directory isn't empty. */
  1204. dirValid = 1;
  1205. wc_ReadDirClose(&dirCtx);
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. #endif
  1211. }
  1212. /*
  1213. * If the directory isn't empty, we should be able to load CA
  1214. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1215. * usable.
  1216. */
  1217. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1218. WOLFSSL_SUCCESS) {
  1219. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1220. ret = -1;
  1221. }
  1222. #ifdef OPENSSL_EXTRA
  1223. if (ret == 0 &&
  1224. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1225. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1226. ret = -1;
  1227. }
  1228. #endif /* OPENSSL_EXTRA */
  1229. wolfSSL_CTX_free(ctx);
  1230. res = TEST_RES_CHECK(ret == 0);
  1231. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1232. return res;
  1233. }
  1234. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1235. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1236. {
  1237. int ret;
  1238. WOLFSSL_CERT_MANAGER* cm;
  1239. cm = wolfSSL_CertManagerNew();
  1240. if (cm == NULL) {
  1241. fprintf(stderr, "test_cm_load_ca failed\n");
  1242. return -1;
  1243. }
  1244. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1245. wolfSSL_CertManagerFree(cm);
  1246. return ret;
  1247. }
  1248. static int test_cm_load_ca_file(const char* ca_cert_file)
  1249. {
  1250. int ret = 0;
  1251. byte* cert_buf = NULL;
  1252. size_t cert_sz = 0;
  1253. #if defined(WOLFSSL_PEM_TO_DER)
  1254. DerBuffer* pDer = NULL;
  1255. #endif
  1256. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1257. if (ret == 0) {
  1258. /* normal test */
  1259. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1260. if (ret == WOLFSSL_SUCCESS) {
  1261. /* test including null terminator in length */
  1262. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1263. if (tmp == NULL) {
  1264. ret = MEMORY_E;
  1265. }
  1266. else {
  1267. cert_buf = tmp;
  1268. cert_buf[cert_sz] = '\0';
  1269. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1270. WOLFSSL_FILETYPE_PEM);
  1271. }
  1272. }
  1273. #if defined(WOLFSSL_PEM_TO_DER)
  1274. if (ret == WOLFSSL_SUCCESS) {
  1275. /* test loading DER */
  1276. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1277. if (ret == 0 && pDer != NULL) {
  1278. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1279. WOLFSSL_FILETYPE_ASN1);
  1280. wc_FreeDer(&pDer);
  1281. }
  1282. }
  1283. #endif
  1284. }
  1285. free(cert_buf);
  1286. return ret;
  1287. }
  1288. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1289. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1290. {
  1291. int res = TEST_SKIPPED;
  1292. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1293. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1294. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1295. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1296. defined(HAVE_LIGHTY)
  1297. WOLFSSL_CERT_MANAGER* cm = NULL;
  1298. /* Raw OCSP response bytes captured using the following setup:
  1299. * - Run responder with
  1300. * openssl ocsp -port 9999 -ndays 9999
  1301. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1302. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1303. * -rkey certs/ocsp/ocsp-responder-key.pem
  1304. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1305. * - Run client with
  1306. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1307. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1308. * -cert certs/ocsp/server1-cert.pem
  1309. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1310. * - Copy raw response from Wireshark.
  1311. */
  1312. byte response[] = {
  1313. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1314. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1315. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1316. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1317. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1318. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1319. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1320. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1321. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1322. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1323. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1324. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1325. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1326. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1327. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1328. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1329. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1330. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1331. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1332. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1333. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1334. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1335. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1336. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1337. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1338. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1339. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1340. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1341. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1342. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1343. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1344. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1345. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1346. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1347. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1348. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1349. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1350. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1351. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1352. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1353. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1354. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1355. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1356. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1357. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1358. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1359. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1360. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1361. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1362. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1363. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1364. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1365. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1366. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1367. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1368. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1369. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1370. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1371. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1372. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1373. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1374. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1375. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1376. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1377. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1378. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1379. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1380. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1381. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1382. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1383. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1384. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1385. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1386. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1387. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1388. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1389. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1390. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1391. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1392. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1393. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1394. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1395. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1396. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1397. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1398. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1399. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1400. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1401. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1402. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1403. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1404. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1405. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1406. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1407. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1408. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1409. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1410. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1411. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1412. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1413. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1414. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1415. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1416. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1417. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1418. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1419. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1420. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1421. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1422. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1423. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1424. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1425. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1426. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1427. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1428. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1429. 0x59, 0x59, 0xa0, 0x07
  1430. };
  1431. OcspEntry entry[1];
  1432. CertStatus status[1];
  1433. OcspRequest* request;
  1434. byte serial[] = {0x05};
  1435. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1436. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1437. XMEMSET(entry, 0, sizeof(OcspEntry));
  1438. XMEMSET(status, 0, sizeof(CertStatus));
  1439. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1440. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1441. DYNAMIC_TYPE_OCSP_REQUEST);
  1442. AssertNotNull(request->serial);
  1443. request->serialSz = sizeof(serial);
  1444. XMEMCPY(request->serial, serial, sizeof(serial));
  1445. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1446. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1447. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1448. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1449. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1450. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1451. /* Response should be valid. */
  1452. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1453. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1454. /* Flip a byte in the request serial number, response should be invalid
  1455. * now. */
  1456. request->serial[0] ^= request->serial[0];
  1457. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1458. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1459. wolfSSL_OCSP_REQUEST_free(request);
  1460. wolfSSL_CertManagerFree(cm);
  1461. res = TEST_RES_CHECK(1);
  1462. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1463. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1464. #endif /* HAVE_OCSP */
  1465. return res;
  1466. }
  1467. static int test_wolfSSL_CheckOCSPResponse(void)
  1468. {
  1469. int result = TEST_SKIPPED;
  1470. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1471. const char* responseFile = "./certs/ocsp/test-response.der";
  1472. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1473. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1474. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1475. OcspResponse* res = NULL;
  1476. byte data[4096];
  1477. const unsigned char* pt;
  1478. int dataSz;
  1479. XFILE f;
  1480. WOLFSSL_OCSP_BASICRESP* bs;
  1481. WOLFSSL_X509_STORE* st;
  1482. WOLFSSL_X509* issuer;
  1483. f = XFOPEN(responseFile, "rb");
  1484. AssertTrue(f != XBADFILE);
  1485. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1486. AssertIntGT(dataSz, 0);
  1487. XFCLOSE(f);
  1488. pt = data;
  1489. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1490. AssertNotNull(res);
  1491. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1492. AssertNotNull(issuer);
  1493. st = wolfSSL_X509_STORE_new();
  1494. AssertNotNull(st);
  1495. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1496. bs = wolfSSL_OCSP_response_get1_basic(res);
  1497. AssertNotNull(bs);
  1498. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1499. wolfSSL_OCSP_BASICRESP_free(bs);
  1500. wolfSSL_OCSP_RESPONSE_free(res);
  1501. wolfSSL_X509_STORE_free(st);
  1502. wolfSSL_X509_free(issuer);
  1503. /* check loading a response with optional certs */
  1504. f = XFOPEN(responseNoInternFile, "rb");
  1505. AssertTrue(f != XBADFILE);
  1506. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1507. AssertIntGT(dataSz, 0);
  1508. XFCLOSE(f);
  1509. pt = data;
  1510. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1511. AssertNotNull(res);
  1512. wolfSSL_OCSP_RESPONSE_free(res);
  1513. /* check loading a response with multiple certs */
  1514. {
  1515. WOLFSSL_CERT_MANAGER* cm = NULL;
  1516. OcspEntry *entry;
  1517. CertStatus* status;
  1518. OcspRequest* request;
  1519. byte serial1[] = {0x01};
  1520. byte serial[] = {0x02};
  1521. byte issuerHash[] = {
  1522. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1523. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1524. 0x37, 0x15, 0x8A, 0x88
  1525. };
  1526. byte issuerKeyHash[] = {
  1527. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1528. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1529. 0x7E, 0x72, 0x15, 0x21
  1530. };
  1531. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1532. DYNAMIC_TYPE_OPENSSL);
  1533. AssertNotNull(entry);
  1534. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1535. DYNAMIC_TYPE_OPENSSL);
  1536. AssertNotNull(status);
  1537. XMEMSET(entry, 0, sizeof(OcspEntry));
  1538. XMEMSET(status, 0, sizeof(CertStatus));
  1539. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1540. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1541. DYNAMIC_TYPE_OCSP_REQUEST);
  1542. AssertNotNull(request->serial);
  1543. request->serialSz = sizeof(serial);
  1544. XMEMCPY(request->serial, serial, sizeof(serial));
  1545. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1546. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1547. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1548. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1549. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1550. WOLFSSL_SUCCESS);
  1551. f = XFOPEN(responseMultiFile, "rb");
  1552. AssertTrue(f != XBADFILE);
  1553. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1554. AssertIntGT(dataSz, 0);
  1555. XFCLOSE(f);
  1556. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1557. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1558. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1559. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1560. AssertNotNull(entry->status);
  1561. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1562. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1563. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1564. /* store both status's in the entry to check that "next" is not
  1565. * overwritten */
  1566. status->next = entry->status;
  1567. entry->status = status;
  1568. XMEMCPY(request->serial, serial, sizeof(serial));
  1569. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1570. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1571. AssertNotNull(entry->status->next);
  1572. /* compare the status found */
  1573. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1574. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1575. status->serialSz), 0);
  1576. wolfSSL_OCSP_CERTID_free(entry);
  1577. wolfSSL_OCSP_REQUEST_free(request);
  1578. wolfSSL_CertManagerFree(cm);
  1579. }
  1580. #if defined(WC_RSA_PSS)
  1581. {
  1582. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1583. /* check loading a response with RSA-PSS signature */
  1584. f = XFOPEN(responsePssFile, "rb");
  1585. AssertTrue(f != XBADFILE);
  1586. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1587. AssertIntGT(dataSz, 0);
  1588. XFCLOSE(f);
  1589. pt = data;
  1590. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1591. AssertNotNull(res);
  1592. /* try to verify the response */
  1593. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1594. AssertNotNull(issuer);
  1595. st = wolfSSL_X509_STORE_new();
  1596. AssertNotNull(st);
  1597. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1598. bs = wolfSSL_OCSP_response_get1_basic(res);
  1599. AssertNotNull(bs);
  1600. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1601. wolfSSL_OCSP_BASICRESP_free(bs);
  1602. wolfSSL_OCSP_RESPONSE_free(res);
  1603. wolfSSL_X509_STORE_free(st);
  1604. wolfSSL_X509_free(issuer);
  1605. }
  1606. #endif
  1607. result = TEST_RES_CHECK(1);
  1608. #endif /* HAVE_OCSP */
  1609. return result;
  1610. }
  1611. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1612. {
  1613. int res = TEST_SKIPPED;
  1614. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1615. const char* ca_cert = "./certs/ca-cert.pem";
  1616. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1617. int ret;
  1618. ret = test_cm_load_ca_file(ca_cert);
  1619. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1620. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1621. #elif defined(NO_RSA)
  1622. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1623. #else
  1624. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1625. #endif
  1626. ret = test_cm_load_ca_file(ca_expired_cert);
  1627. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1628. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1629. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1630. #elif defined(NO_RSA)
  1631. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1632. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1633. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1634. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1635. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1636. res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
  1637. #else
  1638. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1639. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1640. #endif
  1641. #endif
  1642. return res;
  1643. }
  1644. static int test_wolfSSL_CertManagerGetCerts(void)
  1645. {
  1646. int res = TEST_SKIPPED;
  1647. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1648. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1649. defined(WOLFSSL_SIGNER_DER_CERT)
  1650. WOLFSSL_CERT_MANAGER* cm = NULL;
  1651. WOLFSSL_STACK* sk = NULL;
  1652. X509* x509 = NULL;
  1653. X509* cert1 = NULL;
  1654. FILE* file1 = NULL;
  1655. #ifdef DEBUG_WOLFSSL_VERBOSE
  1656. WOLFSSL_BIO* bio = NULL;
  1657. #endif
  1658. int i = 0;
  1659. int ret = 0;
  1660. const byte* der;
  1661. int derSz = 0;
  1662. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1663. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1664. fclose(file1);
  1665. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1666. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1667. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1668. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1669. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1670. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1671. * and full OpenSSL compatibility */
  1672. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1673. #else
  1674. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1675. #endif
  1676. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1677. "./certs/ca-cert.pem", NULL));
  1678. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1679. for (i = 0; i < sk_X509_num(sk); i++) {
  1680. x509 = sk_X509_value(sk, i);
  1681. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1682. #ifdef DEBUG_WOLFSSL_VERBOSE
  1683. bio = BIO_new(wolfSSL_BIO_s_file());
  1684. if (bio != NULL) {
  1685. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1686. X509_print(bio, x509);
  1687. BIO_free(bio);
  1688. }
  1689. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1690. }
  1691. wolfSSL_X509_free(cert1);
  1692. sk_X509_pop_free(sk, NULL);
  1693. wolfSSL_CertManagerFree(cm);
  1694. res = TEST_RES_CHECK(1);
  1695. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1696. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1697. defined(WOLFSSL_SIGNER_DER_CERT) */
  1698. return res;
  1699. }
  1700. static int test_wolfSSL_CertManagerSetVerify(void)
  1701. {
  1702. int res = TEST_SKIPPED;
  1703. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1704. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1705. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1706. int ret = 0;
  1707. WOLFSSL_CERT_MANAGER* cm;
  1708. int tmp = myVerifyAction;
  1709. const char* ca_cert = "./certs/ca-cert.pem";
  1710. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1711. cm = wolfSSL_CertManagerNew();
  1712. AssertNotNull(cm);
  1713. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1714. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1715. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1716. AssertIntEQ(ret, -1);
  1717. #else
  1718. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1719. #endif
  1720. /* Use the test CB that always accepts certs */
  1721. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1722. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1723. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1724. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1725. {
  1726. const char* verifyCert = "./certs/server-cert.pem";
  1727. /* Use the test CB that always fails certs */
  1728. myVerifyAction = VERIFY_FORCE_FAIL;
  1729. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1730. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1731. }
  1732. #endif
  1733. wolfSSL_CertManagerFree(cm);
  1734. myVerifyAction = tmp;
  1735. res = TEST_RES_CHECK(1);
  1736. #endif
  1737. return res;
  1738. }
  1739. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1740. defined(DEBUG_UNIT_TEST_CERTS)
  1741. /* Used when debugging name constraint tests. Not static to allow use in
  1742. * multiple locations with complex define guards. */
  1743. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1744. {
  1745. BIO* out = BIO_new_file(fileName, "wb");
  1746. if (out != NULL) {
  1747. PEM_write_bio_X509(out, x509);
  1748. BIO_free(out);
  1749. }
  1750. }
  1751. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1752. {
  1753. BIO* out = BIO_new_file(fileName, "wb");
  1754. if (out != NULL) {
  1755. BIO_write(out, der, derSz);
  1756. BIO_free(out);
  1757. }
  1758. }
  1759. #else
  1760. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1761. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1762. #endif
  1763. static int test_wolfSSL_CertManagerNameConstraint(void)
  1764. {
  1765. int res = TEST_SKIPPED;
  1766. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1767. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1768. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1769. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1770. !defined(NO_SHA256)
  1771. WOLFSSL_CERT_MANAGER* cm;
  1772. WOLFSSL_EVP_PKEY *priv;
  1773. WOLFSSL_X509_NAME* name;
  1774. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1775. const char* server_cert = "./certs/test/server-goodcn.pem";
  1776. int i = 0;
  1777. static const byte extNameConsOid[] = {85, 29, 30};
  1778. RsaKey key;
  1779. WC_RNG rng;
  1780. byte *der;
  1781. int derSz;
  1782. word32 idx = 0;
  1783. byte *pt;
  1784. WOLFSSL_X509 *x509, *ca;
  1785. wc_InitRng(&rng);
  1786. /* load in CA private key for signing */
  1787. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1788. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1789. sizeof_server_key_der_2048), 0);
  1790. /* get ca certificate then alter it */
  1791. AssertNotNull(der =
  1792. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1793. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1794. WOLFSSL_FILETYPE_ASN1));
  1795. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1796. XMEMCPY(der, pt, derSz);
  1797. /* find the name constraint extension and alter it */
  1798. pt = der;
  1799. for (i = 0; i < derSz - 3; i++) {
  1800. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1801. pt += 3;
  1802. break;
  1803. }
  1804. pt++;
  1805. }
  1806. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1807. /* go to the length value and set it to 0 */
  1808. while (i < derSz && *pt != 0x81) {
  1809. pt++;
  1810. i++;
  1811. }
  1812. AssertIntNE(i, derSz); /* did not place to alter */
  1813. pt++;
  1814. *pt = 0x00;
  1815. /* resign the altered certificate */
  1816. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1817. FOURK_BUF, &key, NULL, &rng)), 0);
  1818. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1819. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1820. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1821. wolfSSL_CertManagerFree(cm);
  1822. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1823. wolfSSL_X509_free(x509);
  1824. wc_FreeRsaKey(&key);
  1825. wc_FreeRng(&rng);
  1826. /* add email alt name to satisfy constraint */
  1827. pt = (byte*)server_key_der_2048;
  1828. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1829. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1830. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1831. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1832. WOLFSSL_FILETYPE_ASN1));
  1833. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1834. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1835. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1836. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1837. /* Good cert test with proper alt email name */
  1838. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1839. WOLFSSL_FILETYPE_PEM));
  1840. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1841. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1842. AssertNotNull(name = X509_NAME_new());
  1843. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1844. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1845. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1846. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1847. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1848. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1849. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1850. X509_NAME_free(name);
  1851. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1852. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1853. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1854. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1855. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1856. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1857. wolfSSL_X509_free(x509);
  1858. /* Cert with bad alt name list */
  1859. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1860. WOLFSSL_FILETYPE_PEM));
  1861. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1862. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1863. AssertNotNull(name = X509_NAME_new());
  1864. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1865. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1866. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1867. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1868. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1869. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1870. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1871. X509_NAME_free(name);
  1872. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1873. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1874. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1875. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1876. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1877. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1878. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1879. wolfSSL_CertManagerFree(cm);
  1880. wolfSSL_X509_free(x509);
  1881. wolfSSL_X509_free(ca);
  1882. wolfSSL_EVP_PKEY_free(priv);
  1883. res = TEST_RES_CHECK(1);
  1884. #endif
  1885. return res;
  1886. }
  1887. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1888. {
  1889. int res = TEST_SKIPPED;
  1890. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1891. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1892. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1893. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1894. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1895. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1896. const char* server_cert = "./certs/server-cert.pem";
  1897. WOLFSSL_CERT_MANAGER* cm;
  1898. WOLFSSL_X509 *x509, *ca;
  1899. const unsigned char *der;
  1900. const unsigned char *pt;
  1901. WOLFSSL_EVP_PKEY *priv;
  1902. WOLFSSL_X509_NAME* name;
  1903. int derSz;
  1904. /* C=US*/
  1905. char altName[] = {
  1906. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1907. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1908. };
  1909. /* C=ID */
  1910. char altNameFail[] = {
  1911. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1912. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1913. };
  1914. /* C=US ST=California*/
  1915. char altNameExc[] = {
  1916. 0x30, 0x22,
  1917. 0x31, 0x0B,
  1918. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1919. 0x31, 0x13,
  1920. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1921. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1922. };
  1923. /* load in CA private key for signing */
  1924. pt = ca_key_der_2048;
  1925. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1926. sizeof_ca_key_der_2048));
  1927. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1928. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1929. WOLFSSL_FILETYPE_ASN1));
  1930. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1931. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1932. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1933. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1934. WOLFSSL_FILETYPE_PEM));
  1935. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1936. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1937. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1938. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1939. #else
  1940. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1941. #endif
  1942. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1943. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1944. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1945. /* add in matching DIR alt name and resign */
  1946. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1947. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1948. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1949. #else
  1950. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1951. #endif
  1952. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1953. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1954. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1955. wolfSSL_X509_free(x509);
  1956. /* check verify fail */
  1957. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1958. WOLFSSL_FILETYPE_PEM));
  1959. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1960. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1961. /* add in miss matching DIR alt name and resign */
  1962. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1963. ASN_DIR_TYPE);
  1964. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1965. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1966. #else
  1967. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1968. #endif
  1969. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1970. #ifndef WOLFSSL_NO_ASN_STRICT
  1971. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1972. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1973. #else
  1974. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1975. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1976. #endif
  1977. /* check that it still fails if one bad altname and one good altname is in
  1978. * the certificate */
  1979. wolfSSL_X509_free(x509);
  1980. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1981. WOLFSSL_FILETYPE_PEM));
  1982. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1983. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1984. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1985. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1986. ASN_DIR_TYPE);
  1987. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1988. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1989. #else
  1990. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1991. #endif
  1992. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1993. #ifndef WOLFSSL_NO_ASN_STRICT
  1994. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1995. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1996. #else
  1997. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1999. #endif
  2000. /* check it fails with switching position of bad altname */
  2001. wolfSSL_X509_free(x509);
  2002. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2003. WOLFSSL_FILETYPE_PEM));
  2004. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2005. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2006. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2007. ASN_DIR_TYPE);
  2008. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2009. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2010. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2011. #else
  2012. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2013. #endif
  2014. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2015. #ifndef WOLFSSL_NO_ASN_STRICT
  2016. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2017. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2018. #else
  2019. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2020. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2021. #endif
  2022. wolfSSL_CertManagerFree(cm);
  2023. wolfSSL_X509_free(x509);
  2024. wolfSSL_X509_free(ca);
  2025. /* now test with excluded name constraint */
  2026. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2027. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2028. WOLFSSL_FILETYPE_ASN1));
  2029. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2030. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2031. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2032. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2033. WOLFSSL_FILETYPE_PEM));
  2034. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2035. ASN_DIR_TYPE);
  2036. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2037. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2038. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2039. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2040. #else
  2041. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2042. #endif
  2043. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2044. #ifndef WOLFSSL_NO_ASN_STRICT
  2045. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2046. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2047. #else
  2048. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2049. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2050. #endif
  2051. wolfSSL_CertManagerFree(cm);
  2052. wolfSSL_X509_free(x509);
  2053. wolfSSL_X509_free(ca);
  2054. wolfSSL_EVP_PKEY_free(priv);
  2055. res = TEST_RES_CHECK(1);
  2056. #endif
  2057. return res;
  2058. }
  2059. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2060. {
  2061. int res = TEST_SKIPPED;
  2062. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2063. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2064. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2065. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2066. !defined(NO_SHA256)
  2067. WOLFSSL_CERT_MANAGER* cm;
  2068. WOLFSSL_EVP_PKEY *priv;
  2069. WOLFSSL_X509_NAME* name;
  2070. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2071. const char* server_cert = "./certs/test/server-goodcn.pem";
  2072. byte *der;
  2073. int derSz;
  2074. byte *pt;
  2075. WOLFSSL_X509 *x509, *ca;
  2076. pt = (byte*)server_key_der_2048;
  2077. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2078. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2079. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2080. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2081. WOLFSSL_FILETYPE_ASN1));
  2082. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2083. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2084. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2085. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2086. /* check satisfying .wolfssl.com constraint passes */
  2087. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2088. WOLFSSL_FILETYPE_PEM));
  2089. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2090. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2091. AssertNotNull(name = X509_NAME_new());
  2092. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2093. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2094. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2095. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2096. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2097. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2098. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2099. X509_NAME_free(name);
  2100. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2101. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2102. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2103. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2104. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2105. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2106. wolfSSL_X509_free(x509);
  2107. /* check satisfying .random.com constraint passes */
  2108. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2109. WOLFSSL_FILETYPE_PEM));
  2110. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2111. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2112. AssertNotNull(name = X509_NAME_new());
  2113. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2114. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2115. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2116. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2117. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2118. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2119. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2120. X509_NAME_free(name);
  2121. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2122. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2123. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2124. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2125. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2126. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2127. wolfSSL_X509_free(x509);
  2128. /* check fail case when neither constraint is matched */
  2129. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2130. WOLFSSL_FILETYPE_PEM));
  2131. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2132. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2133. AssertNotNull(name = X509_NAME_new());
  2134. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2135. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2136. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2137. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2138. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2139. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2140. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2141. X509_NAME_free(name);
  2142. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2143. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2144. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2145. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2146. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2147. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2148. wolfSSL_CertManagerFree(cm);
  2149. wolfSSL_X509_free(x509);
  2150. wolfSSL_X509_free(ca);
  2151. wolfSSL_EVP_PKEY_free(priv);
  2152. res = TEST_RES_CHECK(1);
  2153. #endif
  2154. return res;
  2155. }
  2156. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2157. {
  2158. int res = TEST_SKIPPED;
  2159. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2160. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2161. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2162. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2163. !defined(NO_SHA256)
  2164. WOLFSSL_CERT_MANAGER* cm;
  2165. WOLFSSL_EVP_PKEY *priv;
  2166. WOLFSSL_X509_NAME* name;
  2167. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2168. const char* server_cert = "./certs/test/server-goodcn.pem";
  2169. byte *der;
  2170. int derSz;
  2171. byte *pt;
  2172. WOLFSSL_X509 *x509, *ca;
  2173. pt = (byte*)server_key_der_2048;
  2174. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2175. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2176. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2177. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2178. WOLFSSL_FILETYPE_ASN1));
  2179. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2180. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2181. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2182. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2183. /* check satisfying wolfssl.com constraint passes */
  2184. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2185. WOLFSSL_FILETYPE_PEM));
  2186. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2187. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2188. AssertNotNull(name = X509_NAME_new());
  2189. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2190. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2191. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2192. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2193. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2194. X509_NAME_free(name);
  2195. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2196. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2197. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2198. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2199. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2200. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2201. wolfSSL_X509_free(x509);
  2202. /* check satisfying example.com constraint passes */
  2203. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2204. WOLFSSL_FILETYPE_PEM));
  2205. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2206. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2207. AssertNotNull(name = X509_NAME_new());
  2208. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2209. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2210. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2211. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2212. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2213. X509_NAME_free(name);
  2214. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2215. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2216. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2217. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2218. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2219. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2220. wolfSSL_X509_free(x509);
  2221. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2222. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2223. WOLFSSL_FILETYPE_PEM));
  2224. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2225. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2226. AssertNotNull(name = X509_NAME_new());
  2227. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2228. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2229. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2230. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2231. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2232. X509_NAME_free(name);
  2233. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2234. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2235. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2236. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2237. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2238. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2239. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2240. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2241. wolfSSL_X509_free(x509);
  2242. /* check fail when one DNS in the list is bad */
  2243. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2244. WOLFSSL_FILETYPE_PEM));
  2245. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2246. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2247. AssertNotNull(name = X509_NAME_new());
  2248. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2249. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2250. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2251. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2252. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2253. X509_NAME_free(name);
  2254. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2255. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2256. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2257. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2258. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2259. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2260. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2261. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2262. wolfSSL_X509_free(x509);
  2263. /* check fail case when neither constraint is matched */
  2264. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2265. WOLFSSL_FILETYPE_PEM));
  2266. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2267. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2268. AssertNotNull(name = X509_NAME_new());
  2269. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2270. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2271. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2272. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2273. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2274. X509_NAME_free(name);
  2275. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2276. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2277. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2278. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2279. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2280. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2281. wolfSSL_CertManagerFree(cm);
  2282. wolfSSL_X509_free(x509);
  2283. wolfSSL_X509_free(ca);
  2284. wolfSSL_EVP_PKEY_free(priv);
  2285. res = TEST_RES_CHECK(1);
  2286. #endif
  2287. return res;
  2288. }
  2289. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2290. {
  2291. int res = TEST_SKIPPED;
  2292. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2293. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2294. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2295. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2296. !defined(NO_SHA256)
  2297. WOLFSSL_CERT_MANAGER* cm;
  2298. WOLFSSL_EVP_PKEY *priv;
  2299. WOLFSSL_X509_NAME* name;
  2300. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2301. const char* server_cert = "./certs/test/server-goodcn.pem";
  2302. byte *der;
  2303. int derSz;
  2304. byte *pt;
  2305. WOLFSSL_X509 *x509, *ca;
  2306. pt = (byte*)server_key_der_2048;
  2307. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2308. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2309. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2310. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2311. WOLFSSL_FILETYPE_ASN1));
  2312. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2313. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2314. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2315. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2316. /* check satisfying wolfssl.com constraint passes */
  2317. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2318. WOLFSSL_FILETYPE_PEM));
  2319. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2320. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2321. AssertNotNull(name = X509_NAME_new());
  2322. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2323. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2324. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2325. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2326. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2327. X509_NAME_free(name);
  2328. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2329. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2330. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2331. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2332. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2333. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2334. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2335. wolfSSL_X509_free(x509);
  2336. /* fail with DNS check because of common name */
  2337. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2338. WOLFSSL_FILETYPE_PEM));
  2339. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2340. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2341. AssertNotNull(name = X509_NAME_new());
  2342. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2343. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2344. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2345. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2346. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2347. X509_NAME_free(name);
  2348. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2349. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2350. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2351. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2352. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2353. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2354. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2355. wolfSSL_X509_free(x509);
  2356. /* fail on permitted DNS name constraint */
  2357. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2358. WOLFSSL_FILETYPE_PEM));
  2359. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2360. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2361. AssertNotNull(name = X509_NAME_new());
  2362. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2363. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2364. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2365. X509_NAME_free(name);
  2366. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2367. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2368. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2369. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2370. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2371. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2372. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2373. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2374. wolfSSL_X509_free(x509);
  2375. /* fail on permitted email name constraint */
  2376. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2377. WOLFSSL_FILETYPE_PEM));
  2378. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2379. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2380. AssertNotNull(name = X509_NAME_new());
  2381. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2382. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2383. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2384. X509_NAME_free(name);
  2385. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2386. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2387. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2388. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2389. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2390. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2391. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2392. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2393. wolfSSL_X509_free(x509);
  2394. /* success with empty email name */
  2395. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2396. WOLFSSL_FILETYPE_PEM));
  2397. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2398. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2399. AssertNotNull(name = X509_NAME_new());
  2400. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2401. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2402. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2403. X509_NAME_free(name);
  2404. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2405. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2406. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2407. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2408. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2409. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2410. wolfSSL_X509_free(x509);
  2411. wolfSSL_CertManagerFree(cm);
  2412. wolfSSL_X509_free(ca);
  2413. wolfSSL_EVP_PKEY_free(priv);
  2414. res = TEST_RES_CHECK(1);
  2415. #endif
  2416. return res;
  2417. }
  2418. static int test_wolfSSL_FPKI(void)
  2419. {
  2420. int res = TEST_SKIPPED;
  2421. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2422. XFILE f;
  2423. const char* fpkiCert = "./certs/fpki-cert.der";
  2424. DecodedCert cert;
  2425. byte buf[4096];
  2426. byte* uuid;
  2427. byte* fascn;
  2428. word32 fascnSz;
  2429. word32 uuidSz;
  2430. int bytes;
  2431. f = XFOPEN(fpkiCert, "rb");
  2432. AssertTrue((f != XBADFILE));
  2433. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2434. XFCLOSE(f);
  2435. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2436. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2437. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2438. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2439. AssertNotNull(fascn);
  2440. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2441. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2442. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2443. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2444. AssertNotNull(uuid);
  2445. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2446. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2447. wc_FreeDecodedCert(&cert);
  2448. res = TEST_RES_CHECK(1);
  2449. #endif
  2450. return res;
  2451. }
  2452. /* use RID in confuncture with other names to test parsing of unknown other
  2453. * names */
  2454. static int test_wolfSSL_OtherName(void)
  2455. {
  2456. int res = TEST_SKIPPED;
  2457. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2458. XFILE f;
  2459. const char* ridCert = "./certs/rid-cert.der";
  2460. DecodedCert cert;
  2461. byte buf[4096];
  2462. int bytes;
  2463. f = XFOPEN(ridCert, "rb");
  2464. AssertTrue((f != XBADFILE));
  2465. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2466. XFCLOSE(f);
  2467. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2468. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2469. wc_FreeDecodedCert(&cert);
  2470. res = TEST_RES_CHECK(1);
  2471. #endif
  2472. return res;
  2473. }
  2474. static int test_wolfSSL_CertRsaPss(void)
  2475. {
  2476. int res = TEST_SKIPPED;
  2477. /* FIPS v2 and below don't support long salts. */
  2478. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2479. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2480. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2481. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2482. XFILE f;
  2483. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2484. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2485. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2486. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2487. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2488. #endif
  2489. DecodedCert cert;
  2490. byte buf[4096];
  2491. int bytes;
  2492. WOLFSSL_CERT_MANAGER* cm;
  2493. cm = wolfSSL_CertManagerNew();
  2494. AssertNotNull(cm);
  2495. AssertIntEQ(WOLFSSL_SUCCESS,
  2496. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2497. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2498. AssertIntEQ(WOLFSSL_SUCCESS,
  2499. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2500. #endif
  2501. f = XFOPEN(rsaPssSha256Cert, "rb");
  2502. AssertTrue((f != XBADFILE));
  2503. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2504. XFCLOSE(f);
  2505. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2506. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2507. wc_FreeDecodedCert(&cert);
  2508. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2509. RSA_MAX_SIZE >= 3072
  2510. f = XFOPEN(rsaPssSha384Cert, "rb");
  2511. AssertTrue((f != XBADFILE));
  2512. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2513. XFCLOSE(f);
  2514. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2515. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2516. wc_FreeDecodedCert(&cert);
  2517. #endif
  2518. wolfSSL_CertManagerFree(cm);
  2519. res = TEST_RES_CHECK(1);
  2520. #endif
  2521. return res;
  2522. }
  2523. static int test_wolfSSL_CertManagerCRL(void)
  2524. {
  2525. int res = TEST_SKIPPED;
  2526. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2527. !defined(NO_RSA)
  2528. const char* ca_cert = "./certs/ca-cert.pem";
  2529. const char* crl1 = "./certs/crl/crl.pem";
  2530. const char* crl2 = "./certs/crl/crl2.pem";
  2531. WOLFSSL_CERT_MANAGER* cm = NULL;
  2532. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2533. AssertIntEQ(WOLFSSL_SUCCESS,
  2534. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2535. AssertIntEQ(WOLFSSL_SUCCESS,
  2536. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2537. AssertIntEQ(WOLFSSL_SUCCESS,
  2538. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2539. wolfSSL_CertManagerFreeCRL(cm);
  2540. AssertIntEQ(WOLFSSL_SUCCESS,
  2541. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2542. AssertIntEQ(WOLFSSL_SUCCESS,
  2543. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2544. wolfSSL_CertManagerFree(cm);
  2545. res = TEST_RES_CHECK(1);
  2546. #endif
  2547. return res;
  2548. }
  2549. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2550. {
  2551. int res = TEST_SKIPPED;
  2552. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2553. !defined(NO_WOLFSSL_CLIENT)
  2554. WOLFSSL_CTX* ctx;
  2555. const char* ca_cert = "./certs/ca-cert.pem";
  2556. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2557. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2558. AssertNotNull(ctx);
  2559. /* test good CA */
  2560. AssertTrue(WOLFSSL_SUCCESS ==
  2561. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2562. WOLFSSL_LOAD_FLAG_NONE));
  2563. /* test expired CA */
  2564. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2565. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2566. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2567. #else
  2568. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2569. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2570. #endif
  2571. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2572. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2573. wolfSSL_CTX_free(ctx);
  2574. res = TEST_RES_CHECK(1);
  2575. #endif
  2576. return res;
  2577. }
  2578. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2579. {
  2580. int res = TEST_SKIPPED;
  2581. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2582. defined(USE_CERT_BUFFERS_2048)
  2583. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2584. WOLFSSL_CTX* ctx;
  2585. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2586. byte ca_expired_cert[TWOK_BUF];
  2587. word32 sizeof_ca_expired_cert;
  2588. XFILE fp;
  2589. #ifndef NO_WOLFSSL_CLIENT
  2590. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2591. #else
  2592. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2593. #endif
  2594. AssertNotNull(ctx);
  2595. /* test good CA */
  2596. AssertTrue(WOLFSSL_SUCCESS ==
  2597. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2598. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2599. WOLFSSL_LOAD_FLAG_NONE));
  2600. /* load expired CA */
  2601. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2602. fp = XFOPEN(ca_expired_cert_file, "rb");
  2603. AssertTrue(fp != XBADFILE);
  2604. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2605. sizeof(ca_expired_cert), fp);
  2606. XFCLOSE(fp);
  2607. /* test expired CA failure */
  2608. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2609. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2610. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2611. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2612. #else
  2613. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2614. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2615. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2616. #endif
  2617. /* test expired CA success */
  2618. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2619. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2620. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2621. wolfSSL_CTX_free(ctx);
  2622. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2623. res = TEST_RES_CHECK(1);
  2624. #endif
  2625. return res;
  2626. }
  2627. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2628. {
  2629. int res = TEST_SKIPPED;
  2630. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2631. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2632. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2633. WOLFSSL_CTX* ctx;
  2634. #ifndef NO_WOLFSSL_CLIENT
  2635. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2636. #else
  2637. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2638. #endif
  2639. AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2640. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2641. WOLFSSL_FILETYPE_ASN1));
  2642. wolfSSL_CTX_free(ctx);
  2643. res = TEST_RES_CHECK(1);
  2644. #endif
  2645. return res;
  2646. }
  2647. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2648. {
  2649. int res = TEST_SKIPPED;
  2650. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2651. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2652. WOLFSSL_CTX* ctx;
  2653. WOLFSSL* ssl;
  2654. const char *certChain[] = {
  2655. "./certs/intermediate/client-int-cert.pem",
  2656. "./certs/intermediate/ca-int2-cert.pem",
  2657. "./certs/intermediate/ca-int-cert.pem",
  2658. "./certs/ca-cert.pem",
  2659. NULL
  2660. };
  2661. const char** cert;
  2662. WOLFSSL_X509* x509;
  2663. WOLF_STACK_OF(X509)* chain = NULL;
  2664. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2665. AssertNotNull(ssl = wolfSSL_new(ctx));
  2666. for (cert = certChain; *cert != NULL; cert++) {
  2667. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2668. AssertNotNull(x509);
  2669. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2670. X509_free(x509);
  2671. }
  2672. for (cert = certChain; *cert != NULL; cert++) {
  2673. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2674. AssertNotNull(x509);
  2675. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2676. X509_free(x509);
  2677. }
  2678. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2679. AssertIntEQ(sk_X509_num(chain), 3);
  2680. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2681. AssertIntEQ(sk_X509_num(chain), 3);
  2682. SSL_free(ssl);
  2683. SSL_CTX_free(ctx);
  2684. res = TEST_RES_CHECK(1);
  2685. #endif
  2686. return res;
  2687. }
  2688. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2689. {
  2690. int res = TEST_SKIPPED;
  2691. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2692. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2693. const char* server_chain_der = "./certs/server-cert-chain.der";
  2694. const char* client_single_pem = "./certs/client-cert.pem";
  2695. WOLFSSL_CTX* ctx;
  2696. int ret = 0;
  2697. (void)server_chain_der;
  2698. (void)client_single_pem;
  2699. (void)ctx;
  2700. #ifndef NO_WOLFSSL_CLIENT
  2701. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2702. AssertNotNull(ctx);
  2703. #else
  2704. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2705. AssertNotNull(ctx);
  2706. #endif
  2707. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2708. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2709. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2710. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2711. wolfSSL_CTX_free(ctx);
  2712. res = TEST_RES_CHECK(ret == 0);
  2713. #endif
  2714. return res;
  2715. }
  2716. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2717. {
  2718. int res = TEST_SKIPPED;
  2719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2720. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2721. WOLFSSL_CTX *ctx;
  2722. (void)ctx;
  2723. #ifndef NO_WOLFSSL_CLIENT
  2724. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2725. #else
  2726. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2727. #endif
  2728. /* invalid context */
  2729. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2730. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2731. /* invalid dhParamFile file */
  2732. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2733. NULL, WOLFSSL_FILETYPE_PEM));
  2734. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2735. bogusFile, WOLFSSL_FILETYPE_PEM));
  2736. /* success */
  2737. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2738. WOLFSSL_FILETYPE_PEM));
  2739. wolfSSL_CTX_free(ctx);
  2740. res = TEST_RES_CHECK(1);
  2741. #endif
  2742. return res;
  2743. }
  2744. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2745. {
  2746. int res = TEST_SKIPPED;
  2747. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2748. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2749. WOLFSSL_CTX *ctx;
  2750. (void)ctx;
  2751. #ifndef NO_WOLFSSL_CLIENT
  2752. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2753. #else
  2754. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2755. #endif
  2756. /* invalid context */
  2757. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2758. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2759. /* invalid dhParamFile file */
  2760. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2761. 0, WOLFSSL_FILETYPE_ASN1));
  2762. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2763. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2764. /* success */
  2765. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2766. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2767. wolfSSL_CTX_free(ctx);
  2768. res = TEST_RES_CHECK(1);
  2769. #endif
  2770. return res;
  2771. }
  2772. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2773. {
  2774. int res = TEST_SKIPPED;
  2775. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2776. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2777. WOLFSSL_CTX *ctx;
  2778. (void)ctx;
  2779. #ifndef NO_WOLFSSL_CLIENT
  2780. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2781. AssertNotNull(ctx);
  2782. #else
  2783. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2784. AssertNotNull(ctx);
  2785. #endif
  2786. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2787. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2788. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2789. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2790. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2791. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2792. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2793. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2794. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2795. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2796. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2797. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2798. wolfSSL_CTX_free(ctx);
  2799. res = TEST_RES_CHECK(1);
  2800. #endif
  2801. return res;
  2802. }
  2803. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2804. {
  2805. int res = TEST_SKIPPED;
  2806. #if defined(WOLFSSL_DER_LOAD) && \
  2807. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2808. WOLFSSL_CTX* ctx = NULL;
  2809. const char* derCert = "./certs/server-cert.der";
  2810. const char* nullPath = NULL;
  2811. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2812. const char* emptyPath = "";
  2813. /* der load Case 1 ctx NULL */
  2814. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2815. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2816. #ifndef NO_WOLFSSL_CLIENT
  2817. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2818. #else
  2819. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2820. #endif
  2821. /* Case 2 filePath NULL */
  2822. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2823. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2824. /* Case 3 invalid format */
  2825. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2826. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2827. /* Case 4 filePath not valid */
  2828. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2829. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2830. /* Case 5 filePath empty */
  2831. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2832. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2833. #ifndef NO_RSA
  2834. /* Case 6 success case */
  2835. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2836. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2837. #endif
  2838. wolfSSL_CTX_free(ctx);
  2839. res = TEST_RES_CHECK(1);
  2840. #endif
  2841. return res;
  2842. }
  2843. static int test_wolfSSL_CTX_enable_disable(void)
  2844. {
  2845. int res = TEST_SKIPPED;
  2846. #ifndef NO_CERTS
  2847. WOLFSSL_CTX* ctx = NULL;
  2848. #ifdef HAVE_CRL
  2849. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2850. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2851. #endif
  2852. #ifdef HAVE_OCSP
  2853. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2854. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2855. #endif
  2856. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2857. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2858. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2859. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2860. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2861. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2862. #endif
  2863. #ifndef NO_WOLFSSL_CLIENT
  2864. #ifdef HAVE_EXTENDED_MASTER
  2865. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2866. #endif
  2867. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2868. AssertNotNull(ctx);
  2869. #ifdef HAVE_EXTENDED_MASTER
  2870. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2871. #endif
  2872. #elif !defined(NO_WOLFSSL_SERVER)
  2873. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2874. #else
  2875. return TEST_SUCCESS;
  2876. #endif
  2877. #ifdef HAVE_CRL
  2878. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2879. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2880. #endif
  2881. #ifdef HAVE_OCSP
  2882. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2883. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2884. WOLFSSL_SUCCESS);
  2885. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2886. WOLFSSL_SUCCESS);
  2887. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2888. WOLFSSL_SUCCESS);
  2889. #endif
  2890. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2891. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2892. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2893. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2894. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2895. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2896. #endif
  2897. wolfSSL_CTX_free(ctx);
  2898. res = TEST_RES_CHECK(1);
  2899. #endif /* NO_CERTS */
  2900. return res;
  2901. }
  2902. static int test_wolfSSL_CTX_ticket_API(void)
  2903. {
  2904. int res = TEST_SKIPPED;
  2905. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2906. WOLFSSL_CTX* ctx = NULL;
  2907. void *userCtx = (void*)"this is my ctx";
  2908. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2909. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2910. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2911. wolfSSL_CTX_free(ctx);
  2912. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2913. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2914. res = TEST_RES_CHECK(1);
  2915. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2916. return res;
  2917. }
  2918. static int test_wolfSSL_set_minmax_proto_version(void)
  2919. {
  2920. int res = TEST_SKIPPED;
  2921. #ifdef OPENSSL_EXTRA
  2922. WOLFSSL_CTX *ctx;
  2923. WOLFSSL *ssl;
  2924. int ret;
  2925. (void)ret;
  2926. (void)ssl;
  2927. #ifndef NO_WOLFSSL_CLIENT
  2928. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2929. AssertNotNull(ssl = wolfSSL_new(ctx));
  2930. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2931. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2932. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2933. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2934. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2935. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2936. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2937. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2938. wolfSSL_free(ssl);
  2939. wolfSSL_CTX_free(ctx);
  2940. #endif
  2941. #ifndef NO_WOLFSSL_SERVER
  2942. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2943. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2944. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2945. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2946. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2947. wolfSSL_CTX_free(ctx);
  2948. #endif
  2949. res = TEST_RES_CHECK(1);
  2950. #endif
  2951. return res;
  2952. }
  2953. /*----------------------------------------------------------------------------*
  2954. | SSL
  2955. *----------------------------------------------------------------------------*/
  2956. static int test_server_wolfSSL_new(void)
  2957. {
  2958. int res = TEST_SKIPPED;
  2959. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2960. !defined(NO_WOLFSSL_SERVER)
  2961. WOLFSSL_CTX *ctx;
  2962. WOLFSSL_CTX *ctx_nocert;
  2963. WOLFSSL *ssl;
  2964. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2965. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2966. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2967. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2968. /* invalid context */
  2969. AssertNull(ssl = wolfSSL_new(NULL));
  2970. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2971. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2972. #endif
  2973. /* success */
  2974. AssertNotNull(ssl = wolfSSL_new(ctx));
  2975. wolfSSL_free(ssl);
  2976. wolfSSL_CTX_free(ctx);
  2977. wolfSSL_CTX_free(ctx_nocert);
  2978. res = TEST_RES_CHECK(1);
  2979. #endif
  2980. return res;
  2981. }
  2982. static int test_client_wolfSSL_new(void)
  2983. {
  2984. int res = TEST_SKIPPED;
  2985. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2986. !defined(NO_WOLFSSL_CLIENT)
  2987. WOLFSSL_CTX *ctx;
  2988. WOLFSSL_CTX *ctx_nocert;
  2989. WOLFSSL *ssl;
  2990. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2991. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2992. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2993. /* invalid context */
  2994. AssertNull(ssl = wolfSSL_new(NULL));
  2995. /* success */
  2996. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2997. wolfSSL_free(ssl);
  2998. /* success */
  2999. AssertNotNull(ssl = wolfSSL_new(ctx));
  3000. wolfSSL_free(ssl);
  3001. wolfSSL_CTX_free(ctx);
  3002. wolfSSL_CTX_free(ctx_nocert);
  3003. res = TEST_RES_CHECK(1);
  3004. #endif
  3005. return res;
  3006. }
  3007. static int test_wolfSSL_SetTmpDH_file(void)
  3008. {
  3009. int res = TEST_SKIPPED;
  3010. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3011. !defined(NO_WOLFSSL_SERVER)
  3012. WOLFSSL_CTX *ctx;
  3013. WOLFSSL *ssl;
  3014. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3015. #ifndef NO_RSA
  3016. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3017. WOLFSSL_FILETYPE_PEM));
  3018. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3019. WOLFSSL_FILETYPE_PEM));
  3020. #elif defined(HAVE_ECC)
  3021. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3022. WOLFSSL_FILETYPE_PEM));
  3023. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3024. WOLFSSL_FILETYPE_PEM));
  3025. #elif defined(HAVE_ED25519)
  3026. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3027. WOLFSSL_FILETYPE_PEM));
  3028. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3029. WOLFSSL_FILETYPE_PEM));
  3030. #elif defined(HAVE_ED448)
  3031. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3032. WOLFSSL_FILETYPE_PEM));
  3033. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3034. WOLFSSL_FILETYPE_PEM));
  3035. #endif
  3036. AssertNotNull(ssl = wolfSSL_new(ctx));
  3037. /* invalid ssl */
  3038. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3039. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3040. /* invalid dhParamFile file */
  3041. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3042. NULL, WOLFSSL_FILETYPE_PEM));
  3043. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3044. bogusFile, WOLFSSL_FILETYPE_PEM));
  3045. /* success */
  3046. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3047. WOLFSSL_FILETYPE_PEM));
  3048. wolfSSL_free(ssl);
  3049. wolfSSL_CTX_free(ctx);
  3050. res = TEST_RES_CHECK(1);
  3051. #endif
  3052. return res;
  3053. }
  3054. static int test_wolfSSL_SetTmpDH_buffer(void)
  3055. {
  3056. int res = TEST_SKIPPED;
  3057. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3058. WOLFSSL_CTX *ctx;
  3059. WOLFSSL *ssl;
  3060. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3061. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3062. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3063. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3064. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3065. AssertNotNull(ssl = wolfSSL_new(ctx));
  3066. /* invalid ssl */
  3067. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3068. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3069. /* invalid dhParamFile file */
  3070. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3071. 0, WOLFSSL_FILETYPE_ASN1));
  3072. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3073. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3074. /* success */
  3075. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3076. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3077. wolfSSL_free(ssl);
  3078. wolfSSL_CTX_free(ctx);
  3079. res = TEST_RES_CHECK(1);
  3080. #endif
  3081. return res;
  3082. }
  3083. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3084. {
  3085. int res = TEST_SKIPPED;
  3086. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3087. WOLFSSL_CTX *ctx, *ctx2;
  3088. WOLFSSL *ssl, *ssl2;
  3089. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3090. AssertNotNull(ctx);
  3091. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3092. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3093. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3094. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3095. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3096. ssl = wolfSSL_new(ctx);
  3097. AssertNotNull(ssl);
  3098. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3099. AssertNotNull(ctx2);
  3100. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3101. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3102. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3103. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3104. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3105. ssl2 = wolfSSL_new(ctx2);
  3106. AssertNotNull(ssl2);
  3107. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3108. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3109. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3110. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3111. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3112. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3113. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3114. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3115. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3116. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3117. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3118. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3119. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3120. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3121. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3122. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3123. wolfSSL_free(ssl2);
  3124. wolfSSL_CTX_free(ctx2);
  3125. wolfSSL_free(ssl);
  3126. wolfSSL_CTX_free(ctx);
  3127. res = TEST_RES_CHECK(1);
  3128. #endif
  3129. return res;
  3130. }
  3131. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3132. * allowed.
  3133. * POST: return 1 on success.
  3134. */
  3135. static int test_wolfSSL_SetMinVersion(void)
  3136. {
  3137. int res = TEST_SKIPPED;
  3138. #ifndef NO_WOLFSSL_CLIENT
  3139. int failFlag = WOLFSSL_SUCCESS;
  3140. WOLFSSL_CTX* ctx;
  3141. WOLFSSL* ssl;
  3142. int itr;
  3143. #ifndef NO_OLD_TLS
  3144. const int versions[] = {
  3145. #ifdef WOLFSSL_ALLOW_TLSV10
  3146. WOLFSSL_TLSV1,
  3147. #endif
  3148. WOLFSSL_TLSV1_1,
  3149. WOLFSSL_TLSV1_2};
  3150. #elif !defined(WOLFSSL_NO_TLS12)
  3151. const int versions[] = { WOLFSSL_TLSV1_2 };
  3152. #else
  3153. const int versions[] = { WOLFSSL_TLSV1_3 };
  3154. #endif
  3155. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3156. ssl = wolfSSL_new(ctx);
  3157. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3158. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3159. failFlag = WOLFSSL_FAILURE;
  3160. }
  3161. }
  3162. wolfSSL_free(ssl);
  3163. wolfSSL_CTX_free(ctx);
  3164. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3165. #endif
  3166. return res;
  3167. } /* END test_wolfSSL_SetMinVersion */
  3168. /*----------------------------------------------------------------------------*
  3169. | EC
  3170. *----------------------------------------------------------------------------*/
  3171. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  3172. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  3173. */
  3174. #ifdef OPENSSL_EXTRA
  3175. static int test_wolfSSL_EC(void)
  3176. {
  3177. int res = TEST_SKIPPED;
  3178. #if !defined(WOLFSSL_SP_MATH) && \
  3179. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  3180. #if defined(HAVE_ECC)
  3181. BN_CTX *ctx;
  3182. EC_GROUP *group;
  3183. EC_GROUP *group2;
  3184. EC_POINT *Gxy, *new_point, *set_point;
  3185. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  3186. BIGNUM *X, *Y;
  3187. BIGNUM *set_point_bn;
  3188. char* hexStr;
  3189. int group_bits;
  3190. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  3191. /* NISTP256R1 Gx/Gy */
  3192. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3193. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3194. #ifndef HAVE_SELFTEST
  3195. EC_POINT *tmp;
  3196. size_t bin_len;
  3197. unsigned char* buf = NULL;
  3198. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3199. const unsigned char binUncompG[] = {
  3200. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3201. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3202. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3203. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  3204. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  3205. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  3206. };
  3207. #ifdef HAVE_COMP_KEY
  3208. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3209. const unsigned char binCompG[] = {
  3210. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3211. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3212. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3213. };
  3214. #endif
  3215. #endif
  3216. AssertNotNull(ctx = BN_CTX_new());
  3217. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  3218. AssertNotNull(group2 = EC_GROUP_dup(group));
  3219. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  3220. AssertNotNull(Gxy = EC_POINT_new(group));
  3221. AssertNotNull(new_point = EC_POINT_new(group));
  3222. AssertNotNull(set_point = EC_POINT_new(group));
  3223. AssertNotNull(X = BN_new());
  3224. AssertNotNull(Y = BN_new());
  3225. AssertNotNull(set_point_bn = BN_new());
  3226. /* load test values */
  3227. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  3228. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  3229. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  3230. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  3231. /* populate coordinates for input point */
  3232. Gxy->X = Gx;
  3233. Gxy->Y = Gy;
  3234. Gxy->Z = Gz;
  3235. #ifndef HAVE_SELFTEST
  3236. /* perform point multiplication */
  3237. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  3238. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3239. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3240. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3241. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3242. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3243. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3244. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3245. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3246. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  3247. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3248. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3249. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3250. #else
  3251. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  3252. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3253. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3254. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3255. #endif
  3256. /* check if point X coordinate is zero */
  3257. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3258. #ifdef USE_ECC_B_PARAM
  3259. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  3260. #endif /* USE_ECC_B_PARAM */
  3261. /* Force non-affine coordinates */
  3262. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3263. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  3264. new_point->inSet = 0;
  3265. /* extract the coordinates from point */
  3266. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3267. /* check if point X coordinate is zero */
  3268. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  3269. /* set the same X and Y points in another object */
  3270. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3271. /* compare points as they should be the same */
  3272. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  3273. /* Test copying */
  3274. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  3275. /* Test inverting */
  3276. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  3277. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  3278. set_point_bn, ctx), set_point_bn);
  3279. /* check bn2hex */
  3280. hexStr = BN_bn2hex(k);
  3281. AssertStrEQ(hexStr, kTest);
  3282. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  3283. BN_print_fp(stderr, k);
  3284. fprintf(stderr, "\n");
  3285. #endif
  3286. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3287. hexStr = BN_bn2hex(Gx);
  3288. AssertStrEQ(hexStr, kGx);
  3289. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  3290. BN_print_fp(stderr, Gx);
  3291. fprintf(stderr, "\n");
  3292. #endif
  3293. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3294. hexStr = BN_bn2hex(Gy);
  3295. AssertStrEQ(hexStr, kGy);
  3296. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  3297. BN_print_fp(stderr, Gy);
  3298. fprintf(stderr, "\n");
  3299. #endif
  3300. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3301. #ifndef HAVE_SELFTEST
  3302. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  3303. AssertStrEQ(hexStr, uncompG);
  3304. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3305. #ifdef HAVE_COMP_KEY
  3306. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  3307. AssertStrEQ(hexStr, compG);
  3308. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3309. #endif
  3310. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  3311. AssertIntEQ(bin_len, sizeof(binUncompG));
  3312. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3313. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  3314. bin_len, ctx), bin_len);
  3315. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  3316. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3317. #ifdef HAVE_COMP_KEY
  3318. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  3319. AssertIntEQ(bin_len, sizeof(binCompG));
  3320. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3321. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  3322. bin_len, ctx), bin_len);
  3323. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  3324. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3325. #endif
  3326. AssertNotNull(tmp = EC_POINT_new(group));
  3327. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  3328. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3329. EC_POINT_free(tmp);
  3330. #ifdef HAVE_COMP_KEY
  3331. AssertNotNull(tmp = EC_POINT_new(group));
  3332. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  3333. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3334. EC_POINT_free(tmp);
  3335. #endif
  3336. #endif
  3337. /* test BN_mod_add */
  3338. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3339. (WOLFSSL_BIGNUM*)BN_value_one(),
  3340. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  3341. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  3342. /* cleanup */
  3343. BN_free(X);
  3344. BN_free(Y);
  3345. BN_free(k);
  3346. BN_free(set_point_bn);
  3347. EC_POINT_free(new_point);
  3348. EC_POINT_free(set_point);
  3349. EC_POINT_free(Gxy);
  3350. EC_GROUP_free(group);
  3351. EC_GROUP_free(group2);
  3352. BN_CTX_free(ctx);
  3353. res = TEST_RES_CHECK(1);
  3354. #endif /* HAVE_ECC */
  3355. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  3356. return res;
  3357. }
  3358. #endif /* OPENSSL_EXTRA */
  3359. #ifndef NO_BIO
  3360. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  3361. {
  3362. int res = TEST_SKIPPED;
  3363. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  3364. EC_GROUP *group;
  3365. BIO* bio;
  3366. AssertNotNull(bio = BIO_new(BIO_s_file()));
  3367. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  3368. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  3369. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  3370. EC_GROUP_free(group);
  3371. BIO_free(bio);
  3372. res = TEST_RES_CHECK(1);
  3373. #endif /* HAVE_ECC */
  3374. return res;
  3375. }
  3376. #endif /* !NO_BIO */
  3377. # if defined(OPENSSL_EXTRA)
  3378. static int test_wolfSSL_ECDSA_SIG(void)
  3379. {
  3380. int res = TEST_SKIPPED;
  3381. #ifdef HAVE_ECC
  3382. WOLFSSL_ECDSA_SIG* sig = NULL;
  3383. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  3384. const unsigned char* cp;
  3385. unsigned char* p;
  3386. unsigned char outSig[8];
  3387. unsigned char sigData[8] =
  3388. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  3389. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  3390. AssertNull(sig);
  3391. cp = sigData;
  3392. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  3393. AssertIntEQ((cp == sigData + 8), 1);
  3394. cp = sigData;
  3395. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  3396. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  3397. AssertIntEQ((sig == sig2), 1);
  3398. cp = outSig;
  3399. p = outSig;
  3400. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  3401. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  3402. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  3403. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  3404. AssertIntEQ((p == outSig + 8), 1);
  3405. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  3406. wolfSSL_ECDSA_SIG_free(sig);
  3407. res = TEST_RES_CHECK(1);
  3408. #endif /* HAVE_ECC */
  3409. return res;
  3410. }
  3411. static int test_EC_i2d(void)
  3412. {
  3413. int res = TEST_SKIPPED;
  3414. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  3415. EC_KEY *key;
  3416. EC_KEY *copy;
  3417. int len;
  3418. unsigned char *buf = NULL;
  3419. const unsigned char *tmp = NULL;
  3420. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3421. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3422. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  3423. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  3424. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3425. buf = NULL;
  3426. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  3427. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  3428. tmp = buf;
  3429. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  3430. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3431. buf = NULL;
  3432. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  3433. tmp = buf;
  3434. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  3435. AssertIntEQ(EC_KEY_check_key(key), 1);
  3436. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  3437. EC_KEY_free(key);
  3438. EC_KEY_free(copy);
  3439. res = TEST_RES_CHECK(1);
  3440. #endif /* HAVE_ECC */
  3441. return res;
  3442. }
  3443. static int test_ECDSA_size_sign(void)
  3444. {
  3445. int res = TEST_SKIPPED;
  3446. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  3447. EC_KEY *key;
  3448. int id;
  3449. byte hash[WC_MAX_DIGEST_SIZE];
  3450. byte sig[ECC_MAX_SIG_SIZE];
  3451. unsigned int sigSz = sizeof(sig);
  3452. XMEMSET(hash, 123, sizeof(hash));
  3453. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  3454. AssertIntEQ(id, ECC_SECP256R1);
  3455. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3456. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3457. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  3458. AssertIntGE(ECDSA_size(key), sigSz);
  3459. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  3460. EC_KEY_free(key);
  3461. res = TEST_RES_CHECK(1);
  3462. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  3463. return res;
  3464. }
  3465. static int test_ED25519(void)
  3466. {
  3467. int res = TEST_SKIPPED;
  3468. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3469. defined(WOLFSSL_KEY_GEN)
  3470. byte priv[ED25519_PRV_KEY_SIZE];
  3471. unsigned int privSz = (unsigned int)sizeof(priv);
  3472. byte pub[ED25519_PUB_KEY_SIZE];
  3473. unsigned int pubSz = (unsigned int)sizeof(pub);
  3474. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3475. const char* msg = TEST_STRING;
  3476. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3477. byte sig[ED25519_SIG_SIZE];
  3478. unsigned int sigSz = (unsigned int)sizeof(sig);
  3479. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3480. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3481. WOLFSSL_SUCCESS);
  3482. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3483. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3484. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3485. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3486. &sigSz), WOLFSSL_SUCCESS);
  3487. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3488. #ifdef HAVE_ED25519_VERIFY
  3489. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3490. sigSz), WOLFSSL_SUCCESS);
  3491. #endif /* HAVE_ED25519_VERIFY */
  3492. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3493. res = TEST_RES_CHECK(1);
  3494. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3495. return res;
  3496. }
  3497. static int test_ED448(void)
  3498. {
  3499. int res = TEST_SKIPPED;
  3500. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3501. defined(WOLFSSL_KEY_GEN)
  3502. byte priv[ED448_PRV_KEY_SIZE];
  3503. unsigned int privSz = (unsigned int)sizeof(priv);
  3504. byte pub[ED448_PUB_KEY_SIZE];
  3505. unsigned int pubSz = (unsigned int)sizeof(pub);
  3506. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3507. const char* msg = TEST_STRING;
  3508. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3509. byte sig[ED448_SIG_SIZE];
  3510. unsigned int sigSz = (unsigned int)sizeof(sig);
  3511. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3512. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3513. WOLFSSL_SUCCESS);
  3514. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3515. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3516. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3517. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3518. &sigSz), WOLFSSL_SUCCESS);
  3519. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3520. #ifdef HAVE_ED448_VERIFY
  3521. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3522. sigSz), WOLFSSL_SUCCESS);
  3523. #endif /* HAVE_ED448_VERIFY */
  3524. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3525. res = TEST_RES_CHECK(1);
  3526. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3527. return res;
  3528. }
  3529. #endif /* OPENSSL_EXTRA */
  3530. #include <wolfssl/openssl/pem.h>
  3531. /*----------------------------------------------------------------------------*
  3532. | EVP
  3533. *----------------------------------------------------------------------------*/
  3534. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3535. {
  3536. int res = TEST_SKIPPED;
  3537. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3538. WOLFSSL_BIO* rbio = NULL;
  3539. WOLFSSL_BIO* wbio = NULL;
  3540. WOLFSSL_EVP_PKEY* pkey = NULL;
  3541. char line[256] = { 0 };
  3542. char line1[256] = { 0 };
  3543. int i;
  3544. /* test error cases */
  3545. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3546. /*
  3547. * test RSA public key print
  3548. * in this test, pass '3' for indent
  3549. */
  3550. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3551. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3552. sizeof_client_keypub_der_1024);
  3553. AssertNotNull(rbio);
  3554. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3555. AssertNotNull(pkey);
  3556. wbio = BIO_new(BIO_s_mem());
  3557. AssertNotNull(wbio);
  3558. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3559. BIO_gets(wbio, line, sizeof(line));
  3560. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3561. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3562. BIO_gets(wbio, line, sizeof(line));
  3563. strcpy(line1, " Modulus:\n");
  3564. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3565. BIO_gets(wbio, line, sizeof(line));
  3566. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3567. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3568. /* skip to the end of modulus element*/
  3569. for (i = 0; i < 8 ;i++) {
  3570. BIO_gets(wbio, line, sizeof(line));
  3571. }
  3572. BIO_gets(wbio, line, sizeof(line));
  3573. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3574. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3575. /* should reach EOF */
  3576. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3577. EVP_PKEY_free(pkey);
  3578. pkey = NULL;
  3579. BIO_free(rbio);
  3580. BIO_free(wbio);
  3581. rbio = NULL;
  3582. wbio = NULL;
  3583. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3584. /*
  3585. * test DSA public key print
  3586. */
  3587. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3588. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3589. sizeof_dsa_pub_key_der_2048);
  3590. AssertNotNull(rbio);
  3591. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3592. AssertNotNull(pkey);
  3593. wbio = BIO_new(BIO_s_mem());
  3594. AssertNotNull(wbio);
  3595. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3596. BIO_gets(wbio, line, sizeof(line));
  3597. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3598. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3599. BIO_gets(wbio, line, sizeof(line));
  3600. strcpy(line1, "pub:\n");
  3601. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3602. BIO_gets(wbio, line, sizeof(line));
  3603. strcpy(line1,
  3604. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3605. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3606. /* skip to the end of pub element*/
  3607. for (i = 0; i < 17 ;i++) {
  3608. BIO_gets(wbio, line, sizeof(line));
  3609. }
  3610. BIO_gets(wbio, line, sizeof(line));
  3611. strcpy(line1, "P:\n");
  3612. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3613. /* skip to the end of P element*/
  3614. for (i = 0; i < 18 ;i++) {
  3615. BIO_gets(wbio, line, sizeof(line));
  3616. }
  3617. BIO_gets(wbio, line, sizeof(line));
  3618. strcpy(line1, "Q:\n");
  3619. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3620. /* skip to the end of Q element*/
  3621. for (i = 0; i < 3 ;i++) {
  3622. BIO_gets(wbio, line, sizeof(line));
  3623. }
  3624. BIO_gets(wbio, line, sizeof(line));
  3625. strcpy(line1, "G:\n");
  3626. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3627. /* skip to the end of G element*/
  3628. for (i = 0; i < 18 ;i++) {
  3629. BIO_gets(wbio, line, sizeof(line));
  3630. }
  3631. /* should reach EOF */
  3632. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3633. EVP_PKEY_free(pkey);
  3634. pkey = NULL;
  3635. BIO_free(rbio);
  3636. BIO_free(wbio);
  3637. rbio = NULL;
  3638. wbio = NULL;
  3639. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3640. /*
  3641. * test ECC public key print
  3642. */
  3643. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3644. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3645. sizeof_ecc_clikeypub_der_256);
  3646. AssertNotNull(rbio);
  3647. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3648. AssertNotNull(pkey);
  3649. wbio = BIO_new(BIO_s_mem());
  3650. AssertNotNull(wbio);
  3651. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3652. BIO_gets(wbio, line, sizeof(line));
  3653. strcpy(line1, "Public-Key: (256 bit)\n");
  3654. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3655. BIO_gets(wbio, line, sizeof(line));
  3656. strcpy(line1, "pub:\n");
  3657. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3658. BIO_gets(wbio, line, sizeof(line));
  3659. strcpy(line1,
  3660. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3661. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3662. /* skip to the end of pub element*/
  3663. for (i = 0; i < 4 ;i++) {
  3664. BIO_gets(wbio, line, sizeof(line));
  3665. }
  3666. BIO_gets(wbio, line, sizeof(line));
  3667. strcpy(line1, "ASN1 OID: prime256v1\n");
  3668. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3669. BIO_gets(wbio, line, sizeof(line));
  3670. strcpy(line1, "NIST CURVE: P-256\n");
  3671. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3672. /* should reach EOF */
  3673. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3674. EVP_PKEY_free(pkey);
  3675. pkey = NULL;
  3676. BIO_free(rbio);
  3677. BIO_free(wbio);
  3678. rbio = NULL;
  3679. wbio = NULL;
  3680. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3681. /*
  3682. * test DH public key print
  3683. */
  3684. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3685. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3686. sizeof_dh_pub_key_der_2048);
  3687. AssertNotNull(rbio);
  3688. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3689. AssertNotNull(pkey);
  3690. wbio = BIO_new(BIO_s_mem());
  3691. AssertNotNull(wbio);
  3692. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3693. BIO_gets(wbio, line, sizeof(line));
  3694. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3695. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3696. BIO_gets(wbio, line, sizeof(line));
  3697. strcpy(line1, "public-key:\n");
  3698. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3699. BIO_gets(wbio, line, sizeof(line));
  3700. strcpy(line1,
  3701. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3702. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3703. /* skip to the end of public-key element*/
  3704. for (i = 0; i < 17 ;i++) {
  3705. BIO_gets(wbio, line, sizeof(line));
  3706. }
  3707. BIO_gets(wbio, line, sizeof(line));
  3708. strcpy(line1, "prime:\n");
  3709. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3710. BIO_gets(wbio, line, sizeof(line));
  3711. strcpy(line1,
  3712. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3713. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3714. /* skip to the end of prime element*/
  3715. for (i = 0; i < 17 ;i++) {
  3716. BIO_gets(wbio, line, sizeof(line));
  3717. }
  3718. BIO_gets(wbio, line, sizeof(line));
  3719. strcpy(line1, "generator: 2 (0x02)\n");
  3720. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3721. /* should reach EOF */
  3722. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3723. EVP_PKEY_free(pkey);
  3724. pkey = NULL;
  3725. BIO_free(rbio);
  3726. BIO_free(wbio);
  3727. rbio = NULL;
  3728. wbio = NULL;
  3729. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3730. /* to prevent "unused variable" warning */
  3731. (void)pkey;
  3732. (void)wbio;
  3733. (void)rbio;
  3734. (void)line;
  3735. (void)line1;
  3736. (void)i;
  3737. res = TEST_RES_CHECK(1);
  3738. #endif /* OPENSSL_EXTRA */
  3739. return res;
  3740. }
  3741. /* Test functions for base64 encode/decode */
  3742. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3743. {
  3744. int res = TEST_SKIPPED;
  3745. #if defined(OPENSSL_EXTRA) && \
  3746. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3747. EVP_ENCODE_CTX* ctx = NULL;
  3748. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3749. AssertIntEQ( ctx->remaining,0);
  3750. AssertIntEQ( ctx->data[0],0);
  3751. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3752. EVP_ENCODE_CTX_free(ctx);
  3753. res = TEST_RES_CHECK(1);
  3754. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3755. return res;
  3756. }
  3757. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3758. {
  3759. int res = TEST_SKIPPED;
  3760. #if defined(OPENSSL_EXTRA) && \
  3761. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3762. EVP_ENCODE_CTX* ctx = NULL;
  3763. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3764. EVP_ENCODE_CTX_free(ctx);
  3765. res = TEST_RES_CHECK(1);
  3766. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3767. return res;
  3768. }
  3769. static int test_wolfSSL_EVP_EncodeInit(void)
  3770. {
  3771. int res = TEST_SKIPPED;
  3772. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3773. EVP_ENCODE_CTX* ctx = NULL;
  3774. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3775. AssertIntEQ( ctx->remaining,0);
  3776. AssertIntEQ( ctx->data[0],0);
  3777. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3778. /* make ctx dirty */
  3779. ctx->remaining = 10;
  3780. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3781. EVP_EncodeInit(ctx);
  3782. AssertIntEQ( ctx->remaining,0);
  3783. AssertIntEQ( ctx->data[0],0);
  3784. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3785. EVP_ENCODE_CTX_free(ctx);
  3786. res = TEST_RES_CHECK(1);
  3787. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3788. return res;
  3789. }
  3790. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3791. {
  3792. int res = TEST_SKIPPED;
  3793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3794. int outl;
  3795. int total;
  3796. const unsigned char plain0[] = {"Th"};
  3797. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3798. const unsigned char plain2[] = {"This is additional data."};
  3799. const unsigned char enc0[] = {"VGg=\n"};
  3800. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3801. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3802. const unsigned char enc2[] =
  3803. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3804. unsigned char encOutBuff[300];
  3805. EVP_ENCODE_CTX* ctx = NULL;
  3806. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3807. EVP_EncodeInit(ctx);
  3808. /* illegal parameter test */
  3809. AssertIntEQ(
  3810. EVP_EncodeUpdate(
  3811. NULL, /* pass NULL as ctx */
  3812. encOutBuff,
  3813. &outl,
  3814. plain1,
  3815. sizeof(plain1)-1),
  3816. 0 /* expected result code 0: fail */
  3817. );
  3818. AssertIntEQ(
  3819. EVP_EncodeUpdate(
  3820. ctx,
  3821. NULL, /* pass NULL as out buff */
  3822. &outl,
  3823. plain1,
  3824. sizeof(plain1)-1),
  3825. 0 /* expected result code 0: fail */
  3826. );
  3827. AssertIntEQ(
  3828. EVP_EncodeUpdate(
  3829. ctx,
  3830. encOutBuff,
  3831. NULL, /* pass NULL as outl */
  3832. plain1,
  3833. sizeof(plain1)-1),
  3834. 0 /* expected result code 0: fail */
  3835. );
  3836. AssertIntEQ(
  3837. EVP_EncodeUpdate(
  3838. ctx,
  3839. encOutBuff,
  3840. &outl,
  3841. NULL, /* pass NULL as in */
  3842. sizeof(plain1)-1),
  3843. 0 /* expected result code 0: fail */
  3844. );
  3845. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3846. /* meaningless parameter test */
  3847. AssertIntEQ(
  3848. EVP_EncodeUpdate(
  3849. ctx,
  3850. encOutBuff,
  3851. &outl,
  3852. plain1,
  3853. 0), /* pass zero input */
  3854. 1 /* expected result code 1: success */
  3855. );
  3856. /* very small data encoding test */
  3857. EVP_EncodeInit(ctx);
  3858. AssertIntEQ(
  3859. EVP_EncodeUpdate(
  3860. ctx,
  3861. encOutBuff,
  3862. &outl,
  3863. plain0,
  3864. sizeof(plain0)-1),
  3865. 1 /* expected result code 1: success */
  3866. );
  3867. AssertIntEQ(outl,0);
  3868. EVP_EncodeFinal(
  3869. ctx,
  3870. encOutBuff + outl,
  3871. &outl);
  3872. AssertIntEQ( outl, sizeof(enc0)-1);
  3873. AssertIntEQ(
  3874. XSTRNCMP(
  3875. (const char*)encOutBuff,
  3876. (const char*)enc0,sizeof(enc0) ),
  3877. 0);
  3878. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3879. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3880. sizeof(enc0)-1);
  3881. AssertIntEQ(
  3882. XSTRNCMP(
  3883. (const char*)encOutBuff,
  3884. (const char*)enc0,sizeof(enc0) ),
  3885. 0);
  3886. /* pass small size( < 48bytes ) input, then make sure they are not
  3887. * encoded and just stored in ctx
  3888. */
  3889. EVP_EncodeInit(ctx);
  3890. total = 0;
  3891. outl = 0;
  3892. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3893. AssertIntEQ(
  3894. EVP_EncodeUpdate(
  3895. ctx,
  3896. encOutBuff, /* buffer for output */
  3897. &outl, /* size of output */
  3898. plain1, /* input */
  3899. sizeof(plain1)-1), /* size of input */
  3900. 1); /* expected result code 1:success */
  3901. total += outl;
  3902. AssertIntEQ(outl, 0); /* no output expected */
  3903. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3904. AssertTrue(
  3905. XSTRNCMP((const char*)(ctx->data),
  3906. (const char*)plain1,
  3907. ctx->remaining) ==0 );
  3908. AssertTrue(encOutBuff[0] == 0);
  3909. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3910. * the stored data and the new input together
  3911. */
  3912. AssertIntEQ(
  3913. EVP_EncodeUpdate(
  3914. ctx,
  3915. encOutBuff + outl, /* buffer for output */
  3916. &outl, /* size of output */
  3917. plain2, /* additional input */
  3918. sizeof(plain2) -1), /* size of additional input */
  3919. 1); /* expected result code 1:success */
  3920. total += outl;
  3921. AssertIntNE(outl, 0); /* some output is expected this time*/
  3922. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3923. AssertIntEQ(
  3924. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3925. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3926. EVP_EncodeFinal(
  3927. ctx,
  3928. encOutBuff + outl,
  3929. &outl);
  3930. total += outl;
  3931. AssertIntNE(total,0);
  3932. AssertIntNE(outl,0);
  3933. AssertIntEQ(XSTRNCMP(
  3934. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3935. /* test with illeagal parameters */
  3936. outl = 1;
  3937. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3938. AssertIntEQ(outl, 0);
  3939. outl = 1;
  3940. EVP_EncodeFinal(ctx, NULL, &outl);
  3941. AssertIntEQ(outl, 0);
  3942. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3943. EVP_EncodeFinal(NULL, NULL, NULL);
  3944. EVP_ENCODE_CTX_free(ctx);
  3945. res = TEST_RES_CHECK(1);
  3946. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3947. return res;
  3948. }
  3949. static int test_wolfSSL_EVP_EncodeFinal(void)
  3950. {
  3951. int res = TEST_SKIPPED;
  3952. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3953. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3954. * test_wolfSSL_EVP_EncodeUpdate
  3955. */
  3956. res = TEST_RES_CHECK(1);
  3957. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3958. return res;
  3959. }
  3960. static int test_wolfSSL_EVP_DecodeInit(void)
  3961. {
  3962. int res = TEST_SKIPPED;
  3963. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3964. EVP_ENCODE_CTX* ctx = NULL;
  3965. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3966. AssertIntEQ( ctx->remaining,0);
  3967. AssertIntEQ( ctx->data[0],0);
  3968. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3969. /* make ctx dirty */
  3970. ctx->remaining = 10;
  3971. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3972. EVP_DecodeInit(ctx);
  3973. AssertIntEQ( ctx->remaining,0);
  3974. AssertIntEQ( ctx->data[0],0);
  3975. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3976. EVP_ENCODE_CTX_free(ctx);
  3977. res = TEST_RES_CHECK(1);
  3978. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3979. return res;
  3980. }
  3981. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3982. {
  3983. int res = TEST_SKIPPED;
  3984. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3985. int outl;
  3986. unsigned char decOutBuff[300];
  3987. EVP_ENCODE_CTX* ctx;
  3988. static const unsigned char enc1[] =
  3989. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3990. /* const unsigned char plain1[] =
  3991. {"This is a base64 decoding test."} */
  3992. ctx = EVP_ENCODE_CTX_new();
  3993. EVP_DecodeInit(ctx);
  3994. /* illegal parameter tests */
  3995. /* pass NULL as ctx */
  3996. AssertIntEQ(
  3997. EVP_DecodeUpdate(
  3998. NULL, /* pass NULL as ctx */
  3999. decOutBuff,
  4000. &outl,
  4001. enc1,
  4002. sizeof(enc1)-1),
  4003. -1 /* expected result code -1: fail */
  4004. );
  4005. AssertIntEQ( outl, 0);
  4006. /* pass NULL as output */
  4007. AssertIntEQ(
  4008. EVP_DecodeUpdate(
  4009. ctx,
  4010. NULL, /* pass NULL as out buff */
  4011. &outl,
  4012. enc1,
  4013. sizeof(enc1)-1),
  4014. -1 /* expected result code -1: fail */
  4015. );
  4016. AssertIntEQ( outl, 0);
  4017. /* pass NULL as outl */
  4018. AssertIntEQ(
  4019. EVP_DecodeUpdate(
  4020. ctx,
  4021. decOutBuff,
  4022. NULL, /* pass NULL as outl */
  4023. enc1,
  4024. sizeof(enc1)-1),
  4025. -1 /* expected result code -1: fail */
  4026. );
  4027. /* pass NULL as input */
  4028. AssertIntEQ(
  4029. EVP_DecodeUpdate(
  4030. ctx,
  4031. decOutBuff,
  4032. &outl,
  4033. NULL, /* pass NULL as in */
  4034. sizeof(enc1)-1),
  4035. -1 /* expected result code -1: fail */
  4036. );
  4037. AssertIntEQ( outl, 0);
  4038. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4039. /* pass zero length input */
  4040. AssertIntEQ(
  4041. EVP_DecodeUpdate(
  4042. ctx,
  4043. decOutBuff,
  4044. &outl,
  4045. enc1,
  4046. 0), /* pass zero as input len */
  4047. 1 /* expected result code 1: success */
  4048. );
  4049. /* decode correct base64 string */
  4050. {
  4051. static const unsigned char enc2[] =
  4052. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4053. static const unsigned char plain2[] =
  4054. {"This is a base64 decoding test."};
  4055. EVP_EncodeInit(ctx);
  4056. AssertIntEQ(
  4057. EVP_DecodeUpdate(
  4058. ctx,
  4059. decOutBuff,
  4060. &outl,
  4061. enc2,
  4062. sizeof(enc2)-1),
  4063. 0 /* expected result code 0: success */
  4064. );
  4065. AssertIntEQ(outl,sizeof(plain2) -1);
  4066. AssertIntEQ(
  4067. EVP_DecodeFinal(
  4068. ctx,
  4069. decOutBuff + outl,
  4070. &outl),
  4071. 1 /* expected result code 1: success */
  4072. );
  4073. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  4074. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4075. sizeof(plain2) -1 ),0);
  4076. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4077. sizeof(plain2)-1);
  4078. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4079. sizeof(plain2) -1 ),0);
  4080. }
  4081. /* decode correct base64 string which does not have '\n' in its last*/
  4082. {
  4083. static const unsigned char enc3[] =
  4084. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4085. static const unsigned char plain3[] =
  4086. {"This is a base64 decoding test."}; /* 31 chars */
  4087. EVP_EncodeInit(ctx);
  4088. AssertIntEQ(
  4089. EVP_DecodeUpdate(
  4090. ctx,
  4091. decOutBuff,
  4092. &outl,
  4093. enc3,
  4094. sizeof(enc3)-1),
  4095. 0 /* expected result code 0: success */
  4096. );
  4097. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4098. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4099. sizeof(plain3) -1 ),0);
  4100. AssertIntEQ(
  4101. EVP_DecodeFinal(
  4102. ctx,
  4103. decOutBuff + outl,
  4104. &outl),
  4105. 1 /* expected result code 1: success */
  4106. );
  4107. AssertIntEQ(outl,0 );
  4108. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4109. sizeof(plain3)-1);
  4110. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4111. sizeof(plain3) -1 ),0);
  4112. }
  4113. /* decode string which has a padding char ('=') in the illegal position*/
  4114. {
  4115. static const unsigned char enc4[] =
  4116. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4117. EVP_EncodeInit(ctx);
  4118. AssertIntEQ(
  4119. EVP_DecodeUpdate(
  4120. ctx,
  4121. decOutBuff,
  4122. &outl,
  4123. enc4,
  4124. sizeof(enc4)-1),
  4125. -1 /* expected result code -1: error */
  4126. );
  4127. AssertIntEQ(outl,0);
  4128. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4129. }
  4130. /* small data decode test */
  4131. {
  4132. static const unsigned char enc00[] = {"VG"};
  4133. static const unsigned char enc01[] = {"g=\n"};
  4134. static const unsigned char plain4[] = {"Th"};
  4135. EVP_EncodeInit(ctx);
  4136. AssertIntEQ(
  4137. EVP_DecodeUpdate(
  4138. ctx,
  4139. decOutBuff,
  4140. &outl,
  4141. enc00,
  4142. sizeof(enc00)-1),
  4143. 1 /* expected result code 1: success */
  4144. );
  4145. AssertIntEQ(outl,0);
  4146. AssertIntEQ(
  4147. EVP_DecodeUpdate(
  4148. ctx,
  4149. decOutBuff + outl,
  4150. &outl,
  4151. enc01,
  4152. sizeof(enc01)-1),
  4153. 0 /* expected result code 0: success */
  4154. );
  4155. AssertIntEQ(outl,sizeof(plain4)-1);
  4156. /* test with illegal parameters */
  4157. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4158. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4159. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4160. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4161. EVP_DecodeFinal(
  4162. ctx,
  4163. decOutBuff + outl,
  4164. &outl);
  4165. AssertIntEQ( outl, 0);
  4166. AssertIntEQ(
  4167. XSTRNCMP(
  4168. (const char*)decOutBuff,
  4169. (const char*)plain4,sizeof(plain4)-1 ),
  4170. 0);
  4171. }
  4172. EVP_ENCODE_CTX_free(ctx);
  4173. res = TEST_RES_CHECK(1);
  4174. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4175. return res;
  4176. }
  4177. static int test_wolfSSL_EVP_DecodeFinal(void)
  4178. {
  4179. int res = TEST_SKIPPED;
  4180. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4181. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4182. * test_wolfSSL_EVP_DecodeUpdate
  4183. */
  4184. res = TEST_RES_CHECK(1);
  4185. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4186. return res;
  4187. }
  4188. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4189. */
  4190. #ifdef OPENSSL_EXTRA
  4191. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4192. {
  4193. #ifndef NO_AES
  4194. const WOLFSSL_EVP_CIPHER* c;
  4195. c = wolfSSL_EVP_get_cipherbynid(419);
  4196. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4197. defined(WOLFSSL_AES_128)
  4198. AssertNotNull(c);
  4199. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4200. #else
  4201. AssertNull(c);
  4202. #endif
  4203. c = wolfSSL_EVP_get_cipherbynid(423);
  4204. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4205. defined(WOLFSSL_AES_192)
  4206. AssertNotNull(c);
  4207. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4208. #else
  4209. AssertNull(c);
  4210. #endif
  4211. c = wolfSSL_EVP_get_cipherbynid(427);
  4212. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4213. defined(WOLFSSL_AES_256)
  4214. AssertNotNull(c);
  4215. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4216. #else
  4217. AssertNull(c);
  4218. #endif
  4219. c = wolfSSL_EVP_get_cipherbynid(904);
  4220. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4221. AssertNotNull(c);
  4222. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4223. #else
  4224. AssertNull(c);
  4225. #endif
  4226. c = wolfSSL_EVP_get_cipherbynid(905);
  4227. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4228. AssertNotNull(c);
  4229. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4230. #else
  4231. AssertNull(c);
  4232. #endif
  4233. c = wolfSSL_EVP_get_cipherbynid(906);
  4234. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4235. AssertNotNull(c);
  4236. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4237. #else
  4238. AssertNull(c);
  4239. #endif
  4240. c = wolfSSL_EVP_get_cipherbynid(418);
  4241. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4242. AssertNotNull(c);
  4243. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4244. #else
  4245. AssertNull(c);
  4246. #endif
  4247. c = wolfSSL_EVP_get_cipherbynid(422);
  4248. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4249. AssertNotNull(c);
  4250. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4251. #else
  4252. AssertNull(c);
  4253. #endif
  4254. c = wolfSSL_EVP_get_cipherbynid(426);
  4255. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4256. AssertNotNull(c);
  4257. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4258. #else
  4259. AssertNull(c);
  4260. #endif
  4261. #endif /* !NO_AES */
  4262. #ifndef NO_DES3
  4263. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4264. #ifdef WOLFSSL_DES_ECB
  4265. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4266. #endif
  4267. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4268. #ifdef WOLFSSL_DES_ECB
  4269. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4270. #endif
  4271. #endif /* !NO_DES3 */
  4272. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4273. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4274. #endif
  4275. /* test for nid is out of range */
  4276. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4277. return TEST_RES_CHECK(1);
  4278. }
  4279. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4280. {
  4281. int res = TEST_SKIPPED;
  4282. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4283. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4284. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4285. const EVP_CIPHER *test;
  4286. byte key[AES_BLOCK_SIZE] = {0};
  4287. byte iv[AES_BLOCK_SIZE] = {0};
  4288. AssertNotNull(ctx);
  4289. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4290. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4291. test = EVP_CIPHER_CTX_cipher(ctx);
  4292. AssertTrue(init == test);
  4293. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4294. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4295. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4296. EVP_CIPHER_CTX_free(ctx);
  4297. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4298. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4299. res = TEST_RES_CHECK(1);
  4300. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4301. return res;
  4302. }
  4303. #endif /* OPENSSL_EXTRA */
  4304. /*----------------------------------------------------------------------------*
  4305. | IO
  4306. *----------------------------------------------------------------------------*/
  4307. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  4308. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  4309. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  4310. #define HAVE_IO_TESTS_DEPENDENCIES
  4311. #endif
  4312. /* helper functions */
  4313. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4314. #ifdef WOLFSSL_SESSION_EXPORT
  4315. #ifdef WOLFSSL_DTLS
  4316. /* set up function for sending session information */
  4317. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4318. {
  4319. WOLFSSL_CTX* ctx = NULL;
  4320. WOLFSSL* ssl = NULL;
  4321. AssertNotNull(inSsl);
  4322. AssertNotNull(buf);
  4323. AssertIntNE(0, sz);
  4324. /* Set ctx to DTLS 1.2 */
  4325. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4326. AssertNotNull(ctx);
  4327. ssl = wolfSSL_new(ctx);
  4328. AssertNotNull(ssl);
  4329. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4330. wolfSSL_free(ssl);
  4331. wolfSSL_CTX_free(ctx);
  4332. (void)userCtx;
  4333. return 0;
  4334. }
  4335. #endif
  4336. /* returns negative value on fail and positive (including 0) on success */
  4337. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4338. {
  4339. int ret, err, loop_count, count, timeout = 10;
  4340. char msg[] = "I hear you fa shizzle!";
  4341. char input[1024];
  4342. loop_count = ((func_args*)args)->argc;
  4343. #ifdef WOLFSSL_ASYNC_CRYPT
  4344. err = 0; /* Reset error */
  4345. #endif
  4346. do {
  4347. #ifdef WOLFSSL_ASYNC_CRYPT
  4348. if (err == WC_PENDING_E) {
  4349. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4350. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4351. }
  4352. #endif
  4353. ret = wolfSSL_accept(ssl);
  4354. err = wolfSSL_get_error(ssl, 0);
  4355. if (err == WOLFSSL_ERROR_WANT_READ ||
  4356. err == WOLFSSL_ERROR_WANT_WRITE) {
  4357. int select_ret;
  4358. err = WC_PENDING_E;
  4359. select_ret = tcp_select(*sockfd, timeout);
  4360. if (select_ret == TEST_TIMEOUT) {
  4361. return WOLFSSL_FATAL_ERROR;
  4362. }
  4363. }
  4364. } while (err == WC_PENDING_E);
  4365. if (ret != WOLFSSL_SUCCESS) {
  4366. char buff[WOLFSSL_MAX_ERROR_SZ];
  4367. fprintf(stderr, "error = %d, %s\n", err,
  4368. wolfSSL_ERR_error_string(err, buff));
  4369. return ret;
  4370. }
  4371. for (count = 0; count < loop_count; count++) {
  4372. int select_ret;
  4373. select_ret = tcp_select(*sockfd, timeout);
  4374. if (select_ret == TEST_TIMEOUT) {
  4375. ret = WOLFSSL_FATAL_ERROR;
  4376. break;
  4377. }
  4378. do {
  4379. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4380. if (ret > 0) {
  4381. input[ret] = '\0';
  4382. fprintf(stderr, "Client message: %s\n", input);
  4383. }
  4384. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4385. do {
  4386. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4387. return WOLFSSL_FATAL_ERROR;
  4388. }
  4389. err = wolfSSL_get_error(ssl, ret);
  4390. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4391. }
  4392. return ret;
  4393. }
  4394. #endif /* WOLFSSL_SESSION_EXPORT */
  4395. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4396. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4397. defined(HAVE_AES_CBC)
  4398. typedef struct openssl_key_ctx {
  4399. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4400. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4401. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4402. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4403. } openssl_key_ctx;
  4404. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4405. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4406. static WC_INLINE int OpenSSLTicketInit(void)
  4407. {
  4408. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4409. if (ret != 0) return ret;
  4410. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4411. sizeof(myOpenSSLKey_ctx.name));
  4412. if (ret != 0) return ret;
  4413. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4414. sizeof(myOpenSSLKey_ctx.key));
  4415. if (ret != 0) return ret;
  4416. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4417. sizeof(myOpenSSLKey_ctx.hmacKey));
  4418. if (ret != 0) return ret;
  4419. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4420. sizeof(myOpenSSLKey_ctx.iv));
  4421. if (ret != 0) return ret;
  4422. return 0;
  4423. }
  4424. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4425. byte name[WOLFSSL_TICKET_NAME_SZ],
  4426. byte iv[WOLFSSL_TICKET_IV_SZ],
  4427. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4428. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4429. (void)ssl;
  4430. if (enc) {
  4431. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4432. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4433. }
  4434. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4435. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4436. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4437. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4438. return 0;
  4439. }
  4440. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4441. if (enc)
  4442. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4443. else
  4444. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4445. return 1;
  4446. }
  4447. static WC_INLINE void OpenSSLTicketCleanup(void)
  4448. {
  4449. wc_FreeRng(&myOpenSSLKey_rng);
  4450. }
  4451. #endif
  4452. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4453. #ifdef WC_SHA512_DIGEST_SIZE
  4454. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4455. #else
  4456. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4457. #endif
  4458. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4459. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4460. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4461. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4462. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4463. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4464. {
  4465. SOCKET_T sockfd = 0;
  4466. SOCKET_T clientfd = 0;
  4467. word16 port;
  4468. callback_functions* cbf;
  4469. WOLFSSL_CTX* ctx = 0;
  4470. WOLFSSL* ssl = 0;
  4471. func_args* opts = (func_args*)args;
  4472. char msg[] = "I hear you fa shizzle!";
  4473. char input[1024];
  4474. int idx;
  4475. int ret, err = 0;
  4476. int sharedCtx = 0;
  4477. int doUdp = 0;
  4478. SOCKADDR_IN_T cliAddr;
  4479. socklen_t cliLen;
  4480. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4481. size_t msg_len = 0;
  4482. #endif
  4483. #ifdef WOLFSSL_TIRTOS
  4484. fdOpenSession(Task_self());
  4485. #endif
  4486. opts->return_code = TEST_FAIL;
  4487. cbf = opts->callbacks;
  4488. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4489. if (cbf != NULL && cbf->ctx) {
  4490. ctx = cbf->ctx;
  4491. sharedCtx = 1;
  4492. }
  4493. else
  4494. #endif
  4495. {
  4496. WOLFSSL_METHOD* method = NULL;
  4497. if (cbf != NULL && cbf->method != NULL) {
  4498. method = cbf->method();
  4499. }
  4500. else {
  4501. method = wolfSSLv23_server_method();
  4502. }
  4503. ctx = wolfSSL_CTX_new(method);
  4504. }
  4505. if (ctx == NULL) {
  4506. goto done;
  4507. }
  4508. if (cbf == NULL || !cbf->ticNoInit) {
  4509. #if defined(HAVE_SESSION_TICKET) && \
  4510. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4511. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4512. OpenSSLTicketInit();
  4513. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4514. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4515. TicketInit();
  4516. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4517. #endif
  4518. #endif
  4519. }
  4520. #if defined(USE_WINDOWS_API)
  4521. port = opts->signal->port;
  4522. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4523. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4524. /* Let tcp_listen assign port */
  4525. port = 0;
  4526. #else
  4527. /* Use default port */
  4528. port = wolfSSLPort;
  4529. #endif
  4530. if (cbf != NULL)
  4531. doUdp = cbf->doUdp;
  4532. /* do it here to detect failure */
  4533. tcp_accept(
  4534. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4535. if (doUdp) {
  4536. cliLen = sizeof(cliAddr);
  4537. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4538. (struct sockaddr*)&cliAddr, &cliLen);
  4539. AssertIntGT(idx, 0);
  4540. }
  4541. else {
  4542. CloseSocket(sockfd);
  4543. }
  4544. wolfSSL_CTX_set_verify(ctx,
  4545. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4546. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4547. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4548. #endif
  4549. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4550. != WOLFSSL_SUCCESS) {
  4551. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4552. goto done;
  4553. }
  4554. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4555. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4556. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4557. #else
  4558. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4559. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4560. #endif
  4561. /*err_sys("can't load server cert chain file, "
  4562. "Please run from wolfSSL home dir");*/
  4563. goto done;
  4564. }
  4565. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4566. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4567. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4568. #else
  4569. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4570. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4571. #endif
  4572. /*err_sys("can't load server key file, "
  4573. "Please run from wolfSSL home dir");*/
  4574. goto done;
  4575. }
  4576. /* call ctx setup callback */
  4577. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4578. cbf->ctx_ready(ctx);
  4579. }
  4580. ssl = wolfSSL_new(ctx);
  4581. if (ssl == NULL) {
  4582. goto done;
  4583. }
  4584. if (doUdp) {
  4585. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4586. if (err != WOLFSSL_SUCCESS)
  4587. goto done;
  4588. }
  4589. #ifdef WOLFSSL_SESSION_EXPORT
  4590. /* only add in more complex nonblocking case with session export tests */
  4591. if (args && opts->argc > 0) {
  4592. /* set as nonblock and time out for waiting on read/write */
  4593. tcp_set_nonblocking(&clientfd);
  4594. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4595. }
  4596. #endif
  4597. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4598. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4599. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4600. #else
  4601. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  4602. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4603. #endif
  4604. /*err_sys("can't load server cert chain file, "
  4605. "Please run from wolfSSL home dir");*/
  4606. goto done;
  4607. }
  4608. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4609. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4610. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4611. #else
  4612. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4613. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4614. #endif
  4615. /*err_sys("can't load server key file, "
  4616. "Please run from wolfSSL home dir");*/
  4617. goto done;
  4618. }
  4619. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4620. /*err_sys("SSL_set_fd failed");*/
  4621. goto done;
  4622. }
  4623. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4624. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4625. #elif !defined(NO_DH)
  4626. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4627. #endif
  4628. /* call ssl setup callback */
  4629. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4630. cbf->ssl_ready(ssl);
  4631. }
  4632. #ifdef WOLFSSL_SESSION_EXPORT
  4633. /* only add in more complex nonblocking case with session export tests */
  4634. if (opts->argc > 0) {
  4635. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4636. if (ret >= 0) {
  4637. opts->return_code = TEST_SUCCESS;
  4638. }
  4639. #ifdef WOLFSSL_TIRTOS
  4640. Task_yield();
  4641. #endif
  4642. goto done;
  4643. }
  4644. #endif
  4645. #ifdef WOLFSSL_ASYNC_CRYPT
  4646. err = 0; /* Reset error */
  4647. #endif
  4648. do {
  4649. #ifdef WOLFSSL_ASYNC_CRYPT
  4650. if (err == WC_PENDING_E) {
  4651. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4652. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4653. }
  4654. #endif
  4655. ret = wolfSSL_accept(ssl);
  4656. err = wolfSSL_get_error(ssl, 0);
  4657. } while (err == WC_PENDING_E);
  4658. if (ret != WOLFSSL_SUCCESS) {
  4659. char buff[WOLFSSL_MAX_ERROR_SZ];
  4660. fprintf(stderr, "error = %d, %s\n", err,
  4661. wolfSSL_ERR_error_string(err, buff));
  4662. /*err_sys("SSL_accept failed");*/
  4663. goto done;
  4664. }
  4665. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4666. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4667. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4668. AssertIntGE(msg_len, 0);
  4669. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4670. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4671. AssertIntGE(msg_len, 0);
  4672. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4673. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4674. if (idx > 0) {
  4675. input[idx] = '\0';
  4676. fprintf(stderr, "Client message: %s\n", input);
  4677. }
  4678. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4679. /*err_sys("SSL_write failed");*/
  4680. #ifdef WOLFSSL_TIRTOS
  4681. return;
  4682. #else
  4683. return 0;
  4684. #endif
  4685. }
  4686. if (cbf != NULL && cbf->on_result != NULL)
  4687. cbf->on_result(ssl);
  4688. #ifdef WOLFSSL_TIRTOS
  4689. Task_yield();
  4690. #endif
  4691. opts->return_code = TEST_SUCCESS;
  4692. done:
  4693. if (cbf != NULL)
  4694. cbf->last_err = err;
  4695. wolfSSL_shutdown(ssl);
  4696. wolfSSL_free(ssl);
  4697. if (!sharedCtx)
  4698. wolfSSL_CTX_free(ctx);
  4699. CloseSocket(clientfd);
  4700. #ifdef WOLFSSL_TIRTOS
  4701. fdCloseSession(Task_self());
  4702. #endif
  4703. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4704. && defined(HAVE_THREAD_LS)
  4705. wc_ecc_fp_free(); /* free per thread cache */
  4706. #endif
  4707. if (cbf == NULL || !cbf->ticNoInit) {
  4708. #if defined(HAVE_SESSION_TICKET) && \
  4709. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4710. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4711. OpenSSLTicketCleanup();
  4712. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4713. TicketCleanup();
  4714. #endif
  4715. #endif
  4716. }
  4717. #ifndef WOLFSSL_TIRTOS
  4718. return 0;
  4719. #endif
  4720. }
  4721. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4722. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4723. {
  4724. SOCKET_T sockfd = 0;
  4725. SOCKET_T clientfd = 0;
  4726. word16 port;
  4727. callback_functions* cbf;
  4728. WOLFSSL_CTX* ctx = 0;
  4729. WOLFSSL* ssl = 0;
  4730. char msg[] = "I hear you fa shizzle!";
  4731. char input[1024];
  4732. int idx;
  4733. int ret, err = 0;
  4734. int sharedCtx = 0;
  4735. int loop_count = ((func_args*)args)->argc;
  4736. int count = 0;
  4737. #ifdef WOLFSSL_TIRTOS
  4738. fdOpenSession(Task_self());
  4739. #endif
  4740. ((func_args*)args)->return_code = TEST_FAIL;
  4741. cbf = ((func_args*)args)->callbacks;
  4742. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4743. if (cbf != NULL && cbf->ctx) {
  4744. ctx = cbf->ctx;
  4745. sharedCtx = 1;
  4746. }
  4747. else
  4748. #endif
  4749. {
  4750. WOLFSSL_METHOD* method = NULL;
  4751. if (cbf != NULL && cbf->method != NULL) {
  4752. method = cbf->method();
  4753. }
  4754. else {
  4755. method = wolfSSLv23_server_method();
  4756. }
  4757. ctx = wolfSSL_CTX_new(method);
  4758. }
  4759. #if defined(USE_WINDOWS_API)
  4760. port = ((func_args*)args)->signal->port;
  4761. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4762. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4763. /* Let tcp_listen assign port */
  4764. port = 0;
  4765. #else
  4766. /* Use default port */
  4767. port = wolfSSLPort;
  4768. #endif
  4769. wolfSSL_CTX_set_verify(ctx,
  4770. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4771. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4772. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4773. #endif
  4774. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4775. != WOLFSSL_SUCCESS) {
  4776. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4777. goto done;
  4778. }
  4779. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4780. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4781. /*err_sys("can't load server cert chain file, "
  4782. "Please run from wolfSSL home dir");*/
  4783. goto done;
  4784. }
  4785. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4786. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4787. /*err_sys("can't load server key file, "
  4788. "Please run from wolfSSL home dir");*/
  4789. goto done;
  4790. }
  4791. /* call ctx setup callback */
  4792. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4793. cbf->ctx_ready(ctx);
  4794. }
  4795. while (count != loop_count) {
  4796. ssl = wolfSSL_new(ctx);
  4797. if (ssl == NULL) {
  4798. goto done;
  4799. }
  4800. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4801. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4802. /*err_sys("can't load server cert chain file, "
  4803. "Please run from wolfSSL home dir");*/
  4804. goto done;
  4805. }
  4806. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4807. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4808. /*err_sys("can't load server key file, "
  4809. "Please run from wolfSSL home dir");*/
  4810. goto done;
  4811. }
  4812. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4813. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4814. #elif !defined(NO_DH)
  4815. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4816. #endif
  4817. /* call ssl setup callback */
  4818. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4819. cbf->ssl_ready(ssl);
  4820. }
  4821. /* do it here to detect failure */
  4822. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4823. CloseSocket(sockfd);
  4824. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4825. /*err_sys("SSL_set_fd failed");*/
  4826. goto done;
  4827. }
  4828. #ifdef WOLFSSL_ASYNC_CRYPT
  4829. err = 0; /* Reset error */
  4830. #endif
  4831. do {
  4832. #ifdef WOLFSSL_ASYNC_CRYPT
  4833. if (err == WC_PENDING_E) {
  4834. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4835. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4836. }
  4837. #endif
  4838. ret = wolfSSL_accept(ssl);
  4839. err = wolfSSL_get_error(ssl, 0);
  4840. } while (err == WC_PENDING_E);
  4841. if (ret != WOLFSSL_SUCCESS) {
  4842. char buff[WOLFSSL_MAX_ERROR_SZ];
  4843. fprintf(stderr, "error = %d, %s\n", err,
  4844. wolfSSL_ERR_error_string(err, buff));
  4845. /*err_sys("SSL_accept failed");*/
  4846. goto done;
  4847. }
  4848. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4849. if (idx > 0) {
  4850. input[idx] = '\0';
  4851. fprintf(stderr, "Client message: %s\n", input);
  4852. }
  4853. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4854. /*err_sys("SSL_write failed");*/
  4855. #ifdef WOLFSSL_TIRTOS
  4856. return;
  4857. #else
  4858. return 0;
  4859. #endif
  4860. }
  4861. /* free ssl for this connection */
  4862. wolfSSL_shutdown(ssl);
  4863. wolfSSL_free(ssl); ssl = NULL;
  4864. CloseSocket(clientfd);
  4865. count++;
  4866. }
  4867. #ifdef WOLFSSL_TIRTOS
  4868. Task_yield();
  4869. #endif
  4870. ((func_args*)args)->return_code = TEST_SUCCESS;
  4871. done:
  4872. if (ssl != NULL) {
  4873. wolfSSL_shutdown(ssl);
  4874. wolfSSL_free(ssl);
  4875. }
  4876. if (!sharedCtx)
  4877. wolfSSL_CTX_free(ctx);
  4878. CloseSocket(clientfd);
  4879. #ifdef WOLFSSL_TIRTOS
  4880. fdCloseSession(Task_self());
  4881. #endif
  4882. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4883. && defined(HAVE_THREAD_LS)
  4884. wc_ecc_fp_free(); /* free per thread cache */
  4885. #endif
  4886. #ifndef WOLFSSL_TIRTOS
  4887. return 0;
  4888. #endif
  4889. }
  4890. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4891. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4892. static int test_client_nofail(void* args, cbType cb)
  4893. {
  4894. #if !defined(NO_WOLFSSL_CLIENT)
  4895. SOCKET_T sockfd = 0;
  4896. callback_functions* cbf;
  4897. WOLFSSL_CTX* ctx = 0;
  4898. WOLFSSL* ssl = 0;
  4899. WOLFSSL_CIPHER* cipher;
  4900. char msg[64] = "hello wolfssl!";
  4901. char reply[1024];
  4902. int input;
  4903. int msgSz = (int)XSTRLEN(msg);
  4904. int ret, err = 0;
  4905. int cipherSuite;
  4906. int sharedCtx = 0;
  4907. int doUdp = 0;
  4908. const char* cipherName1, *cipherName2;
  4909. #ifdef WOLFSSL_TIRTOS
  4910. fdOpenSession(Task_self());
  4911. #endif
  4912. ((func_args*)args)->return_code = TEST_FAIL;
  4913. cbf = ((func_args*)args)->callbacks;
  4914. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4915. if (cbf != NULL && cbf->ctx) {
  4916. ctx = cbf->ctx;
  4917. sharedCtx = cbf->isSharedCtx;
  4918. }
  4919. else
  4920. #endif
  4921. {
  4922. WOLFSSL_METHOD* method = NULL;
  4923. if (cbf != NULL && cbf->method != NULL) {
  4924. method = cbf->method();
  4925. }
  4926. else {
  4927. method = wolfSSLv23_client_method();
  4928. }
  4929. ctx = wolfSSL_CTX_new(method);
  4930. }
  4931. if (cbf != NULL)
  4932. doUdp = cbf->doUdp;
  4933. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4934. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4935. #endif
  4936. /* Do connect here so server detects failures */
  4937. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4938. doUdp, 0, NULL);
  4939. /* Connect the socket so that we don't have to set the peer later on */
  4940. if (doUdp)
  4941. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4942. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4943. {
  4944. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4945. goto done;
  4946. }
  4947. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4948. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4949. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4950. #else
  4951. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4952. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4953. #endif
  4954. /*err_sys("can't load client cert file, "
  4955. "Please run from wolfSSL home dir");*/
  4956. goto done;
  4957. }
  4958. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4959. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4960. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4961. #else
  4962. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4964. #endif
  4965. /*err_sys("can't load client key file, "
  4966. "Please run from wolfSSL home dir");*/
  4967. goto done;
  4968. }
  4969. /* call ctx setup callback */
  4970. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4971. cbf->ctx_ready(ctx);
  4972. }
  4973. ssl = wolfSSL_new(ctx);
  4974. if (ssl == NULL) {
  4975. goto done;
  4976. }
  4977. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4978. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4979. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4980. #else
  4981. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4982. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4983. #endif
  4984. /*err_sys("can't load client cert file, "
  4985. "Please run from wolfSSL home dir");*/
  4986. goto done;
  4987. }
  4988. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4989. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4990. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4991. #else
  4992. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4993. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4994. #endif
  4995. /*err_sys("can't load client key file, "
  4996. "Please run from wolfSSL home dir");*/
  4997. goto done;
  4998. }
  4999. if (!doUdp) {
  5000. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5001. /*err_sys("SSL_set_fd failed");*/
  5002. goto done;
  5003. }
  5004. }
  5005. else {
  5006. #ifdef WOLFSSL_DTLS
  5007. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5008. /*err_sys("SSL_set_fd failed");*/
  5009. goto done;
  5010. }
  5011. #else
  5012. goto done;
  5013. #endif
  5014. }
  5015. /* call ssl setup callback */
  5016. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5017. cbf->ssl_ready(ssl);
  5018. }
  5019. #ifdef WOLFSSL_ASYNC_CRYPT
  5020. err = 0; /* Reset error */
  5021. #endif
  5022. do {
  5023. #ifdef WOLFSSL_ASYNC_CRYPT
  5024. if (err == WC_PENDING_E) {
  5025. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5026. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5027. }
  5028. #endif
  5029. ret = wolfSSL_connect(ssl);
  5030. err = wolfSSL_get_error(ssl, 0);
  5031. } while (err == WC_PENDING_E);
  5032. if (ret != WOLFSSL_SUCCESS) {
  5033. char buff[WOLFSSL_MAX_ERROR_SZ];
  5034. fprintf(stderr, "error = %d, %s\n", err,
  5035. wolfSSL_ERR_error_string(err, buff));
  5036. /*err_sys("SSL_connect failed");*/
  5037. goto done;
  5038. }
  5039. /* test the various get cipher methods */
  5040. /* Internal cipher suite names */
  5041. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5042. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5043. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5044. (cipherSuite >> 8), cipherSuite & 0xFF);
  5045. AssertStrEQ(cipherName1, cipherName2);
  5046. /* IANA Cipher Suites Names */
  5047. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5048. then it's the internal cipher suite name */
  5049. cipher = wolfSSL_get_current_cipher(ssl);
  5050. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  5051. cipherName2 = wolfSSL_get_cipher(ssl);
  5052. AssertStrEQ(cipherName1, cipherName2);
  5053. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  5054. !defined(WOLFSSL_QT)
  5055. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  5056. (cipherSuite >> 8), cipherSuite & 0xFF);
  5057. AssertStrEQ(cipherName1, cipherName2);
  5058. #endif
  5059. if (cb != NULL)
  5060. (cb)(ctx, ssl);
  5061. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5062. /*err_sys("SSL_write failed");*/
  5063. goto done;
  5064. }
  5065. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5066. if (input > 0) {
  5067. reply[input] = '\0';
  5068. fprintf(stderr, "Server response: %s\n", reply);
  5069. }
  5070. if (cbf != NULL && cbf->on_result != NULL)
  5071. cbf->on_result(ssl);
  5072. ((func_args*)args)->return_code = TEST_SUCCESS;
  5073. done:
  5074. if (cbf != NULL)
  5075. cbf->last_err = err;
  5076. wolfSSL_free(ssl);
  5077. if (!sharedCtx)
  5078. wolfSSL_CTX_free(ctx);
  5079. CloseSocket(sockfd);
  5080. #ifdef WOLFSSL_TIRTOS
  5081. fdCloseSession(Task_self());
  5082. #endif
  5083. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5084. && defined(HAVE_THREAD_LS)
  5085. wc_ecc_fp_free(); /* free per thread cache */
  5086. #endif
  5087. #else
  5088. (void)args;
  5089. (void)cb;
  5090. #endif /* !NO_WOLFSSL_CLIENT */
  5091. return 0;
  5092. }
  5093. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  5094. callback_functions* server_cb)
  5095. {
  5096. func_args client_args;
  5097. func_args server_args;
  5098. tcp_ready ready;
  5099. THREAD_TYPE serverThread;
  5100. XMEMSET(&client_args, 0, sizeof(func_args));
  5101. XMEMSET(&server_args, 0, sizeof(func_args));
  5102. #ifdef WOLFSSL_TIRTOS
  5103. fdOpenSession(Task_self());
  5104. #endif
  5105. StartTCP();
  5106. InitTcpReady(&ready);
  5107. #if defined(USE_WINDOWS_API)
  5108. /* use RNG to get random port if using windows */
  5109. ready.port = GetRandomPort();
  5110. #endif
  5111. server_args.signal = &ready;
  5112. server_args.callbacks = server_cb;
  5113. client_args.signal = &ready;
  5114. client_args.callbacks = client_cb;
  5115. start_thread(test_server_nofail, &server_args, &serverThread);
  5116. wait_tcp_ready(&server_args);
  5117. test_client_nofail(&client_args, NULL);
  5118. join_thread(serverThread);
  5119. client_cb->return_code = client_args.return_code;
  5120. server_cb->return_code = server_args.return_code;
  5121. FreeTcpReady(&ready);
  5122. #ifdef WOLFSSL_TIRTOS
  5123. fdOpenSession(Task_self());
  5124. #endif
  5125. }
  5126. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5127. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  5128. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  5129. {
  5130. SOCKET_T sockfd = 0;
  5131. callback_functions* cbf;
  5132. WOLFSSL_CTX* ctx = 0;
  5133. WOLFSSL* ssl = 0;
  5134. WOLFSSL_SESSION* session = NULL;
  5135. char msg[64] = "hello wolfssl!";
  5136. char reply[1024];
  5137. int input;
  5138. int msgSz = (int)XSTRLEN(msg);
  5139. int ret, err = 0;
  5140. int sharedCtx = 0;
  5141. #ifdef WOLFSSL_TIRTOS
  5142. fdOpenSession(Task_self());
  5143. #endif
  5144. ((func_args*)args)->return_code = TEST_FAIL;
  5145. cbf = ((func_args*)args)->callbacks;
  5146. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5147. if (cbf != NULL && cbf->ctx) {
  5148. ctx = cbf->ctx;
  5149. sharedCtx = 1;
  5150. }
  5151. else
  5152. #endif
  5153. {
  5154. WOLFSSL_METHOD* method = NULL;
  5155. if (cbf != NULL && cbf->method != NULL) {
  5156. method = cbf->method();
  5157. }
  5158. else {
  5159. method = wolfSSLv23_client_method();
  5160. }
  5161. ctx = wolfSSL_CTX_new(method);
  5162. }
  5163. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5164. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5165. #endif
  5166. /* Do connect here so server detects failures */
  5167. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5168. 0, 0, NULL);
  5169. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5170. {
  5171. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5172. goto done;
  5173. }
  5174. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5175. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5176. /*err_sys("can't load client cert file, "
  5177. "Please run from wolfSSL home dir");*/
  5178. goto done;
  5179. }
  5180. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5181. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5182. /*err_sys("can't load client key file, "
  5183. "Please run from wolfSSL home dir");*/
  5184. goto done;
  5185. }
  5186. /* call ctx setup callback */
  5187. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5188. cbf->ctx_ready(ctx);
  5189. }
  5190. ssl = wolfSSL_new(ctx);
  5191. if (ssl == NULL) {
  5192. goto done;
  5193. }
  5194. /* keep handshakre resources for re-using WOLFSSL obj */
  5195. wolfSSL_KeepArrays(ssl);
  5196. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5197. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5198. goto done;
  5199. }
  5200. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5201. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5202. /*err_sys("can't load client cert file, "
  5203. "Please run from wolfSSL home dir");*/
  5204. goto done;
  5205. }
  5206. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5207. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5208. /*err_sys("can't load client key file, "
  5209. "Please run from wolfSSL home dir");*/
  5210. goto done;
  5211. }
  5212. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5213. /*err_sys("SSL_set_fd failed");*/
  5214. goto done;
  5215. }
  5216. /* call ssl setup callback */
  5217. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5218. cbf->ssl_ready(ssl);
  5219. }
  5220. #ifdef WOLFSSL_ASYNC_CRYPT
  5221. err = 0; /* Reset error */
  5222. #endif
  5223. do {
  5224. #ifdef WOLFSSL_ASYNC_CRYPT
  5225. if (err == WC_PENDING_E) {
  5226. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5227. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5228. }
  5229. #endif
  5230. ret = wolfSSL_connect(ssl);
  5231. err = wolfSSL_get_error(ssl, 0);
  5232. } while (err == WC_PENDING_E);
  5233. if (ret != WOLFSSL_SUCCESS) {
  5234. char buff[WOLFSSL_MAX_ERROR_SZ];
  5235. fprintf(stderr, "error = %d, %s\n", err,
  5236. wolfSSL_ERR_error_string(err, buff));
  5237. /*err_sys("SSL_connect failed");*/
  5238. goto done;
  5239. }
  5240. /* Build first session */
  5241. if (cb != NULL)
  5242. ((cbType)cb)(ctx, ssl);
  5243. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5244. /*err_sys("SSL_write failed");*/
  5245. goto done;
  5246. }
  5247. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5248. if (input > 0) {
  5249. reply[input] = '\0';
  5250. fprintf(stderr, "Server response: %s\n", reply);
  5251. }
  5252. /* Session Resumption by re-using WOLFSSL object */
  5253. wolfSSL_set_quiet_shutdown(ssl, 1);
  5254. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5255. /* err_sys ("SSL shutdown failed"); */
  5256. goto done;
  5257. }
  5258. session = wolfSSL_get1_session(ssl);
  5259. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5260. /* err_sys ("SSL_clear failed"); */
  5261. goto done;
  5262. }
  5263. wolfSSL_set_session(ssl, session);
  5264. wolfSSL_SESSION_free(session);
  5265. session = NULL;
  5266. /* close socket once */
  5267. CloseSocket(sockfd);
  5268. sockfd = 0;
  5269. /* wait until server ready */
  5270. wait_tcp_ready((func_args*)server_args);
  5271. fprintf(stderr, "session resumption\n");
  5272. /* Do re-connect */
  5273. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5274. 0, 0, NULL);
  5275. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5276. /*err_sys("SSL_set_fd failed");*/
  5277. goto done;
  5278. }
  5279. #ifdef WOLFSSL_ASYNC_CRYPT
  5280. err = 0; /* Reset error */
  5281. #endif
  5282. do {
  5283. #ifdef WOLFSSL_ASYNC_CRYPT
  5284. if (err == WC_PENDING_E) {
  5285. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5286. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5287. }
  5288. #endif
  5289. ret = wolfSSL_connect(ssl);
  5290. err = wolfSSL_get_error(ssl, 0);
  5291. } while (err == WC_PENDING_E);
  5292. if (ret != WOLFSSL_SUCCESS) {
  5293. char buff[WOLFSSL_MAX_ERROR_SZ];
  5294. fprintf(stderr, "error = %d, %s\n", err,
  5295. wolfSSL_ERR_error_string(err, buff));
  5296. /*err_sys("SSL_connect failed");*/
  5297. goto done;
  5298. }
  5299. /* Build first session */
  5300. if (cb != NULL)
  5301. ((cbType)cb)(ctx, ssl);
  5302. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5303. /*err_sys("SSL_write failed");*/
  5304. goto done;
  5305. }
  5306. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5307. if (input > 0) {
  5308. reply[input] = '\0';
  5309. fprintf(stderr, "Server response: %s\n", reply);
  5310. }
  5311. ((func_args*)args)->return_code = TEST_SUCCESS;
  5312. done:
  5313. wolfSSL_free(ssl);
  5314. if (!sharedCtx)
  5315. wolfSSL_CTX_free(ctx);
  5316. CloseSocket(sockfd);
  5317. #ifdef WOLFSSL_TIRTOS
  5318. fdCloseSession(Task_self());
  5319. #endif
  5320. return;
  5321. }
  5322. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5323. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5324. static int test_client_verifyDepth(void* args)
  5325. {
  5326. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5327. SOCKET_T sockfd = 0;
  5328. callback_functions* cbf;
  5329. WOLFSSL_CTX* ctx = 0;
  5330. WOLFSSL* ssl = 0;
  5331. char msg[64] = "hello wolfssl!";
  5332. char reply[1024];
  5333. int input;
  5334. int msgSz = (int)XSTRLEN(msg);
  5335. int ret, err = 0;
  5336. int verify_depth = ((func_args*)args)->argc;
  5337. ((func_args*)args)->return_code = TEST_FAIL;
  5338. cbf = ((func_args*)args)->callbacks;
  5339. {
  5340. WOLFSSL_METHOD* method = NULL;
  5341. if (cbf != NULL && cbf->method != NULL) {
  5342. method = cbf->method();
  5343. }
  5344. else {
  5345. method = wolfSSLv23_client_method();
  5346. }
  5347. ctx = wolfSSL_CTX_new(method);
  5348. }
  5349. /* Do connect here so server detects failures */
  5350. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5351. 0, 0, NULL);
  5352. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5353. != WOLFSSL_SUCCESS)
  5354. {
  5355. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5356. goto done;
  5357. }
  5358. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5359. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5360. /*err_sys("can't load client cert file, "
  5361. "Please run from wolfSSL home dir");*/
  5362. goto done;
  5363. }
  5364. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5365. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5366. /*err_sys("can't load client key file, "
  5367. "Please run from wolfSSL home dir");*/
  5368. goto done;
  5369. }
  5370. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5371. /* set verify depth */
  5372. if (verify_depth == 0) {
  5373. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5374. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5375. }
  5376. else if (verify_depth == -1) {
  5377. myVerifyAction = VERIFY_USE_PREVERFIY;
  5378. SSL_CTX_set_verify_depth(ctx, 0);
  5379. }
  5380. else if (verify_depth > 0) {
  5381. myVerifyAction = VERIFY_USE_PREVERFIY;
  5382. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5383. }
  5384. ssl = wolfSSL_new(ctx);
  5385. if (ssl == NULL) {
  5386. goto done;
  5387. }
  5388. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5389. /*err_sys("SSL_set_fd failed");*/
  5390. goto done;
  5391. }
  5392. #ifdef WOLFSSL_ASYNC_CRYPT
  5393. err = 0; /* Reset error */
  5394. #endif
  5395. do {
  5396. #ifdef WOLFSSL_ASYNC_CRYPT
  5397. if (err == WC_PENDING_E) {
  5398. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5399. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5400. }
  5401. #endif
  5402. ret = wolfSSL_connect(ssl);
  5403. err = wolfSSL_get_error(ssl, 0);
  5404. } while (err == WC_PENDING_E);
  5405. if (ret != WOLFSSL_SUCCESS) {
  5406. char buff[WOLFSSL_MAX_ERROR_SZ];
  5407. fprintf(stderr, "error = %d, %s\n", err,
  5408. wolfSSL_ERR_error_string(err, buff));
  5409. goto done;
  5410. }
  5411. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5412. goto done;
  5413. }
  5414. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5415. if (input > 0) {
  5416. reply[input] = '\0';
  5417. fprintf(stderr, "Server response: %s\n", reply);
  5418. }
  5419. ((func_args*)args)->return_code = TEST_SUCCESS;
  5420. done:
  5421. wolfSSL_free(ssl);
  5422. wolfSSL_CTX_free(ctx);
  5423. CloseSocket(sockfd);
  5424. #else
  5425. (void)args;
  5426. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5427. return 0;
  5428. }
  5429. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5430. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5431. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5432. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5433. #define HAVE_ALPN_PROTOS_SUPPORT
  5434. #endif
  5435. /* Generic TLS client / server with callbacks for API unit tests
  5436. * Used by SNI / ALPN / crypto callback helper functions */
  5437. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5438. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5439. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5440. #define ENABLE_TLS_CALLBACK_TEST
  5441. #endif
  5442. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5443. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5444. /* TLS server for API unit testing - generic */
  5445. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5446. {
  5447. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5448. WOLFSSL_CTX* ctx = NULL;
  5449. WOLFSSL* ssl = NULL;
  5450. SOCKET_T sfd = 0;
  5451. SOCKET_T cfd = 0;
  5452. word16 port;
  5453. char msg[] = "I hear you fa shizzle!";
  5454. int len = (int) XSTRLEN(msg);
  5455. char input[1024];
  5456. int idx;
  5457. int ret, err = 0;
  5458. ((func_args*)args)->return_code = TEST_FAIL;
  5459. #ifdef WOLFSSL_STATIC_MEMORY
  5460. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5461. callbacks->memSz > 0) {
  5462. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5463. callbacks->mem, callbacks->memSz, 0, 1);
  5464. if (ret != WOLFSSL_SUCCESS) {
  5465. fprintf(stderr, "CTX static new failed %d\n", ret);
  5466. return 0;
  5467. }
  5468. }
  5469. #else
  5470. if (ctx == NULL) {
  5471. ctx = wolfSSL_CTX_new(callbacks->method());
  5472. }
  5473. if (ctx == NULL) {
  5474. fprintf(stderr, "CTX new failed\n");
  5475. return 0;
  5476. }
  5477. #endif
  5478. /* set defaults */
  5479. if (callbacks->caPemFile == NULL)
  5480. callbacks->caPemFile = cliCertFile;
  5481. if (callbacks->certPemFile == NULL)
  5482. callbacks->certPemFile = svrCertFile;
  5483. if (callbacks->keyPemFile == NULL)
  5484. callbacks->keyPemFile = svrKeyFile;
  5485. #ifdef WOLFSSL_TIRTOS
  5486. fdOpenSession(Task_self());
  5487. #endif
  5488. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5489. #if defined(USE_WINDOWS_API)
  5490. port = ((func_args*)args)->signal->port;
  5491. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5492. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5493. /* Let tcp_listen assign port */
  5494. port = 0;
  5495. #else
  5496. /* Use default port */
  5497. port = wolfSSLPort;
  5498. #endif
  5499. wolfSSL_CTX_set_verify(ctx,
  5500. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5501. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5502. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5503. #endif
  5504. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5505. if (callbacks->method == wolfDTLSv1_2_server_method) {
  5506. AssertIntEQ(WOLFSSL_SUCCESS,
  5507. wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5508. }
  5509. #endif
  5510. AssertIntEQ(WOLFSSL_SUCCESS,
  5511. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5512. AssertIntEQ(WOLFSSL_SUCCESS,
  5513. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5514. WOLFSSL_FILETYPE_PEM));
  5515. AssertIntEQ(WOLFSSL_SUCCESS,
  5516. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5517. WOLFSSL_FILETYPE_PEM));
  5518. if (callbacks->ctx_ready)
  5519. callbacks->ctx_ready(ctx);
  5520. ssl = wolfSSL_new(ctx);
  5521. if (ssl == NULL) {
  5522. fprintf(stderr, "SSL new failed\n");
  5523. wolfSSL_CTX_free(ctx);
  5524. return 0;
  5525. }
  5526. if (wolfSSL_dtls(ssl)) {
  5527. SOCKADDR_IN_T cliAddr;
  5528. socklen_t cliLen;
  5529. cliLen = sizeof(cliAddr);
  5530. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5531. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5532. (struct sockaddr*)&cliAddr, &cliLen);
  5533. AssertIntGT(idx, 0);
  5534. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5535. }
  5536. else {
  5537. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5538. CloseSocket(sfd);
  5539. }
  5540. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5541. if (callbacks->loadToSSL) {
  5542. wolfSSL_SetDevId(ssl, callbacks->devId);
  5543. AssertIntEQ(WOLFSSL_SUCCESS,
  5544. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5545. WOLFSSL_FILETYPE_PEM));
  5546. AssertIntEQ(WOLFSSL_SUCCESS,
  5547. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5548. WOLFSSL_FILETYPE_PEM));
  5549. }
  5550. #ifdef NO_PSK
  5551. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5552. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5553. #elif !defined(NO_DH)
  5554. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5555. #endif
  5556. #endif
  5557. if (callbacks->ssl_ready)
  5558. callbacks->ssl_ready(ssl);
  5559. #ifdef WOLFSSL_ASYNC_CRYPT
  5560. err = 0; /* Reset error */
  5561. #endif
  5562. do {
  5563. #ifdef WOLFSSL_ASYNC_CRYPT
  5564. if (err == WC_PENDING_E) {
  5565. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5566. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5567. }
  5568. #endif
  5569. ret = wolfSSL_accept(ssl);
  5570. err = wolfSSL_get_error(ssl, ret);
  5571. } while (err == WC_PENDING_E);
  5572. if (ret != WOLFSSL_SUCCESS) {
  5573. char buff[WOLFSSL_MAX_ERROR_SZ];
  5574. fprintf(stderr, "accept error = %d, %s\n", err,
  5575. wolfSSL_ERR_error_string(err, buff));
  5576. /*err_sys("SSL_accept failed");*/
  5577. }
  5578. else {
  5579. #ifdef WOLFSSL_ASYNC_CRYPT
  5580. err = 0; /* Reset error */
  5581. #endif
  5582. do {
  5583. #ifdef WOLFSSL_ASYNC_CRYPT
  5584. if (err == WC_PENDING_E) {
  5585. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5586. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5587. }
  5588. #endif
  5589. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5590. err = wolfSSL_get_error(ssl, idx);
  5591. } while (err == WC_PENDING_E);
  5592. if (idx > 0) {
  5593. input[idx] = 0;
  5594. fprintf(stderr, "Client message: %s\n", input);
  5595. }
  5596. #ifdef WOLFSSL_ASYNC_CRYPT
  5597. err = 0; /* Reset error */
  5598. #endif
  5599. do {
  5600. #ifdef WOLFSSL_ASYNC_CRYPT
  5601. if (err == WC_PENDING_E) {
  5602. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5603. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5604. }
  5605. #endif
  5606. ret = wolfSSL_write(ssl, msg, len);
  5607. err = wolfSSL_get_error(ssl, ret);
  5608. } while (err == WC_PENDING_E);
  5609. AssertIntEQ(len, ret);
  5610. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5611. defined(WOLFSSL_DTLS)
  5612. if (wolfSSL_dtls(ssl)) {
  5613. byte* import;
  5614. word32 sz;
  5615. wolfSSL_dtls_export(ssl, NULL, &sz);
  5616. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5617. AssertNotNull(import);
  5618. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5619. AssertIntGE(idx, 0);
  5620. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5621. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5622. }
  5623. #endif
  5624. #ifdef WOLFSSL_TIRTOS
  5625. Task_yield();
  5626. #endif
  5627. ((func_args*)args)->return_code = TEST_SUCCESS;
  5628. }
  5629. if (callbacks->on_result)
  5630. callbacks->on_result(ssl);
  5631. wolfSSL_shutdown(ssl);
  5632. wolfSSL_free(ssl);
  5633. wolfSSL_CTX_free(ctx);
  5634. CloseSocket(cfd);
  5635. #ifdef WOLFSSL_TIRTOS
  5636. fdCloseSession(Task_self());
  5637. #endif
  5638. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5639. && defined(HAVE_THREAD_LS)
  5640. wc_ecc_fp_free(); /* free per thread cache */
  5641. #endif
  5642. #ifndef WOLFSSL_TIRTOS
  5643. return 0;
  5644. #endif
  5645. }
  5646. /* TLS Client for API unit testing - generic */
  5647. static void run_wolfssl_client(void* args)
  5648. {
  5649. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5650. WOLFSSL_CTX* ctx = NULL;
  5651. WOLFSSL* ssl = NULL;
  5652. SOCKET_T sfd = 0;
  5653. char msg[] = "hello wolfssl server!";
  5654. int len = (int) XSTRLEN(msg);
  5655. char input[1024];
  5656. int ret, err = 0;
  5657. ((func_args*)args)->return_code = TEST_FAIL;
  5658. /* set defaults */
  5659. if (callbacks->caPemFile == NULL)
  5660. callbacks->caPemFile = caCertFile;
  5661. if (callbacks->certPemFile == NULL)
  5662. callbacks->certPemFile = cliCertFile;
  5663. if (callbacks->keyPemFile == NULL)
  5664. callbacks->keyPemFile = cliKeyFile;
  5665. #ifdef WOLFSSL_STATIC_MEMORY
  5666. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5667. callbacks->memSz > 0) {
  5668. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5669. callbacks->mem, callbacks->memSz, 0, 1);
  5670. if (ret != WOLFSSL_SUCCESS) {
  5671. fprintf(stderr, "CTX static new failed %d\n", ret);
  5672. return;
  5673. }
  5674. }
  5675. #else
  5676. if (ctx == NULL) {
  5677. ctx = wolfSSL_CTX_new(callbacks->method());
  5678. }
  5679. if (ctx == NULL) {
  5680. fprintf(stderr, "CTX new failed\n");
  5681. return;
  5682. }
  5683. #endif
  5684. #ifdef WOLFSSL_TIRTOS
  5685. fdOpenSession(Task_self());
  5686. #endif
  5687. if (!callbacks->loadToSSL) {
  5688. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5689. }
  5690. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5691. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5692. #endif
  5693. AssertIntEQ(WOLFSSL_SUCCESS,
  5694. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5695. if (!callbacks->loadToSSL) {
  5696. AssertIntEQ(WOLFSSL_SUCCESS,
  5697. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5698. WOLFSSL_FILETYPE_PEM));
  5699. AssertIntEQ(WOLFSSL_SUCCESS,
  5700. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5701. WOLFSSL_FILETYPE_PEM));
  5702. }
  5703. if (callbacks->ctx_ready)
  5704. callbacks->ctx_ready(ctx);
  5705. ssl = wolfSSL_new(ctx);
  5706. if (wolfSSL_dtls(ssl)) {
  5707. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5708. 1, 0, ssl);
  5709. }
  5710. else {
  5711. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5712. 0, 0, ssl);
  5713. }
  5714. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5715. if (callbacks->loadToSSL) {
  5716. wolfSSL_SetDevId(ssl, callbacks->devId);
  5717. AssertIntEQ(WOLFSSL_SUCCESS,
  5718. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5719. WOLFSSL_FILETYPE_PEM));
  5720. AssertIntEQ(WOLFSSL_SUCCESS,
  5721. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5722. WOLFSSL_FILETYPE_PEM));
  5723. }
  5724. if (callbacks->ssl_ready)
  5725. callbacks->ssl_ready(ssl);
  5726. #ifdef WOLFSSL_ASYNC_CRYPT
  5727. err = 0; /* Reset error */
  5728. #endif
  5729. do {
  5730. #ifdef WOLFSSL_ASYNC_CRYPT
  5731. if (err == WC_PENDING_E) {
  5732. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5733. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5734. }
  5735. #endif
  5736. ret = wolfSSL_connect(ssl);
  5737. err = wolfSSL_get_error(ssl, ret);
  5738. } while (err == WC_PENDING_E);
  5739. if (ret != WOLFSSL_SUCCESS) {
  5740. char buff[WOLFSSL_MAX_ERROR_SZ];
  5741. fprintf(stderr, "error = %d, %s\n", err,
  5742. wolfSSL_ERR_error_string(err, buff));
  5743. /*err_sys("SSL_connect failed");*/
  5744. }
  5745. else {
  5746. #ifdef WOLFSSL_ASYNC_CRYPT
  5747. err = 0; /* Reset error */
  5748. #endif
  5749. do {
  5750. #ifdef WOLFSSL_ASYNC_CRYPT
  5751. if (err == WC_PENDING_E) {
  5752. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5753. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5754. }
  5755. #endif
  5756. ret = wolfSSL_write(ssl, msg, len);
  5757. err = wolfSSL_get_error(ssl, ret);
  5758. } while (err == WC_PENDING_E);
  5759. AssertIntEQ(len, ret);
  5760. #ifdef WOLFSSL_ASYNC_CRYPT
  5761. err = 0; /* Reset error */
  5762. #endif
  5763. do {
  5764. #ifdef WOLFSSL_ASYNC_CRYPT
  5765. if (err == WC_PENDING_E) {
  5766. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5767. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5768. }
  5769. #endif
  5770. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5771. err = wolfSSL_get_error(ssl, ret);
  5772. } while (err == WC_PENDING_E);
  5773. if (ret > 0) {
  5774. input[ret] = '\0'; /* null term */
  5775. fprintf(stderr, "Server response: %s\n", input);
  5776. }
  5777. ((func_args*)args)->return_code = TEST_SUCCESS;
  5778. }
  5779. if (callbacks->on_result)
  5780. callbacks->on_result(ssl);
  5781. wolfSSL_free(ssl);
  5782. wolfSSL_CTX_free(ctx);
  5783. CloseSocket(sfd);
  5784. #ifdef WOLFSSL_TIRTOS
  5785. fdCloseSession(Task_self());
  5786. #endif
  5787. }
  5788. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5789. static int test_wolfSSL_read_write(void)
  5790. {
  5791. /* The unit testing for read and write shall happen simultaneously, since
  5792. * one can't do anything with one without the other. (Except for a failure
  5793. * test case.) This function will call all the others that will set up,
  5794. * execute, and report their test findings.
  5795. *
  5796. * Set up the success case first. This function will become the template
  5797. * for the other tests. This should eventually be renamed
  5798. *
  5799. * The success case isn't interesting, how can this fail?
  5800. * - Do not give the client context a CA certificate. The connect should
  5801. * fail. Do not need server for this?
  5802. * - Using NULL for the ssl object on server. Do not need client for this.
  5803. * - Using NULL for the ssl object on client. Do not need server for this.
  5804. * - Good ssl objects for client and server. Client write() without server
  5805. * read().
  5806. * - Good ssl objects for client and server. Server write() without client
  5807. * read().
  5808. * - Forgetting the password callback?
  5809. */
  5810. tcp_ready ready;
  5811. func_args client_args;
  5812. func_args server_args;
  5813. THREAD_TYPE serverThread;
  5814. XMEMSET(&client_args, 0, sizeof(func_args));
  5815. XMEMSET(&server_args, 0, sizeof(func_args));
  5816. #ifdef WOLFSSL_TIRTOS
  5817. fdOpenSession(Task_self());
  5818. #endif
  5819. StartTCP();
  5820. InitTcpReady(&ready);
  5821. #if defined(USE_WINDOWS_API)
  5822. /* use RNG to get random port if using windows */
  5823. ready.port = GetRandomPort();
  5824. #endif
  5825. server_args.signal = &ready;
  5826. client_args.signal = &ready;
  5827. start_thread(test_server_nofail, &server_args, &serverThread);
  5828. wait_tcp_ready(&server_args);
  5829. test_client_nofail(&client_args, NULL);
  5830. join_thread(serverThread);
  5831. AssertTrue(client_args.return_code);
  5832. AssertTrue(server_args.return_code);
  5833. FreeTcpReady(&ready);
  5834. #ifdef WOLFSSL_TIRTOS
  5835. fdOpenSession(Task_self());
  5836. #endif
  5837. return TEST_RES_CHECK(1);
  5838. }
  5839. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5840. {
  5841. int res = TEST_SKIPPED;
  5842. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5843. !defined(WOLFSSL_TLS13)
  5844. /* The unit test for session resumption by re-using WOLFSSL object.
  5845. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5846. * for second connection.
  5847. */
  5848. tcp_ready ready;
  5849. func_args client_args;
  5850. func_args server_args;
  5851. THREAD_TYPE serverThread;
  5852. XMEMSET(&client_args, 0, sizeof(func_args));
  5853. XMEMSET(&server_args, 0, sizeof(func_args));
  5854. #ifdef WOLFSSL_TIRTOS
  5855. fdOpenSession(Task_self());
  5856. #endif
  5857. StartTCP();
  5858. InitTcpReady(&ready);
  5859. #if defined(USE_WINDOWS_API)
  5860. /* use RNG to get random port if using windows */
  5861. ready.port = GetRandomPort();
  5862. #endif
  5863. server_args.signal = &ready;
  5864. client_args.signal = &ready;
  5865. /* the var is used for loop number */
  5866. server_args.argc = 2;
  5867. start_thread(test_server_loop, &server_args, &serverThread);
  5868. wait_tcp_ready(&server_args);
  5869. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5870. join_thread(serverThread);
  5871. AssertTrue(client_args.return_code);
  5872. AssertTrue(server_args.return_code);
  5873. FreeTcpReady(&ready);
  5874. #ifdef WOLFSSL_TIRTOS
  5875. fdOpenSession(Task_self());
  5876. #endif
  5877. res = TEST_RES_CHECK(1);
  5878. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5879. return res;
  5880. }
  5881. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5882. {
  5883. int res = TEST_SKIPPED;
  5884. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5885. /* This unit test is to check set verify Depth */
  5886. tcp_ready ready;
  5887. func_args client_args;
  5888. func_args server_args;
  5889. THREAD_TYPE serverThread;
  5890. callback_functions client_cbf;
  5891. XMEMSET(&client_args, 0, sizeof(func_args));
  5892. XMEMSET(&server_args, 0, sizeof(func_args));
  5893. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5894. #ifdef WOLFSSL_TLS13
  5895. client_cbf.method = wolfTLSv1_3_client_method;
  5896. #endif /* WOLFSSL_TLS13 */
  5897. client_args.callbacks = &client_cbf;
  5898. StartTCP();
  5899. InitTcpReady(&ready);
  5900. #if defined(USE_WINDOWS_API)
  5901. /* use RNG to get random port if using windows */
  5902. ready.port = GetRandomPort();
  5903. #endif
  5904. server_args.signal = &ready;
  5905. client_args.signal = &ready;
  5906. /* the var is used for loop number */
  5907. server_args.argc = 1;
  5908. /* test case 1 verify depth is equal to peer chain */
  5909. {
  5910. start_thread(test_server_nofail, &server_args, &serverThread);
  5911. wait_tcp_ready(&server_args);
  5912. /* the var is used for verify depth */
  5913. client_args.argc = 2;
  5914. test_client_verifyDepth(&client_args);
  5915. join_thread(serverThread);
  5916. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5917. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5918. }
  5919. /* test case 2
  5920. * verify depth is zero, number of peer's chain is 2.
  5921. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5922. * callback.
  5923. */
  5924. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5925. {
  5926. start_thread(test_server_nofail, &server_args, &serverThread);
  5927. wait_tcp_ready(&server_args);
  5928. client_args.argc = 0;
  5929. test_client_verifyDepth(&client_args);
  5930. join_thread(serverThread);
  5931. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5932. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5933. }
  5934. /* test case 3
  5935. * verify depth is zero, number of peer's chain is 2
  5936. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5937. * therefore, handshake becomes failure.
  5938. */
  5939. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5940. {
  5941. start_thread(test_server_nofail, &server_args, &serverThread);
  5942. wait_tcp_ready(&server_args);
  5943. client_args.argc = -1;
  5944. test_client_verifyDepth(&client_args);
  5945. join_thread(serverThread);
  5946. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5947. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5948. }
  5949. FreeTcpReady(&ready);
  5950. res = TEST_RES_CHECK(1);
  5951. #else
  5952. (void)test_client_verifyDepth;
  5953. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5954. return res;
  5955. }
  5956. static int test_wolfSSL_CTX_set_cipher_list(void)
  5957. {
  5958. int res = TEST_SKIPPED;
  5959. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5960. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  5961. && !defined(NO_SHA256)
  5962. WOLFSSL_CTX* ctx;
  5963. WOLFSSL_CTX* ctxClient;
  5964. tcp_ready ready;
  5965. func_args client_args;
  5966. func_args server_args;
  5967. callback_functions client_cb;
  5968. callback_functions server_cb;
  5969. THREAD_TYPE serverThread;
  5970. XMEMSET(&client_args, 0, sizeof(func_args));
  5971. XMEMSET(&server_args, 0, sizeof(func_args));
  5972. StartTCP();
  5973. InitTcpReady(&ready);
  5974. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5975. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5976. AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
  5977. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  5978. AssertIntEQ(WOLFSSL_SUCCESS,
  5979. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5980. AssertIntEQ(WOLFSSL_SUCCESS,
  5981. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5982. AssertIntEQ(WOLFSSL_SUCCESS,
  5983. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5984. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5985. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
  5986. client_cb.ctx = ctxClient;
  5987. server_cb.ctx = ctx;
  5988. /* we are responsible for free'ing WOLFSSL_CTX */
  5989. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  5990. server_args.signal = &ready;
  5991. server_args.callbacks = &server_cb;
  5992. client_args.signal = &ready;
  5993. client_args.callbacks = &client_cb;
  5994. client_args.return_code = TEST_FAIL;
  5995. start_thread(test_server_nofail, &server_args, &serverThread);
  5996. wait_tcp_ready(&server_args);
  5997. test_client_nofail(&client_args, NULL);
  5998. join_thread(serverThread);
  5999. wolfSSL_CTX_free(client_cb.ctx);
  6000. wolfSSL_CTX_free(server_cb.ctx);
  6001. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6002. FreeTcpReady(&ready);
  6003. res = TEST_RES_CHECK(1);
  6004. #endif
  6005. return res;
  6006. }
  6007. static int test_client_get_finished(void* args, cbType cb)
  6008. {
  6009. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  6010. SOCKET_T sockfd = 0;
  6011. callback_functions* cbf;
  6012. WOLFSSL_CTX* ctx = 0;
  6013. WOLFSSL* ssl = 0;
  6014. char msg[64] = "hello wolfssl!";
  6015. char reply[1024];
  6016. int msgSz = (int)XSTRLEN(msg);
  6017. int ret, err = 0;
  6018. WOLFSSL_METHOD* method = NULL;
  6019. size_t msg_len = 0;
  6020. (void) args;
  6021. (void) cb;
  6022. ((func_args*)args)->return_code = TEST_FAIL;
  6023. cbf = ((func_args*)args)->callbacks;
  6024. if (cbf != NULL && cbf->method != NULL) {
  6025. method = cbf->method();
  6026. }
  6027. else {
  6028. method = wolfSSLv23_client_method();
  6029. }
  6030. ctx = wolfSSL_CTX_new(method);
  6031. /* Do connect here so server detects failures */
  6032. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6033. 0, 0, NULL);
  6034. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6035. {
  6036. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6037. goto done;
  6038. }
  6039. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6040. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6041. goto done;
  6042. }
  6043. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6044. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6045. goto done;
  6046. }
  6047. /* call ctx setup callback */
  6048. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6049. cbf->ctx_ready(ctx);
  6050. }
  6051. ssl = wolfSSL_new(ctx);
  6052. if (ssl == NULL) {
  6053. goto done;
  6054. }
  6055. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6056. goto done;
  6057. }
  6058. /* call ssl setup callback */
  6059. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6060. cbf->ssl_ready(ssl);
  6061. }
  6062. #ifdef WOLFSSL_ASYNC_CRYPT
  6063. err = 0; /* Reset error */
  6064. #endif
  6065. do {
  6066. #ifdef WOLFSSL_ASYNC_CRYPT
  6067. if (err == WC_PENDING_E) {
  6068. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6069. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6070. }
  6071. #endif
  6072. ret = wolfSSL_connect(ssl);
  6073. err = wolfSSL_get_error(ssl, 0);
  6074. } while (err == WC_PENDING_E);
  6075. if (ret != WOLFSSL_SUCCESS) {
  6076. char buff[WOLFSSL_MAX_ERROR_SZ];
  6077. fprintf(stderr, "error = %d, %s\n", err,
  6078. wolfSSL_ERR_error_string(err, buff));
  6079. goto done;
  6080. }
  6081. /* get_finished test */
  6082. /* 1. get own sent message */
  6083. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6084. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6085. AssertIntGE(msg_len, 0);
  6086. /* 2. get peer message */
  6087. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6088. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6089. AssertIntGE(msg_len, 0);
  6090. if (cb != NULL)
  6091. (cb)(ctx, ssl);
  6092. #ifdef WOLFSSL_ASYNC_CRYPT
  6093. err = 0; /* Reset error */
  6094. #endif
  6095. do {
  6096. #ifdef WOLFSSL_ASYNC_CRYPT
  6097. if (err == WC_PENDING_E) {
  6098. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6099. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6100. }
  6101. #endif
  6102. ret = wolfSSL_write(ssl, msg, msgSz);
  6103. err = wolfSSL_get_error(ssl, 0);
  6104. } while (err == WC_PENDING_E);
  6105. if (ret != msgSz) {
  6106. /*err_sys("SSL_write failed");*/
  6107. goto done;
  6108. }
  6109. #ifdef WOLFSSL_ASYNC_CRYPT
  6110. err = 0; /* Reset error */
  6111. #endif
  6112. do {
  6113. #ifdef WOLFSSL_ASYNC_CRYPT
  6114. if (err == WC_PENDING_E) {
  6115. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6116. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6117. }
  6118. #endif
  6119. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6120. err = wolfSSL_get_error(ssl, 0);
  6121. } while (err == WC_PENDING_E);
  6122. if (ret > 0) {
  6123. reply[ret] = '\0';
  6124. fprintf(stderr, "Server response: %s\n", reply);
  6125. }
  6126. ((func_args*)args)->return_code = TEST_SUCCESS;
  6127. done:
  6128. wolfSSL_free(ssl);
  6129. wolfSSL_CTX_free(ctx);
  6130. CloseSocket(sockfd);
  6131. #else
  6132. (void)args;
  6133. (void)cb;
  6134. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  6135. return 0;
  6136. }
  6137. static int test_wolfSSL_get_finished(void)
  6138. {
  6139. int res = TEST_SKIPPED;
  6140. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6141. tcp_ready ready;
  6142. func_args client_args;
  6143. func_args server_args;
  6144. THREAD_TYPE serverThread;
  6145. XMEMSET(&client_args, 0, sizeof(func_args));
  6146. XMEMSET(&server_args, 0, sizeof(func_args));
  6147. StartTCP();
  6148. InitTcpReady(&ready);
  6149. #if defined(USE_WINDOWS_API)
  6150. /* use RNG to get random port if using windows */
  6151. ready.port = GetRandomPort();
  6152. #endif
  6153. server_args.signal = &ready;
  6154. client_args.signal = &ready;
  6155. start_thread(test_server_nofail, &server_args, &serverThread);
  6156. wait_tcp_ready(&server_args);
  6157. test_client_get_finished(&client_args, NULL);
  6158. join_thread(serverThread);
  6159. AssertTrue(client_args.return_code);
  6160. AssertTrue(server_args.return_code);
  6161. /* test received msg vs sent msg */
  6162. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6163. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6164. FreeTcpReady(&ready);
  6165. res = TEST_RES_CHECK(1);
  6166. #else
  6167. (void)test_client_get_finished;
  6168. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6169. return res;
  6170. }
  6171. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6172. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6173. !defined(NO_SESSION_CACHE)
  6174. /* Sessions to restore/store */
  6175. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6176. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6177. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6178. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6179. {
  6180. /* Don't store sessions. Lookup is still enabled. */
  6181. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6182. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6183. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6184. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6185. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6186. /* Require both peers to provide certs */
  6187. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6188. }
  6189. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6190. {
  6191. WOLFSSL_SESSION** sess;
  6192. if (wolfSSL_is_server(ssl))
  6193. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6194. else
  6195. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6196. if (*sess == NULL) {
  6197. #ifdef NO_SESSION_CACHE_REF
  6198. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6199. #else
  6200. /* Test for backwards compatibility */
  6201. if (wolfSSL_is_server(ssl)) {
  6202. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6203. }
  6204. else {
  6205. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6206. }
  6207. #endif
  6208. /* Now save the session in the internal store to make it available
  6209. * for lookup. For TLS 1.3, we can't save the session without
  6210. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6211. * session from cache. */
  6212. if (wolfSSL_is_server(ssl)
  6213. #ifndef WOLFSSL_TICKET_HAVE_ID
  6214. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6215. #endif
  6216. )
  6217. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6218. *sess), WOLFSSL_SUCCESS);
  6219. }
  6220. else {
  6221. /* If we have a session retrieved then remaining connections should be
  6222. * resuming on that session */
  6223. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6224. }
  6225. /* Save CTX to be able to decrypt tickets */
  6226. if (wolfSSL_is_server(ssl) &&
  6227. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6228. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6229. = wolfSSL_get_SSL_CTX(ssl));
  6230. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6231. WOLFSSL_SUCCESS);
  6232. }
  6233. #ifdef SESSION_CERTS
  6234. #ifndef WOLFSSL_TICKET_HAVE_ID
  6235. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6236. wolfSSL_session_reused(ssl))
  6237. #endif
  6238. {
  6239. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6240. * for all connections. TLS 1.3 only has tickets so if we don't
  6241. * include the session id in the ticket then the certificates
  6242. * will not be available on resumption. */
  6243. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6244. AssertNotNull(peer);
  6245. wolfSSL_X509_free(peer);
  6246. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6247. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  6248. }
  6249. #endif
  6250. }
  6251. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6252. {
  6253. /* Set the session to reuse for the client */
  6254. AssertIntEQ(wolfSSL_set_session(ssl,
  6255. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6256. }
  6257. #endif
  6258. static int test_wolfSSL_CTX_add_session(void)
  6259. {
  6260. int res = TEST_SKIPPED;
  6261. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6262. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6263. !defined(NO_SESSION_CACHE)
  6264. tcp_ready ready;
  6265. func_args client_args;
  6266. func_args server_args;
  6267. THREAD_TYPE serverThread;
  6268. callback_functions client_cb;
  6269. callback_functions server_cb;
  6270. method_provider methods[][2] = {
  6271. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6272. !defined(NO_DES3))
  6273. /* Without AES there are almost no ciphersuites available. This leads
  6274. * to no ciphersuites being available and an error. */
  6275. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6276. #endif
  6277. #ifndef WOLFSSL_NO_TLS12
  6278. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6279. #endif
  6280. /* Needs the default ticket callback since it is tied to the
  6281. * connection context and this makes it easy to carry over the ticket
  6282. * crypto context between connections */
  6283. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6284. defined(HAVE_SESSION_TICKET)
  6285. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6286. #endif
  6287. };
  6288. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6289. size_t i, j;
  6290. for (i = 0; i < methodsLen; i++) {
  6291. /* First run creates a connection while the second+ run will attempt
  6292. * to resume the connection. The trick is that the internal cache
  6293. * is turned off. wolfSSL_CTX_add_session should put the session in
  6294. * the cache anyway. */
  6295. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6296. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6297. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6298. for (j = 0; j < 5; j++) {
  6299. #ifdef WOLFSSL_TIRTOS
  6300. fdOpenSession(Task_self());
  6301. #endif
  6302. StartTCP();
  6303. InitTcpReady(&ready);
  6304. XMEMSET(&client_args, 0, sizeof(func_args));
  6305. XMEMSET(&server_args, 0, sizeof(func_args));
  6306. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6307. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6308. client_cb.method = methods[i][0];
  6309. server_cb.method = methods[i][1];
  6310. server_args.signal = &ready;
  6311. server_args.callbacks = &server_cb;
  6312. client_args.signal = &ready;
  6313. client_args.callbacks = &client_cb;
  6314. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6315. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6316. server_cb.isSharedCtx = 1;
  6317. }
  6318. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6319. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6320. if (j != 0)
  6321. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6322. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6323. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6324. server_cb.ticNoInit = 1; /* Use default builtin */
  6325. start_thread(test_server_nofail, &server_args, &serverThread);
  6326. wait_tcp_ready(&server_args);
  6327. test_client_nofail(&client_args, NULL);
  6328. join_thread(serverThread);
  6329. AssertTrue(client_args.return_code);
  6330. AssertTrue(server_args.return_code);
  6331. FreeTcpReady(&ready);
  6332. }
  6333. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6334. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6335. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6336. }
  6337. res = TEST_RES_CHECK(1);
  6338. #endif
  6339. return res;
  6340. }
  6341. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6342. /* canned export of a session using older version 3 */
  6343. static unsigned char version_3[] = {
  6344. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6345. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6346. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6347. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6348. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6349. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6351. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6352. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6355. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6356. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6357. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6358. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6359. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6360. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6361. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6362. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6368. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6369. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6374. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6375. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  6376. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6377. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6378. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6379. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6380. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6381. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6382. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6383. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6384. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6386. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6387. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6388. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6389. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6390. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6391. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6392. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6393. 0x2E, 0x31, 0xED, 0x4F
  6394. };
  6395. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6396. static int test_wolfSSL_dtls_export(void)
  6397. {
  6398. int res = TEST_SKIPPED;
  6399. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6400. tcp_ready ready;
  6401. func_args client_args;
  6402. func_args server_args;
  6403. THREAD_TYPE serverThread;
  6404. callback_functions server_cbf;
  6405. callback_functions client_cbf;
  6406. #ifdef WOLFSSL_TIRTOS
  6407. fdOpenSession(Task_self());
  6408. #endif
  6409. InitTcpReady(&ready);
  6410. #if defined(USE_WINDOWS_API)
  6411. /* use RNG to get random port if using windows */
  6412. ready.port = GetRandomPort();
  6413. #endif
  6414. /* set using dtls */
  6415. XMEMSET(&client_args, 0, sizeof(func_args));
  6416. XMEMSET(&server_args, 0, sizeof(func_args));
  6417. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6418. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6419. server_cbf.method = wolfDTLSv1_2_server_method;
  6420. client_cbf.method = wolfDTLSv1_2_client_method;
  6421. server_args.callbacks = &server_cbf;
  6422. client_args.callbacks = &client_cbf;
  6423. server_args.signal = &ready;
  6424. client_args.signal = &ready;
  6425. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6426. wait_tcp_ready(&server_args);
  6427. run_wolfssl_client(&client_args);
  6428. join_thread(serverThread);
  6429. AssertTrue(client_args.return_code);
  6430. AssertTrue(server_args.return_code);
  6431. FreeTcpReady(&ready);
  6432. #ifdef WOLFSSL_TIRTOS
  6433. fdOpenSession(Task_self());
  6434. #endif
  6435. {
  6436. SOCKET_T sockfd = 0;
  6437. WOLFSSL_CTX* ctx;
  6438. WOLFSSL* ssl;
  6439. char msg[64] = "hello wolfssl!";
  6440. char reply[1024];
  6441. int msgSz = (int)XSTRLEN(msg);
  6442. byte *session, *window;
  6443. unsigned int sessionSz, windowSz;
  6444. #ifndef TEST_IPV6
  6445. struct sockaddr_in peerAddr;
  6446. #else
  6447. struct sockaddr_in6 peerAddr;
  6448. #endif /* TEST_IPV6 */
  6449. int i;
  6450. /* Set ctx to DTLS 1.2 */
  6451. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6452. AssertNotNull(ssl = wolfSSL_new(ctx));
  6453. /* test importing version 3 */
  6454. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6455. /* test importing bad length and bad version */
  6456. version_3[2] += 1;
  6457. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6458. version_3[2] -= 1; version_3[1] = 0XA0;
  6459. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6460. wolfSSL_free(ssl);
  6461. wolfSSL_CTX_free(ctx);
  6462. /* check storing client state after connection and storing window only */
  6463. #ifdef WOLFSSL_TIRTOS
  6464. fdOpenSession(Task_self());
  6465. #endif
  6466. InitTcpReady(&ready);
  6467. #if defined(USE_WINDOWS_API)
  6468. /* use RNG to get random port if using windows */
  6469. ready.port = GetRandomPort();
  6470. #endif
  6471. /* set using dtls */
  6472. XMEMSET(&server_args, 0, sizeof(func_args));
  6473. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6474. server_cbf.method = wolfDTLSv1_2_server_method;
  6475. server_cbf.doUdp = 1;
  6476. server_args.callbacks = &server_cbf;
  6477. server_args.argc = 3; /* set loop_count to 3 */
  6478. server_args.signal = &ready;
  6479. start_thread(test_server_nofail, &server_args, &serverThread);
  6480. wait_tcp_ready(&server_args);
  6481. /* create and connect with client */
  6482. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6483. AssertIntEQ(WOLFSSL_SUCCESS,
  6484. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6485. AssertIntEQ(WOLFSSL_SUCCESS,
  6486. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6487. AssertIntEQ(WOLFSSL_SUCCESS,
  6488. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6489. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6490. AssertNotNull(ssl = wolfSSL_new(ctx));
  6491. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6492. /* store server information connected too */
  6493. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6494. #ifndef TEST_IPV6
  6495. peerAddr.sin_family = AF_INET;
  6496. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6497. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6498. #else
  6499. peerAddr.sin6_family = AF_INET6;
  6500. AssertIntEQ(
  6501. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6502. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6503. #endif
  6504. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6505. WOLFSSL_SUCCESS);
  6506. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6507. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6508. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6509. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6510. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6511. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6512. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  6513. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6514. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6515. wolfSSL_free(ssl);
  6516. for (i = 1; i < server_args.argc; i++) {
  6517. /* restore state */
  6518. AssertNotNull(ssl = wolfSSL_new(ctx));
  6519. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  6520. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  6521. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6522. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6523. WOLFSSL_SUCCESS);
  6524. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6525. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6526. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6527. wolfSSL_free(ssl);
  6528. }
  6529. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6530. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6531. wolfSSL_CTX_free(ctx);
  6532. fprintf(stderr, "done and waiting for server\n");
  6533. join_thread(serverThread);
  6534. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6535. FreeTcpReady(&ready);
  6536. #ifdef WOLFSSL_TIRTOS
  6537. fdOpenSession(Task_self());
  6538. #endif
  6539. }
  6540. res = TEST_RES_CHECK(1);
  6541. #endif
  6542. return res;
  6543. }
  6544. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6545. #ifdef WOLFSSL_TLS13
  6546. static const byte canned_client_tls13_session[] = {
  6547. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  6548. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6549. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  6550. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6551. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6552. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6553. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6554. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  6555. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6556. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6557. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6558. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  6559. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6560. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6561. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6562. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6563. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6564. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6565. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6566. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6567. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6568. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6569. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6570. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6571. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6572. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6573. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6574. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6575. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6576. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  6577. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  6578. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  6579. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6580. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6581. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6582. 0x00, 0x03
  6583. };
  6584. static const byte canned_server_tls13_session[] = {
  6585. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  6586. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6587. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  6588. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6589. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6590. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6591. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6592. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  6593. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6594. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6595. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6596. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  6597. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6598. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6599. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6600. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6601. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6602. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6603. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6604. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6605. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6606. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6607. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6608. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6609. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6610. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6611. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6612. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6613. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6614. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  6615. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  6616. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  6617. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6618. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6619. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6620. 0x00, 0x04
  6621. };
  6622. #endif /* WOLFSSL_TLS13 */
  6623. static const byte canned_client_session[] = {
  6624. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6625. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6626. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  6627. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6628. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6629. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6630. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6631. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  6632. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6633. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6634. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6635. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6636. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6637. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6638. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6639. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6640. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6641. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6642. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6643. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6644. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6645. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6646. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6647. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6648. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6649. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6650. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6651. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6652. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6654. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6655. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6656. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6657. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6658. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6659. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6660. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6661. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6662. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6663. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6664. 0x00, 0x03
  6665. };
  6666. static const byte canned_server_session[] = {
  6667. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6668. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6669. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6670. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6671. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6672. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6673. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6674. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6675. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6676. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6677. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6678. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6679. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6680. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6681. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6682. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6683. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6684. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6685. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6686. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6687. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6688. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6689. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6690. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6691. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6692. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6693. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6694. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6695. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6698. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6699. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6700. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6701. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6702. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6703. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6704. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6705. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6706. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6707. 0x00, 0x04
  6708. };
  6709. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6710. {
  6711. SOCKET_T sockfd = 0;
  6712. SOCKET_T clientfd = 0;
  6713. word16 port;
  6714. callback_functions* cbf;
  6715. WOLFSSL_CTX* ctx = 0;
  6716. WOLFSSL* ssl = 0;
  6717. char msg[] = "I hear you fa shizzle!";
  6718. char input[1024];
  6719. int idx;
  6720. #ifdef WOLFSSL_TIRTOS
  6721. fdOpenSession(Task_self());
  6722. #endif
  6723. ((func_args*)args)->return_code = TEST_FAIL;
  6724. cbf = ((func_args*)args)->callbacks;
  6725. {
  6726. WOLFSSL_METHOD* method = NULL;
  6727. if (cbf != NULL && cbf->method != NULL) {
  6728. method = cbf->method();
  6729. }
  6730. else {
  6731. method = wolfTLSv1_2_server_method();
  6732. }
  6733. ctx = wolfSSL_CTX_new(method);
  6734. }
  6735. if (ctx == NULL) {
  6736. goto done;
  6737. }
  6738. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6739. #if defined(USE_WINDOWS_API)
  6740. port = ((func_args*)args)->signal->port;
  6741. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6742. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6743. /* Let tcp_listen assign port */
  6744. port = 0;
  6745. #else
  6746. /* Use default port */
  6747. port = wolfSSLPort;
  6748. #endif
  6749. /* do it here to detect failure */
  6750. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6751. CloseSocket(sockfd);
  6752. /* call ctx setup callback */
  6753. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6754. cbf->ctx_ready(ctx);
  6755. }
  6756. ssl = wolfSSL_new(ctx);
  6757. if (ssl == NULL) {
  6758. goto done;
  6759. }
  6760. wolfSSL_set_fd(ssl, clientfd);
  6761. /* call ssl setup callback */
  6762. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6763. cbf->ssl_ready(ssl);
  6764. }
  6765. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6766. if (idx > 0) {
  6767. input[idx] = '\0';
  6768. fprintf(stderr, "Client message export/import: %s\n", input);
  6769. }
  6770. else {
  6771. fprintf(stderr, "ret = %d error = %d\n", idx,
  6772. wolfSSL_get_error(ssl, idx));
  6773. goto done;
  6774. }
  6775. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6776. /*err_sys("SSL_write failed");*/
  6777. #ifdef WOLFSSL_TIRTOS
  6778. return;
  6779. #else
  6780. return 0;
  6781. #endif
  6782. }
  6783. #ifdef WOLFSSL_TIRTOS
  6784. Task_yield();
  6785. #endif
  6786. ((func_args*)args)->return_code = TEST_SUCCESS;
  6787. done:
  6788. wolfSSL_shutdown(ssl);
  6789. wolfSSL_free(ssl);
  6790. wolfSSL_CTX_free(ctx);
  6791. CloseSocket(clientfd);
  6792. #ifdef WOLFSSL_TIRTOS
  6793. fdCloseSession(Task_self());
  6794. #endif
  6795. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6796. && defined(HAVE_THREAD_LS)
  6797. wc_ecc_fp_free(); /* free per thread cache */
  6798. #endif
  6799. #if defined(HAVE_SESSION_TICKET) && \
  6800. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6801. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6802. OpenSSLTicketCleanup();
  6803. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6804. TicketCleanup();
  6805. #endif
  6806. #endif
  6807. #ifndef WOLFSSL_TIRTOS
  6808. return 0;
  6809. #endif
  6810. }
  6811. static void load_tls12_canned_server(WOLFSSL* ssl)
  6812. {
  6813. int clientfd = wolfSSL_get_fd(ssl);
  6814. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6815. sizeof(canned_server_session)), sizeof(canned_server_session));
  6816. wolfSSL_set_fd(ssl, clientfd);
  6817. }
  6818. #ifdef WOLFSSL_TLS13
  6819. static void load_tls13_canned_server(WOLFSSL* ssl)
  6820. {
  6821. int clientfd = wolfSSL_get_fd(ssl);
  6822. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6823. sizeof(canned_server_tls13_session)),
  6824. sizeof(canned_server_tls13_session));
  6825. wolfSSL_set_fd(ssl, clientfd);
  6826. }
  6827. #endif
  6828. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6829. static int test_wolfSSL_tls_export_run(int v)
  6830. {
  6831. SOCKET_T sockfd = 0;
  6832. WOLFSSL_CTX* ctx = 0;
  6833. WOLFSSL* ssl = 0;
  6834. char msg[64] = "hello wolfssl!";
  6835. char reply[1024];
  6836. word32 replySz;
  6837. int msgSz = (int)XSTRLEN(msg);
  6838. const byte* clientSession = NULL;
  6839. int clientSessionSz = 0;
  6840. tcp_ready ready;
  6841. func_args server_args;
  6842. THREAD_TYPE serverThread;
  6843. callback_functions server_cbf;
  6844. #ifdef WOLFSSL_TIRTOS
  6845. fdOpenSession(Task_self());
  6846. #endif
  6847. InitTcpReady(&ready);
  6848. #if defined(USE_WINDOWS_API)
  6849. /* use RNG to get random port if using windows */
  6850. ready.port = GetRandomPort();
  6851. #endif
  6852. XMEMSET(&server_args, 0, sizeof(func_args));
  6853. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6854. switch (v) {
  6855. case WOLFSSL_TLSV1_2:
  6856. server_cbf.method = wolfTLSv1_2_server_method;
  6857. server_cbf.ssl_ready = load_tls12_canned_server;
  6858. /* setup the client side */
  6859. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6860. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6861. clientSession = canned_client_session;
  6862. clientSessionSz = sizeof(canned_client_session);
  6863. break;
  6864. #ifdef WOLFSSL_TLS13
  6865. case WOLFSSL_TLSV1_3:
  6866. server_cbf.method = wolfTLSv1_3_server_method;
  6867. server_cbf.ssl_ready = load_tls13_canned_server;
  6868. /* setup the client side */
  6869. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6870. clientSession = canned_client_tls13_session;
  6871. clientSessionSz = sizeof(canned_client_tls13_session);
  6872. break;
  6873. #endif
  6874. }
  6875. server_args.callbacks = &server_cbf;
  6876. server_args.signal = &ready;
  6877. start_thread(tls_export_server, &server_args, &serverThread);
  6878. wait_tcp_ready(&server_args);
  6879. #ifdef WOLFSSL_TIRTOS
  6880. fdOpenSession(Task_self());
  6881. #endif
  6882. AssertNotNull(ssl = wolfSSL_new(ctx));
  6883. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6884. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6885. clientSessionSz);
  6886. replySz = sizeof(reply);
  6887. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6888. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6889. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6890. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6891. #endif
  6892. wolfSSL_set_fd(ssl, sockfd);
  6893. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6894. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6895. wolfSSL_free(ssl);
  6896. wolfSSL_CTX_free(ctx);
  6897. CloseSocket(sockfd);
  6898. #ifdef WOLFSSL_TIRTOS
  6899. fdCloseSession(Task_self());
  6900. #endif
  6901. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6902. && defined(HAVE_THREAD_LS)
  6903. wc_ecc_fp_free(); /* free per thread cache */
  6904. #endif
  6905. join_thread(serverThread);
  6906. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6907. FreeTcpReady(&ready);
  6908. #ifdef WOLFSSL_TIRTOS
  6909. fdOpenSession(Task_self());
  6910. #endif
  6911. return TEST_RES_CHECK(1);
  6912. }
  6913. #endif
  6914. static int test_wolfSSL_tls_export(void)
  6915. {
  6916. int res = TEST_SKIPPED;
  6917. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6918. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6919. #ifdef WOLFSSL_TLS13
  6920. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6921. #endif
  6922. res = TEST_RES_CHECK(1);
  6923. #endif
  6924. return res;
  6925. }
  6926. /*----------------------------------------------------------------------------*
  6927. | TLS extensions tests
  6928. *----------------------------------------------------------------------------*/
  6929. #ifdef ENABLE_TLS_CALLBACK_TEST
  6930. /* Connection test runner - generic */
  6931. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6932. callback_functions* server_callbacks)
  6933. {
  6934. tcp_ready ready;
  6935. func_args client_args;
  6936. func_args server_args;
  6937. THREAD_TYPE serverThread;
  6938. XMEMSET(&client_args, 0, sizeof(func_args));
  6939. XMEMSET(&server_args, 0, sizeof(func_args));
  6940. StartTCP();
  6941. client_args.callbacks = client_callbacks;
  6942. server_args.callbacks = server_callbacks;
  6943. #ifdef WOLFSSL_TIRTOS
  6944. fdOpenSession(Task_self());
  6945. #endif
  6946. /* RUN Server side */
  6947. InitTcpReady(&ready);
  6948. #if defined(USE_WINDOWS_API)
  6949. /* use RNG to get random port if using windows */
  6950. ready.port = GetRandomPort();
  6951. #endif
  6952. server_args.signal = &ready;
  6953. client_args.signal = &ready;
  6954. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6955. wait_tcp_ready(&server_args);
  6956. /* RUN Client side */
  6957. run_wolfssl_client(&client_args);
  6958. join_thread(serverThread);
  6959. FreeTcpReady(&ready);
  6960. #ifdef WOLFSSL_TIRTOS
  6961. fdCloseSession(Task_self());
  6962. #endif
  6963. client_callbacks->return_code = client_args.return_code;
  6964. server_callbacks->return_code = server_args.return_code;
  6965. }
  6966. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6967. #ifdef HAVE_SNI
  6968. static int test_wolfSSL_UseSNI_params(void)
  6969. {
  6970. int res = TEST_SKIPPED;
  6971. #if !defined(NO_WOLFSSL_CLIENT)
  6972. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6973. WOLFSSL *ssl = wolfSSL_new(ctx);
  6974. AssertNotNull(ctx);
  6975. AssertNotNull(ssl);
  6976. /* invalid [ctx|ssl] */
  6977. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6978. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6979. /* invalid type */
  6980. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6981. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6982. /* invalid data */
  6983. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6984. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6985. /* success case */
  6986. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6987. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6988. wolfSSL_free(ssl);
  6989. wolfSSL_CTX_free(ctx);
  6990. res = TEST_RES_CHECK(1);
  6991. #endif /* !NO_WOLFSSL_CLIENT */
  6992. return res;
  6993. }
  6994. /* BEGIN of connection tests callbacks */
  6995. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6996. {
  6997. AssertIntEQ(WOLFSSL_SUCCESS,
  6998. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6999. }
  7000. static void use_SNI_at_ssl(WOLFSSL* ssl)
  7001. {
  7002. AssertIntEQ(WOLFSSL_SUCCESS,
  7003. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7004. }
  7005. static void different_SNI_at_ssl(WOLFSSL* ssl)
  7006. {
  7007. AssertIntEQ(WOLFSSL_SUCCESS,
  7008. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  7009. }
  7010. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  7011. {
  7012. use_SNI_at_ssl(ssl);
  7013. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7014. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  7015. }
  7016. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  7017. {
  7018. use_SNI_at_ssl(ssl);
  7019. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7020. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  7021. }
  7022. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7023. {
  7024. use_SNI_at_ctx(ctx);
  7025. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7026. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7027. }
  7028. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  7029. {
  7030. use_SNI_at_ssl(ssl);
  7031. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7032. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7033. }
  7034. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7035. {
  7036. use_SNI_at_ctx(ctx);
  7037. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7038. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7039. }
  7040. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  7041. {
  7042. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  7043. }
  7044. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  7045. {
  7046. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  7047. }
  7048. static void verify_SNI_no_matching(WOLFSSL* ssl)
  7049. {
  7050. byte type = WOLFSSL_SNI_HOST_NAME;
  7051. void* request = (void*) &type; /* to be overwritten */
  7052. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  7053. AssertNotNull(request);
  7054. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7055. AssertNull(request);
  7056. }
  7057. static void verify_SNI_real_matching(WOLFSSL* ssl)
  7058. {
  7059. byte type = WOLFSSL_SNI_HOST_NAME;
  7060. void* request = NULL;
  7061. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  7062. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7063. AssertNotNull(request);
  7064. AssertStrEQ("www.wolfssl.com", (char*)request);
  7065. }
  7066. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  7067. {
  7068. byte type = WOLFSSL_SNI_HOST_NAME;
  7069. void* request = NULL;
  7070. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  7071. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7072. AssertNotNull(request);
  7073. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  7074. }
  7075. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7076. {
  7077. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  7078. }
  7079. /* END of connection tests callbacks */
  7080. static int test_wolfSSL_UseSNI_connection(void)
  7081. {
  7082. int res = TEST_SKIPPED;
  7083. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7084. callback_functions client_cb;
  7085. callback_functions server_cb;
  7086. size_t i;
  7087. struct {
  7088. method_provider client_meth;
  7089. method_provider server_meth;
  7090. } methods[] = {
  7091. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  7092. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  7093. #endif
  7094. #ifndef WOLFSSL_NO_TLS12
  7095. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  7096. #endif
  7097. #ifdef WOLFSSL_TLS13
  7098. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  7099. #endif
  7100. };
  7101. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  7102. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7103. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7104. client_cb.method = methods[i].client_meth;
  7105. server_cb.method = methods[i].server_meth;
  7106. client_cb.devId = testDevId;
  7107. server_cb.devId = testDevId;
  7108. /* success case at ctx */
  7109. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7110. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7111. test_wolfSSL_client_server(&client_cb, &server_cb);
  7112. /* success case at ssl */
  7113. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  7114. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  7115. test_wolfSSL_client_server(&client_cb, &server_cb);
  7116. /* default mismatch behavior */
  7117. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7118. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  7119. test_wolfSSL_client_server(&client_cb, &server_cb);
  7120. /* continue on mismatch */
  7121. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7122. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  7123. test_wolfSSL_client_server(&client_cb, &server_cb);
  7124. /* fake answer on mismatch */
  7125. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7126. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  7127. test_wolfSSL_client_server(&client_cb, &server_cb);
  7128. /* sni abort - success */
  7129. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7130. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7131. test_wolfSSL_client_server(&client_cb, &server_cb);
  7132. /* sni abort - abort when absent (ctx) */
  7133. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7134. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7135. test_wolfSSL_client_server(&client_cb, &server_cb);
  7136. /* sni abort - abort when absent (ssl) */
  7137. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7138. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7139. test_wolfSSL_client_server(&client_cb, &server_cb);
  7140. /* sni abort - success when overwritten */
  7141. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7142. 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;
  7143. test_wolfSSL_client_server(&client_cb, &server_cb);
  7144. /* sni abort - success when allowing mismatches */
  7145. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7146. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  7147. test_wolfSSL_client_server(&client_cb, &server_cb);
  7148. }
  7149. res = TEST_RES_CHECK(1);
  7150. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7151. return res;
  7152. }
  7153. static int test_wolfSSL_SNI_GetFromBuffer(void)
  7154. {
  7155. byte buff[] = { /* www.paypal.com */
  7156. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  7157. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  7158. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  7159. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7160. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7161. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  7162. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  7163. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  7164. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7165. };
  7166. byte buff2[] = { /* api.textmate.org */
  7167. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  7168. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7169. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7170. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7171. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7172. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7173. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7174. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7175. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7176. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7177. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7178. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7179. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7180. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7181. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7182. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7183. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7184. };
  7185. byte buff3[] = { /* no sni extension */
  7186. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7187. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7188. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7189. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7190. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7191. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7192. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7193. };
  7194. byte buff4[] = { /* last extension has zero size */
  7195. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7196. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7197. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7198. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7199. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7200. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7201. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7202. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7203. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7204. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7205. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7206. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7207. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7208. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7209. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7210. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7211. 0x12, 0x00, 0x00
  7212. };
  7213. byte buff5[] = { /* SSL v2.0 client hello */
  7214. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7215. /* dummy bytes bellow, just to pass size check */
  7216. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7217. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7218. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7219. };
  7220. byte result[32] = {0};
  7221. word32 length = 32;
  7222. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7223. 0, result, &length));
  7224. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7225. 0, result, &length));
  7226. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7227. 1, result, &length));
  7228. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7229. 0, result, &length));
  7230. buff[0] = 0x16;
  7231. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7232. 0, result, &length));
  7233. buff[1] = 0x03;
  7234. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7235. sizeof(buff), 0, result, &length));
  7236. buff[2] = 0x03;
  7237. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7238. sizeof(buff), 0, result, &length));
  7239. buff[4] = 0x64;
  7240. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7241. 0, result, &length));
  7242. result[length] = 0;
  7243. AssertStrEQ("www.paypal.com", (const char*) result);
  7244. length = 32;
  7245. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7246. 0, result, &length));
  7247. result[length] = 0;
  7248. AssertStrEQ("api.textmate.org", (const char*) result);
  7249. /* SSL v2.0 tests */
  7250. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7251. sizeof(buff5), 0, result, &length));
  7252. buff5[2] = 0x02;
  7253. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7254. sizeof(buff5), 0, result, &length));
  7255. buff5[2] = 0x01; buff5[6] = 0x08;
  7256. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7257. sizeof(buff5), 0, result, &length));
  7258. buff5[6] = 0x09; buff5[8] = 0x01;
  7259. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7260. sizeof(buff5), 0, result, &length));
  7261. return TEST_RES_CHECK(1);
  7262. }
  7263. #endif /* HAVE_SNI */
  7264. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7265. static int test_wolfSSL_UseTrustedCA(void)
  7266. {
  7267. int res = TEST_SKIPPED;
  7268. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7269. && !defined(NO_RSA)
  7270. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7271. WOLFSSL_CTX *ctx;
  7272. WOLFSSL *ssl;
  7273. byte id[20];
  7274. #ifndef NO_WOLFSSL_SERVER
  7275. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7276. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7277. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7278. #else
  7279. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7280. #endif
  7281. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7282. XMEMSET(id, 0, sizeof(id));
  7283. /* error cases */
  7284. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7285. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7286. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7287. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7288. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7289. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7290. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7291. #ifdef NO_SHA
  7292. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7293. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7294. #endif
  7295. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7296. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7297. /* success cases */
  7298. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7299. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7300. #ifndef NO_SHA
  7301. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7302. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7303. #endif
  7304. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7305. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7306. wolfSSL_free(ssl);
  7307. wolfSSL_CTX_free(ctx);
  7308. res = TEST_RES_CHECK(1);
  7309. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7310. #endif /* HAVE_TRUSTED_CA */
  7311. return res;
  7312. }
  7313. static int test_wolfSSL_UseMaxFragment(void)
  7314. {
  7315. int res = TEST_SKIPPED;
  7316. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7317. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7318. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7319. #ifndef NO_WOLFSSL_SERVER
  7320. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7321. #else
  7322. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7323. #endif
  7324. WOLFSSL *ssl;
  7325. #ifdef OPENSSL_EXTRA
  7326. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7327. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7328. #else
  7329. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7330. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7331. #endif
  7332. #ifndef NO_WOLFSSL_SERVER
  7333. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7334. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7335. #endif
  7336. AssertNotNull(ctx);
  7337. ssl = wolfSSL_new(ctx);
  7338. AssertNotNull(ssl);
  7339. #ifdef OPENSSL_EXTRA
  7340. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7341. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7342. #else
  7343. UseMaxFragment = wolfSSL_UseMaxFragment;
  7344. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7345. #endif
  7346. /* error cases */
  7347. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7348. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7349. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7350. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7351. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7352. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7353. /* success case */
  7354. #ifdef OPENSSL_EXTRA
  7355. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7356. #else
  7357. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7358. #endif
  7359. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7360. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7361. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7362. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7363. #ifdef OPENSSL_EXTRA
  7364. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7365. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7366. #else
  7367. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7368. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7369. #endif
  7370. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7371. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7372. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7373. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7374. #ifdef OPENSSL_EXTRA
  7375. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7376. #else
  7377. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7378. #endif
  7379. wolfSSL_free(ssl);
  7380. wolfSSL_CTX_free(ctx);
  7381. res = TEST_RES_CHECK(1);
  7382. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7383. #endif
  7384. return res;
  7385. }
  7386. static int test_wolfSSL_UseTruncatedHMAC(void)
  7387. {
  7388. int res = TEST_SKIPPED;
  7389. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7390. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7391. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7392. #ifndef NO_WOLFSSL_SERVER
  7393. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7394. #else
  7395. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7396. #endif
  7397. WOLFSSL *ssl;
  7398. AssertNotNull(ctx);
  7399. #ifndef NO_WOLFSSL_SERVER
  7400. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7401. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7402. #endif
  7403. ssl = wolfSSL_new(ctx);
  7404. AssertNotNull(ssl);
  7405. /* error cases */
  7406. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7407. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7408. /* success case */
  7409. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7410. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7411. wolfSSL_free(ssl);
  7412. wolfSSL_CTX_free(ctx);
  7413. res = TEST_RES_CHECK(1);
  7414. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7415. #endif
  7416. return res;
  7417. }
  7418. static int test_wolfSSL_UseSupportedCurve(void)
  7419. {
  7420. int res = TEST_SKIPPED;
  7421. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7422. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7423. WOLFSSL *ssl = wolfSSL_new(ctx);
  7424. AssertNotNull(ctx);
  7425. AssertNotNull(ssl);
  7426. /* error cases */
  7427. AssertIntNE(WOLFSSL_SUCCESS,
  7428. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7429. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7430. AssertIntNE(WOLFSSL_SUCCESS,
  7431. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7432. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7433. /* success case */
  7434. AssertIntEQ(WOLFSSL_SUCCESS,
  7435. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7436. AssertIntEQ(WOLFSSL_SUCCESS,
  7437. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7438. wolfSSL_free(ssl);
  7439. wolfSSL_CTX_free(ctx);
  7440. res = TEST_RES_CHECK(1);
  7441. #endif
  7442. return res;
  7443. }
  7444. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7445. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7446. {
  7447. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7448. }
  7449. static void use_ALPN_all(WOLFSSL* ssl)
  7450. {
  7451. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7452. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7453. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7454. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7455. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7456. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7457. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7458. }
  7459. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7460. {
  7461. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7462. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7463. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7464. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7465. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7466. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7467. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7468. }
  7469. static void use_ALPN_one(WOLFSSL* ssl)
  7470. {
  7471. /* spdy/2 */
  7472. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7473. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7474. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7475. }
  7476. static void use_ALPN_unknown(WOLFSSL* ssl)
  7477. {
  7478. /* http/2.0 */
  7479. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7480. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7481. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7482. }
  7483. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7484. {
  7485. /* http/2.0 */
  7486. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7487. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7488. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7489. }
  7490. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7491. {
  7492. /* spdy/3 */
  7493. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7494. char *proto = NULL;
  7495. word16 protoSz = 0;
  7496. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7497. /* check value */
  7498. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7499. if (proto) {
  7500. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7501. }
  7502. }
  7503. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  7504. {
  7505. char *proto = NULL;
  7506. word16 protoSz = 0;
  7507. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  7508. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7509. /* check value */
  7510. AssertIntEQ(1, (0 == protoSz));
  7511. AssertIntEQ(1, (NULL == proto));
  7512. }
  7513. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  7514. {
  7515. /* http/1.1 */
  7516. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7517. char *proto;
  7518. word16 protoSz = 0;
  7519. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7520. /* check value */
  7521. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7522. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7523. }
  7524. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  7525. {
  7526. /* spdy/2 */
  7527. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7528. char *proto;
  7529. word16 protoSz = 0;
  7530. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7531. /* check value */
  7532. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7533. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7534. }
  7535. static void verify_ALPN_client_list(WOLFSSL* ssl)
  7536. {
  7537. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7538. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7539. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7540. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7541. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7542. char *clist = NULL;
  7543. word16 clistSz = 0;
  7544. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  7545. &clistSz));
  7546. /* check value */
  7547. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  7548. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  7549. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  7550. }
  7551. static int test_wolfSSL_UseALPN_connection(void)
  7552. {
  7553. int res = TEST_SKIPPED;
  7554. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7555. callback_functions client_cb;
  7556. callback_functions server_cb;
  7557. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7558. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7559. client_cb.method = wolfSSLv23_client_method;
  7560. server_cb.method = wolfSSLv23_server_method;
  7561. client_cb.devId = testDevId;
  7562. server_cb.devId = testDevId;
  7563. /* success case same list */
  7564. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7565. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  7566. test_wolfSSL_client_server(&client_cb, &server_cb);
  7567. /* success case only one for server */
  7568. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7569. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  7570. test_wolfSSL_client_server(&client_cb, &server_cb);
  7571. /* success case only one for client */
  7572. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  7573. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  7574. test_wolfSSL_client_server(&client_cb, &server_cb);
  7575. /* success case none for client */
  7576. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7577. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  7578. test_wolfSSL_client_server(&client_cb, &server_cb);
  7579. /* success case mismatch behavior but option 'continue' set */
  7580. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  7581. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  7582. test_wolfSSL_client_server(&client_cb, &server_cb);
  7583. /* success case read protocol send by client */
  7584. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7585. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  7586. test_wolfSSL_client_server(&client_cb, &server_cb);
  7587. /* mismatch behavior with same list
  7588. * the first and only this one must be taken */
  7589. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7590. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  7591. test_wolfSSL_client_server(&client_cb, &server_cb);
  7592. /* default mismatch behavior */
  7593. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7594. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  7595. test_wolfSSL_client_server(&client_cb, &server_cb);
  7596. res = TEST_RES_CHECK(1);
  7597. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7598. return res;
  7599. }
  7600. static int test_wolfSSL_UseALPN_params(void)
  7601. {
  7602. int res = TEST_SKIPPED;
  7603. #ifndef NO_WOLFSSL_CLIENT
  7604. /* "http/1.1" */
  7605. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7606. /* "spdy/1" */
  7607. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  7608. /* "spdy/2" */
  7609. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7610. /* "spdy/3" */
  7611. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7612. char buff[256];
  7613. word32 idx;
  7614. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7615. WOLFSSL *ssl = wolfSSL_new(ctx);
  7616. AssertNotNull(ctx);
  7617. AssertNotNull(ssl);
  7618. /* error cases */
  7619. AssertIntNE(WOLFSSL_SUCCESS,
  7620. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  7621. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7622. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  7623. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7624. /* success case */
  7625. /* http1 only */
  7626. AssertIntEQ(WOLFSSL_SUCCESS,
  7627. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  7628. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7629. /* http1, spdy1 */
  7630. XMEMCPY(buff, http1, sizeof(http1));
  7631. idx = sizeof(http1);
  7632. buff[idx++] = ',';
  7633. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7634. idx += sizeof(spdy1);
  7635. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7636. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7637. /* http1, spdy2, spdy1 */
  7638. XMEMCPY(buff, http1, sizeof(http1));
  7639. idx = sizeof(http1);
  7640. buff[idx++] = ',';
  7641. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7642. idx += sizeof(spdy2);
  7643. buff[idx++] = ',';
  7644. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7645. idx += sizeof(spdy1);
  7646. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7647. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7648. /* spdy3, http1, spdy2, spdy1 */
  7649. XMEMCPY(buff, spdy3, sizeof(spdy3));
  7650. idx = sizeof(spdy3);
  7651. buff[idx++] = ',';
  7652. XMEMCPY(buff+idx, http1, sizeof(http1));
  7653. idx += sizeof(http1);
  7654. buff[idx++] = ',';
  7655. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7656. idx += sizeof(spdy2);
  7657. buff[idx++] = ',';
  7658. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7659. idx += sizeof(spdy1);
  7660. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7661. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7662. wolfSSL_free(ssl);
  7663. wolfSSL_CTX_free(ctx);
  7664. res = TEST_RES_CHECK(1);
  7665. #endif
  7666. return res;
  7667. }
  7668. #endif /* HAVE_ALPN */
  7669. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7670. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7671. {
  7672. unsigned char p[] = {
  7673. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7674. 6, 's', 'p', 'd', 'y', '/', '2',
  7675. 6, 's', 'p', 'd', 'y', '/', '1',
  7676. };
  7677. unsigned char p_len = sizeof(p);
  7678. int ret;
  7679. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7680. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7681. AssertIntEQ(ret, 0);
  7682. #else
  7683. AssertIntEQ(ret, SSL_SUCCESS);
  7684. #endif
  7685. }
  7686. static void set_alpn_protos(SSL* ssl)
  7687. {
  7688. unsigned char p[] = {
  7689. 6, 's', 'p', 'd', 'y', '/', '3',
  7690. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7691. 6, 's', 'p', 'd', 'y', '/', '2',
  7692. 6, 's', 'p', 'd', 'y', '/', '1',
  7693. };
  7694. unsigned char p_len = sizeof(p);
  7695. int ret;
  7696. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7697. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7698. AssertIntEQ(ret, 0);
  7699. #else
  7700. AssertIntEQ(ret, SSL_SUCCESS);
  7701. #endif
  7702. }
  7703. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7704. {
  7705. /* "spdy/3" */
  7706. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7707. const unsigned char *proto;
  7708. unsigned int protoSz = 0;
  7709. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7710. /* check value */
  7711. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7712. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7713. }
  7714. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7715. {
  7716. /* "http/1.1" */
  7717. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7718. const unsigned char *proto;
  7719. unsigned int protoSz = 0;
  7720. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7721. /* check value */
  7722. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7723. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7724. }
  7725. static int test_wolfSSL_set_alpn_protos(void)
  7726. {
  7727. int res = TEST_SKIPPED;
  7728. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7729. callback_functions client_cb;
  7730. callback_functions server_cb;
  7731. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7732. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7733. client_cb.method = wolfSSLv23_client_method;
  7734. server_cb.method = wolfSSLv23_server_method;
  7735. client_cb.devId = testDevId;
  7736. server_cb.devId = testDevId;
  7737. /* use CTX_alpn_protos */
  7738. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7739. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7740. test_wolfSSL_client_server(&client_cb, &server_cb);
  7741. /* use set_alpn_protos */
  7742. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7743. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7744. test_wolfSSL_client_server(&client_cb, &server_cb);
  7745. res = TEST_RES_CHECK(1);
  7746. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7747. return res;
  7748. }
  7749. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7750. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7751. {
  7752. int res = TEST_SKIPPED;
  7753. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7754. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7755. WOLFSSL *ssl = wolfSSL_new(ctx);
  7756. AssertNotNull(ctx);
  7757. AssertNotNull(ssl);
  7758. /* error cases */
  7759. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7760. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7761. /* success cases */
  7762. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7763. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7764. wolfSSL_free(ssl);
  7765. wolfSSL_CTX_free(ctx);
  7766. res = TEST_RES_CHECK(1);
  7767. #endif
  7768. return res;
  7769. }
  7770. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7771. {
  7772. int res = TEST_SKIPPED;
  7773. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7774. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7775. WOLFSSL *ssl = wolfSSL_new(ctx);
  7776. AssertNotNull(ctx);
  7777. AssertNotNull(ssl);
  7778. /* error cases */
  7779. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7780. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7781. /* success cases */
  7782. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7783. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7784. wolfSSL_free(ssl);
  7785. wolfSSL_CTX_free(ctx);
  7786. res = TEST_RES_CHECK(1);
  7787. #endif
  7788. return res;
  7789. }
  7790. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC))
  7791. /* Called when writing. */
  7792. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7793. {
  7794. (void)ssl;
  7795. (void)buf;
  7796. (void)sz;
  7797. (void)ctx;
  7798. /* Force error return from wolfSSL_accept_TLSv13(). */
  7799. return WANT_WRITE;
  7800. }
  7801. /* Called when reading. */
  7802. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  7803. {
  7804. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  7805. int len = (int)msg->length;
  7806. (void)ssl;
  7807. (void)sz;
  7808. /* Pass back as much of message as will fit in buffer. */
  7809. if (len > sz)
  7810. len = sz;
  7811. XMEMCPY(buf, msg->buffer, len);
  7812. /* Move over returned data. */
  7813. msg->buffer += len;
  7814. msg->length -= len;
  7815. /* Amount actually copied. */
  7816. return len;
  7817. }
  7818. #endif
  7819. /* Test the detection of duplicate known TLS extensions.
  7820. * Specifically in a ClientHello.
  7821. */
  7822. static int test_tls_ext_duplicate(void)
  7823. {
  7824. int res = TEST_SKIPPED;
  7825. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC))
  7826. const unsigned char clientHelloDupTlsExt[] = {
  7827. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  7828. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  7829. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  7830. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  7831. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  7832. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  7833. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  7834. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  7835. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7836. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  7837. 0x00, 0x9e, 0x01, 0x00,
  7838. /* Extensions - duplicate signature algorithms. */
  7839. 0x00, 0x19, 0x00, 0x0d,
  7840. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  7841. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  7842. /* Supported Versions extension for TLS 1.3. */
  7843. 0x00, 0x2b,
  7844. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  7845. };
  7846. WOLFSSL_BUFFER_INFO msg;
  7847. const char* testCertFile;
  7848. const char* testKeyFile;
  7849. WOLFSSL_CTX *ctx;
  7850. WOLFSSL *ssl;
  7851. #ifndef NO_RSA
  7852. testCertFile = svrCertFile;
  7853. testKeyFile = svrKeyFile;
  7854. #elif defined(HAVE_ECC)
  7855. testCertFile = eccCertFile;
  7856. testKeyFile = eccKeyFile;
  7857. #endif
  7858. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7859. AssertNotNull(ctx);
  7860. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  7861. WOLFSSL_FILETYPE_PEM));
  7862. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  7863. WOLFSSL_FILETYPE_PEM));
  7864. /* Read from 'msg'. */
  7865. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  7866. /* No where to send to - dummy sender. */
  7867. wolfSSL_SetIOSend(ctx, DummySend);
  7868. ssl = wolfSSL_new(ctx);
  7869. AssertNotNull(ssl);
  7870. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  7871. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  7872. wolfSSL_SetIOReadCtx(ssl, &msg);
  7873. AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  7874. AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  7875. wolfSSL_free(ssl);
  7876. wolfSSL_CTX_free(ctx);
  7877. res = TEST_RES_CHECK(1);
  7878. #endif
  7879. return res;
  7880. }
  7881. /*----------------------------------------------------------------------------*
  7882. | X509 Tests
  7883. *----------------------------------------------------------------------------*/
  7884. static int test_wolfSSL_X509_NAME_get_entry(void)
  7885. {
  7886. int res = TEST_SKIPPED;
  7887. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7888. #if defined(OPENSSL_ALL) || \
  7889. (defined(OPENSSL_EXTRA) && \
  7890. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7891. /* use openssl like name to test mapping */
  7892. X509_NAME_ENTRY* ne;
  7893. X509_NAME* name;
  7894. X509* x509;
  7895. #ifndef NO_FILESYSTEM
  7896. ASN1_STRING* asn;
  7897. char* subCN = NULL;
  7898. #endif
  7899. int idx;
  7900. ASN1_OBJECT *object = NULL;
  7901. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7902. defined(WOLFSSL_NGINX)
  7903. #ifndef NO_BIO
  7904. BIO* bio;
  7905. #endif
  7906. #endif
  7907. #ifndef NO_FILESYSTEM
  7908. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7909. WOLFSSL_FILETYPE_PEM);
  7910. AssertNotNull(x509);
  7911. name = X509_get_subject_name(x509);
  7912. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7913. AssertIntGE(idx, 0);
  7914. ne = X509_NAME_get_entry(name, idx);
  7915. AssertNotNull(ne);
  7916. asn = X509_NAME_ENTRY_get_data(ne);
  7917. AssertNotNull(asn);
  7918. subCN = (char*)ASN1_STRING_data(asn);
  7919. AssertNotNull(subCN);
  7920. wolfSSL_FreeX509(x509);
  7921. #endif
  7922. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7923. WOLFSSL_FILETYPE_PEM);
  7924. AssertNotNull(x509);
  7925. name = X509_get_subject_name(x509);
  7926. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7927. AssertIntGE(idx, 0);
  7928. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  7929. defined(WOLFSSL_NGINX)
  7930. #ifndef NO_BIO
  7931. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7932. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7933. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7934. AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  7935. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7936. BIO_free(bio);
  7937. #endif
  7938. #endif
  7939. ne = X509_NAME_get_entry(name, idx);
  7940. AssertNotNull(ne);
  7941. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7942. wolfSSL_FreeX509(x509);
  7943. res = TEST_RES_CHECK(1);
  7944. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7945. #endif /* !NO_CERTS && !NO_RSA */
  7946. return res;
  7947. }
  7948. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7949. static int test_wolfSSL_PKCS12(void)
  7950. {
  7951. int res = TEST_SKIPPED;
  7952. /* .p12 file is encrypted with DES3 */
  7953. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7954. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7955. * Password Key
  7956. */
  7957. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7958. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7959. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7960. byte buf[6000];
  7961. char file[] = "./certs/test-servercert.p12";
  7962. char order[] = "./certs/ecc-rsa-server.p12";
  7963. #ifdef WC_RC2
  7964. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7965. #endif
  7966. char pass[] = "a password";
  7967. const char goodPsw[] = "wolfSSL test";
  7968. const char badPsw[] = "bad";
  7969. #ifdef HAVE_ECC
  7970. WOLFSSL_X509_NAME* subject;
  7971. WOLFSSL_X509 *x509;
  7972. #endif
  7973. XFILE f;
  7974. int bytes, ret, goodPswLen, badPswLen;
  7975. WOLFSSL_BIO *bio;
  7976. WOLFSSL_EVP_PKEY *pkey;
  7977. WC_PKCS12 *pkcs12;
  7978. WC_PKCS12 *pkcs12_2;
  7979. WOLFSSL_X509 *cert;
  7980. WOLFSSL_X509 *tmp;
  7981. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7982. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7983. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7984. WOLFSSL_CTX *ctx;
  7985. WOLFSSL *ssl;
  7986. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7987. #endif
  7988. f = XFOPEN(file, "rb");
  7989. AssertTrue((f != XBADFILE));
  7990. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7991. XFCLOSE(f);
  7992. goodPswLen = (int)XSTRLEN(goodPsw);
  7993. badPswLen = (int)XSTRLEN(badPsw);
  7994. bio = BIO_new_mem_buf((void*)buf, bytes);
  7995. AssertNotNull(bio);
  7996. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7997. AssertNotNull(pkcs12);
  7998. PKCS12_free(pkcs12);
  7999. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  8000. d2i_PKCS12_bio(bio, &pkcs12);
  8001. AssertNotNull(pkcs12);
  8002. BIO_free(bio);
  8003. /* check verify MAC directly */
  8004. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  8005. AssertIntEQ(ret, 1);
  8006. /* check verify MAC fail case directly */
  8007. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  8008. AssertIntEQ(ret, 0);
  8009. /* check verify MAC fail case */
  8010. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8011. AssertIntEQ(ret, 0);
  8012. AssertNull(pkey);
  8013. AssertNull(cert);
  8014. /* check parse with no extra certs kept */
  8015. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8016. AssertIntEQ(ret, 1);
  8017. AssertNotNull(pkey);
  8018. AssertNotNull(cert);
  8019. wolfSSL_EVP_PKEY_free(pkey);
  8020. wolfSSL_X509_free(cert);
  8021. /* check parse with extra certs kept */
  8022. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8023. AssertIntEQ(ret, 1);
  8024. AssertNotNull(pkey);
  8025. AssertNotNull(cert);
  8026. AssertNotNull(ca);
  8027. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8028. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8029. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  8030. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8031. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8032. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8033. #else
  8034. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8035. #endif
  8036. /* Copy stack structure */
  8037. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  8038. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  8039. /* CTX now owns the tmp_ca stack structure */
  8040. tmp_ca = NULL;
  8041. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  8042. AssertNotNull(tmp_ca);
  8043. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  8044. /* Check that the main cert is also set */
  8045. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  8046. AssertNotNull(ssl = SSL_new(ctx));
  8047. AssertNotNull(SSL_get_certificate(ssl));
  8048. SSL_free(ssl);
  8049. SSL_CTX_free(ctx);
  8050. #endif
  8051. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8052. /* should be 2 other certs on stack */
  8053. tmp = sk_X509_pop(ca);
  8054. AssertNotNull(tmp);
  8055. X509_free(tmp);
  8056. tmp = sk_X509_pop(ca);
  8057. AssertNotNull(tmp);
  8058. X509_free(tmp);
  8059. AssertNull(sk_X509_pop(ca));
  8060. EVP_PKEY_free(pkey);
  8061. X509_free(cert);
  8062. sk_X509_pop_free(ca, X509_free);
  8063. /* check PKCS12_create */
  8064. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  8065. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  8066. SSL_SUCCESS);
  8067. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8068. -1, -1, 100, -1, 0)));
  8069. EVP_PKEY_free(pkey);
  8070. X509_free(cert);
  8071. sk_X509_pop_free(ca, NULL);
  8072. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8073. SSL_SUCCESS);
  8074. PKCS12_free(pkcs12_2);
  8075. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8076. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8077. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8078. 2000, 1, 0)));
  8079. EVP_PKEY_free(pkey);
  8080. X509_free(cert);
  8081. sk_X509_pop_free(ca, NULL);
  8082. /* convert to DER then back and parse */
  8083. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8084. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  8085. PKCS12_free(pkcs12_2);
  8086. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  8087. BIO_free(bio);
  8088. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8089. SSL_SUCCESS);
  8090. /* should be 2 other certs on stack */
  8091. tmp = sk_X509_pop(ca);
  8092. AssertNotNull(tmp);
  8093. X509_free(tmp);
  8094. tmp = sk_X509_pop(ca);
  8095. AssertNotNull(tmp);
  8096. X509_free(tmp);
  8097. AssertNull(sk_X509_pop(ca));
  8098. #ifndef NO_RC4
  8099. PKCS12_free(pkcs12_2);
  8100. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  8101. NID_pbe_WithSHA1And128BitRC4,
  8102. NID_pbe_WithSHA1And128BitRC4,
  8103. 2000, 1, 0)));
  8104. EVP_PKEY_free(pkey);
  8105. X509_free(cert);
  8106. sk_X509_pop_free(ca, NULL);
  8107. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8108. SSL_SUCCESS);
  8109. #endif /* NO_RC4 */
  8110. EVP_PKEY_free(pkey);
  8111. X509_free(cert);
  8112. PKCS12_free(pkcs12);
  8113. PKCS12_free(pkcs12_2);
  8114. sk_X509_pop_free(ca, NULL);
  8115. #ifdef HAVE_ECC
  8116. /* test order of parsing */
  8117. f = XFOPEN(order, "rb");
  8118. AssertTrue(f != XBADFILE);
  8119. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8120. XFCLOSE(f);
  8121. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8122. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8123. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  8124. WOLFSSL_SUCCESS);
  8125. /* check use of pkey after parse */
  8126. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8127. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8128. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8129. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8130. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8131. #else
  8132. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8133. #endif
  8134. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  8135. SSL_CTX_free(ctx);
  8136. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8137. #endif
  8138. AssertNotNull(pkey);
  8139. AssertNotNull(cert);
  8140. AssertNotNull(ca);
  8141. /* compare subject lines of certificates */
  8142. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8143. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  8144. SSL_FILETYPE_PEM));
  8145. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8146. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8147. X509_free(x509);
  8148. /* test expected fail case */
  8149. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8150. SSL_FILETYPE_PEM));
  8151. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8152. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8153. X509_free(x509);
  8154. X509_free(cert);
  8155. /* get subject line from ca stack */
  8156. AssertNotNull(cert = sk_X509_pop(ca));
  8157. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8158. /* compare subject from certificate in ca to expected */
  8159. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8160. SSL_FILETYPE_PEM));
  8161. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8162. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8163. EVP_PKEY_free(pkey);
  8164. X509_free(x509);
  8165. X509_free(cert);
  8166. BIO_free(bio);
  8167. PKCS12_free(pkcs12);
  8168. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  8169. /* test order of parsing */
  8170. f = XFOPEN(file, "rb");
  8171. AssertTrue(f != XBADFILE);
  8172. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8173. XFCLOSE(f);
  8174. /* check verify MAC fail case */
  8175. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8176. AssertIntEQ(ret, 0);
  8177. AssertNull(pkey);
  8178. AssertNull(cert);
  8179. /* check parse with no extra certs kept */
  8180. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8181. AssertIntEQ(ret, 1);
  8182. AssertNotNull(pkey);
  8183. AssertNotNull(cert);
  8184. wolfSSL_EVP_PKEY_free(pkey);
  8185. wolfSSL_X509_free(cert);
  8186. /* check parse with extra certs kept */
  8187. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8188. AssertIntEQ(ret, 1);
  8189. AssertNotNull(pkey);
  8190. AssertNotNull(cert);
  8191. AssertNotNull(ca);
  8192. wolfSSL_EVP_PKEY_free(pkey);
  8193. wolfSSL_X509_free(cert);
  8194. sk_X509_pop_free(ca, NULL);
  8195. PKCS12_free(pkcs12);
  8196. #endif /* HAVE_ECC */
  8197. #ifdef WC_RC2
  8198. /* test PKCS#12 with RC2 encryption */
  8199. f = XFOPEN(rc2p12, "rb");
  8200. AssertTrue(f != XBADFILE);
  8201. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8202. XFCLOSE(f);
  8203. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8204. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8205. /* check verify MAC fail case */
  8206. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8207. AssertIntEQ(ret, 0);
  8208. AssertNull(pkey);
  8209. AssertNull(cert);
  8210. /* check parse iwth not extra certs kept */
  8211. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8212. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8213. AssertNotNull(pkey);
  8214. AssertNotNull(cert);
  8215. /* check parse with extra certs kept */
  8216. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8217. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8218. AssertNotNull(pkey);
  8219. AssertNotNull(cert);
  8220. AssertNotNull(ca);
  8221. wolfSSL_EVP_PKEY_free(pkey);
  8222. wolfSSL_X509_free(cert);
  8223. sk_X509_pop_free(ca, NULL);
  8224. BIO_free(bio);
  8225. PKCS12_free(pkcs12);
  8226. #endif /* WC_RC2 */
  8227. /* Test i2d_PKCS12_bio */
  8228. f = XFOPEN(file, "rb");
  8229. AssertTrue((f != XBADFILE));
  8230. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8231. XFCLOSE(f);
  8232. bio = BIO_new(BIO_s_mem());
  8233. AssertNotNull(bio);
  8234. ret = i2d_PKCS12_bio(bio, pkcs12);
  8235. AssertIntEQ(ret, 1);
  8236. ret = i2d_PKCS12_bio(NULL, pkcs12);
  8237. AssertIntEQ(ret, 0);
  8238. ret = i2d_PKCS12_bio(bio, NULL);
  8239. AssertIntEQ(ret, 0);
  8240. PKCS12_free(pkcs12);
  8241. BIO_free(bio);
  8242. (void)order;
  8243. res = TEST_RES_CHECK(1);
  8244. #endif /* OPENSSL_EXTRA */
  8245. #endif /* HAVE_FIPS */
  8246. return res;
  8247. }
  8248. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8249. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  8250. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  8251. #define TEST_PKCS8_ENC
  8252. #endif
  8253. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8254. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8255. /* used to keep track if FailTestCallback was called */
  8256. static int failTestCallbackCalled = 0;
  8257. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  8258. {
  8259. (void)passwd;
  8260. (void)sz;
  8261. (void)rw;
  8262. (void)userdata;
  8263. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8264. failTestCallbackCalled = 1;
  8265. return -1;
  8266. }
  8267. #endif
  8268. static int test_wolfSSL_no_password_cb(void)
  8269. {
  8270. int res = TEST_SKIPPED;
  8271. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8272. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8273. WOLFSSL_CTX* ctx;
  8274. byte buff[FOURK_BUF];
  8275. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8276. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8277. XFILE f;
  8278. int bytes;
  8279. #ifndef NO_WOLFSSL_CLIENT
  8280. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8281. #else
  8282. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8283. #endif
  8284. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8285. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8286. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8287. XFCLOSE(f);
  8288. AssertIntLE(bytes, sizeof(buff));
  8289. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8290. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8291. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8292. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8293. XFCLOSE(f);
  8294. AssertIntLE(bytes, sizeof(buff));
  8295. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8296. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8297. wolfSSL_CTX_free(ctx);
  8298. if (failTestCallbackCalled != 0) {
  8299. Fail(("Password callback should not be called by default"),
  8300. ("Password callback was called without attempting "
  8301. "to first decipher private key without password."));
  8302. }
  8303. res = TEST_RES_CHECK(1);
  8304. #endif
  8305. return res;
  8306. }
  8307. #ifdef TEST_PKCS8_ENC
  8308. /* for PKCS8 test case */
  8309. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8310. {
  8311. int flag = 0;
  8312. (void)rw;
  8313. if (userdata != NULL) {
  8314. flag = *((int*)userdata); /* user set data */
  8315. }
  8316. switch (flag) {
  8317. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8318. * can be anything the user wishes to be passed to the callback
  8319. * associated with the WOLFSSL_CTX */
  8320. XSTRNCPY(passwd, "yassl123", sz);
  8321. return 8;
  8322. default:
  8323. return BAD_FUNC_ARG;
  8324. }
  8325. }
  8326. #endif /* TEST_PKCS8_ENC */
  8327. /* Testing functions dealing with PKCS8 */
  8328. static int test_wolfSSL_PKCS8(void)
  8329. {
  8330. int res = TEST_SKIPPED;
  8331. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  8332. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8333. byte buff[FOURK_BUF];
  8334. byte der[FOURK_BUF];
  8335. #ifndef NO_RSA
  8336. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8337. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8338. #endif
  8339. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8340. #ifdef HAVE_ECC
  8341. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8342. #endif
  8343. XFILE f;
  8344. int bytes;
  8345. WOLFSSL_CTX* ctx;
  8346. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8347. int ret;
  8348. ecc_key key;
  8349. word32 x = 0;
  8350. #endif
  8351. #ifdef TEST_PKCS8_ENC
  8352. #if !defined(NO_RSA) && !defined(NO_SHA)
  8353. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8354. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8355. #endif
  8356. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8357. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8358. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8359. #endif
  8360. int flag;
  8361. #endif
  8362. (void)der;
  8363. #ifndef NO_WOLFSSL_CLIENT
  8364. #ifndef WOLFSSL_NO_TLS12
  8365. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8366. #else
  8367. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8368. #endif
  8369. #else
  8370. #ifndef WOLFSSL_NO_TLS12
  8371. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8372. #else
  8373. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8374. #endif
  8375. #endif
  8376. #ifdef TEST_PKCS8_ENC
  8377. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8378. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8379. flag = 1; /* used by password callback as return code */
  8380. #if !defined(NO_RSA) && !defined(NO_SHA)
  8381. /* test loading PEM PKCS8 encrypted file */
  8382. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8383. AssertTrue((f != XBADFILE));
  8384. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8385. XFCLOSE(f);
  8386. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8387. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8388. /* this next case should fail because of password callback return code */
  8389. flag = 0; /* used by password callback as return code */
  8390. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8391. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8392. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8393. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8394. "yassl123"), 0);
  8395. /* test that error value is returned with a bad password */
  8396. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8397. "bad"), 0);
  8398. /* test loading PEM PKCS8 encrypted file */
  8399. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8400. AssertTrue((f != XBADFILE));
  8401. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8402. XFCLOSE(f);
  8403. flag = 1; /* used by password callback as return code */
  8404. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8405. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8406. /* this next case should fail because of password callback return code */
  8407. flag = 0; /* used by password callback as return code */
  8408. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8409. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8410. #endif /* !NO_RSA && !NO_SHA */
  8411. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8412. /* test loading PEM PKCS8 encrypted ECC Key file */
  8413. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8414. AssertTrue((f != XBADFILE));
  8415. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8416. XFCLOSE(f);
  8417. flag = 1; /* used by password callback as return code */
  8418. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8419. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8420. /* this next case should fail because of password callback return code */
  8421. flag = 0; /* used by password callback as return code */
  8422. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8423. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8424. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8425. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8426. "yassl123"), 0);
  8427. /* test that error value is returned with a bad password */
  8428. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8429. "bad"), 0);
  8430. /* test loading DER PKCS8 encrypted ECC Key file */
  8431. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8432. AssertTrue((f != XBADFILE));
  8433. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8434. XFCLOSE(f);
  8435. flag = 1; /* used by password callback as return code */
  8436. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8437. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8438. /* this next case should fail because of password callback return code */
  8439. flag = 0; /* used by password callback as return code */
  8440. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8441. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8442. /* leave flag as "okay" */
  8443. flag = 1;
  8444. #endif /* HAVE_ECC && !NO_SHA */
  8445. #endif /* TEST_PKCS8_ENC */
  8446. #ifndef NO_RSA
  8447. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8448. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8449. AssertTrue((f != XBADFILE));
  8450. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8451. XFCLOSE(f);
  8452. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8453. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8454. /* test loading PEM PKCS8 private key file (not encrypted) */
  8455. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  8456. AssertTrue((f != XBADFILE));
  8457. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8458. XFCLOSE(f);
  8459. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8460. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8461. #endif /* !NO_RSA */
  8462. /* Test PKCS8 PEM ECC key no crypt */
  8463. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  8464. AssertTrue((f != XBADFILE));
  8465. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8466. XFCLOSE(f);
  8467. #ifdef HAVE_ECC
  8468. /* Test PKCS8 PEM ECC key no crypt */
  8469. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8470. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8471. #ifndef NO_CODING
  8472. /* decrypt PKCS8 PEM to key in DER format */
  8473. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  8474. (word32)sizeof(der), NULL)), 0);
  8475. ret = wc_ecc_init(&key);
  8476. if (ret == 0) {
  8477. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  8478. wc_ecc_free(&key);
  8479. }
  8480. AssertIntEQ(ret, 0);
  8481. #endif
  8482. /* Test PKCS8 DER ECC key no crypt */
  8483. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  8484. AssertTrue((f != XBADFILE));
  8485. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8486. XFCLOSE(f);
  8487. /* Test using a PKCS8 ECC PEM */
  8488. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8489. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8490. #else
  8491. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  8492. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  8493. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  8494. #endif /* HAVE_ECC */
  8495. wolfSSL_CTX_free(ctx);
  8496. res = TEST_RES_CHECK(1);
  8497. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8498. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  8499. return res;
  8500. }
  8501. static int test_wolfSSL_PKCS8_ED25519(void)
  8502. {
  8503. int res = TEST_SKIPPED;
  8504. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8505. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  8506. defined(HAVE_ED25519_KEY_IMPORT)
  8507. const byte encPrivKey[] = \
  8508. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8509. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  8510. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  8511. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  8512. "2L9W4v7swXkV+X+a1ww=\n"
  8513. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8514. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8515. byte der[FOURK_BUF];
  8516. WOLFSSL_CTX* ctx;
  8517. int bytes;
  8518. XMEMSET(der, 0, sizeof(der));
  8519. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8520. (word32)sizeof(der), password)), 0);
  8521. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8522. #ifndef NO_WOLFSSL_SERVER
  8523. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8524. #else
  8525. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8526. #endif
  8527. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8528. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8529. wolfSSL_CTX_free(ctx);
  8530. res = TEST_RES_CHECK(1);
  8531. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8532. #endif
  8533. return res;
  8534. }
  8535. static int test_wolfSSL_PKCS8_ED448(void)
  8536. {
  8537. int res = TEST_SKIPPED;
  8538. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8539. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  8540. defined(HAVE_ED448_KEY_IMPORT)
  8541. const byte encPrivKey[] = \
  8542. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8543. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  8544. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  8545. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  8546. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  8547. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8548. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8549. byte der[FOURK_BUF];
  8550. WOLFSSL_CTX* ctx;
  8551. int bytes;
  8552. XMEMSET(der, 0, sizeof(der));
  8553. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8554. (word32)sizeof(der), password)), 0);
  8555. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8556. #ifndef NO_WOLFSSL_SERVER
  8557. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8558. #else
  8559. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8560. #endif
  8561. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8562. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8563. wolfSSL_CTX_free(ctx);
  8564. res = TEST_RES_CHECK(1);
  8565. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8566. #endif
  8567. return res;
  8568. }
  8569. /* Testing functions dealing with PKCS5 */
  8570. static int test_wolfSSL_PKCS5(void)
  8571. {
  8572. int res = TEST_SKIPPED;
  8573. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  8574. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  8575. const char* passwd = "myfipsPa$$W0rd";
  8576. #else
  8577. const char *passwd = "pass1234";
  8578. #endif
  8579. const unsigned char *salt = (unsigned char *)"salt1234";
  8580. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  8581. DYNAMIC_TYPE_TMP_BUFFER);
  8582. int ret = 0;
  8583. AssertNotNull(out);
  8584. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  8585. (int)XSTRLEN((const char *) salt), 10,
  8586. WC_SHA_DIGEST_SIZE,out);
  8587. AssertIntEQ(ret, SSL_SUCCESS);
  8588. #ifdef WOLFSSL_SHA512
  8589. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  8590. (int)XSTRLEN((const char *) salt), 10,
  8591. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  8592. AssertIntEQ(ret, SSL_SUCCESS);
  8593. #endif
  8594. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8595. res = TEST_RES_CHECK(1);
  8596. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  8597. return res;
  8598. }
  8599. /* test parsing URI from certificate */
  8600. static int test_wolfSSL_URI(void)
  8601. {
  8602. int res = TEST_SKIPPED;
  8603. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8604. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  8605. defined(OPENSSL_EXTRA))
  8606. WOLFSSL_X509* x509;
  8607. const char uri[] = "./certs/client-uri-cert.pem";
  8608. const char badUri[] = "./certs/client-relative-uri.pem";
  8609. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  8610. AssertNotNull(x509);
  8611. wolfSSL_FreeX509(x509);
  8612. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  8613. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  8614. && !defined(WOLFSSL_FPKI)
  8615. AssertNull(x509);
  8616. #else
  8617. AssertNotNull(x509);
  8618. wolfSSL_FreeX509(x509);
  8619. #endif
  8620. res = TEST_RES_CHECK(1);
  8621. #endif
  8622. return res;
  8623. }
  8624. static int test_wolfSSL_TBS(void)
  8625. {
  8626. int res = TEST_SKIPPED;
  8627. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8628. && defined(OPENSSL_EXTRA)
  8629. WOLFSSL_X509* x509;
  8630. const unsigned char* tbs;
  8631. int tbsSz;
  8632. AssertNotNull(x509 =
  8633. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8634. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  8635. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  8636. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  8637. AssertIntEQ(tbsSz, 1003);
  8638. wolfSSL_FreeX509(x509);
  8639. res = TEST_RES_CHECK(1);
  8640. #endif
  8641. return res;
  8642. }
  8643. static int test_wolfSSL_X509_verify(void)
  8644. {
  8645. int res = TEST_SKIPPED;
  8646. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8647. && defined(OPENSSL_EXTRA)
  8648. WOLFSSL_X509* ca;
  8649. WOLFSSL_X509* serv;
  8650. WOLFSSL_EVP_PKEY* pkey;
  8651. unsigned char buf[2048];
  8652. const unsigned char* pt = NULL;
  8653. int bufSz;
  8654. AssertNotNull(ca =
  8655. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8656. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  8657. WOLFSSL_SUCCESS);
  8658. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  8659. WOLFSSL_SUCCESS);
  8660. AssertIntEQ(bufSz, 294);
  8661. bufSz = 2048;
  8662. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  8663. WOLFSSL_SUCCESS);
  8664. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  8665. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  8666. AssertNotNull(serv =
  8667. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  8668. /* success case */
  8669. pt = buf;
  8670. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8671. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  8672. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  8673. wolfSSL_EVP_PKEY_free(pkey);
  8674. /* fail case */
  8675. bufSz = 2048;
  8676. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  8677. WOLFSSL_SUCCESS);
  8678. pt = buf;
  8679. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8680. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  8681. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  8682. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  8683. wolfSSL_EVP_PKEY_free(pkey);
  8684. wolfSSL_FreeX509(ca);
  8685. wolfSSL_FreeX509(serv);
  8686. res = TEST_RES_CHECK(1);
  8687. #endif
  8688. return res;
  8689. }
  8690. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8691. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8692. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8693. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8694. /* create certificate with version 2 */
  8695. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  8696. {
  8697. WOLFSSL_X509 *x509, *x509v2;
  8698. WOLFSSL_EVP_PKEY *priv, *pub;
  8699. unsigned char *der = NULL, *key = NULL, *pt;
  8700. char *header, *name;
  8701. int derSz;
  8702. long keySz;
  8703. XFILE fp;
  8704. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  8705. time_t t;
  8706. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8707. WOLFSSL_FILETYPE_PEM));
  8708. fp = XFOPEN(cliKeyFile, "rb");
  8709. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  8710. WOLFSSL_SUCCESS);
  8711. XFCLOSE(fp);
  8712. pt = key;
  8713. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  8714. (const unsigned char**)&pt, keySz));
  8715. /* create the version 2 certificate */
  8716. AssertNotNull(x509v2 = X509_new());
  8717. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  8718. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  8719. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  8720. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  8721. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  8722. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  8723. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  8724. t = time(NULL);
  8725. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  8726. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  8727. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  8728. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  8729. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  8730. derSz = wolfSSL_i2d_X509(x509v2, &der);
  8731. AssertIntGT(derSz, 0);
  8732. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  8733. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8734. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  8735. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8736. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8737. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8738. wolfSSL_X509_free(x509);
  8739. wolfSSL_X509_free(x509v2);
  8740. wolfSSL_EVP_PKEY_free(priv);
  8741. wolfSSL_EVP_PKEY_free(pub);
  8742. wolfSSL_ASN1_TIME_free(notBefore);
  8743. wolfSSL_ASN1_TIME_free(notAfter);
  8744. }
  8745. /* override certificate version error */
  8746. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  8747. {
  8748. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8749. AssertIntEQ(store->error, ASN_VERSION_E);
  8750. #else
  8751. AssertIntEQ(store->error, 0);
  8752. #endif
  8753. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8754. (void)preverify;
  8755. return 1;
  8756. }
  8757. /* set verify callback that will override bad certificate version */
  8758. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8759. {
  8760. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8761. }
  8762. #endif
  8763. static int test_wolfSSL_X509_TLS_version(void)
  8764. {
  8765. int res = TEST_SKIPPED;
  8766. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8767. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8768. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8769. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8770. tcp_ready ready;
  8771. func_args server_args;
  8772. func_args client_args;
  8773. THREAD_TYPE serverThread;
  8774. callback_functions func_cb_client;
  8775. callback_functions func_cb_server;
  8776. /* test server rejects a client certificate that is not version 3 */
  8777. #ifdef WOLFSSL_TIRTOS
  8778. fdOpenSession(Task_self());
  8779. #endif
  8780. XMEMSET(&server_args, 0, sizeof(func_args));
  8781. XMEMSET(&client_args, 0, sizeof(func_args));
  8782. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8783. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8784. StartTCP();
  8785. InitTcpReady(&ready);
  8786. #if defined(USE_WINDOWS_API)
  8787. /* use RNG to get random port if using windows */
  8788. ready.port = GetRandomPort();
  8789. #endif
  8790. server_args.signal = &ready;
  8791. client_args.signal = &ready;
  8792. server_args.return_code = TEST_FAIL;
  8793. client_args.return_code = TEST_FAIL;
  8794. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8795. #ifndef WOLFSSL_NO_TLS12
  8796. func_cb_client.method = wolfTLSv1_2_client_method;
  8797. #else
  8798. func_cb_client.method = wolfTLSv1_3_client_method;
  8799. #endif
  8800. client_args.callbacks = &func_cb_client;
  8801. #ifndef WOLFSSL_NO_TLS12
  8802. func_cb_server.method = wolfTLSv1_2_server_method;
  8803. #else
  8804. func_cb_server.method = wolfTLSv1_3_server_method;
  8805. #endif
  8806. server_args.callbacks = &func_cb_server;
  8807. start_thread(test_server_nofail, &server_args, &serverThread);
  8808. wait_tcp_ready(&server_args);
  8809. test_client_nofail(&client_args, NULL);
  8810. join_thread(serverThread);
  8811. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8812. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8813. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8814. #else
  8815. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8816. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8817. #endif
  8818. FreeTcpReady(&ready);
  8819. #ifdef WOLFSSL_TIRTOS
  8820. fdCloseSession(Task_self());
  8821. #endif
  8822. /* Now re run but override the bad X509 version */
  8823. #ifdef WOLFSSL_TIRTOS
  8824. fdOpenSession(Task_self());
  8825. #endif
  8826. XMEMSET(&server_args, 0, sizeof(func_args));
  8827. XMEMSET(&client_args, 0, sizeof(func_args));
  8828. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8829. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8830. StartTCP();
  8831. InitTcpReady(&ready);
  8832. #if defined(USE_WINDOWS_API)
  8833. /* use RNG to get random port if using windows */
  8834. ready.port = GetRandomPort();
  8835. #endif
  8836. server_args.signal = &ready;
  8837. client_args.signal = &ready;
  8838. server_args.return_code = TEST_FAIL;
  8839. client_args.return_code = TEST_FAIL;
  8840. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8841. func_cb_server.ctx_ready = &test_set_override_x509;
  8842. #ifndef WOLFSSL_NO_TLS12
  8843. func_cb_client.method = wolfTLSv1_2_client_method;
  8844. #else
  8845. func_cb_client.method = wolfTLSv1_3_client_method;
  8846. #endif
  8847. client_args.callbacks = &func_cb_client;
  8848. #ifndef WOLFSSL_NO_TLS12
  8849. func_cb_server.method = wolfTLSv1_2_server_method;
  8850. #else
  8851. func_cb_server.method = wolfTLSv1_3_server_method;
  8852. #endif
  8853. server_args.callbacks = &func_cb_server;
  8854. start_thread(test_server_nofail, &server_args, &serverThread);
  8855. wait_tcp_ready(&server_args);
  8856. test_client_nofail(&client_args, NULL);
  8857. join_thread(serverThread);
  8858. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8859. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8860. FreeTcpReady(&ready);
  8861. #ifdef WOLFSSL_TIRTOS
  8862. fdCloseSession(Task_self());
  8863. #endif
  8864. res = TEST_RES_CHECK(1);
  8865. #endif
  8866. return res;
  8867. }
  8868. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8869. * version allowed.
  8870. * POST: 1 on success.
  8871. */
  8872. static int test_wolfSSL_CTX_SetMinVersion(void)
  8873. {
  8874. int res = TEST_SKIPPED;
  8875. #ifndef NO_WOLFSSL_CLIENT
  8876. int failFlag = WOLFSSL_SUCCESS;
  8877. WOLFSSL_CTX* ctx;
  8878. int itr;
  8879. #ifndef NO_OLD_TLS
  8880. const int versions[] = {
  8881. #ifdef WOLFSSL_ALLOW_TLSV10
  8882. WOLFSSL_TLSV1,
  8883. #endif
  8884. WOLFSSL_TLSV1_1,
  8885. WOLFSSL_TLSV1_2 };
  8886. #elif !defined(WOLFSSL_NO_TLS12)
  8887. const int versions[] = { WOLFSSL_TLSV1_2 };
  8888. #elif defined(WOLFSSL_TLS13)
  8889. const int versions[] = { WOLFSSL_TLSV1_3 };
  8890. #else
  8891. const int versions[0];
  8892. #endif
  8893. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8894. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  8895. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  8896. != WOLFSSL_SUCCESS) {
  8897. failFlag = WOLFSSL_FAILURE;
  8898. }
  8899. }
  8900. wolfSSL_CTX_free(ctx);
  8901. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  8902. #endif
  8903. return res;
  8904. } /* END test_wolfSSL_CTX_SetMinVersion */
  8905. /*----------------------------------------------------------------------------*
  8906. | OCSP Stapling
  8907. *----------------------------------------------------------------------------*/
  8908. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8909. * need to contact the CA, lowering the cost of cert revocation checking.
  8910. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8911. * POST: 1 returned for success.
  8912. */
  8913. static int test_wolfSSL_UseOCSPStapling(void)
  8914. {
  8915. int res = TEST_SKIPPED;
  8916. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8917. !defined(NO_WOLFSSL_CLIENT)
  8918. int ret;
  8919. WOLFSSL_CTX* ctx;
  8920. WOLFSSL* ssl;
  8921. #ifndef NO_WOLFSSL_CLIENT
  8922. #ifndef WOLFSSL_NO_TLS12
  8923. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8924. #else
  8925. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8926. #endif
  8927. #else
  8928. #ifndef WOLFSSL_NO_TLS12
  8929. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8930. #else
  8931. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8932. #endif
  8933. #endif
  8934. ssl = wolfSSL_new(ctx);
  8935. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8936. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8937. wolfSSL_free(ssl);
  8938. wolfSSL_CTX_free(ctx);
  8939. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8940. #endif
  8941. return res;
  8942. } /*END test_wolfSSL_UseOCSPStapling */
  8943. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8944. * stapling eliminates the need to contact the CA and lowers cert revocation
  8945. * check.
  8946. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8947. */
  8948. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8949. {
  8950. int res = TEST_SKIPPED;
  8951. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8952. !defined(NO_WOLFSSL_CLIENT)
  8953. int ret;
  8954. WOLFSSL_CTX* ctx;
  8955. WOLFSSL* ssl;
  8956. #ifndef NO_WOLFSSL_CLIENT
  8957. #ifndef WOLFSSL_NO_TLS12
  8958. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8959. #else
  8960. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8961. #endif
  8962. #else
  8963. #ifndef WOLFSSL_NO_TLS12
  8964. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8965. #else
  8966. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8967. #endif
  8968. #endif
  8969. ssl = wolfSSL_new(ctx);
  8970. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8971. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8972. wolfSSL_free(ssl);
  8973. wolfSSL_CTX_free(ctx);
  8974. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  8975. #endif
  8976. return res;
  8977. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8978. /*----------------------------------------------------------------------------*
  8979. | Multicast Tests
  8980. *----------------------------------------------------------------------------*/
  8981. static int test_wolfSSL_mcast(void)
  8982. {
  8983. int res = TEST_SKIPPED;
  8984. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8985. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8986. WOLFSSL_CTX* ctx;
  8987. WOLFSSL* ssl;
  8988. int result;
  8989. byte preMasterSecret[512];
  8990. byte clientRandom[32];
  8991. byte serverRandom[32];
  8992. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8993. byte buf[256];
  8994. word16 newId;
  8995. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8996. AssertNotNull(ctx);
  8997. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8998. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8999. ssl = wolfSSL_new(ctx);
  9000. AssertNotNull(ssl);
  9001. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  9002. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  9003. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  9004. result = wolfSSL_set_secret(ssl, 23,
  9005. preMasterSecret, sizeof(preMasterSecret),
  9006. clientRandom, serverRandom, suite);
  9007. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9008. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  9009. AssertIntLE(result, 0);
  9010. AssertIntLE(newId, 100);
  9011. wolfSSL_free(ssl);
  9012. wolfSSL_CTX_free(ctx);
  9013. res = TEST_RES_CHECK(1);
  9014. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  9015. * WOLFSSL_SNIFFER) */
  9016. return res;
  9017. }
  9018. /*----------------------------------------------------------------------------*
  9019. | Wolfcrypt
  9020. *----------------------------------------------------------------------------*/
  9021. /*
  9022. * Unit test for the wc_InitBlake2b()
  9023. */
  9024. static int test_wc_InitBlake2b(void)
  9025. {
  9026. int res = TEST_SKIPPED;
  9027. #ifdef HAVE_BLAKE2
  9028. Blake2b blake;
  9029. int ret = 0;
  9030. /* Test good arg. */
  9031. ret = wc_InitBlake2b(&blake, 64);
  9032. if (ret != 0) {
  9033. ret = WOLFSSL_FATAL_ERROR;
  9034. }
  9035. /* Test bad arg. */
  9036. if (!ret) {
  9037. ret = wc_InitBlake2b(NULL, 64);
  9038. if (ret == 0) {
  9039. ret = WOLFSSL_FATAL_ERROR;
  9040. }
  9041. else {
  9042. ret = 0;
  9043. }
  9044. }
  9045. if (!ret) {
  9046. ret = wc_InitBlake2b(NULL, 128);
  9047. if (ret == 0) {
  9048. ret = WOLFSSL_FATAL_ERROR;
  9049. }
  9050. else {
  9051. ret = 0;
  9052. }
  9053. }
  9054. if (!ret) {
  9055. ret = wc_InitBlake2b(&blake, 128);
  9056. if (ret == 0) {
  9057. ret = WOLFSSL_FATAL_ERROR;
  9058. }
  9059. else {
  9060. ret = 0;
  9061. }
  9062. }
  9063. if (!ret) {
  9064. ret = wc_InitBlake2b(NULL, 0);
  9065. if (ret == 0) {
  9066. ret = WOLFSSL_FATAL_ERROR;
  9067. }
  9068. else {
  9069. ret = 0;
  9070. }
  9071. }
  9072. if (!ret) {
  9073. ret = wc_InitBlake2b(&blake, 0);
  9074. if (ret == 0) {
  9075. ret = WOLFSSL_FATAL_ERROR;
  9076. }
  9077. else {
  9078. ret = 0;
  9079. }
  9080. }
  9081. res = TEST_RES_CHECK(ret == 0);
  9082. #endif
  9083. return res;
  9084. } /*END test_wc_InitBlake2b*/
  9085. /*
  9086. * Unit test for the wc_InitBlake2b_WithKey()
  9087. */
  9088. static int test_wc_InitBlake2b_WithKey(void)
  9089. {
  9090. int res = TEST_SKIPPED;
  9091. #ifdef HAVE_BLAKE2
  9092. Blake2b blake;
  9093. word32 digestSz = BLAKE2B_KEYBYTES;
  9094. byte key[BLAKE2B_KEYBYTES];
  9095. word32 keylen = BLAKE2B_KEYBYTES;
  9096. int ret = 0;
  9097. XMEMSET(key, 0, sizeof(key));
  9098. /* Test good arg. */
  9099. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  9100. if (ret != 0) {
  9101. ret = WOLFSSL_FATAL_ERROR;
  9102. }
  9103. /* Test bad args. */
  9104. if (ret == 0) {
  9105. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  9106. if (ret == BAD_FUNC_ARG) {
  9107. ret = 0;
  9108. }
  9109. }
  9110. if (ret == 0) {
  9111. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  9112. if (ret == BAD_FUNC_ARG) {
  9113. ret = 0;
  9114. }
  9115. }
  9116. if (ret == 0) {
  9117. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  9118. }
  9119. res = TEST_RES_CHECK(ret == 0);
  9120. #endif
  9121. return res;
  9122. } /*END wc_InitBlake2b_WithKey*/
  9123. /*
  9124. * Unit test for the wc_InitBlake2s_WithKey()
  9125. */
  9126. static int test_wc_InitBlake2s_WithKey(void)
  9127. {
  9128. int res = TEST_SKIPPED;
  9129. #ifdef HAVE_BLAKE2S
  9130. Blake2s blake;
  9131. word32 digestSz = BLAKE2S_KEYBYTES;
  9132. byte *key = (byte*)"01234567890123456789012345678901";
  9133. word32 keylen = BLAKE2S_KEYBYTES;
  9134. int ret = 0;
  9135. /* Test good arg. */
  9136. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  9137. if (ret != 0) {
  9138. ret = WOLFSSL_FATAL_ERROR;
  9139. }
  9140. /* Test bad args. */
  9141. if (ret == 0) {
  9142. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  9143. if (ret == BAD_FUNC_ARG) {
  9144. ret = 0;
  9145. }
  9146. }
  9147. if (ret == 0) {
  9148. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  9149. if (ret == BAD_FUNC_ARG) {
  9150. ret = 0;
  9151. }
  9152. }
  9153. if (ret == 0) {
  9154. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  9155. }
  9156. res = TEST_RES_CHECK(ret == 0);
  9157. #endif
  9158. return res;
  9159. } /*END wc_InitBlake2s_WithKey*/
  9160. /*
  9161. * Unit test for the wc_InitMd5()
  9162. */
  9163. static int test_wc_InitMd5(void)
  9164. {
  9165. int res = TEST_SKIPPED;
  9166. #ifndef NO_MD5
  9167. wc_Md5 md5;
  9168. int ret;
  9169. int flag = 0;
  9170. /* Test good arg. */
  9171. ret = wc_InitMd5(&md5);
  9172. if (ret != 0) {
  9173. flag = WOLFSSL_FATAL_ERROR;
  9174. }
  9175. /* Test bad arg. */
  9176. if (!flag) {
  9177. ret = wc_InitMd5(NULL);
  9178. if (ret != BAD_FUNC_ARG) {
  9179. flag = WOLFSSL_FATAL_ERROR;
  9180. }
  9181. }
  9182. wc_Md5Free(&md5);
  9183. res = TEST_RES_CHECK(flag == 0);
  9184. #endif
  9185. return res;
  9186. } /* END test_wc_InitMd5 */
  9187. /*
  9188. * Testing wc_UpdateMd5()
  9189. */
  9190. static int test_wc_Md5Update(void)
  9191. {
  9192. int res = TEST_SKIPPED;
  9193. #ifndef NO_MD5
  9194. wc_Md5 md5;
  9195. byte hash[WC_MD5_DIGEST_SIZE];
  9196. testVector a, b, c;
  9197. int ret;
  9198. int flag = 0;
  9199. ret = wc_InitMd5(&md5);
  9200. if (ret != 0) {
  9201. flag = ret;
  9202. }
  9203. /* Input */
  9204. if (!flag) {
  9205. a.input = "a";
  9206. a.inLen = XSTRLEN(a.input);
  9207. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  9208. if (ret != 0) {
  9209. flag = ret;
  9210. }
  9211. }
  9212. if (!flag) {
  9213. ret = wc_Md5Final(&md5, hash);
  9214. if (ret != 0) {
  9215. flag = ret;
  9216. }
  9217. }
  9218. /* Update input. */
  9219. if (!flag) {
  9220. a.input = "abc";
  9221. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  9222. "\x72";
  9223. a.inLen = XSTRLEN(a.input);
  9224. a.outLen = XSTRLEN(a.output);
  9225. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  9226. if (ret != 0) {
  9227. flag = ret;
  9228. }
  9229. }
  9230. if (!flag) {
  9231. ret = wc_Md5Final(&md5, hash);
  9232. if (ret != 0) {
  9233. flag = ret;
  9234. }
  9235. }
  9236. if (!flag) {
  9237. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  9238. flag = WOLFSSL_FATAL_ERROR;
  9239. }
  9240. }
  9241. /*Pass in bad values. */
  9242. if (!flag) {
  9243. b.input = NULL;
  9244. b.inLen = 0;
  9245. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  9246. if (ret != 0) {
  9247. flag = ret;
  9248. }
  9249. }
  9250. if (!flag) {
  9251. c.input = NULL;
  9252. c.inLen = WC_MD5_DIGEST_SIZE;
  9253. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  9254. if (ret != BAD_FUNC_ARG) {
  9255. flag = WOLFSSL_FATAL_ERROR;
  9256. }
  9257. }
  9258. if (!flag) {
  9259. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  9260. if (ret != BAD_FUNC_ARG) {
  9261. flag = WOLFSSL_FATAL_ERROR;
  9262. }
  9263. }
  9264. wc_Md5Free(&md5);
  9265. res = TEST_RES_CHECK(flag == 0);
  9266. #endif
  9267. return res;
  9268. } /* END test_wc_Md5Update() */
  9269. /*
  9270. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9271. */
  9272. static int test_wc_Md5Final(void)
  9273. {
  9274. int res = TEST_SKIPPED;
  9275. #ifndef NO_MD5
  9276. /* Instantiate */
  9277. wc_Md5 md5;
  9278. byte* hash_test[3];
  9279. byte hash1[WC_MD5_DIGEST_SIZE];
  9280. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9281. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9282. int times, i, ret;
  9283. int flag = 0;
  9284. /* Initialize */
  9285. ret = wc_InitMd5(&md5);
  9286. if (ret != 0) {
  9287. flag = ret;
  9288. }
  9289. if (!flag) {
  9290. hash_test[0] = hash1;
  9291. hash_test[1] = hash2;
  9292. hash_test[2] = hash3;
  9293. }
  9294. times = sizeof(hash_test)/sizeof(byte*);
  9295. for (i = 0; i < times; i++) {
  9296. if (!flag) {
  9297. ret = wc_Md5Final(&md5, hash_test[i]);
  9298. if (ret != 0) {
  9299. flag = WOLFSSL_FATAL_ERROR;
  9300. }
  9301. }
  9302. }
  9303. /* Test bad args. */
  9304. if (!flag) {
  9305. ret = wc_Md5Final(NULL, NULL);
  9306. if (ret != BAD_FUNC_ARG) {
  9307. flag = WOLFSSL_FATAL_ERROR;
  9308. }
  9309. }
  9310. if (!flag) {
  9311. ret = wc_Md5Final(NULL, hash1);
  9312. if (ret != BAD_FUNC_ARG) {
  9313. flag = WOLFSSL_FATAL_ERROR;
  9314. }
  9315. }
  9316. if (!flag) {
  9317. ret = wc_Md5Final(&md5, NULL);
  9318. if (ret != BAD_FUNC_ARG) {
  9319. flag = WOLFSSL_FATAL_ERROR;
  9320. }
  9321. }
  9322. wc_Md5Free(&md5);
  9323. res = TEST_RES_CHECK(flag == 0);
  9324. #endif
  9325. return res;
  9326. }
  9327. /*
  9328. * Unit test for the wc_InitSha()
  9329. */
  9330. static int test_wc_InitSha(void)
  9331. {
  9332. int res = TEST_SKIPPED;
  9333. #ifndef NO_SHA
  9334. wc_Sha sha;
  9335. int ret;
  9336. int flag = 0;
  9337. /* Test good arg. */
  9338. ret = wc_InitSha(&sha);
  9339. if (ret != 0) {
  9340. flag = WOLFSSL_FATAL_ERROR;
  9341. }
  9342. /* Test bad arg. */
  9343. if (!flag) {
  9344. ret = wc_InitSha(NULL);
  9345. if (ret != BAD_FUNC_ARG) {
  9346. flag = WOLFSSL_FATAL_ERROR;
  9347. }
  9348. }
  9349. wc_ShaFree(&sha);
  9350. res = TEST_RES_CHECK(flag == 0);
  9351. #endif
  9352. return res;
  9353. } /* END test_wc_InitSha */
  9354. /*
  9355. * Tesing wc_ShaUpdate()
  9356. */
  9357. static int test_wc_ShaUpdate(void)
  9358. {
  9359. int res = TEST_SKIPPED;
  9360. #ifndef NO_SHA
  9361. wc_Sha sha;
  9362. byte hash[WC_SHA_DIGEST_SIZE];
  9363. testVector a, b, c;
  9364. int flag = 0;
  9365. int ret;
  9366. ret = wc_InitSha(&sha);
  9367. if (ret != 0) {
  9368. flag = ret;
  9369. }
  9370. /* Input. */
  9371. if (!flag) {
  9372. a.input = "a";
  9373. a.inLen = XSTRLEN(a.input);
  9374. ret = wc_ShaUpdate(&sha, NULL, 0);
  9375. if (ret != 0) {
  9376. flag = ret;
  9377. }
  9378. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9379. if (ret != 0) {
  9380. flag = ret;
  9381. }
  9382. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9383. if (ret != 0) {
  9384. flag = ret;
  9385. }
  9386. }
  9387. if (!flag) {
  9388. ret = wc_ShaFinal(&sha, hash);
  9389. if (ret != 0) {
  9390. flag = ret;
  9391. }
  9392. }
  9393. /* Update input. */
  9394. if (!flag) {
  9395. a.input = "abc";
  9396. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9397. "\x6C\x9C\xD0\xD8\x9D";
  9398. a.inLen = XSTRLEN(a.input);
  9399. a.outLen = XSTRLEN(a.output);
  9400. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9401. if (ret != 0) {
  9402. flag = ret;
  9403. }
  9404. }
  9405. if (!flag) {
  9406. ret = wc_ShaFinal(&sha, hash);
  9407. if (ret !=0) {
  9408. flag = ret;
  9409. }
  9410. }
  9411. if (!flag) {
  9412. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9413. flag = WOLFSSL_FATAL_ERROR;
  9414. }
  9415. }
  9416. /* Try passing in bad values. */
  9417. if (!flag) {
  9418. b.input = NULL;
  9419. b.inLen = 0;
  9420. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9421. if (ret != 0) {
  9422. flag = ret;
  9423. }
  9424. }
  9425. if (!flag) {
  9426. c.input = NULL;
  9427. c.inLen = WC_SHA_DIGEST_SIZE;
  9428. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9429. if (ret != BAD_FUNC_ARG) {
  9430. flag = WOLFSSL_FATAL_ERROR;
  9431. }
  9432. }
  9433. if (!flag) {
  9434. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9435. if (ret != BAD_FUNC_ARG) {
  9436. flag = WOLFSSL_FATAL_ERROR;
  9437. }
  9438. }
  9439. wc_ShaFree(&sha);
  9440. res = TEST_RES_CHECK(flag == 0);
  9441. #endif
  9442. return res;
  9443. } /* END test_wc_ShaUpdate() */
  9444. /*
  9445. * Unit test on wc_ShaFinal
  9446. */
  9447. static int test_wc_ShaFinal(void)
  9448. {
  9449. int res = TEST_SKIPPED;
  9450. #ifndef NO_SHA
  9451. wc_Sha sha;
  9452. byte* hash_test[3];
  9453. byte hash1[WC_SHA_DIGEST_SIZE];
  9454. byte hash2[2*WC_SHA_DIGEST_SIZE];
  9455. byte hash3[5*WC_SHA_DIGEST_SIZE];
  9456. int times, i, ret;
  9457. int flag = 0;
  9458. /*Initialize*/
  9459. ret = wc_InitSha(&sha);
  9460. if (ret) {
  9461. flag = ret;
  9462. }
  9463. if (!flag) {
  9464. hash_test[0] = hash1;
  9465. hash_test[1] = hash2;
  9466. hash_test[2] = hash3;
  9467. }
  9468. times = sizeof(hash_test)/sizeof(byte*);
  9469. for (i = 0; i < times; i++) {
  9470. if (!flag) {
  9471. ret = wc_ShaFinal(&sha, hash_test[i]);
  9472. if (ret != 0) {
  9473. flag = WOLFSSL_FATAL_ERROR;
  9474. }
  9475. }
  9476. }
  9477. /* Test bad args. */
  9478. if (!flag) {
  9479. ret = wc_ShaFinal(NULL, NULL);
  9480. if (ret != BAD_FUNC_ARG) {
  9481. flag = WOLFSSL_FATAL_ERROR;
  9482. }
  9483. }
  9484. if (!flag) {
  9485. ret = wc_ShaFinal(NULL, hash1);
  9486. if (ret != BAD_FUNC_ARG) {
  9487. flag = WOLFSSL_FATAL_ERROR;
  9488. }
  9489. }
  9490. if (!flag) {
  9491. ret = wc_ShaFinal(&sha, NULL);
  9492. if (ret != BAD_FUNC_ARG) {
  9493. flag = WOLFSSL_FATAL_ERROR;
  9494. }
  9495. }
  9496. wc_ShaFree(&sha);
  9497. res = TEST_RES_CHECK(flag == 0);
  9498. #endif
  9499. return res;
  9500. } /* END test_wc_ShaFinal */
  9501. /*
  9502. * Unit test for wc_InitSha256()
  9503. */
  9504. static int test_wc_InitSha256(void)
  9505. {
  9506. int res = TEST_SKIPPED;
  9507. #ifndef NO_SHA256
  9508. wc_Sha256 sha256;
  9509. int ret;
  9510. int flag = 0;
  9511. /* Test good arg. */
  9512. ret = wc_InitSha256(&sha256);
  9513. if (ret != 0) {
  9514. flag = WOLFSSL_FATAL_ERROR;
  9515. }
  9516. /* Test bad arg. */
  9517. if (!flag) {
  9518. ret = wc_InitSha256(NULL);
  9519. if (ret != BAD_FUNC_ARG) {
  9520. flag = WOLFSSL_FATAL_ERROR;
  9521. }
  9522. }
  9523. wc_Sha256Free(&sha256);
  9524. res = TEST_RES_CHECK(flag == 0);
  9525. #endif
  9526. return res;
  9527. } /* END test_wc_InitSha256 */
  9528. /*
  9529. * Unit test for wc_Sha256Update()
  9530. */
  9531. static int test_wc_Sha256Update(void)
  9532. {
  9533. int res = TEST_SKIPPED;
  9534. #ifndef NO_SHA256
  9535. wc_Sha256 sha256;
  9536. byte hash[WC_SHA256_DIGEST_SIZE];
  9537. testVector a, b, c;
  9538. int ret;
  9539. int flag = 0;
  9540. ret = wc_InitSha256(&sha256);
  9541. if (ret != 0) {
  9542. flag = ret;
  9543. }
  9544. /* Input. */
  9545. if (!flag) {
  9546. a.input = "a";
  9547. a.inLen = XSTRLEN(a.input);
  9548. ret = wc_Sha256Update(&sha256, NULL, 0);
  9549. if (ret != 0) {
  9550. flag = ret;
  9551. }
  9552. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  9553. if (ret != 0) {
  9554. flag = ret;
  9555. }
  9556. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9557. if (ret != 0) {
  9558. flag = ret;
  9559. }
  9560. }
  9561. if (!flag) {
  9562. ret = wc_Sha256Final(&sha256, hash);
  9563. if (ret != 0) {
  9564. flag = ret;
  9565. }
  9566. }
  9567. /* Update input. */
  9568. if (!flag) {
  9569. a.input = "abc";
  9570. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  9571. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  9572. "\x15\xAD";
  9573. a.inLen = XSTRLEN(a.input);
  9574. a.outLen = XSTRLEN(a.output);
  9575. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9576. if (ret != 0) {
  9577. flag = ret;
  9578. }
  9579. }
  9580. if (!flag) {
  9581. ret = wc_Sha256Final(&sha256, hash);
  9582. if (ret != 0) {
  9583. flag = ret;
  9584. }
  9585. }
  9586. if (!flag) {
  9587. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  9588. flag = WOLFSSL_FATAL_ERROR;
  9589. }
  9590. }
  9591. /* Try passing in bad values */
  9592. if (!flag) {
  9593. b.input = NULL;
  9594. b.inLen = 0;
  9595. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  9596. if (ret != 0) {
  9597. flag = ret;
  9598. }
  9599. }
  9600. if (!flag) {
  9601. c.input = NULL;
  9602. c.inLen = WC_SHA256_DIGEST_SIZE;
  9603. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  9604. if (ret != BAD_FUNC_ARG) {
  9605. flag = WOLFSSL_FATAL_ERROR;
  9606. }
  9607. }
  9608. if (!flag) {
  9609. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9610. if (ret != BAD_FUNC_ARG) {
  9611. flag = WOLFSSL_FATAL_ERROR;
  9612. }
  9613. }
  9614. wc_Sha256Free(&sha256);
  9615. res = TEST_RES_CHECK(flag == 0);
  9616. #endif
  9617. return res;
  9618. } /* END test_wc_Sha256Update */
  9619. /*
  9620. * Unit test function for wc_Sha256Final()
  9621. */
  9622. static int test_wc_Sha256Final(void)
  9623. {
  9624. int res = TEST_SKIPPED;
  9625. #ifndef NO_SHA256
  9626. wc_Sha256 sha256;
  9627. byte* hash_test[3];
  9628. byte hash1[WC_SHA256_DIGEST_SIZE];
  9629. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9630. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9631. int times, i, ret;
  9632. int flag = 0;
  9633. /* Initialize */
  9634. ret = wc_InitSha256(&sha256);
  9635. if (ret != 0) {
  9636. flag = ret;
  9637. }
  9638. if (!flag) {
  9639. hash_test[0] = hash1;
  9640. hash_test[1] = hash2;
  9641. hash_test[2] = hash3;
  9642. }
  9643. times = sizeof(hash_test) / sizeof(byte*);
  9644. for (i = 0; i < times; i++) {
  9645. if (!flag) {
  9646. ret = wc_Sha256Final(&sha256, hash_test[i]);
  9647. if (ret != 0) {
  9648. flag = WOLFSSL_FATAL_ERROR;
  9649. }
  9650. }
  9651. }
  9652. /* Test bad args. */
  9653. if (!flag ) {
  9654. ret = wc_Sha256Final(NULL, NULL);
  9655. if (ret != BAD_FUNC_ARG) {
  9656. flag = WOLFSSL_FATAL_ERROR;
  9657. }
  9658. }
  9659. if (!flag) {
  9660. ret = wc_Sha256Final(NULL, hash1);
  9661. if (ret != BAD_FUNC_ARG) {
  9662. flag = WOLFSSL_FATAL_ERROR;
  9663. }
  9664. }
  9665. if (!flag) {
  9666. ret = wc_Sha256Final(&sha256, NULL);
  9667. if (ret != BAD_FUNC_ARG) {
  9668. flag = WOLFSSL_FATAL_ERROR;
  9669. }
  9670. }
  9671. wc_Sha256Free(&sha256);
  9672. res = TEST_RES_CHECK(flag == 0);
  9673. #endif
  9674. return res;
  9675. } /* END test_wc_Sha256Final */
  9676. /*
  9677. * Unit test function for wc_Sha256FinalRaw()
  9678. */
  9679. static int test_wc_Sha256FinalRaw(void)
  9680. {
  9681. int res = TEST_SKIPPED;
  9682. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  9683. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  9684. !defined(WOLFSSL_NO_HASH_RAW)
  9685. wc_Sha256 sha256;
  9686. byte* hash_test[3];
  9687. byte hash1[WC_SHA256_DIGEST_SIZE];
  9688. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9689. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9690. int times, i, ret;
  9691. int flag = 0;
  9692. /* Initialize */
  9693. ret = wc_InitSha256(&sha256);
  9694. if (ret != 0) {
  9695. flag = ret;
  9696. }
  9697. if (!flag) {
  9698. hash_test[0] = hash1;
  9699. hash_test[1] = hash2;
  9700. hash_test[2] = hash3;
  9701. }
  9702. times = sizeof(hash_test) / sizeof(byte*);
  9703. for (i = 0; i < times; i++) {
  9704. if (!flag) {
  9705. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  9706. if (ret != 0) {
  9707. flag = WOLFSSL_FATAL_ERROR;
  9708. }
  9709. }
  9710. }
  9711. /* Test bad args. */
  9712. if (!flag) {
  9713. ret = wc_Sha256FinalRaw(NULL, NULL);
  9714. if (ret != BAD_FUNC_ARG) {
  9715. flag = WOLFSSL_FATAL_ERROR;
  9716. }
  9717. }
  9718. if (!flag) {
  9719. ret = wc_Sha256FinalRaw(NULL, hash1);
  9720. if (ret != BAD_FUNC_ARG) {
  9721. flag = WOLFSSL_FATAL_ERROR;
  9722. }
  9723. }
  9724. if (!flag) {
  9725. ret = wc_Sha256FinalRaw(&sha256, NULL);
  9726. if (ret != BAD_FUNC_ARG) {
  9727. flag = WOLFSSL_FATAL_ERROR;
  9728. }
  9729. }
  9730. wc_Sha256Free(&sha256);
  9731. res = TEST_RES_CHECK(flag == 0);
  9732. #endif
  9733. return res;
  9734. } /* END test_wc_Sha256FinalRaw */
  9735. /*
  9736. * Unit test function for wc_Sha256GetFlags()
  9737. */
  9738. static int test_wc_Sha256GetFlags(void)
  9739. {
  9740. int res = TEST_SKIPPED;
  9741. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  9742. wc_Sha256 sha256;
  9743. word32 flags = 0;
  9744. int flag = 0;
  9745. /* Initialize */
  9746. flag = wc_InitSha256(&sha256);
  9747. if (flag == 0) {
  9748. flag = wc_Sha256GetFlags(&sha256, &flags);
  9749. }
  9750. if (flag == 0) {
  9751. if (flags & WC_HASH_FLAG_ISCOPY) {
  9752. flag = 0;
  9753. }
  9754. }
  9755. wc_Sha256Free(&sha256);
  9756. res = TEST_RES_CHECK(flag == 0);
  9757. #endif
  9758. return res;
  9759. } /* END test_wc_Sha256GetFlags */
  9760. /*
  9761. * Unit test function for wc_Sha256Free()
  9762. */
  9763. static int test_wc_Sha256Free(void)
  9764. {
  9765. int res = TEST_SKIPPED;
  9766. #ifndef NO_SHA256
  9767. wc_Sha256Free(NULL);
  9768. res = TEST_RES_CHECK(1);
  9769. #endif
  9770. return res;
  9771. } /* END test_wc_Sha256Free */
  9772. /*
  9773. * Unit test function for wc_Sha256GetHash()
  9774. */
  9775. static int test_wc_Sha256GetHash(void)
  9776. {
  9777. int res = TEST_SKIPPED;
  9778. #ifndef NO_SHA256
  9779. wc_Sha256 sha256;
  9780. byte hash1[WC_SHA256_DIGEST_SIZE];
  9781. int flag = 0;
  9782. /* Initialize */
  9783. flag = wc_InitSha256(&sha256);
  9784. if (flag == 0) {
  9785. flag = wc_Sha256GetHash(&sha256, hash1);
  9786. }
  9787. /*test bad arguments*/
  9788. if (flag == 0) {
  9789. flag = wc_Sha256GetHash(NULL, NULL);
  9790. if (flag == BAD_FUNC_ARG) {
  9791. flag = 0;
  9792. }
  9793. }
  9794. if (flag == 0) {
  9795. flag = wc_Sha256GetHash(NULL, hash1);
  9796. if (flag == BAD_FUNC_ARG) {
  9797. flag = 0;
  9798. }
  9799. }
  9800. if (flag == 0) {
  9801. flag = wc_Sha256GetHash(&sha256, NULL);
  9802. if (flag == BAD_FUNC_ARG) {
  9803. flag = 0;
  9804. }
  9805. }
  9806. wc_Sha256Free(&sha256);
  9807. res = TEST_RES_CHECK(flag == 0);
  9808. #endif
  9809. return res;
  9810. } /* END test_wc_Sha256GetHash */
  9811. /*
  9812. * Unit test function for wc_Sha256Copy()
  9813. */
  9814. static int test_wc_Sha256Copy(void)
  9815. {
  9816. int res = TEST_SKIPPED;
  9817. #ifndef NO_SHA256
  9818. wc_Sha256 sha256;
  9819. wc_Sha256 temp;
  9820. int flag = 0;
  9821. /* Initialize */
  9822. flag = wc_InitSha256(&sha256);
  9823. if (flag == 0) {
  9824. flag = wc_InitSha256(&temp);
  9825. }
  9826. if (flag == 0) {
  9827. flag = wc_Sha256Copy(&sha256, &temp);
  9828. }
  9829. /*test bad arguments*/
  9830. if (flag == 0) {
  9831. flag = wc_Sha256Copy(NULL, NULL);
  9832. if (flag == BAD_FUNC_ARG) {
  9833. flag = 0;
  9834. }
  9835. }
  9836. if (flag == 0) {
  9837. flag = wc_Sha256Copy(NULL, &temp);
  9838. if (flag == BAD_FUNC_ARG) {
  9839. flag = 0;
  9840. }
  9841. }
  9842. if (flag == 0) {
  9843. flag = wc_Sha256Copy(&sha256, NULL);
  9844. if (flag == BAD_FUNC_ARG) {
  9845. flag = 0;
  9846. }
  9847. }
  9848. wc_Sha256Free(&sha256);
  9849. wc_Sha256Free(&temp);
  9850. res = TEST_RES_CHECK(flag == 0);
  9851. #endif
  9852. return res;
  9853. } /* END test_wc_Sha256Copy */
  9854. /*
  9855. * Testing wc_InitSha512()
  9856. */
  9857. static int test_wc_InitSha512(void)
  9858. {
  9859. int res = TEST_SKIPPED;
  9860. #ifdef WOLFSSL_SHA512
  9861. wc_Sha512 sha512;
  9862. int ret;
  9863. int flag = 0;
  9864. /* Test good arg. */
  9865. ret = wc_InitSha512(&sha512);
  9866. if (ret != 0) {
  9867. flag = WOLFSSL_FATAL_ERROR;
  9868. }
  9869. /* Test bad arg. */
  9870. if (!flag) {
  9871. ret = wc_InitSha512(NULL);
  9872. if (ret != BAD_FUNC_ARG) {
  9873. flag = WOLFSSL_FATAL_ERROR;
  9874. }
  9875. }
  9876. wc_Sha512Free(&sha512);
  9877. res = TEST_RES_CHECK(flag == 0);
  9878. #endif
  9879. return res;
  9880. } /* END test_wc_InitSha512 */
  9881. /*
  9882. * wc_Sha512Update() test.
  9883. */
  9884. static int test_wc_Sha512Update(void)
  9885. {
  9886. int res = TEST_SKIPPED;
  9887. #ifdef WOLFSSL_SHA512
  9888. wc_Sha512 sha512;
  9889. byte hash[WC_SHA512_DIGEST_SIZE];
  9890. testVector a, b, c;
  9891. int ret;
  9892. int flag = 0;
  9893. ret = wc_InitSha512(&sha512);
  9894. if (ret != 0) {
  9895. flag = ret;
  9896. }
  9897. /* Input. */
  9898. if (!flag) {
  9899. a.input = "a";
  9900. a.inLen = XSTRLEN(a.input);
  9901. ret = wc_Sha512Update(&sha512, NULL, 0);
  9902. if (ret != 0) {
  9903. flag = ret;
  9904. }
  9905. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9906. if (ret != 0) {
  9907. flag = ret;
  9908. }
  9909. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9910. if (ret != 0) {
  9911. flag = ret;
  9912. }
  9913. ret = wc_Sha512Final(&sha512, hash);
  9914. if (ret != 0) {
  9915. flag = ret;
  9916. }
  9917. }
  9918. /* Update input. */
  9919. if (!flag) {
  9920. a.input = "abc";
  9921. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9922. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9923. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9924. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9925. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9926. a.inLen = XSTRLEN(a.input);
  9927. a.outLen = XSTRLEN(a.output);
  9928. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9929. if (ret != 0) {
  9930. flag = ret;
  9931. }
  9932. }
  9933. if (!flag) {
  9934. ret = wc_Sha512Final(&sha512, hash);
  9935. if (ret != 0) {
  9936. flag = ret;
  9937. }
  9938. }
  9939. if (!flag) {
  9940. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9941. flag = WOLFSSL_FATAL_ERROR;
  9942. }
  9943. }
  9944. /* Try passing in bad values */
  9945. if (!flag) {
  9946. b.input = NULL;
  9947. b.inLen = 0;
  9948. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9949. if (ret != 0) {
  9950. flag = ret;
  9951. }
  9952. }
  9953. if (!flag) {
  9954. c.input = NULL;
  9955. c.inLen = WC_SHA512_DIGEST_SIZE;
  9956. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9957. if (ret != BAD_FUNC_ARG) {
  9958. flag = WOLFSSL_FATAL_ERROR;
  9959. }
  9960. }
  9961. if (!flag) {
  9962. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9963. if (ret != BAD_FUNC_ARG) {
  9964. flag = WOLFSSL_FATAL_ERROR;
  9965. }
  9966. }
  9967. wc_Sha512Free(&sha512);
  9968. res = TEST_RES_CHECK(flag == 0);
  9969. #endif
  9970. return res;
  9971. } /* END test_wc_Sha512Update */
  9972. #ifdef WOLFSSL_SHA512
  9973. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9974. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9975. /* Perfoms test for
  9976. * - wc_Sha512Final/wc_Sha512FinalRaw
  9977. * - wc_Sha512_224Final/wc_Sha512_224Final
  9978. * - wc_Sha512_256Final/wc_Sha512_256Final
  9979. * parameter:
  9980. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9981. * WC_HASH_TYPE_SHA512_256
  9982. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9983. *return 0 on success
  9984. */
  9985. static int test_Sha512_Family_Final(int type, int isRaw)
  9986. {
  9987. wc_Sha512 sha512;
  9988. byte* hash_test[3];
  9989. byte hash1[WC_SHA512_DIGEST_SIZE];
  9990. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9991. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9992. int times, i, ret;
  9993. int(*initFp)(wc_Sha512*);
  9994. int(*finalFp)(wc_Sha512*, byte*);
  9995. void(*freeFp)(wc_Sha512*);
  9996. if (type == WC_HASH_TYPE_SHA512) {
  9997. initFp = wc_InitSha512;
  9998. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9999. !defined(WOLFSSL_NO_HASH_RAW)
  10000. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  10001. #else
  10002. finalFp = (isRaw)? NULL : wc_Sha512Final;
  10003. #endif
  10004. freeFp = wc_Sha512Free;
  10005. }
  10006. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10007. #if !defined(WOLFSSL_NOSHA512_224)
  10008. else if (type == WC_HASH_TYPE_SHA512_224) {
  10009. initFp = wc_InitSha512_224;
  10010. #if !defined(WOLFSSL_NO_HASH_RAW)
  10011. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  10012. #else
  10013. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  10014. #endif
  10015. freeFp = wc_Sha512_224Free;
  10016. }
  10017. #endif
  10018. #if !defined(WOLFSSL_NOSHA512_256)
  10019. else if (type == WC_HASH_TYPE_SHA512_256) {
  10020. initFp = wc_InitSha512_256;
  10021. #if !defined(WOLFSSL_NO_HASH_RAW)
  10022. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  10023. #else
  10024. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  10025. #endif
  10026. freeFp = wc_Sha512_256Free;
  10027. }
  10028. #endif
  10029. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10030. else
  10031. return TEST_FAIL;
  10032. /* Initialize */
  10033. ret = initFp(&sha512);
  10034. if (!ret) {
  10035. hash_test[0] = hash1;
  10036. hash_test[1] = hash2;
  10037. hash_test[2] = hash3;
  10038. }
  10039. times = sizeof(hash_test) / sizeof(byte *);
  10040. /* Good test args. */
  10041. for (i = 0; i < times && ret == 0; i++) {
  10042. ret = finalFp(&sha512, hash_test[i]);
  10043. }
  10044. /* Test bad args. */
  10045. if (!ret) {
  10046. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  10047. ret = WOLFSSL_FATAL_ERROR;
  10048. }
  10049. }
  10050. if (!ret) {
  10051. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  10052. ret = WOLFSSL_FATAL_ERROR;
  10053. }
  10054. }
  10055. if (!ret) {
  10056. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  10057. ret = WOLFSSL_FATAL_ERROR;
  10058. }
  10059. }
  10060. freeFp(&sha512);
  10061. return ret;
  10062. }
  10063. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10064. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10065. #endif /* WOLFSSL_SHA512 */
  10066. /*
  10067. * Unit test function for wc_Sha512Final()
  10068. */
  10069. static int test_wc_Sha512Final(void)
  10070. {
  10071. int res = TEST_SKIPPED;
  10072. #ifdef WOLFSSL_SHA512
  10073. wc_Sha512 sha512;
  10074. byte* hash_test[3];
  10075. byte hash1[WC_SHA512_DIGEST_SIZE];
  10076. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10077. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10078. int times, i, ret;
  10079. int flag = 0;
  10080. /* Initialize */
  10081. ret = wc_InitSha512(&sha512);
  10082. if (ret != 0) {
  10083. flag = ret;
  10084. }
  10085. if (!flag) {
  10086. hash_test[0] = hash1;
  10087. hash_test[1] = hash2;
  10088. hash_test[2] = hash3;
  10089. }
  10090. times = sizeof(hash_test) / sizeof(byte *);
  10091. for (i = 0; i < times; i++) {
  10092. if (!flag) {
  10093. ret = wc_Sha512Final(&sha512, hash_test[i]);
  10094. if (ret != 0) {
  10095. flag = WOLFSSL_FATAL_ERROR;
  10096. }
  10097. }
  10098. }
  10099. /* Test bad args. */
  10100. if (!flag) {
  10101. ret = wc_Sha512Final(NULL, NULL);
  10102. if (ret != BAD_FUNC_ARG) {
  10103. flag = WOLFSSL_FATAL_ERROR;
  10104. }
  10105. }
  10106. if (!flag) {
  10107. ret = wc_Sha512Final(NULL, hash1);
  10108. if (ret != BAD_FUNC_ARG) {
  10109. flag = WOLFSSL_FATAL_ERROR;
  10110. }
  10111. }
  10112. if (!flag) {
  10113. ret = wc_Sha512Final(&sha512, NULL);
  10114. if (ret != BAD_FUNC_ARG) {
  10115. flag = WOLFSSL_FATAL_ERROR;
  10116. }
  10117. }
  10118. wc_Sha512Free(&sha512);
  10119. res = TEST_RES_CHECK(flag == 0);
  10120. #endif
  10121. return res;
  10122. } /* END test_wc_Sha512Final */
  10123. /*
  10124. * Unit test function for wc_Sha512GetFlags()
  10125. */
  10126. static int test_wc_Sha512GetFlags(void)
  10127. {
  10128. int res = TEST_SKIPPED;
  10129. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  10130. wc_Sha512 sha512;
  10131. word32 flags = 0;
  10132. int flag = 0;
  10133. /* Initialize */
  10134. flag = wc_InitSha512(&sha512);
  10135. if (flag == 0) {
  10136. flag = wc_Sha512GetFlags(&sha512, &flags);
  10137. }
  10138. if (flag == 0) {
  10139. if (flags & WC_HASH_FLAG_ISCOPY) {
  10140. flag = 0;
  10141. }
  10142. }
  10143. wc_Sha512Free(&sha512);
  10144. res = TEST_RES_CHECK(flag == 0);
  10145. #endif
  10146. return res;
  10147. } /* END test_wc_Sha512GetFlags */
  10148. /*
  10149. * Unit test function for wc_Sha512FinalRaw()
  10150. */
  10151. static int test_wc_Sha512FinalRaw(void)
  10152. {
  10153. int res = TEST_SKIPPED;
  10154. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10155. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10156. !defined(WOLFSSL_NO_HASH_RAW)
  10157. wc_Sha512 sha512;
  10158. byte* hash_test[3];
  10159. byte hash1[WC_SHA512_DIGEST_SIZE];
  10160. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10161. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10162. int times, i, ret;
  10163. int flag = 0;
  10164. /* Initialize */
  10165. ret = wc_InitSha512(&sha512);
  10166. if (ret != 0) {
  10167. flag = ret;
  10168. }
  10169. if (!flag) {
  10170. hash_test[0] = hash1;
  10171. hash_test[1] = hash2;
  10172. hash_test[2] = hash3;
  10173. }
  10174. times = sizeof(hash_test) / sizeof(byte*);
  10175. /* Good test args. */
  10176. for (i = 0; i < times; i++) {
  10177. if (!flag) {
  10178. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  10179. if (ret != 0) {
  10180. flag = WOLFSSL_FATAL_ERROR;
  10181. }
  10182. }
  10183. }
  10184. /* Test bad args. */
  10185. if (!flag ) {
  10186. ret = wc_Sha512FinalRaw(NULL, NULL);
  10187. if (ret != BAD_FUNC_ARG) {
  10188. flag = WOLFSSL_FATAL_ERROR;
  10189. }
  10190. }
  10191. if (!flag) {
  10192. ret = wc_Sha512FinalRaw(NULL, hash1);
  10193. if (ret != BAD_FUNC_ARG) {
  10194. flag = WOLFSSL_FATAL_ERROR;
  10195. }
  10196. }
  10197. if (!flag) {
  10198. ret = wc_Sha512FinalRaw(&sha512, NULL);
  10199. if (ret != BAD_FUNC_ARG) {
  10200. flag = WOLFSSL_FATAL_ERROR;
  10201. }
  10202. }
  10203. wc_Sha512Free(&sha512);
  10204. res = TEST_RES_CHECK(flag == 0);
  10205. #endif
  10206. return res;
  10207. } /* END test_wc_Sha512FinalRaw */
  10208. /*
  10209. * Unit test function for wc_Sha512Free()
  10210. */
  10211. static int test_wc_Sha512Free(void)
  10212. {
  10213. int res = TEST_SKIPPED;
  10214. #ifdef WOLFSSL_SHA512
  10215. wc_Sha512Free(NULL);
  10216. res = TEST_RES_CHECK(1);
  10217. #endif
  10218. return res;
  10219. } /* END test_wc_Sha512Free */
  10220. #ifdef WOLFSSL_SHA512
  10221. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10222. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10223. static int test_Sha512_Family_GetHash(int type )
  10224. {
  10225. int flag = 0;
  10226. int(*initFp)(wc_Sha512*);
  10227. int(*ghashFp)(wc_Sha512*, byte*);
  10228. wc_Sha512 sha512;
  10229. byte hash1[WC_SHA512_DIGEST_SIZE];
  10230. if (type == WC_HASH_TYPE_SHA512) {
  10231. initFp = wc_InitSha512;
  10232. ghashFp = wc_Sha512GetHash;
  10233. }
  10234. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10235. #if !defined(WOLFSSL_NOSHA512_224)
  10236. else if (type == WC_HASH_TYPE_SHA512_224) {
  10237. initFp = wc_InitSha512_224;
  10238. ghashFp = wc_Sha512_224GetHash;
  10239. }
  10240. #endif
  10241. #if !defined(WOLFSSL_NOSHA512_256)
  10242. else if (type == WC_HASH_TYPE_SHA512_256) {
  10243. initFp = wc_InitSha512_256;
  10244. ghashFp = wc_Sha512_256GetHash;
  10245. }
  10246. #endif
  10247. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10248. else {
  10249. initFp = NULL;
  10250. ghashFp = NULL;
  10251. }
  10252. if (initFp == NULL || ghashFp == NULL)
  10253. return TEST_FAIL;
  10254. if (!flag) {
  10255. flag = initFp(&sha512);
  10256. }
  10257. if (!flag) {
  10258. flag = ghashFp(&sha512, hash1);
  10259. }
  10260. /*test bad arguments*/
  10261. if (!flag) {
  10262. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10263. flag = WOLFSSL_FATAL_ERROR;
  10264. }
  10265. if (!flag) {
  10266. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10267. flag = WOLFSSL_FATAL_ERROR;
  10268. }
  10269. if (!flag) {
  10270. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10271. flag = WOLFSSL_FATAL_ERROR;
  10272. }
  10273. wc_Sha512Free(&sha512);
  10274. return flag;
  10275. }
  10276. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10277. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10278. #endif /* WOLFSSL_SHA512 */
  10279. /*
  10280. * Unit test function for wc_Sha512GetHash()
  10281. */
  10282. static int test_wc_Sha512GetHash(void)
  10283. {
  10284. int res = TEST_SKIPPED;
  10285. #ifdef WOLFSSL_SHA512
  10286. wc_Sha512 sha512;
  10287. byte hash1[WC_SHA512_DIGEST_SIZE];
  10288. int flag = 0;
  10289. /* Initialize */
  10290. flag = wc_InitSha512(&sha512);
  10291. if (flag == 0) {
  10292. flag = wc_Sha512GetHash(&sha512, hash1);
  10293. }
  10294. /*test bad arguments*/
  10295. if (flag == 0) {
  10296. flag = wc_Sha512GetHash(NULL, NULL);
  10297. if (flag == BAD_FUNC_ARG) {
  10298. flag = 0;
  10299. }
  10300. }
  10301. if (flag == 0) {
  10302. flag = wc_Sha512GetHash(NULL, hash1);
  10303. if (flag == BAD_FUNC_ARG) {
  10304. flag = 0;
  10305. }
  10306. }
  10307. if (flag == 0) {
  10308. flag = wc_Sha512GetHash(&sha512, NULL);
  10309. if (flag == BAD_FUNC_ARG) {
  10310. flag = 0;
  10311. }
  10312. }
  10313. wc_Sha512Free(&sha512);
  10314. res = TEST_RES_CHECK(flag == 0);
  10315. #endif
  10316. return res;
  10317. } /* END test_wc_Sha512GetHash */
  10318. /*
  10319. * Unit test function for wc_Sha512Copy()
  10320. */
  10321. static int test_wc_Sha512Copy(void)
  10322. {
  10323. int res = TEST_SKIPPED;
  10324. #ifdef WOLFSSL_SHA512
  10325. wc_Sha512 sha512;
  10326. wc_Sha512 temp;
  10327. int flag;
  10328. /* Initialize */
  10329. flag = wc_InitSha512(&sha512);
  10330. if (flag == 0) {
  10331. flag = wc_InitSha512(&temp);
  10332. }
  10333. if (flag == 0) {
  10334. flag = wc_Sha512Copy(&sha512, &temp);
  10335. }
  10336. /*test bad arguments*/
  10337. if (flag == 0) {
  10338. flag = wc_Sha512Copy(NULL, NULL);
  10339. if (flag == BAD_FUNC_ARG) {
  10340. flag = 0;
  10341. }
  10342. }
  10343. if (flag == 0) {
  10344. flag = wc_Sha512Copy(NULL, &temp);
  10345. if (flag == BAD_FUNC_ARG) {
  10346. flag = 0;
  10347. }
  10348. }
  10349. if (flag == 0) {
  10350. flag = wc_Sha512Copy(&sha512, NULL);
  10351. if (flag == BAD_FUNC_ARG) {
  10352. flag = 0;
  10353. }
  10354. }
  10355. wc_Sha512Free(&sha512);
  10356. wc_Sha512Free(&temp);
  10357. res = TEST_RES_CHECK(flag == 0);
  10358. #endif
  10359. return res;
  10360. } /* END test_wc_Sha512Copy */
  10361. static int test_wc_InitSha512_224(void)
  10362. {
  10363. int res = TEST_SKIPPED;
  10364. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10365. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10366. wc_Sha512 sha512;
  10367. int ret;
  10368. int flag = 0;
  10369. /* Test good arg. */
  10370. ret = wc_InitSha512_224(&sha512);
  10371. if (ret != 0) {
  10372. flag = WOLFSSL_FATAL_ERROR;
  10373. }
  10374. /* Test bad arg. */
  10375. if (!flag) {
  10376. ret = wc_InitSha512_224(NULL);
  10377. if (ret != BAD_FUNC_ARG) {
  10378. flag = WOLFSSL_FATAL_ERROR;
  10379. }
  10380. }
  10381. wc_Sha512_224Free(&sha512);
  10382. res = TEST_RES_CHECK(flag == 0);
  10383. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10384. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10385. return res;
  10386. }
  10387. static int test_wc_Sha512_224Update(void)
  10388. {
  10389. int res = TEST_SKIPPED;
  10390. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10391. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10392. wc_Sha512 sha512;
  10393. byte hash[WC_SHA512_DIGEST_SIZE];
  10394. testVector a, c;
  10395. int ret;
  10396. int flag = 0;
  10397. ret = wc_InitSha512_224(&sha512);
  10398. if (ret != 0) {
  10399. flag = ret;
  10400. }
  10401. /* Input. */
  10402. if (!flag) {
  10403. a.input = "a";
  10404. a.inLen = XSTRLEN(a.input);
  10405. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10406. if (ret != 0) {
  10407. flag = ret;
  10408. }
  10409. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10410. if (ret != 0) {
  10411. flag = ret;
  10412. }
  10413. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10414. if (ret != 0) {
  10415. flag = ret;
  10416. }
  10417. ret = wc_Sha512_224Final(&sha512, hash);
  10418. if (ret != 0) {
  10419. flag = ret;
  10420. }
  10421. }
  10422. /* Update input. */
  10423. if (!flag) {
  10424. a.input = "abc";
  10425. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10426. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10427. a.inLen = XSTRLEN(a.input);
  10428. a.outLen = XSTRLEN(a.output);
  10429. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10430. if (ret != 0) {
  10431. flag = ret;
  10432. }
  10433. }
  10434. if (!flag) {
  10435. ret = wc_Sha512_224Final(&sha512, hash);
  10436. if (ret != 0) {
  10437. flag = ret;
  10438. }
  10439. }
  10440. if (!flag) {
  10441. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10442. flag = WOLFSSL_FATAL_ERROR;
  10443. }
  10444. }
  10445. if (!flag) {
  10446. c.input = NULL;
  10447. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  10448. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10449. if (ret != BAD_FUNC_ARG) {
  10450. flag = WOLFSSL_FATAL_ERROR;
  10451. }
  10452. }
  10453. if (!flag) {
  10454. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10455. if (ret != BAD_FUNC_ARG) {
  10456. flag = WOLFSSL_FATAL_ERROR;
  10457. }
  10458. }
  10459. wc_Sha512_224Free(&sha512);
  10460. res = TEST_RES_CHECK(flag == 0);
  10461. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10462. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10463. return res;
  10464. }
  10465. static int test_wc_Sha512_224Final(void)
  10466. {
  10467. int res = TEST_SKIPPED;
  10468. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10469. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10470. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  10471. res = TEST_RES_CHECK(ret == 0);
  10472. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10473. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10474. return res;
  10475. }
  10476. static int test_wc_Sha512_224GetFlags(void)
  10477. {
  10478. int res = TEST_SKIPPED;
  10479. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10480. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  10481. wc_Sha512 sha512, copy;
  10482. word32 flags = 0;
  10483. int flag = 0;
  10484. /* Initialize */
  10485. flag = wc_InitSha512_224(&sha512);
  10486. if (!flag) {
  10487. flag = wc_InitSha512_224(&copy);
  10488. }
  10489. if (!flag) {
  10490. flag = wc_Sha512_224Copy(&sha512, &copy);
  10491. }
  10492. if (!flag) {
  10493. flag = wc_Sha512_224GetFlags(&copy, &flags);
  10494. }
  10495. if (!flag) {
  10496. if (flags & WC_HASH_FLAG_ISCOPY)
  10497. flag = 0;
  10498. else
  10499. flag = WOLFSSL_FATAL_ERROR;
  10500. }
  10501. wc_Sha512_224Free(&copy);
  10502. wc_Sha512_224Free(&sha512);
  10503. res = TEST_RES_CHECK(flag == 0);
  10504. #endif
  10505. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10506. return res;
  10507. }
  10508. static int test_wc_Sha512_224FinalRaw(void)
  10509. {
  10510. int res = TEST_SKIPPED;
  10511. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10512. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  10513. !defined(WOLFSSL_NO_HASH_RAW)
  10514. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  10515. res = TEST_RES_CHECK(ret == 0);
  10516. #endif
  10517. return res;
  10518. }
  10519. static int test_wc_Sha512_224Free(void)
  10520. {
  10521. int res = TEST_SKIPPED;
  10522. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10523. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10524. wc_Sha512_224Free(NULL);
  10525. res = TEST_RES_CHECK(1);
  10526. #endif
  10527. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10528. return res;
  10529. }
  10530. static int test_wc_Sha512_224GetHash(void)
  10531. {
  10532. int res = TEST_SKIPPED;
  10533. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10534. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10535. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  10536. res = TEST_RES_CHECK(ret == 0);
  10537. #endif
  10538. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10539. return res;
  10540. }
  10541. static int test_wc_Sha512_224Copy(void)
  10542. {
  10543. int res = TEST_SKIPPED;
  10544. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10545. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10546. wc_Sha512 sha512;
  10547. wc_Sha512 temp;
  10548. int flag = 0;
  10549. /* Initialize */
  10550. flag = wc_InitSha512_224(&sha512);
  10551. if (flag == 0) {
  10552. flag = wc_InitSha512_224(&temp);
  10553. }
  10554. if (flag == 0) {
  10555. flag = wc_Sha512_224Copy(&sha512, &temp);
  10556. }
  10557. /*test bad arguments*/
  10558. if (flag == 0) {
  10559. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  10560. flag = WOLFSSL_FATAL_ERROR;
  10561. }
  10562. if (flag == 0) {
  10563. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  10564. flag = WOLFSSL_FATAL_ERROR;
  10565. }
  10566. if (flag == 0) {
  10567. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10568. flag = WOLFSSL_FATAL_ERROR;
  10569. }
  10570. wc_Sha512_224Free(&sha512);
  10571. wc_Sha512_224Free(&temp);
  10572. res = TEST_RES_CHECK(flag == 0);
  10573. #endif
  10574. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10575. return res;
  10576. }
  10577. static int test_wc_InitSha512_256(void)
  10578. {
  10579. int res = TEST_SKIPPED;
  10580. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10581. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10582. wc_Sha512 sha512;
  10583. int ret;
  10584. int flag = 0;
  10585. /* Test good arg. */
  10586. ret = wc_InitSha512_256(&sha512);
  10587. if (ret != 0) {
  10588. flag = WOLFSSL_FATAL_ERROR;
  10589. }
  10590. /* Test bad arg. */
  10591. if (!flag) {
  10592. ret = wc_InitSha512_256(NULL);
  10593. if (ret != BAD_FUNC_ARG) {
  10594. flag = WOLFSSL_FATAL_ERROR;
  10595. }
  10596. }
  10597. wc_Sha512_256Free(&sha512);
  10598. res = TEST_RES_CHECK(flag == 0);
  10599. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10600. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10601. return res;
  10602. }
  10603. static int test_wc_Sha512_256Update(void)
  10604. {
  10605. int res = TEST_SKIPPED;
  10606. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10607. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10608. wc_Sha512 sha512;
  10609. byte hash[WC_SHA512_DIGEST_SIZE];
  10610. testVector a, c;
  10611. int ret;
  10612. int flag = 0;
  10613. ret = wc_InitSha512_256(&sha512);
  10614. if (ret != 0) {
  10615. flag = ret;
  10616. }
  10617. /* Input. */
  10618. if (!flag) {
  10619. a.input = "a";
  10620. a.inLen = XSTRLEN(a.input);
  10621. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  10622. if (ret != 0) {
  10623. flag = ret;
  10624. }
  10625. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  10626. if (ret != 0) {
  10627. flag = ret;
  10628. }
  10629. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10630. if (ret != 0) {
  10631. flag = ret;
  10632. }
  10633. ret = wc_Sha512_256Final(&sha512, hash);
  10634. if (ret != 0) {
  10635. flag = ret;
  10636. }
  10637. }
  10638. /* Update input. */
  10639. if (!flag) {
  10640. a.input = "abc";
  10641. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  10642. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  10643. "\x07\xe7\xaf\x23";
  10644. a.inLen = XSTRLEN(a.input);
  10645. a.outLen = XSTRLEN(a.output);
  10646. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10647. if (ret != 0) {
  10648. flag = ret;
  10649. }
  10650. }
  10651. if (!flag) {
  10652. ret = wc_Sha512_256Final(&sha512, hash);
  10653. if (ret != 0) {
  10654. flag = ret;
  10655. }
  10656. }
  10657. if (!flag) {
  10658. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  10659. flag = WOLFSSL_FATAL_ERROR;
  10660. }
  10661. }
  10662. if (!flag) {
  10663. c.input = NULL;
  10664. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  10665. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10666. if (ret != BAD_FUNC_ARG) {
  10667. flag = WOLFSSL_FATAL_ERROR;
  10668. }
  10669. }
  10670. if (!flag) {
  10671. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10672. if (ret != BAD_FUNC_ARG) {
  10673. flag = WOLFSSL_FATAL_ERROR;
  10674. }
  10675. }
  10676. wc_Sha512_256Free(&sha512);
  10677. res = TEST_RES_CHECK(flag == 0);
  10678. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10679. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10680. return res;
  10681. }
  10682. static int test_wc_Sha512_256Final(void)
  10683. {
  10684. int res = TEST_SKIPPED;
  10685. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10686. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10687. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  10688. res = TEST_RES_CHECK(ret == 0);
  10689. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10690. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10691. return res;
  10692. }
  10693. static int test_wc_Sha512_256GetFlags(void)
  10694. {
  10695. int res = TEST_SKIPPED;
  10696. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10697. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  10698. wc_Sha512 sha512, copy;
  10699. word32 flags = 0;
  10700. int flag = 0;
  10701. /* Initialize */
  10702. flag = wc_InitSha512_256(&sha512);
  10703. if (!flag ) {
  10704. flag = wc_InitSha512_256(&copy);
  10705. }
  10706. if (!flag ) {
  10707. flag = wc_Sha512_256Copy(&sha512, &copy);
  10708. }
  10709. if (!flag ) {
  10710. flag = wc_Sha512_256GetFlags(&copy, &flags);
  10711. }
  10712. if (!flag) {
  10713. if (flags & WC_HASH_FLAG_ISCOPY)
  10714. flag = 0;
  10715. else
  10716. flag = WOLFSSL_FATAL_ERROR;
  10717. }
  10718. wc_Sha512_256Free(&sha512);
  10719. res = TEST_RES_CHECK(flag == 0);
  10720. #endif
  10721. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10722. return res;
  10723. }
  10724. static int test_wc_Sha512_256FinalRaw(void)
  10725. {
  10726. int res = TEST_SKIPPED;
  10727. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10728. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  10729. !defined(WOLFSSL_NO_HASH_RAW)
  10730. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  10731. res = TEST_RES_CHECK(ret == 0);
  10732. #endif
  10733. return res;
  10734. }
  10735. static int test_wc_Sha512_256Free(void)
  10736. {
  10737. int res = TEST_SKIPPED;
  10738. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10739. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10740. wc_Sha512_256Free(NULL);
  10741. res = TEST_RES_CHECK(1);
  10742. #endif
  10743. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10744. return res;
  10745. }
  10746. static int test_wc_Sha512_256GetHash(void)
  10747. {
  10748. int res = TEST_SKIPPED;
  10749. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10750. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10751. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10752. res = TEST_RES_CHECK(ret == 0);
  10753. #endif
  10754. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10755. return res;
  10756. }
  10757. static int test_wc_Sha512_256Copy(void)
  10758. {
  10759. int res = TEST_SKIPPED;
  10760. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10761. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10762. wc_Sha512 sha512;
  10763. wc_Sha512 temp;
  10764. int flag = 0;
  10765. /* Initialize */
  10766. flag = wc_InitSha512_256(&sha512);
  10767. if (flag == 0) {
  10768. flag = wc_InitSha512_256(&temp);
  10769. }
  10770. if (flag == 0) {
  10771. flag = wc_Sha512_256Copy(&sha512, &temp);
  10772. }
  10773. /*test bad arguments*/
  10774. if (flag == 0) {
  10775. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10776. flag = WOLFSSL_FATAL_ERROR;
  10777. }
  10778. if (flag == 0) {
  10779. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10780. flag = WOLFSSL_FATAL_ERROR;
  10781. }
  10782. if (flag == 0) {
  10783. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10784. flag = WOLFSSL_FATAL_ERROR;
  10785. }
  10786. wc_Sha512_256Free(&sha512);
  10787. wc_Sha512_256Free(&temp);
  10788. res = TEST_RES_CHECK(flag == 0);
  10789. #endif
  10790. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10791. return res;
  10792. }
  10793. /*
  10794. * Testing wc_InitSha384()
  10795. */
  10796. static int test_wc_InitSha384(void)
  10797. {
  10798. int res = TEST_SKIPPED;
  10799. #ifdef WOLFSSL_SHA384
  10800. wc_Sha384 sha384;
  10801. int ret;
  10802. int flag = 0;
  10803. /* Test good arg. */
  10804. ret = wc_InitSha384(&sha384);
  10805. if (ret != 0) {
  10806. flag = WOLFSSL_FATAL_ERROR;
  10807. }
  10808. /* Test bad arg. */
  10809. if (!flag) {
  10810. ret = wc_InitSha384(NULL);
  10811. if (ret != BAD_FUNC_ARG) {
  10812. flag = WOLFSSL_FATAL_ERROR;
  10813. }
  10814. }
  10815. wc_Sha384Free(&sha384);
  10816. res = TEST_RES_CHECK(flag == 0);
  10817. #endif
  10818. return res;
  10819. } /* END test_wc_InitSha384 */
  10820. /*
  10821. * test wc_Sha384Update()
  10822. */
  10823. static int test_wc_Sha384Update(void)
  10824. {
  10825. int res = TEST_SKIPPED;
  10826. #ifdef WOLFSSL_SHA384
  10827. wc_Sha384 sha384;
  10828. byte hash[WC_SHA384_DIGEST_SIZE];
  10829. testVector a, b, c;
  10830. int ret;
  10831. int flag = 0;
  10832. ret = wc_InitSha384(&sha384);
  10833. if (ret != 0) {
  10834. flag = ret;
  10835. }
  10836. /* Input */
  10837. if (!flag) {
  10838. a.input = "a";
  10839. a.inLen = XSTRLEN(a.input);
  10840. ret = wc_Sha384Update(&sha384, NULL, 0);
  10841. if (ret != 0) {
  10842. flag = ret;
  10843. }
  10844. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10845. if (ret != 0) {
  10846. flag = ret;
  10847. }
  10848. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10849. if (ret != 0) {
  10850. flag = ret;
  10851. }
  10852. }
  10853. if (!flag) {
  10854. ret = wc_Sha384Final(&sha384, hash);
  10855. if (ret != 0) {
  10856. flag = ret;
  10857. }
  10858. }
  10859. /* Update input. */
  10860. if (!flag) {
  10861. a.input = "abc";
  10862. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10863. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10864. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10865. "\xc8\x25\xa7";
  10866. a.inLen = XSTRLEN(a.input);
  10867. a.outLen = XSTRLEN(a.output);
  10868. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10869. if (ret != 0) {
  10870. flag = ret;
  10871. }
  10872. }
  10873. if (!flag) {
  10874. ret = wc_Sha384Final(&sha384, hash);
  10875. if (ret != 0) {
  10876. flag = ret;
  10877. }
  10878. }
  10879. if (!flag) {
  10880. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10881. flag = WOLFSSL_FATAL_ERROR;
  10882. }
  10883. }
  10884. /* Pass in bad values. */
  10885. if (!flag) {
  10886. b.input = NULL;
  10887. b.inLen = 0;
  10888. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10889. if (ret != 0) {
  10890. flag = ret;
  10891. }
  10892. }
  10893. if (!flag) {
  10894. c.input = NULL;
  10895. c.inLen = WC_SHA384_DIGEST_SIZE;
  10896. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10897. if (ret != BAD_FUNC_ARG) {
  10898. flag = WOLFSSL_FATAL_ERROR;
  10899. }
  10900. }
  10901. if (!flag) {
  10902. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10903. if (ret != BAD_FUNC_ARG) {
  10904. flag = WOLFSSL_FATAL_ERROR;
  10905. }
  10906. }
  10907. wc_Sha384Free(&sha384);
  10908. res = TEST_RES_CHECK(flag == 0);
  10909. #endif
  10910. return res;
  10911. } /* END test_wc_Sha384Update */
  10912. /*
  10913. * Unit test function for wc_Sha384Final();
  10914. */
  10915. static int test_wc_Sha384Final(void)
  10916. {
  10917. int res = TEST_SKIPPED;
  10918. #ifdef WOLFSSL_SHA384
  10919. wc_Sha384 sha384;
  10920. byte* hash_test[3];
  10921. byte hash1[WC_SHA384_DIGEST_SIZE];
  10922. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10923. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10924. int times, i, ret;
  10925. int flag = 0;
  10926. /* Initialize */
  10927. ret = wc_InitSha384(&sha384);
  10928. if (ret) {
  10929. flag = ret;
  10930. }
  10931. if (!flag) {
  10932. hash_test[0] = hash1;
  10933. hash_test[1] = hash2;
  10934. hash_test[2] = hash3;
  10935. }
  10936. times = sizeof(hash_test) / sizeof(byte*);
  10937. /* Good test args. */
  10938. for (i = 0; i < times; i++) {
  10939. if (!flag) {
  10940. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10941. if (ret != 0) {
  10942. flag = WOLFSSL_FATAL_ERROR;
  10943. }
  10944. }
  10945. }
  10946. /* Test bad args. */
  10947. if (!flag) {
  10948. ret = wc_Sha384Final(NULL, NULL);
  10949. if (ret != BAD_FUNC_ARG) {
  10950. flag = WOLFSSL_FATAL_ERROR;
  10951. }
  10952. }
  10953. if (!flag) {
  10954. ret = wc_Sha384Final(NULL, hash1);
  10955. if (ret != BAD_FUNC_ARG) {
  10956. flag = WOLFSSL_FATAL_ERROR;
  10957. }
  10958. }
  10959. if (!flag) {
  10960. ret = wc_Sha384Final(&sha384, NULL);
  10961. if (ret != BAD_FUNC_ARG) {
  10962. flag = WOLFSSL_FATAL_ERROR;
  10963. }
  10964. }
  10965. wc_Sha384Free(&sha384);
  10966. res = TEST_RES_CHECK(flag == 0);
  10967. #endif
  10968. return res;
  10969. } /* END test_wc_Sha384Final */
  10970. /*
  10971. * Unit test function for wc_Sha384GetFlags()
  10972. */
  10973. static int test_wc_Sha384GetFlags(void)
  10974. {
  10975. int res = TEST_SKIPPED;
  10976. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10977. wc_Sha384 sha384;
  10978. word32 flags = 0;
  10979. int flag = 0;
  10980. /* Initialize */
  10981. flag = wc_InitSha384(&sha384);
  10982. if (flag == 0) {
  10983. flag = wc_Sha384GetFlags(&sha384, &flags);
  10984. }
  10985. if (flag == 0) {
  10986. if (flags & WC_HASH_FLAG_ISCOPY) {
  10987. flag = 0;
  10988. }
  10989. }
  10990. wc_Sha384Free(&sha384);
  10991. res = TEST_RES_CHECK(flag == 0);
  10992. #endif
  10993. return res;
  10994. } /* END test_wc_Sha384GetFlags */
  10995. /*
  10996. * Unit test function for wc_Sha384FinalRaw()
  10997. */
  10998. static int test_wc_Sha384FinalRaw(void)
  10999. {
  11000. int res = TEST_SKIPPED;
  11001. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11002. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11003. !defined(WOLFSSL_NO_HASH_RAW)
  11004. wc_Sha384 sha384;
  11005. byte* hash_test[3];
  11006. byte hash1[WC_SHA384_DIGEST_SIZE];
  11007. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11008. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11009. int times, i, ret;
  11010. int flag = 0;
  11011. /* Initialize */
  11012. ret = wc_InitSha384(&sha384);
  11013. if (ret != 0) {
  11014. flag = ret;
  11015. }
  11016. if (!flag) {
  11017. hash_test[0] = hash1;
  11018. hash_test[1] = hash2;
  11019. hash_test[2] = hash3;
  11020. }
  11021. times = sizeof(hash_test) / sizeof(byte*);
  11022. /* Good test args. */
  11023. for (i = 0; i < times; i++) {
  11024. if (!flag) {
  11025. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  11026. if (ret != 0) {
  11027. flag = WOLFSSL_FATAL_ERROR;
  11028. }
  11029. }
  11030. }
  11031. /* Test bad args. */
  11032. if (!flag ) {
  11033. ret = wc_Sha384FinalRaw(NULL, NULL);
  11034. if (ret != BAD_FUNC_ARG) {
  11035. flag = WOLFSSL_FATAL_ERROR;
  11036. }
  11037. }
  11038. if (!flag) {
  11039. ret = wc_Sha384FinalRaw(NULL, hash1);
  11040. if (ret != BAD_FUNC_ARG) {
  11041. flag = WOLFSSL_FATAL_ERROR;
  11042. }
  11043. }
  11044. if (!flag) {
  11045. ret = wc_Sha384FinalRaw(&sha384, NULL);
  11046. if (ret != BAD_FUNC_ARG) {
  11047. flag = WOLFSSL_FATAL_ERROR;
  11048. }
  11049. }
  11050. wc_Sha384Free(&sha384);
  11051. res = TEST_RES_CHECK(flag == 0);
  11052. #endif
  11053. return res;
  11054. } /* END test_wc_Sha384FinalRaw */
  11055. /*
  11056. * Unit test function for wc_Sha384Free()
  11057. */
  11058. static int test_wc_Sha384Free(void)
  11059. {
  11060. int res = TEST_SKIPPED;
  11061. #ifdef WOLFSSL_SHA384
  11062. wc_Sha384Free(NULL);
  11063. res = TEST_RES_CHECK(1);
  11064. #endif
  11065. return res;
  11066. } /* END test_wc_Sha384Free */
  11067. /*
  11068. * Unit test function for wc_Sha384GetHash()
  11069. */
  11070. static int test_wc_Sha384GetHash(void)
  11071. {
  11072. int res = TEST_SKIPPED;
  11073. #ifdef WOLFSSL_SHA384
  11074. wc_Sha384 sha384;
  11075. byte hash1[WC_SHA384_DIGEST_SIZE];
  11076. int flag = 0;
  11077. /* Initialize */
  11078. flag = wc_InitSha384(&sha384);
  11079. if (flag == 0) {
  11080. flag = wc_Sha384GetHash(&sha384, hash1);
  11081. }
  11082. /*test bad arguments*/
  11083. if (flag == 0) {
  11084. flag = wc_Sha384GetHash(NULL, NULL);
  11085. if (flag == BAD_FUNC_ARG) {
  11086. flag = 0;
  11087. }
  11088. }
  11089. if (flag == 0) {
  11090. flag = wc_Sha384GetHash(NULL, hash1);
  11091. if (flag == BAD_FUNC_ARG) {
  11092. flag = 0;
  11093. }
  11094. }
  11095. if (flag == 0) {
  11096. flag = wc_Sha384GetHash(&sha384, NULL);
  11097. if (flag == BAD_FUNC_ARG) {
  11098. flag = 0;
  11099. }
  11100. }
  11101. wc_Sha384Free(&sha384);
  11102. res = TEST_RES_CHECK(flag == 0);
  11103. #endif
  11104. return res;
  11105. } /* END test_wc_Sha384GetHash */
  11106. /*
  11107. * Unit test function for wc_Sha384Copy()
  11108. */
  11109. static int test_wc_Sha384Copy(void)
  11110. {
  11111. int res = TEST_SKIPPED;
  11112. #ifdef WOLFSSL_SHA384
  11113. wc_Sha384 sha384;
  11114. wc_Sha384 temp;
  11115. int flag = 0;
  11116. /* Initialize */
  11117. flag = wc_InitSha384(&sha384);
  11118. if (flag == 0) {
  11119. flag = wc_InitSha384(&temp);
  11120. }
  11121. if (flag == 0) {
  11122. flag = wc_Sha384Copy(&sha384, &temp);
  11123. }
  11124. /*test bad arguments*/
  11125. if (flag == 0) {
  11126. flag = wc_Sha384Copy(NULL, NULL);
  11127. if (flag == BAD_FUNC_ARG) {
  11128. flag = 0;
  11129. }
  11130. }
  11131. if (flag == 0) {
  11132. flag = wc_Sha384Copy(NULL, &temp);
  11133. if (flag == BAD_FUNC_ARG) {
  11134. flag = 0;
  11135. }
  11136. }
  11137. if (flag == 0) {
  11138. flag = wc_Sha384Copy(&sha384, NULL);
  11139. if (flag == BAD_FUNC_ARG) {
  11140. flag = 0;
  11141. }
  11142. }
  11143. wc_Sha384Free(&sha384);
  11144. wc_Sha384Free(&temp);
  11145. res = TEST_RES_CHECK(flag == 0);
  11146. #endif
  11147. return res;
  11148. } /* END test_wc_Sha384Copy */
  11149. /*
  11150. * Testing wc_InitSha224();
  11151. */
  11152. static int test_wc_InitSha224(void)
  11153. {
  11154. int res = TEST_SKIPPED;
  11155. #ifdef WOLFSSL_SHA224
  11156. wc_Sha224 sha224;
  11157. int ret;
  11158. int flag = 0;
  11159. /* Test good arg. */
  11160. ret = wc_InitSha224(&sha224);
  11161. if (ret != 0) {
  11162. flag = WOLFSSL_FATAL_ERROR;
  11163. }
  11164. /* Test bad arg. */
  11165. if (!flag) {
  11166. ret = wc_InitSha224(NULL);
  11167. if (ret != BAD_FUNC_ARG) {
  11168. flag = WOLFSSL_FATAL_ERROR;
  11169. }
  11170. }
  11171. wc_Sha224Free(&sha224);
  11172. res = TEST_RES_CHECK(flag == 0);
  11173. #endif
  11174. return res;
  11175. } /* END test_wc_InitSha224 */
  11176. /*
  11177. * Unit test on wc_Sha224Update
  11178. */
  11179. static int test_wc_Sha224Update(void)
  11180. {
  11181. int res = TEST_SKIPPED;
  11182. #ifdef WOLFSSL_SHA224
  11183. wc_Sha224 sha224;
  11184. byte hash[WC_SHA224_DIGEST_SIZE];
  11185. testVector a, b, c;
  11186. int ret;
  11187. int flag = 0;
  11188. ret = wc_InitSha224(&sha224);
  11189. if (ret != 0) {
  11190. flag = ret;
  11191. }
  11192. /* Input. */
  11193. if (!flag) {
  11194. a.input = "a";
  11195. a.inLen = XSTRLEN(a.input);
  11196. ret = wc_Sha224Update(&sha224, NULL, 0);
  11197. if (ret != 0) {
  11198. flag = ret;
  11199. }
  11200. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  11201. if (ret != 0) {
  11202. flag = ret;
  11203. }
  11204. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11205. if (ret != 0) {
  11206. flag = ret;
  11207. }
  11208. }
  11209. if (!flag) {
  11210. ret = wc_Sha224Final(&sha224, hash);
  11211. if (ret != 0) {
  11212. flag = ret;
  11213. }
  11214. }
  11215. /* Update input. */
  11216. if (!flag) {
  11217. a.input = "abc";
  11218. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11219. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11220. a.inLen = XSTRLEN(a.input);
  11221. a.outLen = XSTRLEN(a.output);
  11222. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11223. if (ret != 0) {
  11224. flag = ret;
  11225. }
  11226. }
  11227. if (!flag) {
  11228. ret = wc_Sha224Final(&sha224, hash);
  11229. if (ret != 0) {
  11230. flag = ret;
  11231. }
  11232. }
  11233. if (!flag) {
  11234. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11235. flag = WOLFSSL_FATAL_ERROR;
  11236. }
  11237. }
  11238. /* Pass in bad values. */
  11239. if (!flag) {
  11240. b.input = NULL;
  11241. b.inLen = 0;
  11242. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11243. if (ret != 0) {
  11244. flag = ret;
  11245. }
  11246. }
  11247. if (!flag) {
  11248. c.input = NULL;
  11249. c.inLen = WC_SHA224_DIGEST_SIZE;
  11250. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  11251. if (ret != BAD_FUNC_ARG) {
  11252. flag = WOLFSSL_FATAL_ERROR;
  11253. }
  11254. }
  11255. if (!flag) {
  11256. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11257. if (ret != BAD_FUNC_ARG) {
  11258. flag = WOLFSSL_FATAL_ERROR;
  11259. }
  11260. }
  11261. wc_Sha224Free(&sha224);
  11262. res = TEST_RES_CHECK(flag == 0);
  11263. #endif
  11264. return res;
  11265. } /* END test_wc_Sha224Update */
  11266. /*
  11267. * Unit test for wc_Sha224Final();
  11268. */
  11269. static int test_wc_Sha224Final(void)
  11270. {
  11271. int res = TEST_SKIPPED;
  11272. #ifdef WOLFSSL_SHA224
  11273. wc_Sha224 sha224;
  11274. byte* hash_test[3];
  11275. byte hash1[WC_SHA224_DIGEST_SIZE];
  11276. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11277. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11278. int times, i, ret;
  11279. int flag = 0;
  11280. /* Initialize */
  11281. ret = wc_InitSha224(&sha224);
  11282. if (ret) {
  11283. flag = ret;
  11284. }
  11285. if (!flag) {
  11286. hash_test[0] = hash1;
  11287. hash_test[1] = hash2;
  11288. hash_test[2] = hash3;
  11289. }
  11290. times = sizeof(hash_test) / sizeof(byte*);
  11291. /* Good test args. */
  11292. /* Testing oversized buffers. */
  11293. for (i = 0; i < times; i++) {
  11294. if (!flag) {
  11295. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11296. if (ret != 0) {
  11297. flag = WOLFSSL_FATAL_ERROR;
  11298. }
  11299. }
  11300. }
  11301. /* Test bad args. */
  11302. if (!flag) {
  11303. ret = wc_Sha224Final(NULL, NULL);
  11304. if (ret != BAD_FUNC_ARG) {
  11305. flag = WOLFSSL_FATAL_ERROR;
  11306. }
  11307. }
  11308. if (!flag) {
  11309. ret = wc_Sha224Final(NULL, hash1);
  11310. if (ret != BAD_FUNC_ARG) {
  11311. flag = WOLFSSL_FATAL_ERROR;
  11312. }
  11313. }
  11314. if (!flag) {
  11315. ret = wc_Sha224Final(&sha224, NULL);
  11316. if (ret != BAD_FUNC_ARG) {
  11317. flag = WOLFSSL_FATAL_ERROR;
  11318. }
  11319. }
  11320. wc_Sha224Free(&sha224);
  11321. res = TEST_RES_CHECK(flag == 0);
  11322. #endif
  11323. return res;
  11324. } /* END test_wc_Sha224Final */
  11325. /*
  11326. * Unit test function for wc_Sha224SetFlags()
  11327. */
  11328. static int test_wc_Sha224SetFlags(void)
  11329. {
  11330. int res = TEST_SKIPPED;
  11331. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11332. wc_Sha224 sha224;
  11333. word32 flags = 0;
  11334. int flag = 0;
  11335. /* Initialize */
  11336. flag = wc_InitSha224(&sha224);
  11337. if (flag == 0) {
  11338. flag = wc_Sha224SetFlags(&sha224, flags);
  11339. }
  11340. if (flag == 0) {
  11341. if (flags & WC_HASH_FLAG_ISCOPY) {
  11342. flag = 0;
  11343. }
  11344. }
  11345. wc_Sha224Free(&sha224);
  11346. res = TEST_RES_CHECK(flag == 0);
  11347. #endif
  11348. return res;
  11349. } /* END test_wc_Sha224SetFlags */
  11350. /*
  11351. * Unit test function for wc_Sha224GetFlags()
  11352. */
  11353. static int test_wc_Sha224GetFlags(void)
  11354. {
  11355. int res = TEST_SKIPPED;
  11356. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11357. wc_Sha224 sha224;
  11358. word32 flags = 0;
  11359. int flag = 0;
  11360. /* Initialize */
  11361. flag = wc_InitSha224(&sha224);
  11362. if (flag == 0) {
  11363. flag = wc_Sha224GetFlags(&sha224, &flags);
  11364. }
  11365. if (flag == 0) {
  11366. if (flags & WC_HASH_FLAG_ISCOPY) {
  11367. flag = 0;
  11368. }
  11369. }
  11370. wc_Sha224Free(&sha224);
  11371. res = TEST_RES_CHECK(flag == 0);
  11372. #endif
  11373. return res;
  11374. } /* END test_wc_Sha224GetFlags */
  11375. /*
  11376. * Unit test function for wc_Sha224Free()
  11377. */
  11378. static int test_wc_Sha224Free(void)
  11379. {
  11380. int res = TEST_SKIPPED;
  11381. #ifdef WOLFSSL_SHA224
  11382. wc_Sha224Free(NULL);
  11383. res = TEST_RES_CHECK(1);
  11384. #endif
  11385. return res;
  11386. } /* END test_wc_Sha224Free */
  11387. /*
  11388. * Unit test function for wc_Sha224GetHash()
  11389. */
  11390. static int test_wc_Sha224GetHash(void)
  11391. {
  11392. int res = TEST_SKIPPED;
  11393. #ifdef WOLFSSL_SHA224
  11394. wc_Sha224 sha224;
  11395. byte hash1[WC_SHA224_DIGEST_SIZE];
  11396. int flag = 0;
  11397. /* Initialize */
  11398. flag = wc_InitSha224(&sha224);
  11399. if (flag == 0) {
  11400. flag = wc_Sha224GetHash(&sha224, hash1);
  11401. }
  11402. /*test bad arguments*/
  11403. if (flag == 0) {
  11404. flag = wc_Sha224GetHash(NULL, NULL);
  11405. if (flag == BAD_FUNC_ARG) {
  11406. flag = 0;
  11407. }
  11408. }
  11409. if (flag == 0) {
  11410. flag = wc_Sha224GetHash(NULL, hash1);
  11411. if (flag == BAD_FUNC_ARG) {
  11412. flag = 0;
  11413. }
  11414. }
  11415. if (flag == 0) {
  11416. flag = wc_Sha224GetHash(&sha224, NULL);
  11417. if (flag == BAD_FUNC_ARG) {
  11418. flag = 0;
  11419. }
  11420. }
  11421. wc_Sha224Free(&sha224);
  11422. res = TEST_RES_CHECK(flag == 0);
  11423. #endif
  11424. return res;
  11425. } /* END test_wc_Sha224GetHash */
  11426. /*
  11427. * Unit test function for wc_Sha224Copy()
  11428. */
  11429. static int test_wc_Sha224Copy(void)
  11430. {
  11431. int res = TEST_SKIPPED;
  11432. #ifdef WOLFSSL_SHA224
  11433. wc_Sha224 sha224;
  11434. wc_Sha224 temp;
  11435. int flag = 0;
  11436. /* Initialize */
  11437. flag = wc_InitSha224(&sha224);
  11438. if (flag == 0) {
  11439. flag = wc_InitSha224(&temp);
  11440. }
  11441. if (flag == 0) {
  11442. flag = wc_Sha224Copy(&sha224, &temp);
  11443. }
  11444. /*test bad arguments*/
  11445. if (flag == 0) {
  11446. flag = wc_Sha224Copy(NULL, NULL);
  11447. if (flag == BAD_FUNC_ARG) {
  11448. flag = 0;
  11449. }
  11450. }
  11451. if (flag == 0) {
  11452. flag = wc_Sha224Copy(NULL, &temp);
  11453. if (flag == BAD_FUNC_ARG) {
  11454. flag = 0;
  11455. }
  11456. }
  11457. if (flag == 0) {
  11458. flag = wc_Sha224Copy(&sha224, NULL);
  11459. if (flag == BAD_FUNC_ARG) {
  11460. flag = 0;
  11461. }
  11462. }
  11463. wc_Sha224Free(&sha224);
  11464. wc_Sha224Free(&temp);
  11465. res = TEST_RES_CHECK(flag == 0);
  11466. #endif
  11467. return res;
  11468. } /* END test_wc_Sha224Copy */
  11469. /*
  11470. * Testing wc_InitRipeMd()
  11471. */
  11472. static int test_wc_InitRipeMd(void)
  11473. {
  11474. int res = TEST_SKIPPED;
  11475. #ifdef WOLFSSL_RIPEMD
  11476. RipeMd ripemd;
  11477. int ret;
  11478. int flag = 0;
  11479. /* Test good arg. */
  11480. ret = wc_InitRipeMd(&ripemd);
  11481. if (ret != 0) {
  11482. flag = WOLFSSL_FATAL_ERROR;
  11483. }
  11484. /* Test bad arg. */
  11485. if (!flag) {
  11486. ret = wc_InitRipeMd(NULL);
  11487. if (ret != BAD_FUNC_ARG) {
  11488. flag = WOLFSSL_FATAL_ERROR;
  11489. }
  11490. }
  11491. res = TEST_RES_CHECK(flag == 0);
  11492. #endif
  11493. return res;
  11494. } /* END test_wc_InitRipeMd */
  11495. /*
  11496. * Testing wc_RipeMdUpdate()
  11497. */
  11498. static int test_wc_RipeMdUpdate(void)
  11499. {
  11500. int res = TEST_SKIPPED;
  11501. #ifdef WOLFSSL_RIPEMD
  11502. RipeMd ripemd;
  11503. byte hash[RIPEMD_DIGEST_SIZE];
  11504. testVector a, b, c;
  11505. int ret;
  11506. int flag = 0;
  11507. ret = wc_InitRipeMd(&ripemd);
  11508. if (ret != 0) {
  11509. flag = ret;
  11510. }
  11511. /* Input */
  11512. if (!flag) {
  11513. a.input = "a";
  11514. a.inLen = XSTRLEN(a.input);
  11515. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11516. if (ret != 0) {
  11517. flag = ret;
  11518. }
  11519. }
  11520. if (!flag) {
  11521. ret = wc_RipeMdFinal(&ripemd, hash);
  11522. if (ret != 0) {
  11523. flag = ret;
  11524. }
  11525. }
  11526. /* Update input. */
  11527. if (!flag) {
  11528. a.input = "abc";
  11529. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11530. "\xb0\x87\xf1\x5a\x0b\xfc";
  11531. a.inLen = XSTRLEN(a.input);
  11532. a.outLen = XSTRLEN(a.output);
  11533. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11534. if (ret != 0) {
  11535. flag = ret;
  11536. }
  11537. }
  11538. if (!flag) {
  11539. ret = wc_RipeMdFinal(&ripemd, hash);
  11540. if (ret != 0) {
  11541. flag = ret;
  11542. }
  11543. }
  11544. if (!flag) {
  11545. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  11546. flag = WOLFSSL_FATAL_ERROR;
  11547. }
  11548. }
  11549. /* Pass in bad values. */
  11550. if (!flag) {
  11551. b.input = NULL;
  11552. b.inLen = 0;
  11553. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  11554. if (ret != 0) {
  11555. flag = ret;
  11556. }
  11557. }
  11558. if (!flag) {
  11559. c.input = NULL;
  11560. c.inLen = RIPEMD_DIGEST_SIZE;
  11561. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  11562. if (ret != BAD_FUNC_ARG) {
  11563. flag = WOLFSSL_FATAL_ERROR;
  11564. }
  11565. }
  11566. if (!flag) {
  11567. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  11568. if (ret != BAD_FUNC_ARG) {
  11569. flag = WOLFSSL_FATAL_ERROR;
  11570. }
  11571. }
  11572. res = TEST_RES_CHECK(flag == 0);
  11573. #endif
  11574. return res;
  11575. } /* END test_wc_RipeMdUdpate */
  11576. /*
  11577. * Unit test function for wc_RipeMdFinal()
  11578. */
  11579. static int test_wc_RipeMdFinal(void)
  11580. {
  11581. int res = TEST_SKIPPED;
  11582. #ifdef WOLFSSL_RIPEMD
  11583. RipeMd ripemd;
  11584. byte* hash_test[3];
  11585. byte hash1[RIPEMD_DIGEST_SIZE];
  11586. byte hash2[2*RIPEMD_DIGEST_SIZE];
  11587. byte hash3[5*RIPEMD_DIGEST_SIZE];
  11588. int times, i, ret;
  11589. int flag = 0;
  11590. /* Initialize */
  11591. ret = wc_InitRipeMd(&ripemd);
  11592. if (ret != 0) {
  11593. flag = ret;
  11594. }
  11595. if (!flag) {
  11596. hash_test[0] = hash1;
  11597. hash_test[1] = hash2;
  11598. hash_test[2] = hash3;
  11599. }
  11600. times = sizeof(hash_test) / sizeof(byte*);
  11601. /* Testing oversized buffers. */
  11602. for (i = 0; i < times; i++) {
  11603. if (!flag) {
  11604. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  11605. if (ret != 0) {
  11606. flag = WOLFSSL_FATAL_ERROR;
  11607. }
  11608. }
  11609. }
  11610. /* Test bad args. */
  11611. if (!flag) {
  11612. ret = wc_RipeMdFinal(NULL, NULL);
  11613. if (ret != BAD_FUNC_ARG) {
  11614. flag = WOLFSSL_FATAL_ERROR;
  11615. }
  11616. }
  11617. if (!flag) {
  11618. ret = wc_RipeMdFinal(NULL, hash1);
  11619. if (ret != BAD_FUNC_ARG) {
  11620. flag = WOLFSSL_FATAL_ERROR;
  11621. }
  11622. }
  11623. if (!flag) {
  11624. ret = wc_RipeMdFinal(&ripemd, NULL);
  11625. if (ret != BAD_FUNC_ARG) {
  11626. flag = WOLFSSL_FATAL_ERROR;
  11627. }
  11628. }
  11629. res = TEST_RES_CHECK(flag == 0);
  11630. #endif
  11631. return res;
  11632. } /* END test_wc_RipeMdFinal */
  11633. /*
  11634. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  11635. * wc_InitSha3_512
  11636. */
  11637. static int test_wc_InitSha3(void)
  11638. {
  11639. int res = TEST_SKIPPED;
  11640. #if defined(WOLFSSL_SHA3)
  11641. wc_Sha3 sha3;
  11642. int ret = 0;
  11643. (void)sha3;
  11644. #if !defined(WOLFSSL_NOSHA3_224)
  11645. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11646. /* Test bad args. */
  11647. if (ret == 0) {
  11648. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  11649. if (ret == BAD_FUNC_ARG) {
  11650. ret = 0;
  11651. }
  11652. else if (ret == 0) {
  11653. ret = WOLFSSL_FATAL_ERROR;
  11654. }
  11655. }
  11656. wc_Sha3_224_Free(&sha3);
  11657. #endif /* NOSHA3_224 */
  11658. #if !defined(WOLFSSL_NOSHA3_256)
  11659. if (ret == 0) {
  11660. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11661. /* Test bad args. */
  11662. if (ret == 0) {
  11663. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  11664. if (ret == BAD_FUNC_ARG) {
  11665. ret = 0;
  11666. }
  11667. else if (ret == 0) {
  11668. ret = WOLFSSL_FATAL_ERROR;
  11669. }
  11670. }
  11671. wc_Sha3_256_Free(&sha3);
  11672. } /* END sha3_256 */
  11673. #endif /* NOSHA3_256 */
  11674. #if !defined(WOLFSSL_NOSHA3_384)
  11675. if (ret == 0) {
  11676. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11677. /* Test bad args. */
  11678. if (ret == 0) {
  11679. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  11680. if (ret == BAD_FUNC_ARG) {
  11681. ret = 0;
  11682. }
  11683. else if (ret == 0) {
  11684. ret = WOLFSSL_FATAL_ERROR;
  11685. }
  11686. }
  11687. wc_Sha3_384_Free(&sha3);
  11688. } /* END sha3_384 */
  11689. #endif /* NOSHA3_384 */
  11690. #if !defined(WOLFSSL_NOSHA3_512)
  11691. if (ret == 0) {
  11692. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11693. /* Test bad args. */
  11694. if (ret == 0) {
  11695. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  11696. if (ret == BAD_FUNC_ARG) {
  11697. ret = 0;
  11698. }
  11699. else if (ret == 0) {
  11700. ret = WOLFSSL_FATAL_ERROR;
  11701. }
  11702. }
  11703. wc_Sha3_512_Free(&sha3);
  11704. } /* END sha3_512 */
  11705. #endif /* NOSHA3_512 */
  11706. res = TEST_RES_CHECK(ret == 0);
  11707. #endif
  11708. return res;
  11709. } /* END test_wc_InitSha3 */
  11710. /*
  11711. * Testing wc_Sha3_Update()
  11712. */
  11713. static int testing_wc_Sha3_Update(void)
  11714. {
  11715. int res = TEST_SKIPPED;
  11716. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  11717. !defined(WOLFSSL_AFALG_XILINX)
  11718. wc_Sha3 sha3;
  11719. byte msg[] = "Everybody's working for the weekend.";
  11720. byte msg2[] = "Everybody gets Friday off.";
  11721. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11722. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11723. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11724. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11725. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11726. word32 msglen = sizeof(msg) - 1;
  11727. word32 msg2len = sizeof(msg2);
  11728. word32 msgCmplen = sizeof(msgCmp);
  11729. int ret = 0;
  11730. #if !defined(WOLFSSL_NOSHA3_224)
  11731. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11732. if (ret != 0) {
  11733. return TEST_FAIL;
  11734. }
  11735. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11736. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11737. ret = WOLFSSL_FATAL_ERROR;
  11738. }
  11739. if (ret == 0) {
  11740. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11741. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11742. ret = WOLFSSL_FATAL_ERROR;
  11743. }
  11744. }
  11745. /* Pass bad args. */
  11746. if (ret == 0) {
  11747. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11748. if (ret == BAD_FUNC_ARG) {
  11749. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11750. }
  11751. if (ret == BAD_FUNC_ARG) {
  11752. wc_Sha3_224_Free(&sha3);
  11753. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  11754. return TEST_FAIL;
  11755. }
  11756. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11757. if (ret == 0) {
  11758. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11759. }
  11760. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11761. ret = WOLFSSL_FATAL_ERROR;
  11762. }
  11763. }
  11764. }
  11765. wc_Sha3_224_Free(&sha3);
  11766. #endif /* SHA3_224 */
  11767. #if !defined(WOLFSSL_NOSHA3_256)
  11768. if (ret == 0) {
  11769. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11770. if (ret != 0) {
  11771. return TEST_FAIL;
  11772. }
  11773. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11774. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11775. ret = WOLFSSL_FATAL_ERROR;
  11776. }
  11777. if (ret == 0) {
  11778. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11779. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11780. ret = WOLFSSL_FATAL_ERROR;
  11781. }
  11782. }
  11783. /* Pass bad args. */
  11784. if (ret == 0) {
  11785. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11786. if (ret == BAD_FUNC_ARG) {
  11787. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11788. }
  11789. if (ret == BAD_FUNC_ARG) {
  11790. wc_Sha3_256_Free(&sha3);
  11791. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  11792. return TEST_FAIL;
  11793. }
  11794. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11795. if (ret == 0) {
  11796. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11797. }
  11798. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11799. ret = WOLFSSL_FATAL_ERROR;
  11800. }
  11801. }
  11802. }
  11803. wc_Sha3_256_Free(&sha3);
  11804. }
  11805. #endif /* SHA3_256 */
  11806. #if !defined(WOLFSSL_NOSHA3_384)
  11807. if (ret == 0) {
  11808. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11809. if (ret != 0) {
  11810. return TEST_FAIL;
  11811. }
  11812. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11813. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11814. ret = WOLFSSL_FATAL_ERROR;
  11815. }
  11816. if (ret == 0) {
  11817. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11818. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11819. ret = WOLFSSL_FATAL_ERROR;
  11820. }
  11821. }
  11822. /* Pass bad args. */
  11823. if (ret == 0) {
  11824. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11825. if (ret == BAD_FUNC_ARG) {
  11826. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11827. }
  11828. if (ret == BAD_FUNC_ARG) {
  11829. wc_Sha3_384_Free(&sha3);
  11830. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  11831. return TEST_FAIL;
  11832. }
  11833. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11834. if (ret == 0) {
  11835. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11836. }
  11837. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11838. ret = WOLFSSL_FATAL_ERROR;
  11839. }
  11840. }
  11841. }
  11842. wc_Sha3_384_Free(&sha3);
  11843. }
  11844. #endif /* SHA3_384 */
  11845. #if !defined(WOLFSSL_NOSHA3_512)
  11846. if (ret == 0) {
  11847. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11848. if (ret != 0) {
  11849. return TEST_FAIL;
  11850. }
  11851. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11852. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11853. ret = WOLFSSL_FATAL_ERROR;
  11854. }
  11855. if (ret == 0) {
  11856. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11857. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11858. ret = WOLFSSL_FATAL_ERROR;
  11859. }
  11860. }
  11861. /* Pass bad args. */
  11862. if (ret == 0) {
  11863. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11864. if (ret == BAD_FUNC_ARG) {
  11865. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11866. }
  11867. if (ret == BAD_FUNC_ARG) {
  11868. wc_Sha3_512_Free(&sha3);
  11869. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  11870. return TEST_FAIL;
  11871. }
  11872. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11873. if (ret == 0) {
  11874. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11875. }
  11876. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11877. ret = WOLFSSL_FATAL_ERROR;
  11878. }
  11879. }
  11880. }
  11881. wc_Sha3_512_Free(&sha3);
  11882. }
  11883. #endif /* SHA3_512 */
  11884. res = TEST_RES_CHECK(ret == 0);
  11885. #endif /* WOLFSSL_SHA3 */
  11886. return res;
  11887. } /* END testing_wc_Sha3_Update */
  11888. /*
  11889. * Testing wc_Sha3_224_Final()
  11890. */
  11891. static int test_wc_Sha3_224_Final(void)
  11892. {
  11893. int res = TEST_SKIPPED;
  11894. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11895. wc_Sha3 sha3;
  11896. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11897. "nopnopq";
  11898. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11899. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11900. "\x64\xea\xd0\xfc\xce\x33";
  11901. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11902. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11903. int ret = 0;
  11904. /* Init stack variables. */
  11905. XMEMSET(hash, 0, sizeof(hash));
  11906. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11907. if (ret != 0) {
  11908. return TEST_FAIL;
  11909. }
  11910. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11911. if (ret == 0) {
  11912. ret = wc_Sha3_224_Final(&sha3, hash);
  11913. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11914. ret = WOLFSSL_FATAL_ERROR;
  11915. }
  11916. }
  11917. /* Test bad args. */
  11918. if (ret == 0) {
  11919. ret = wc_Sha3_224_Final(NULL, hash);
  11920. if (ret == 0) {
  11921. ret = wc_Sha3_224_Final(&sha3, NULL);
  11922. }
  11923. if (ret == BAD_FUNC_ARG) {
  11924. ret = 0;
  11925. }
  11926. else if (ret == 0) {
  11927. ret = WOLFSSL_FATAL_ERROR;
  11928. }
  11929. }
  11930. wc_Sha3_224_Free(&sha3);
  11931. if (ret == 0) {
  11932. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11933. if (ret != 0) {
  11934. return TEST_FAIL;
  11935. }
  11936. /* Init stack variables. */
  11937. XMEMSET(hash, 0, sizeof(hash));
  11938. XMEMSET(hashRet, 0, sizeof(hashRet));
  11939. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11940. if (ret == 0) {
  11941. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11942. }
  11943. if (ret == 0) {
  11944. ret = wc_Sha3_224_Final(&sha3, hash);
  11945. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11946. ret = WOLFSSL_FATAL_ERROR;
  11947. }
  11948. }
  11949. if (ret == 0) {
  11950. /* Test bad args. */
  11951. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11952. if (ret == BAD_FUNC_ARG) {
  11953. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11954. }
  11955. if (ret == BAD_FUNC_ARG) {
  11956. ret = 0;
  11957. }
  11958. else if (ret == 0) {
  11959. ret = WOLFSSL_FATAL_ERROR;
  11960. }
  11961. }
  11962. }
  11963. wc_Sha3_224_Free(&sha3);
  11964. res = TEST_RES_CHECK(ret == 0);
  11965. #endif
  11966. return res;
  11967. } /* END test_wc_Sha3_224_Final */
  11968. /*
  11969. * Testing wc_Sha3_256_Final()
  11970. */
  11971. static int test_wc_Sha3_256_Final(void)
  11972. {
  11973. int res = TEST_SKIPPED;
  11974. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11975. wc_Sha3 sha3;
  11976. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11977. "nopnopq";
  11978. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11979. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11980. "\xdd\x97\x49\x6d\x33\x76";
  11981. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11982. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11983. int ret = 0;
  11984. /* Init stack variables. */
  11985. XMEMSET(hash, 0, sizeof(hash));
  11986. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11987. if (ret != 0) {
  11988. return TEST_FAIL;
  11989. }
  11990. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11991. if (ret == 0) {
  11992. ret = wc_Sha3_256_Final(&sha3, hash);
  11993. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11994. ret = WOLFSSL_FATAL_ERROR;
  11995. }
  11996. }
  11997. /* Test bad args. */
  11998. if (ret == 0) {
  11999. ret = wc_Sha3_256_Final(NULL, hash);
  12000. if (ret == 0) {
  12001. ret = wc_Sha3_256_Final(&sha3, NULL);
  12002. }
  12003. if (ret == BAD_FUNC_ARG) {
  12004. ret = 0;
  12005. }
  12006. else if (ret == 0) {
  12007. ret = WOLFSSL_FATAL_ERROR;
  12008. }
  12009. }
  12010. wc_Sha3_256_Free(&sha3);
  12011. if (ret == 0) {
  12012. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12013. if (ret != 0) {
  12014. return TEST_FAIL;
  12015. }
  12016. /* Init stack variables. */
  12017. XMEMSET(hash, 0, sizeof(hash));
  12018. XMEMSET(hashRet, 0, sizeof(hashRet));
  12019. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12020. if (ret == 0) {
  12021. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  12022. }
  12023. if (ret == 0) {
  12024. ret = wc_Sha3_256_Final(&sha3, hash);
  12025. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12026. ret = WOLFSSL_FATAL_ERROR;
  12027. }
  12028. }
  12029. if (ret == 0) {
  12030. /* Test bad args. */
  12031. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  12032. if (ret == BAD_FUNC_ARG) {
  12033. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  12034. }
  12035. if (ret == BAD_FUNC_ARG) {
  12036. ret = 0;
  12037. }
  12038. else if (ret == 0) {
  12039. ret = WOLFSSL_FATAL_ERROR;
  12040. }
  12041. }
  12042. }
  12043. wc_Sha3_256_Free(&sha3);
  12044. res = TEST_RES_CHECK(ret == 0);
  12045. #endif
  12046. return res;
  12047. } /* END test_wc_Sha3_256_Final */
  12048. /*
  12049. * Testing wc_Sha3_384_Final()
  12050. */
  12051. static int test_wc_Sha3_384_Final(void)
  12052. {
  12053. int res = TEST_SKIPPED;
  12054. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12055. wc_Sha3 sha3;
  12056. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12057. "nopnopq";
  12058. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12059. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12060. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12061. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12062. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12063. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12064. int ret = 0;
  12065. /* Init stack variables. */
  12066. XMEMSET(hash, 0, sizeof(hash));
  12067. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12068. if (ret != 0) {
  12069. return TEST_FAIL;
  12070. }
  12071. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12072. if (ret == 0) {
  12073. ret = wc_Sha3_384_Final(&sha3, hash);
  12074. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12075. ret = WOLFSSL_FATAL_ERROR;
  12076. }
  12077. }
  12078. /* Test bad args. */
  12079. if (ret == 0) {
  12080. ret = wc_Sha3_384_Final(NULL, hash);
  12081. if (ret == 0) {
  12082. ret = wc_Sha3_384_Final(&sha3, NULL);
  12083. }
  12084. if (ret == BAD_FUNC_ARG) {
  12085. ret = 0;
  12086. }
  12087. else if (ret == 0) {
  12088. ret = WOLFSSL_FATAL_ERROR;
  12089. }
  12090. }
  12091. wc_Sha3_384_Free(&sha3);
  12092. if (ret == 0) {
  12093. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12094. if (ret != 0) {
  12095. return TEST_FAIL;
  12096. }
  12097. /* Init stack variables. */
  12098. XMEMSET(hash, 0, sizeof(hash));
  12099. XMEMSET(hashRet, 0, sizeof(hashRet));
  12100. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12101. if (ret == 0) {
  12102. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  12103. }
  12104. if (ret == 0) {
  12105. ret = wc_Sha3_384_Final(&sha3, hash);
  12106. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12107. ret = WOLFSSL_FATAL_ERROR;
  12108. }
  12109. }
  12110. if (ret == 0) {
  12111. /* Test bad args. */
  12112. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  12113. if (ret == BAD_FUNC_ARG) {
  12114. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  12115. }
  12116. if (ret == BAD_FUNC_ARG) {
  12117. ret = 0;
  12118. }
  12119. else if (ret == 0) {
  12120. ret = WOLFSSL_FATAL_ERROR;
  12121. }
  12122. }
  12123. }
  12124. wc_Sha3_384_Free(&sha3);
  12125. res = TEST_RES_CHECK(ret == 0);
  12126. #endif
  12127. return res;
  12128. } /* END test_wc_Sha3_384_Final */
  12129. /*
  12130. * Testing wc_Sha3_512_Final()
  12131. */
  12132. static int test_wc_Sha3_512_Final(void)
  12133. {
  12134. int res = TEST_SKIPPED;
  12135. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12136. !defined(WOLFSSL_NOSHA3_384)
  12137. wc_Sha3 sha3;
  12138. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12139. "nopnopq";
  12140. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12141. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12142. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12143. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12144. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12145. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12146. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12147. int ret = 0;
  12148. /* Init stack variables. */
  12149. XMEMSET(hash, 0, sizeof(hash));
  12150. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12151. if (ret != 0) {
  12152. return TEST_FAIL;
  12153. }
  12154. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12155. if (ret == 0) {
  12156. ret = wc_Sha3_512_Final(&sha3, hash);
  12157. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12158. ret = WOLFSSL_FATAL_ERROR;
  12159. }
  12160. }
  12161. /* Test bad args. */
  12162. if (ret == 0) {
  12163. ret = wc_Sha3_512_Final(NULL, hash);
  12164. if (ret == 0) {
  12165. ret = wc_Sha3_384_Final(&sha3, NULL);
  12166. }
  12167. if (ret == BAD_FUNC_ARG) {
  12168. ret = 0;
  12169. }
  12170. else if (ret == 0) {
  12171. ret = WOLFSSL_FATAL_ERROR;
  12172. }
  12173. }
  12174. wc_Sha3_512_Free(&sha3);
  12175. if (ret == 0) {
  12176. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12177. if (ret != 0) {
  12178. return TEST_FAIL;
  12179. }
  12180. /* Init stack variables. */
  12181. XMEMSET(hash, 0, sizeof(hash));
  12182. XMEMSET(hashRet, 0, sizeof(hashRet));
  12183. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12184. if (ret == 0) {
  12185. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  12186. }
  12187. if (ret == 0) {
  12188. ret = wc_Sha3_512_Final(&sha3, hash);
  12189. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12190. ret = WOLFSSL_FATAL_ERROR;
  12191. }
  12192. }
  12193. if (ret == 0) {
  12194. /* Test bad args. */
  12195. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  12196. if (ret == BAD_FUNC_ARG) {
  12197. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  12198. }
  12199. if (ret == BAD_FUNC_ARG) {
  12200. ret = 0;
  12201. }
  12202. else if (ret == 0) {
  12203. ret = WOLFSSL_FATAL_ERROR;
  12204. }
  12205. }
  12206. }
  12207. wc_Sha3_512_Free(&sha3);
  12208. res = TEST_RES_CHECK(ret == 0);
  12209. #endif
  12210. return res;
  12211. } /* END test_wc_Sha3_512_Final */
  12212. /*
  12213. * Testing wc_Sha3_224_Copy()
  12214. */
  12215. static int test_wc_Sha3_224_Copy(void)
  12216. {
  12217. int res = TEST_SKIPPED;
  12218. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12219. wc_Sha3 sha3, sha3Cpy;
  12220. const char* msg = TEST_STRING;
  12221. word32 msglen = (word32)TEST_STRING_SZ;
  12222. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12223. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12224. int ret = 0;
  12225. XMEMSET(hash, 0, sizeof(hash));
  12226. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12227. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12228. if (ret != 0) {
  12229. return TEST_FAIL;
  12230. }
  12231. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12232. if (ret != 0) {
  12233. wc_Sha3_224_Free(&sha3);
  12234. return TEST_FAIL;
  12235. }
  12236. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12237. if (ret == 0) {
  12238. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12239. if (ret == 0) {
  12240. ret = wc_Sha3_224_Final(&sha3, hash);
  12241. if (ret == 0) {
  12242. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12243. }
  12244. }
  12245. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12246. ret = WOLFSSL_FATAL_ERROR;
  12247. }
  12248. }
  12249. /* Test bad args. */
  12250. if (ret == 0) {
  12251. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12252. if (ret == BAD_FUNC_ARG) {
  12253. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12254. }
  12255. if (ret == BAD_FUNC_ARG) {
  12256. ret = 0;
  12257. }
  12258. else if (ret == 0) {
  12259. ret = WOLFSSL_FATAL_ERROR;
  12260. }
  12261. }
  12262. res = TEST_RES_CHECK(ret == 0);
  12263. #endif
  12264. return res;
  12265. } /* END test_wc_Sha3_224_Copy */
  12266. /*
  12267. * Testing wc_Sha3_256_Copy()
  12268. */
  12269. static int test_wc_Sha3_256_Copy(void)
  12270. {
  12271. int res = TEST_SKIPPED;
  12272. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12273. wc_Sha3 sha3, sha3Cpy;
  12274. const char* msg = TEST_STRING;
  12275. word32 msglen = (word32)TEST_STRING_SZ;
  12276. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12277. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12278. int ret = 0;
  12279. XMEMSET(hash, 0, sizeof(hash));
  12280. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12281. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12282. if (ret != 0) {
  12283. return TEST_FAIL;
  12284. }
  12285. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12286. if (ret != 0) {
  12287. wc_Sha3_256_Free(&sha3);
  12288. return TEST_FAIL;
  12289. }
  12290. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12291. if (ret == 0) {
  12292. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12293. if (ret == 0) {
  12294. ret = wc_Sha3_256_Final(&sha3, hash);
  12295. if (ret == 0) {
  12296. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12297. }
  12298. }
  12299. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12300. ret = WOLFSSL_FATAL_ERROR;
  12301. }
  12302. }
  12303. /* Test bad args. */
  12304. if (ret == 0) {
  12305. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12306. if (ret == BAD_FUNC_ARG) {
  12307. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12308. }
  12309. if (ret == BAD_FUNC_ARG) {
  12310. ret = 0;
  12311. }
  12312. else if (ret == 0) {
  12313. ret = WOLFSSL_FATAL_ERROR;
  12314. }
  12315. }
  12316. res = TEST_RES_CHECK(ret == 0);
  12317. #endif
  12318. return res;
  12319. } /* END test_wc_Sha3_256_Copy */
  12320. /*
  12321. * Testing wc_Sha3_384_Copy()
  12322. */
  12323. static int test_wc_Sha3_384_Copy(void)
  12324. {
  12325. int res = TEST_SKIPPED;
  12326. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12327. wc_Sha3 sha3, sha3Cpy;
  12328. const char* msg = TEST_STRING;
  12329. word32 msglen = (word32)TEST_STRING_SZ;
  12330. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12331. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12332. int ret = 0;
  12333. XMEMSET(hash, 0, sizeof(hash));
  12334. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12335. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12336. if (ret != 0) {
  12337. return TEST_FAIL;
  12338. }
  12339. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12340. if (ret != 0) {
  12341. wc_Sha3_384_Free(&sha3);
  12342. return TEST_FAIL;
  12343. }
  12344. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12345. if (ret == 0) {
  12346. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12347. if (ret == 0) {
  12348. ret = wc_Sha3_384_Final(&sha3, hash);
  12349. if (ret == 0) {
  12350. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12351. }
  12352. }
  12353. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12354. ret = WOLFSSL_FATAL_ERROR;
  12355. }
  12356. }
  12357. /* Test bad args. */
  12358. if (ret == 0) {
  12359. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12360. if (ret == BAD_FUNC_ARG) {
  12361. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12362. }
  12363. if (ret == BAD_FUNC_ARG) {
  12364. ret = 0;
  12365. }
  12366. else if (ret == 0) {
  12367. ret = WOLFSSL_FATAL_ERROR;
  12368. }
  12369. }
  12370. res = TEST_RES_CHECK(ret == 0);
  12371. #endif
  12372. return res;
  12373. } /* END test_wc_Sha3_384_Copy */
  12374. /*
  12375. * Testing wc_Sha3_512_Copy()
  12376. */
  12377. static int test_wc_Sha3_512_Copy(void)
  12378. {
  12379. int res = TEST_SKIPPED;
  12380. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12381. wc_Sha3 sha3, sha3Cpy;
  12382. const char* msg = TEST_STRING;
  12383. word32 msglen = (word32)TEST_STRING_SZ;
  12384. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12385. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12386. int ret = 0;
  12387. XMEMSET(hash, 0, sizeof(hash));
  12388. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12389. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12390. if (ret != 0) {
  12391. return TEST_FAIL;
  12392. }
  12393. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12394. if (ret != 0) {
  12395. wc_Sha3_512_Free(&sha3);
  12396. return TEST_FAIL;
  12397. }
  12398. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12399. if (ret == 0) {
  12400. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12401. if (ret == 0) {
  12402. ret = wc_Sha3_512_Final(&sha3, hash);
  12403. if (ret == 0) {
  12404. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12405. }
  12406. }
  12407. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12408. ret = WOLFSSL_FATAL_ERROR;
  12409. }
  12410. }
  12411. /* Test bad args. */
  12412. if (ret == 0) {
  12413. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12414. if (ret == BAD_FUNC_ARG) {
  12415. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12416. }
  12417. if (ret == BAD_FUNC_ARG) {
  12418. ret = 0;
  12419. }
  12420. else if (ret == 0) {
  12421. ret = WOLFSSL_FATAL_ERROR;
  12422. }
  12423. }
  12424. res = TEST_RES_CHECK(ret == 0);
  12425. #endif
  12426. return res;
  12427. } /* END test_wc_Sha3_512_Copy */
  12428. /*
  12429. * Unit test function for wc_Sha3_GetFlags()
  12430. */
  12431. static int test_wc_Sha3_GetFlags(void)
  12432. {
  12433. int res = TEST_SKIPPED;
  12434. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12435. wc_Sha3 sha3;
  12436. word32 flags = 0;
  12437. int ret = 0;
  12438. /* Initialize */
  12439. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12440. if (ret != 0) {
  12441. return TEST_FAIL;
  12442. }
  12443. if (ret == 0) {
  12444. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12445. }
  12446. if (ret == 0) {
  12447. if (flags & WC_HASH_FLAG_ISCOPY) {
  12448. ret = 0;
  12449. }
  12450. }
  12451. wc_Sha3_224_Free(&sha3);
  12452. res = TEST_RES_CHECK(ret == 0);
  12453. #endif
  12454. return res;
  12455. } /* END test_wc_Sha3_GetFlags */
  12456. static int test_wc_InitShake256(void)
  12457. {
  12458. int res = TEST_SKIPPED;
  12459. #ifdef WOLFSSL_SHAKE256
  12460. wc_Shake shake;
  12461. int ret = 0;
  12462. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12463. /* Test bad args. */
  12464. if (ret == 0) {
  12465. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  12466. if (ret == BAD_FUNC_ARG) {
  12467. ret = 0;
  12468. }
  12469. else if (ret == 0) {
  12470. ret = WOLFSSL_FATAL_ERROR;
  12471. }
  12472. }
  12473. wc_Shake256_Free(&shake);
  12474. res = TEST_RES_CHECK(ret == 0);
  12475. #endif
  12476. return res;
  12477. } /* END test_wc_InitSha3 */
  12478. static int testing_wc_Shake256_Update(void)
  12479. {
  12480. int res = TEST_SKIPPED;
  12481. #ifdef WOLFSSL_SHAKE256
  12482. wc_Shake shake;
  12483. byte msg[] = "Everybody's working for the weekend.";
  12484. byte msg2[] = "Everybody gets Friday off.";
  12485. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12486. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12487. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12488. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12489. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12490. word32 msglen = sizeof(msg) - 1;
  12491. word32 msg2len = sizeof(msg2);
  12492. word32 msgCmplen = sizeof(msgCmp);
  12493. int ret = 0;
  12494. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12495. if (ret != 0) {
  12496. return TEST_FAIL;
  12497. }
  12498. ret = wc_Shake256_Update(&shake, msg, msglen);
  12499. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  12500. ret = WOLFSSL_FATAL_ERROR;
  12501. }
  12502. if (ret == 0) {
  12503. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12504. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  12505. ret = WOLFSSL_FATAL_ERROR;
  12506. }
  12507. }
  12508. /* Pass bad args. */
  12509. if (ret == 0) {
  12510. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  12511. if (ret == BAD_FUNC_ARG) {
  12512. ret = wc_Shake256_Update(&shake, NULL, 5);
  12513. }
  12514. if (ret == BAD_FUNC_ARG) {
  12515. wc_Shake256_Free(&shake);
  12516. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  12517. return TEST_FAIL;
  12518. }
  12519. ret = wc_Shake256_Update(&shake, NULL, 0);
  12520. if (ret == 0) {
  12521. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12522. }
  12523. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  12524. ret = WOLFSSL_FATAL_ERROR;
  12525. }
  12526. }
  12527. }
  12528. wc_Shake256_Free(&shake);
  12529. res = TEST_RES_CHECK(ret == 0);
  12530. #endif /* WOLFSSL_SHAKE256 */
  12531. return res;
  12532. }
  12533. static int test_wc_Shake256_Final(void)
  12534. {
  12535. int res = TEST_SKIPPED;
  12536. #ifdef WOLFSSL_SHAKE256
  12537. wc_Shake shake;
  12538. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12539. "nopnopq";
  12540. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12541. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12542. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12543. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12544. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12545. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12546. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12547. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12548. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12549. byte hash[114];
  12550. int ret = 0;
  12551. /* Init stack variables. */
  12552. XMEMSET(hash, 0, sizeof(hash));
  12553. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12554. if (ret != 0) {
  12555. return TEST_FAIL;
  12556. }
  12557. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  12558. if (ret == 0) {
  12559. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  12560. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  12561. ret = WOLFSSL_FATAL_ERROR;
  12562. }
  12563. }
  12564. /* Test bad args. */
  12565. if (ret == 0) {
  12566. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  12567. if (ret == 0) {
  12568. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  12569. }
  12570. if (ret == BAD_FUNC_ARG) {
  12571. ret = 0;
  12572. }
  12573. else if (ret == 0) {
  12574. ret = WOLFSSL_FATAL_ERROR;
  12575. }
  12576. }
  12577. wc_Shake256_Free(&shake);
  12578. res = TEST_RES_CHECK(ret == 0);
  12579. #endif
  12580. return res;
  12581. }
  12582. /*
  12583. * Testing wc_Shake256_Copy()
  12584. */
  12585. static int test_wc_Shake256_Copy(void)
  12586. {
  12587. int res = TEST_SKIPPED;
  12588. #ifdef WOLFSSL_SHAKE256
  12589. wc_Shake shake, shakeCpy;
  12590. const char* msg = TEST_STRING;
  12591. word32 msglen = (word32)TEST_STRING_SZ;
  12592. byte hash[144];
  12593. byte hashCpy[144];
  12594. word32 hashLen = sizeof(hash);
  12595. word32 hashLenCpy = sizeof(hashCpy);
  12596. int ret;
  12597. XMEMSET(hash, 0, sizeof(hash));
  12598. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12599. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12600. if (ret != 0) {
  12601. return TEST_FAIL;
  12602. }
  12603. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  12604. if (ret != 0) {
  12605. wc_Shake256_Free(&shake);
  12606. return TEST_FAIL;
  12607. }
  12608. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  12609. if (ret == 0) {
  12610. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  12611. if (ret == 0) {
  12612. ret = wc_Shake256_Final(&shake, hash, hashLen);
  12613. if (ret == 0) {
  12614. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  12615. }
  12616. }
  12617. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12618. ret = WOLFSSL_FATAL_ERROR;
  12619. }
  12620. }
  12621. /* Test bad args. */
  12622. if (ret == 0) {
  12623. ret = wc_Shake256_Copy(NULL, &shake);
  12624. if (ret == BAD_FUNC_ARG) {
  12625. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  12626. }
  12627. if (ret == BAD_FUNC_ARG) {
  12628. ret = 0;
  12629. }
  12630. else if (ret == 0) {
  12631. ret = WOLFSSL_FATAL_ERROR;
  12632. }
  12633. }
  12634. wc_Shake256_Free(&shake);
  12635. res = TEST_RES_CHECK(ret == 0);
  12636. #endif
  12637. return res;
  12638. } /* END test_wc_Shake256_Copy */
  12639. /*
  12640. * Unit test function for wc_Shake256Hash()
  12641. */
  12642. static int test_wc_Shake256Hash(void)
  12643. {
  12644. int res = TEST_SKIPPED;
  12645. #ifdef WOLFSSL_SHAKE256
  12646. const byte data[] = { /* Hello World */
  12647. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12648. 0x72,0x6c,0x64
  12649. };
  12650. word32 len = sizeof(data);
  12651. byte hash[144];
  12652. word32 hashLen = sizeof(hash);
  12653. int ret;
  12654. ret = wc_Shake256Hash(data, len, hash, hashLen);
  12655. res = TEST_RES_CHECK(ret == 0);
  12656. #endif
  12657. return res;
  12658. } /* END test_wc_Shake256Hash */
  12659. /*
  12660. * Test function for wc_HmacSetKey
  12661. */
  12662. static int test_wc_Md5HmacSetKey(void)
  12663. {
  12664. int res = TEST_SKIPPED;
  12665. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12666. Hmac hmac;
  12667. int ret, times, itr;
  12668. int flag = 0;
  12669. const char* keys[]=
  12670. {
  12671. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12672. #ifndef HAVE_FIPS
  12673. "Jefe", /* smaller than minimum FIPS key size */
  12674. #endif
  12675. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12676. };
  12677. times = sizeof(keys) / sizeof(char*);
  12678. flag = 0;
  12679. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12680. if (ret != 0)
  12681. return TEST_FAIL;
  12682. for (itr = 0; itr < times; itr++) {
  12683. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12684. (word32)XSTRLEN(keys[itr]));
  12685. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12686. wc_HmacFree(&hmac);
  12687. if (ret == BAD_FUNC_ARG) {
  12688. return TEST_SUCCESS;
  12689. }
  12690. else {
  12691. return TEST_FAIL;
  12692. }
  12693. #else
  12694. if (ret != 0) {
  12695. flag = ret;
  12696. }
  12697. #endif
  12698. }
  12699. /* Bad args. */
  12700. if (!flag) {
  12701. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12702. (word32)XSTRLEN(keys[0]));
  12703. if (ret != BAD_FUNC_ARG) {
  12704. flag = WOLFSSL_FATAL_ERROR;
  12705. }
  12706. }
  12707. if (!flag) {
  12708. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  12709. if (ret != BAD_FUNC_ARG) {
  12710. flag = WOLFSSL_FATAL_ERROR;
  12711. }
  12712. }
  12713. if (!flag) {
  12714. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12715. (word32)XSTRLEN(keys[0]));
  12716. if (ret != BAD_FUNC_ARG) {
  12717. flag = WOLFSSL_FATAL_ERROR;
  12718. }
  12719. }
  12720. if (!flag) {
  12721. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12722. #ifdef HAVE_FIPS
  12723. if (ret != HMAC_MIN_KEYLEN_E) {
  12724. flag = WOLFSSL_FATAL_ERROR;
  12725. }
  12726. #else
  12727. if (ret != 0) {
  12728. flag = WOLFSSL_FATAL_ERROR;
  12729. }
  12730. #endif
  12731. }
  12732. wc_HmacFree(&hmac);
  12733. res = TEST_RES_CHECK(flag == 0);
  12734. #endif
  12735. return res;
  12736. } /* END test_wc_Md5HmacSetKey */
  12737. /*
  12738. * testing wc_HmacSetKey() on wc_Sha hash.
  12739. */
  12740. static int test_wc_ShaHmacSetKey(void)
  12741. {
  12742. int res = TEST_SKIPPED;
  12743. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12744. Hmac hmac;
  12745. int ret, times, itr;
  12746. int flag = 0;
  12747. const char* keys[]=
  12748. {
  12749. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12750. "\x0b\x0b\x0b",
  12751. #ifndef HAVE_FIPS
  12752. "Jefe", /* smaller than minimum FIPS key size */
  12753. #endif
  12754. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12755. "\xAA\xAA\xAA"
  12756. };
  12757. times = sizeof(keys) / sizeof(char*);
  12758. flag = 0;
  12759. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12760. if (ret != 0)
  12761. return ret;
  12762. for (itr = 0; itr < times; itr++) {
  12763. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12764. (word32)XSTRLEN(keys[itr]));
  12765. if (ret != 0) {
  12766. flag = ret;
  12767. }
  12768. }
  12769. /* Bad args. */
  12770. if (!flag) {
  12771. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12772. (word32)XSTRLEN(keys[0]));
  12773. if (ret != BAD_FUNC_ARG) {
  12774. flag = WOLFSSL_FATAL_ERROR;
  12775. }
  12776. }
  12777. if (!flag) {
  12778. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12779. if (ret != BAD_FUNC_ARG) {
  12780. flag = WOLFSSL_FATAL_ERROR;
  12781. }
  12782. }
  12783. if (!flag) {
  12784. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12785. (word32)XSTRLEN(keys[0]));
  12786. if (ret != BAD_FUNC_ARG) {
  12787. flag = WOLFSSL_FATAL_ERROR;
  12788. }
  12789. }
  12790. if (!flag) {
  12791. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12792. #ifdef HAVE_FIPS
  12793. if (ret != HMAC_MIN_KEYLEN_E) {
  12794. flag = WOLFSSL_FATAL_ERROR;
  12795. }
  12796. #else
  12797. if (ret != 0) {
  12798. flag = WOLFSSL_FATAL_ERROR;
  12799. }
  12800. #endif
  12801. }
  12802. wc_HmacFree(&hmac);
  12803. res = TEST_RES_CHECK(flag == 0);
  12804. #endif
  12805. return res;
  12806. } /* END test_wc_ShaHmacSetKey() */
  12807. /*
  12808. * testing wc_HmacSetKey() on Sha224 hash.
  12809. */
  12810. static int test_wc_Sha224HmacSetKey(void)
  12811. {
  12812. int res = TEST_SKIPPED;
  12813. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12814. Hmac hmac;
  12815. int ret, times, itr;
  12816. int flag = 0;
  12817. const char* keys[]=
  12818. {
  12819. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12820. "\x0b\x0b\x0b",
  12821. #ifndef HAVE_FIPS
  12822. "Jefe", /* smaller than minimum FIPS key size */
  12823. #endif
  12824. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12825. "\xAA\xAA\xAA"
  12826. };
  12827. times = sizeof(keys) / sizeof(char*);
  12828. flag = 0;
  12829. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12830. if (ret != 0)
  12831. return ret;
  12832. for (itr = 0; itr < times; itr++) {
  12833. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12834. (word32)XSTRLEN(keys[itr]));
  12835. if (ret != 0) {
  12836. flag = ret;
  12837. }
  12838. }
  12839. /* Bad args. */
  12840. if (!flag) {
  12841. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12842. (word32)XSTRLEN(keys[0]));
  12843. if (ret != BAD_FUNC_ARG) {
  12844. flag = WOLFSSL_FATAL_ERROR;
  12845. }
  12846. }
  12847. if (!flag) {
  12848. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12849. if (ret != BAD_FUNC_ARG) {
  12850. flag = WOLFSSL_FATAL_ERROR;
  12851. }
  12852. }
  12853. if (!flag) {
  12854. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12855. (word32)XSTRLEN(keys[0]));
  12856. if (ret != BAD_FUNC_ARG) {
  12857. flag = WOLFSSL_FATAL_ERROR;
  12858. }
  12859. }
  12860. if (!flag) {
  12861. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12862. #ifdef HAVE_FIPS
  12863. if (ret != HMAC_MIN_KEYLEN_E) {
  12864. flag = WOLFSSL_FATAL_ERROR;
  12865. }
  12866. #else
  12867. if (ret != 0) {
  12868. flag = WOLFSSL_FATAL_ERROR;
  12869. }
  12870. #endif
  12871. }
  12872. wc_HmacFree(&hmac);
  12873. res = TEST_RES_CHECK(flag == 0);
  12874. #endif
  12875. return res;
  12876. } /* END test_wc_Sha224HmacSetKey() */
  12877. /*
  12878. * testing wc_HmacSetKey() on Sha256 hash
  12879. */
  12880. static int test_wc_Sha256HmacSetKey(void)
  12881. {
  12882. int res = TEST_SKIPPED;
  12883. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12884. Hmac hmac;
  12885. int ret, times, itr;
  12886. int flag = 0;
  12887. const char* keys[]=
  12888. {
  12889. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12890. "\x0b\x0b\x0b",
  12891. #ifndef HAVE_FIPS
  12892. "Jefe", /* smaller than minimum FIPS key size */
  12893. #endif
  12894. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12895. "\xAA\xAA\xAA"
  12896. };
  12897. times = sizeof(keys) / sizeof(char*);
  12898. flag = 0;
  12899. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12900. if (ret != 0)
  12901. return ret;
  12902. for (itr = 0; itr < times; itr++) {
  12903. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12904. (word32)XSTRLEN(keys[itr]));
  12905. if (ret != 0) {
  12906. flag = ret;
  12907. }
  12908. }
  12909. /* Bad args. */
  12910. if (!flag) {
  12911. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12912. (word32)XSTRLEN(keys[0]));
  12913. if (ret != BAD_FUNC_ARG) {
  12914. flag = WOLFSSL_FATAL_ERROR;
  12915. }
  12916. }
  12917. if (!flag) {
  12918. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12919. if (ret != BAD_FUNC_ARG) {
  12920. flag = WOLFSSL_FATAL_ERROR;
  12921. }
  12922. }
  12923. if (!flag) {
  12924. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12925. (word32)XSTRLEN(keys[0]));
  12926. if (ret != BAD_FUNC_ARG) {
  12927. flag = WOLFSSL_FATAL_ERROR;
  12928. }
  12929. }
  12930. if (!flag) {
  12931. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12932. #ifdef HAVE_FIPS
  12933. if (ret != HMAC_MIN_KEYLEN_E) {
  12934. flag = WOLFSSL_FATAL_ERROR;
  12935. }
  12936. #else
  12937. if (ret != 0) {
  12938. flag = WOLFSSL_FATAL_ERROR;
  12939. }
  12940. #endif
  12941. }
  12942. wc_HmacFree(&hmac);
  12943. res = TEST_RES_CHECK(flag == 0);
  12944. #endif
  12945. return res;
  12946. } /* END test_wc_Sha256HmacSetKey() */
  12947. /*
  12948. * testing wc_HmacSetKey on Sha384 hash.
  12949. */
  12950. static int test_wc_Sha384HmacSetKey(void)
  12951. {
  12952. int res = TEST_SKIPPED;
  12953. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12954. Hmac hmac;
  12955. int ret, times, itr;
  12956. int flag = 0;
  12957. const char* keys[]=
  12958. {
  12959. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12960. "\x0b\x0b\x0b",
  12961. #ifndef HAVE_FIPS
  12962. "Jefe", /* smaller than minimum FIPS key size */
  12963. #endif
  12964. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12965. "\xAA\xAA\xAA"
  12966. };
  12967. times = sizeof(keys) / sizeof(char*);
  12968. flag = 0;
  12969. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12970. if (ret != 0)
  12971. return ret;
  12972. for (itr = 0; itr < times; itr++) {
  12973. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12974. (word32)XSTRLEN(keys[itr]));
  12975. if (ret != 0) {
  12976. flag = ret;
  12977. }
  12978. }
  12979. /* Bad args. */
  12980. if (!flag) {
  12981. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12982. (word32)XSTRLEN(keys[0]));
  12983. if (ret != BAD_FUNC_ARG) {
  12984. flag = WOLFSSL_FATAL_ERROR;
  12985. }
  12986. }
  12987. if (!flag) {
  12988. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12989. if (ret != BAD_FUNC_ARG) {
  12990. flag = WOLFSSL_FATAL_ERROR;
  12991. }
  12992. }
  12993. if (!flag) {
  12994. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12995. (word32)XSTRLEN(keys[0]));
  12996. if (ret != BAD_FUNC_ARG) {
  12997. flag = WOLFSSL_FATAL_ERROR;
  12998. }
  12999. }
  13000. if (!flag) {
  13001. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13002. #ifdef HAVE_FIPS
  13003. if (ret != HMAC_MIN_KEYLEN_E) {
  13004. flag = WOLFSSL_FATAL_ERROR;
  13005. }
  13006. #else
  13007. if (ret != 0) {
  13008. flag = WOLFSSL_FATAL_ERROR;
  13009. }
  13010. #endif
  13011. }
  13012. wc_HmacFree(&hmac);
  13013. res = TEST_RES_CHECK(flag == 0);
  13014. #endif
  13015. return res;
  13016. } /* END test_wc_Sha384HmacSetKey() */
  13017. /*
  13018. * testing wc_HmacUpdate on wc_Md5 hash.
  13019. */
  13020. static int test_wc_Md5HmacUpdate(void)
  13021. {
  13022. int res = TEST_SKIPPED;
  13023. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13024. Hmac hmac;
  13025. testVector a, b;
  13026. int ret;
  13027. int flag = 0;
  13028. #ifdef HAVE_FIPS
  13029. const char* keys =
  13030. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13031. #else
  13032. const char* keys = "Jefe";
  13033. #endif
  13034. a.input = "what do ya want for nothing?";
  13035. a.inLen = XSTRLEN(a.input);
  13036. b.input = "Hi There";
  13037. b.inLen = XSTRLEN(b.input);
  13038. flag = 0;
  13039. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13040. if (ret != 0)
  13041. return ret;
  13042. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  13043. if (ret != 0) {
  13044. flag = ret;
  13045. }
  13046. if (!flag) {
  13047. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13048. if (ret != 0) {
  13049. flag = ret;
  13050. }
  13051. }
  13052. /* Update Hmac. */
  13053. if (!flag) {
  13054. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13055. if (ret != 0) {
  13056. flag = ret;
  13057. }
  13058. }
  13059. /* Test bad args. */
  13060. if (!flag) {
  13061. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13062. if (ret != BAD_FUNC_ARG) {
  13063. flag = WOLFSSL_FATAL_ERROR;
  13064. }
  13065. }
  13066. if (!flag) {
  13067. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13068. if (ret != BAD_FUNC_ARG) {
  13069. flag = WOLFSSL_FATAL_ERROR;
  13070. }
  13071. }
  13072. if (!flag) {
  13073. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13074. if (ret != 0) {
  13075. flag = ret;
  13076. }
  13077. }
  13078. wc_HmacFree(&hmac);
  13079. res = TEST_RES_CHECK(flag == 0);
  13080. #endif
  13081. return res;
  13082. } /* END test_wc_Md5HmacUpdate */
  13083. /*
  13084. * testing wc_HmacUpdate on SHA hash.
  13085. */
  13086. static int test_wc_ShaHmacUpdate(void)
  13087. {
  13088. int res = TEST_SKIPPED;
  13089. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13090. Hmac hmac;
  13091. testVector a, b;
  13092. int ret;
  13093. int flag = 0;
  13094. #ifdef HAVE_FIPS
  13095. const char* keys =
  13096. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13097. #else
  13098. const char* keys = "Jefe";
  13099. #endif
  13100. a.input = "what do ya want for nothing?";
  13101. a.inLen = XSTRLEN(a.input);
  13102. b.input = "Hi There";
  13103. b.inLen = XSTRLEN(b.input);
  13104. flag = 0;
  13105. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13106. if (ret != 0)
  13107. return ret;
  13108. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  13109. if (ret != 0) {
  13110. flag = ret;
  13111. }
  13112. if (!flag) {
  13113. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13114. if (ret != 0) {
  13115. flag = ret;
  13116. }
  13117. }
  13118. /* Update Hmac. */
  13119. if (!flag) {
  13120. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13121. if (ret != 0) {
  13122. flag = ret;
  13123. }
  13124. }
  13125. /* Test bad args. */
  13126. if (!flag) {
  13127. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13128. if (ret != BAD_FUNC_ARG) {
  13129. flag = WOLFSSL_FATAL_ERROR;
  13130. }
  13131. }
  13132. if (!flag) {
  13133. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13134. if (ret != BAD_FUNC_ARG) {
  13135. flag = WOLFSSL_FATAL_ERROR;
  13136. }
  13137. }
  13138. if (!flag) {
  13139. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13140. if (ret != 0) {
  13141. flag = ret;
  13142. }
  13143. }
  13144. wc_HmacFree(&hmac);
  13145. res = TEST_RES_CHECK(flag == 0);
  13146. #endif
  13147. return res;
  13148. } /* END test_wc_ShaHmacUpdate */
  13149. /*
  13150. * testing wc_HmacUpdate on SHA224 hash.
  13151. */
  13152. static int test_wc_Sha224HmacUpdate(void)
  13153. {
  13154. int res = TEST_SKIPPED;
  13155. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13156. Hmac hmac;
  13157. testVector a, b;
  13158. int ret;
  13159. int flag = 0;
  13160. #ifdef HAVE_FIPS
  13161. const char* keys =
  13162. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13163. #else
  13164. const char* keys = "Jefe";
  13165. #endif
  13166. a.input = "what do ya want for nothing?";
  13167. a.inLen = XSTRLEN(a.input);
  13168. b.input = "Hi There";
  13169. b.inLen = XSTRLEN(b.input);
  13170. flag = 0;
  13171. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13172. if (ret != 0)
  13173. return ret;
  13174. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  13175. if (ret != 0) {
  13176. flag = ret;
  13177. }
  13178. if (!flag) {
  13179. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13180. if (ret != 0) {
  13181. flag = ret;
  13182. }
  13183. }
  13184. /* Update Hmac. */
  13185. if (!flag) {
  13186. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13187. if (ret != 0) {
  13188. flag = ret;
  13189. }
  13190. }
  13191. /* Test bad args. */
  13192. if (!flag) {
  13193. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13194. if (ret != BAD_FUNC_ARG) {
  13195. flag = WOLFSSL_FATAL_ERROR;
  13196. }
  13197. }
  13198. if (!flag) {
  13199. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13200. if (ret != BAD_FUNC_ARG) {
  13201. flag = WOLFSSL_FATAL_ERROR;
  13202. }
  13203. }
  13204. if (!flag) {
  13205. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13206. if (ret != 0) {
  13207. flag = ret;
  13208. }
  13209. }
  13210. wc_HmacFree(&hmac);
  13211. res = TEST_RES_CHECK(flag == 0);
  13212. #endif
  13213. return res;
  13214. } /* END test_wc_Sha224HmacUpdate */
  13215. /*
  13216. * testing wc_HmacUpdate on SHA256 hash.
  13217. */
  13218. static int test_wc_Sha256HmacUpdate(void)
  13219. {
  13220. int res = TEST_SKIPPED;
  13221. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13222. Hmac hmac;
  13223. testVector a, b;
  13224. int ret;
  13225. int flag = 0;
  13226. #ifdef HAVE_FIPS
  13227. const char* keys =
  13228. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13229. #else
  13230. const char* keys = "Jefe";
  13231. #endif
  13232. a.input = "what do ya want for nothing?";
  13233. a.inLen = XSTRLEN(a.input);
  13234. b.input = "Hi There";
  13235. b.inLen = XSTRLEN(b.input);
  13236. flag = 0;
  13237. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13238. if (ret != 0)
  13239. return ret;
  13240. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13241. if (ret != 0) {
  13242. flag = ret;
  13243. }
  13244. if (!flag) {
  13245. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13246. if (ret != 0) {
  13247. flag = ret;
  13248. }
  13249. }
  13250. /* Update Hmac. */
  13251. if (!flag) {
  13252. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13253. if (ret != 0) {
  13254. flag = ret;
  13255. }
  13256. }
  13257. /* Test bad args. */
  13258. if (!flag) {
  13259. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13260. if (ret != BAD_FUNC_ARG) {
  13261. flag = WOLFSSL_FATAL_ERROR;
  13262. }
  13263. }
  13264. if (!flag) {
  13265. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13266. if (ret != BAD_FUNC_ARG) {
  13267. flag = WOLFSSL_FATAL_ERROR;
  13268. }
  13269. }
  13270. if (!flag) {
  13271. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13272. if (ret != 0) {
  13273. flag = ret;
  13274. }
  13275. }
  13276. wc_HmacFree(&hmac);
  13277. res = TEST_RES_CHECK(flag == 0);
  13278. #endif
  13279. return res;
  13280. } /* END test_wc_Sha256HmacUpdate */
  13281. /*
  13282. * testing wc_HmacUpdate on SHA384 hash.
  13283. */
  13284. static int test_wc_Sha384HmacUpdate(void)
  13285. {
  13286. int res = TEST_SKIPPED;
  13287. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13288. Hmac hmac;
  13289. testVector a, b;
  13290. int ret;
  13291. int flag = 0;
  13292. #ifdef HAVE_FIPS
  13293. const char* keys =
  13294. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13295. #else
  13296. const char* keys = "Jefe";
  13297. #endif
  13298. a.input = "what do ya want for nothing?";
  13299. a.inLen = XSTRLEN(a.input);
  13300. b.input = "Hi There";
  13301. b.inLen = XSTRLEN(b.input);
  13302. flag = 0;
  13303. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13304. if (ret != 0)
  13305. return ret;
  13306. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13307. if (ret != 0) {
  13308. flag = ret;
  13309. }
  13310. if (!flag) {
  13311. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13312. if (ret != 0) {
  13313. flag = ret;
  13314. }
  13315. }
  13316. /* Update Hmac. */
  13317. if (!flag) {
  13318. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13319. if (ret != 0) {
  13320. flag = ret;
  13321. }
  13322. }
  13323. /* Test bad args. */
  13324. if (!flag) {
  13325. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13326. if (ret != BAD_FUNC_ARG) {
  13327. flag = WOLFSSL_FATAL_ERROR;
  13328. }
  13329. }
  13330. if (!flag) {
  13331. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13332. if (ret != BAD_FUNC_ARG) {
  13333. flag = WOLFSSL_FATAL_ERROR;
  13334. }
  13335. }
  13336. if (!flag) {
  13337. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13338. if (ret != 0) {
  13339. flag = ret;
  13340. }
  13341. }
  13342. wc_HmacFree(&hmac);
  13343. res = TEST_RES_CHECK(flag == 0);
  13344. #endif
  13345. return res;
  13346. } /* END test_wc_Sha384HmacUpdate */
  13347. /*
  13348. * Testing wc_HmacFinal() with MD5
  13349. */
  13350. static int test_wc_Md5HmacFinal(void)
  13351. {
  13352. int res = TEST_SKIPPED;
  13353. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13354. Hmac hmac;
  13355. byte hash[WC_MD5_DIGEST_SIZE];
  13356. testVector a;
  13357. int ret;
  13358. const char* key;
  13359. int flag = 0;
  13360. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13361. a.input = "Hi There";
  13362. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13363. "\x9d";
  13364. a.inLen = XSTRLEN(a.input);
  13365. a.outLen = XSTRLEN(a.output);
  13366. flag = 0;
  13367. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13368. if (ret != 0)
  13369. return ret;
  13370. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13371. if (ret != 0) {
  13372. flag = ret;
  13373. }
  13374. if (!flag) {
  13375. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13376. if (ret != 0) {
  13377. flag = ret;
  13378. }
  13379. }
  13380. if (!flag) {
  13381. ret = wc_HmacFinal(&hmac, hash);
  13382. if (ret != 0) {
  13383. flag = ret;
  13384. }
  13385. }
  13386. if (!flag) {
  13387. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13388. flag = WOLFSSL_FATAL_ERROR;
  13389. }
  13390. }
  13391. /* Try bad parameters. */
  13392. if (!flag) {
  13393. ret = wc_HmacFinal(NULL, hash);
  13394. if (ret != BAD_FUNC_ARG) {
  13395. flag = WOLFSSL_FATAL_ERROR;
  13396. }
  13397. }
  13398. #ifndef HAVE_FIPS
  13399. if (!flag) {
  13400. ret = wc_HmacFinal(&hmac, NULL);
  13401. if (ret != BAD_FUNC_ARG) {
  13402. flag = WOLFSSL_FATAL_ERROR;
  13403. }
  13404. }
  13405. #endif
  13406. wc_HmacFree(&hmac);
  13407. res = TEST_RES_CHECK(flag == 0);
  13408. #endif
  13409. return res;
  13410. } /* END test_wc_Md5HmacFinal */
  13411. /*
  13412. * Testing wc_HmacFinal() with SHA
  13413. */
  13414. static int test_wc_ShaHmacFinal(void)
  13415. {
  13416. int res = TEST_SKIPPED;
  13417. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13418. Hmac hmac;
  13419. byte hash[WC_SHA_DIGEST_SIZE];
  13420. testVector a;
  13421. int ret;
  13422. int flag = 0;
  13423. const char* key;
  13424. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13425. "\x0b\x0b\x0b";
  13426. a.input = "Hi There";
  13427. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13428. "\x8e\xf1\x46\xbe\x00";
  13429. a.inLen = XSTRLEN(a.input);
  13430. a.outLen = XSTRLEN(a.output);
  13431. flag = 0;
  13432. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13433. if (ret != 0)
  13434. return ret;
  13435. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13436. if (ret != 0) {
  13437. flag = ret;
  13438. }
  13439. if (!flag) {
  13440. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13441. if (ret != 0) {
  13442. flag = ret;
  13443. }
  13444. }
  13445. if (!flag) {
  13446. ret = wc_HmacFinal(&hmac, hash);
  13447. if (ret != 0) {
  13448. flag = ret;
  13449. }
  13450. }
  13451. if (!flag) {
  13452. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  13453. flag = WOLFSSL_FATAL_ERROR;
  13454. }
  13455. }
  13456. /* Try bad parameters. */
  13457. if (!flag) {
  13458. ret = wc_HmacFinal(NULL, hash);
  13459. if (ret != BAD_FUNC_ARG) {
  13460. flag = WOLFSSL_FATAL_ERROR;
  13461. }
  13462. }
  13463. #ifndef HAVE_FIPS
  13464. if (!flag) {
  13465. ret = wc_HmacFinal(&hmac, NULL);
  13466. if (ret != BAD_FUNC_ARG) {
  13467. flag = WOLFSSL_FATAL_ERROR;
  13468. }
  13469. }
  13470. #endif
  13471. wc_HmacFree(&hmac);
  13472. res = TEST_RES_CHECK(flag == 0);
  13473. #endif
  13474. return res;
  13475. } /* END test_wc_ShaHmacFinal */
  13476. /*
  13477. * Testing wc_HmacFinal() with SHA224
  13478. */
  13479. static int test_wc_Sha224HmacFinal(void)
  13480. {
  13481. int res = TEST_SKIPPED;
  13482. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13483. Hmac hmac;
  13484. byte hash[WC_SHA224_DIGEST_SIZE];
  13485. testVector a;
  13486. int ret;
  13487. int flag = 0;
  13488. const char* key;
  13489. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13490. "\x0b\x0b\x0b";
  13491. a.input = "Hi There";
  13492. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13493. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13494. a.inLen = XSTRLEN(a.input);
  13495. a.outLen = XSTRLEN(a.output);
  13496. flag = 0;
  13497. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13498. if (ret != 0)
  13499. return ret;
  13500. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  13501. if (ret != 0) {
  13502. flag = ret;
  13503. }
  13504. if (!flag) {
  13505. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13506. if (ret != 0) {
  13507. flag = ret;
  13508. }
  13509. }
  13510. if (!flag) {
  13511. ret = wc_HmacFinal(&hmac, hash);
  13512. if (ret != 0) {
  13513. flag = ret;
  13514. }
  13515. }
  13516. if (!flag) {
  13517. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  13518. flag = WOLFSSL_FATAL_ERROR;
  13519. }
  13520. }
  13521. /* Try bad parameters. */
  13522. if (!flag) {
  13523. ret = wc_HmacFinal(NULL, hash);
  13524. if (ret != BAD_FUNC_ARG) {
  13525. flag = WOLFSSL_FATAL_ERROR;
  13526. }
  13527. }
  13528. #ifndef HAVE_FIPS
  13529. if (!flag) {
  13530. ret = wc_HmacFinal(&hmac, NULL);
  13531. if (ret != BAD_FUNC_ARG) {
  13532. flag = WOLFSSL_FATAL_ERROR;
  13533. }
  13534. }
  13535. #endif
  13536. wc_HmacFree(&hmac);
  13537. res = TEST_RES_CHECK(flag == 0);
  13538. #endif
  13539. return res;
  13540. } /* END test_wc_Sha224HmacFinal */
  13541. /*
  13542. * Testing wc_HmacFinal() with SHA256
  13543. */
  13544. static int test_wc_Sha256HmacFinal(void)
  13545. {
  13546. int res = TEST_SKIPPED;
  13547. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13548. Hmac hmac;
  13549. byte hash[WC_SHA256_DIGEST_SIZE];
  13550. testVector a;
  13551. int ret;
  13552. int flag = 0;
  13553. const char* key;
  13554. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13555. "\x0b\x0b\x0b";
  13556. a.input = "Hi There";
  13557. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13558. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13559. "\xcf\xf7";
  13560. a.inLen = XSTRLEN(a.input);
  13561. a.outLen = XSTRLEN(a.output);
  13562. flag = 0;
  13563. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13564. if (ret != 0)
  13565. return TEST_FAIL;
  13566. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  13567. if (ret != 0) {
  13568. flag = ret;
  13569. }
  13570. if (!flag) {
  13571. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13572. if (ret != 0) {
  13573. flag = ret;
  13574. }
  13575. }
  13576. if (!flag) {
  13577. ret = wc_HmacFinal(&hmac, hash);
  13578. if (ret != 0) {
  13579. flag = ret;
  13580. }
  13581. }
  13582. if (!flag) {
  13583. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  13584. flag = WOLFSSL_FATAL_ERROR;
  13585. }
  13586. }
  13587. /* Try bad parameters. */
  13588. if (!flag) {
  13589. ret = wc_HmacFinal(NULL, hash);
  13590. if (ret != BAD_FUNC_ARG) {
  13591. flag = WOLFSSL_FATAL_ERROR;
  13592. }
  13593. }
  13594. #ifndef HAVE_FIPS
  13595. if (!flag) {
  13596. ret = wc_HmacFinal(&hmac, NULL);
  13597. if (ret != BAD_FUNC_ARG) {
  13598. flag = WOLFSSL_FATAL_ERROR;
  13599. }
  13600. }
  13601. #endif
  13602. wc_HmacFree(&hmac);
  13603. res = TEST_RES_CHECK(flag == 0);
  13604. #endif
  13605. return res;
  13606. } /* END test_wc_Sha256HmacFinal */
  13607. /*
  13608. * Testing wc_HmacFinal() with SHA384
  13609. */
  13610. static int test_wc_Sha384HmacFinal(void)
  13611. {
  13612. int res = TEST_SKIPPED;
  13613. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13614. Hmac hmac;
  13615. byte hash[WC_SHA384_DIGEST_SIZE];
  13616. testVector a;
  13617. int ret;
  13618. int flag = 0;
  13619. const char* key;
  13620. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13621. "\x0b\x0b\x0b";
  13622. a.input = "Hi There";
  13623. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13624. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13625. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13626. "\xfa\x9c\xb6";
  13627. a.inLen = XSTRLEN(a.input);
  13628. a.outLen = XSTRLEN(a.output);
  13629. flag = 0;
  13630. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13631. if (ret != 0)
  13632. return ret;
  13633. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  13634. if (ret != 0) {
  13635. flag = ret;
  13636. }
  13637. if (!flag) {
  13638. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13639. if (ret != 0) {
  13640. flag = ret;
  13641. }
  13642. }
  13643. if (!flag) {
  13644. ret = wc_HmacFinal(&hmac, hash);
  13645. if (ret != 0) {
  13646. flag = ret;
  13647. }
  13648. }
  13649. if (!flag) {
  13650. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  13651. flag = WOLFSSL_FATAL_ERROR;
  13652. }
  13653. }
  13654. /* Try bad parameters. */
  13655. if (!flag) {
  13656. ret = wc_HmacFinal(NULL, hash);
  13657. if (ret != BAD_FUNC_ARG) {
  13658. flag = WOLFSSL_FATAL_ERROR;
  13659. }
  13660. }
  13661. #ifndef HAVE_FIPS
  13662. if (!flag) {
  13663. ret = wc_HmacFinal(&hmac, NULL);
  13664. if (ret != BAD_FUNC_ARG) {
  13665. flag = WOLFSSL_FATAL_ERROR;
  13666. }
  13667. }
  13668. #endif
  13669. wc_HmacFree(&hmac);
  13670. res = TEST_RES_CHECK(flag == 0);
  13671. #endif
  13672. return res;
  13673. } /* END test_wc_Sha384HmacFinal */
  13674. /*
  13675. * Testing wc_InitCmac()
  13676. */
  13677. static int test_wc_InitCmac(void)
  13678. {
  13679. int res = TEST_SKIPPED;
  13680. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13681. Cmac cmac1, cmac2, cmac3;
  13682. /* AES 128 key. */
  13683. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13684. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13685. /* AES 192 key. */
  13686. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13687. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13688. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13689. /* AES 256 key. */
  13690. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13691. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13692. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13693. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13694. word32 key1Sz = (word32)sizeof(key1) - 1;
  13695. word32 key2Sz = (word32)sizeof(key2) - 1;
  13696. word32 key3Sz = (word32)sizeof(key3) - 1;
  13697. int type = WC_CMAC_AES;
  13698. int ret = 0;
  13699. #ifdef WOLFSSL_AES_128
  13700. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  13701. #endif
  13702. #ifdef WOLFSSL_AES_192
  13703. if (ret == 0) {
  13704. wc_AesFree(&cmac1.aes);
  13705. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  13706. }
  13707. #endif
  13708. #ifdef WOLFSSL_AES_256
  13709. if (ret == 0) {
  13710. wc_AesFree(&cmac2.aes);
  13711. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  13712. }
  13713. #endif
  13714. /* Test bad args. */
  13715. if (ret == 0) {
  13716. wc_AesFree(&cmac3.aes);
  13717. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  13718. if (ret == BAD_FUNC_ARG) {
  13719. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  13720. }
  13721. if (ret == BAD_FUNC_ARG) {
  13722. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  13723. }
  13724. if (ret == BAD_FUNC_ARG) {
  13725. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  13726. }
  13727. if (ret == BAD_FUNC_ARG) {
  13728. ret = 0;
  13729. }
  13730. else {
  13731. ret = WOLFSSL_FATAL_ERROR;
  13732. }
  13733. }
  13734. (void)key1;
  13735. (void)key1Sz;
  13736. (void)key2;
  13737. (void)key2Sz;
  13738. (void)cmac1;
  13739. (void)cmac2;
  13740. res = TEST_RES_CHECK(ret == 0);
  13741. #endif
  13742. return res;
  13743. } /* END test_wc_InitCmac */
  13744. /*
  13745. * Testing wc_CmacUpdate()
  13746. */
  13747. static int test_wc_CmacUpdate(void)
  13748. {
  13749. int res = TEST_SKIPPED;
  13750. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13751. Cmac cmac;
  13752. byte key[] =
  13753. {
  13754. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13755. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13756. };
  13757. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13758. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13759. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13760. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13761. "\xf4";
  13762. word32 inSz = (word32)sizeof(in) - 1;
  13763. word32 keySz = (word32)sizeof(key);
  13764. int type = WC_CMAC_AES;
  13765. int ret = 0;
  13766. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13767. if (ret != 0) {
  13768. return ret;
  13769. }
  13770. ret = wc_CmacUpdate(&cmac, in, inSz);
  13771. /* Test bad args. */
  13772. if (ret == 0) {
  13773. ret = wc_CmacUpdate(NULL, in, inSz);
  13774. if (ret == BAD_FUNC_ARG) {
  13775. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13776. }
  13777. if (ret == BAD_FUNC_ARG) {
  13778. ret = 0;
  13779. }
  13780. else if (ret == 0) {
  13781. ret = WOLFSSL_FATAL_ERROR;
  13782. }
  13783. wc_AesFree(&cmac.aes);
  13784. }
  13785. res = TEST_RES_CHECK(ret == 0);
  13786. #endif
  13787. return res;
  13788. } /* END test_wc_CmacUpdate */
  13789. /*
  13790. * Testing wc_CmacFinal()
  13791. */
  13792. static int test_wc_CmacFinal(void)
  13793. {
  13794. int res = TEST_SKIPPED;
  13795. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13796. Cmac cmac;
  13797. byte key[] =
  13798. {
  13799. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13800. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13801. };
  13802. byte msg[] =
  13803. {
  13804. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13805. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13806. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13807. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13808. 0xf4
  13809. };
  13810. /* Test vectors from CMACGenAES128.rsp from
  13811. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13812. * Per RFC4493 truncation of lsb is possible.
  13813. */
  13814. byte expMac[] =
  13815. {
  13816. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13817. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13818. };
  13819. byte mac[AES_BLOCK_SIZE];
  13820. word32 msgSz = (word32)sizeof(msg);
  13821. word32 keySz = (word32)sizeof(key);
  13822. word32 macSz = sizeof(mac);
  13823. word32 badMacSz = 17;
  13824. int expMacSz = sizeof(expMac);
  13825. int type = WC_CMAC_AES;
  13826. int ret = 0;
  13827. XMEMSET(mac, 0, macSz);
  13828. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13829. if (ret != 0) {
  13830. return ret;
  13831. }
  13832. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13833. if (ret == 0) {
  13834. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13835. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13836. ret = WOLFSSL_FATAL_ERROR;
  13837. }
  13838. /* Pass in bad args. */
  13839. if (ret == 0) {
  13840. ret = wc_CmacFinal(NULL, mac, &macSz);
  13841. if (ret == BAD_FUNC_ARG) {
  13842. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13843. }
  13844. if (ret == BAD_FUNC_ARG) {
  13845. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13846. if (ret == BUFFER_E) {
  13847. ret = 0;
  13848. }
  13849. }
  13850. else if (ret == 0) {
  13851. ret = WOLFSSL_FATAL_ERROR;
  13852. }
  13853. }
  13854. }
  13855. res = TEST_RES_CHECK(ret == 0);
  13856. #endif
  13857. return res;
  13858. } /* END test_wc_CmacFinal */
  13859. /*
  13860. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13861. */
  13862. static int test_wc_AesCmacGenerate(void)
  13863. {
  13864. int res = TEST_SKIPPED;
  13865. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13866. Cmac cmac;
  13867. byte key[] =
  13868. {
  13869. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13870. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13871. };
  13872. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13873. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13874. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13875. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13876. byte mac[AES_BLOCK_SIZE];
  13877. word32 keySz = sizeof(key);
  13878. word32 macSz = sizeof(mac);
  13879. word32 msgSz = sizeof(msg) - 1;
  13880. word32 expMacSz = sizeof(expMac) - 1;
  13881. int type = WC_CMAC_AES;
  13882. int ret = 0;
  13883. XMEMSET(mac, 0, macSz);
  13884. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13885. if (ret != 0) {
  13886. return ret;
  13887. }
  13888. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13889. if (ret != 0) {
  13890. return ret;
  13891. }
  13892. else {
  13893. wc_AesFree(&cmac.aes);
  13894. }
  13895. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13896. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13897. ret = WOLFSSL_FATAL_ERROR;
  13898. }
  13899. /* Pass in bad args. */
  13900. if (ret == 0) {
  13901. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13902. if (ret == BAD_FUNC_ARG) {
  13903. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13904. }
  13905. if (ret == BAD_FUNC_ARG) {
  13906. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13907. }
  13908. if (ret == BAD_FUNC_ARG) {
  13909. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13910. }
  13911. if (ret == BAD_FUNC_ARG) {
  13912. ret = 0;
  13913. }
  13914. else if (ret == 0) {
  13915. ret = WOLFSSL_FATAL_ERROR;
  13916. }
  13917. }
  13918. if (ret == 0) {
  13919. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13920. /* Test bad args. */
  13921. if (ret == 0) {
  13922. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13923. if (ret == BAD_FUNC_ARG) {
  13924. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13925. }
  13926. if (ret == BAD_FUNC_ARG) {
  13927. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13928. }
  13929. if (ret == BAD_FUNC_ARG) {
  13930. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13931. }
  13932. if (ret == BAD_FUNC_ARG) {
  13933. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13934. }
  13935. if (ret == BAD_FUNC_ARG) {
  13936. ret = 0;
  13937. }
  13938. else if (ret == 0) {
  13939. ret = WOLFSSL_FATAL_ERROR;
  13940. }
  13941. }
  13942. }
  13943. res = TEST_RES_CHECK(ret == 0);
  13944. #endif
  13945. return res;
  13946. } /* END test_wc_AesCmacGenerate */
  13947. /*
  13948. * Testing streaming AES-GCM API.
  13949. */
  13950. static int test_wc_AesGcmStream(void)
  13951. {
  13952. int res = TEST_SKIPPED;
  13953. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13954. defined(WOLFSSL_AESGCM_STREAM)
  13955. int ret = 0;
  13956. int i;
  13957. WC_RNG rng[1];
  13958. Aes aesEnc[1];
  13959. Aes aesDec[1];
  13960. byte tag[AES_BLOCK_SIZE];
  13961. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13962. byte out[AES_BLOCK_SIZE * 3 + 2];
  13963. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13964. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13965. byte key[AES_128_KEY_SIZE] = { 0, };
  13966. byte iv[AES_IV_SIZE] = { 1, };
  13967. byte ivOut[AES_IV_SIZE];
  13968. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13969. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13970. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13971. };
  13972. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13973. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13974. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13975. };
  13976. static const byte expTag[AES_BLOCK_SIZE] = {
  13977. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13978. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13979. };
  13980. /* Create a random for generating IV/nonce. */
  13981. AssertIntEQ(wc_InitRng(rng), 0);
  13982. /* Initialize data structures. */
  13983. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13984. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13985. /* BadParameters to streaming init. */
  13986. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13987. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13988. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13989. BAD_FUNC_ARG);
  13990. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13991. BAD_FUNC_ARG);
  13992. /* Bad parameters to encrypt update. */
  13993. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13994. BAD_FUNC_ARG);
  13995. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13996. BAD_FUNC_ARG);
  13997. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13998. BAD_FUNC_ARG);
  13999. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14000. BAD_FUNC_ARG);
  14001. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14002. BAD_FUNC_ARG);
  14003. /* Bad parameters to decrypt update. */
  14004. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14005. BAD_FUNC_ARG);
  14006. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14007. BAD_FUNC_ARG);
  14008. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14009. BAD_FUNC_ARG);
  14010. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14011. BAD_FUNC_ARG);
  14012. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14013. BAD_FUNC_ARG);
  14014. /* Bad parameters to encrypt final. */
  14015. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14016. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14017. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14018. BAD_FUNC_ARG);
  14019. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14020. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14021. BAD_FUNC_ARG);
  14022. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14023. BAD_FUNC_ARG);
  14024. /* Bad parameters to decrypt final. */
  14025. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14026. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14027. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14028. BAD_FUNC_ARG);
  14029. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14030. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14031. BAD_FUNC_ARG);
  14032. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14033. BAD_FUNC_ARG);
  14034. /* Check calling final before setting key fails. */
  14035. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14036. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14037. /* Check calling update before setting key else fails. */
  14038. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14039. MISSING_KEY);
  14040. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14041. MISSING_KEY);
  14042. /* Set key but not IV. */
  14043. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14044. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14045. /* Check calling final before setting IV fails. */
  14046. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14047. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14048. /* Check calling update before setting IV else fails. */
  14049. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14050. MISSING_IV);
  14051. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14052. MISSING_IV);
  14053. /* Set IV using fixed part IV and external IV APIs. */
  14054. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14055. rng), 0);
  14056. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14057. GCM_NONCE_MID_SZ), 0);
  14058. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14059. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14060. /* Encrypt and decrypt data. */
  14061. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14062. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14063. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14064. /* Finalize and check tag matches. */
  14065. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14066. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14067. /* Set key and IV through streaming init API. */
  14068. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14069. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14070. /* Encrypt/decrypt one block and AAD of one block. */
  14071. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14072. AES_BLOCK_SIZE), 0);
  14073. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14074. AES_BLOCK_SIZE), 0);
  14075. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14076. /* Finalize and check tag matches. */
  14077. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14078. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14079. /* Set key and IV through streaming init API. */
  14080. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14081. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14082. /* No data to encrypt/decrypt one byte of AAD. */
  14083. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14084. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14085. /* Finalize and check tag matches. */
  14086. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14087. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14088. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14089. /* Set key and IV through streaming init API. */
  14090. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14091. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14092. /* Encrypt/decrypt one byte and no AAD. */
  14093. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14094. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14095. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14096. /* Finalize and check tag matches. */
  14097. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14098. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14099. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14100. /* Set key and IV through streaming init API. */
  14101. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14102. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14103. /* Encryption AES is one byte at a time */
  14104. for (i = 0; i < (int)sizeof(aad); i++) {
  14105. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14106. 0);
  14107. }
  14108. for (i = 0; i < (int)sizeof(in); i++) {
  14109. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14110. 0);
  14111. }
  14112. /* Decryption AES is two bytes at a time */
  14113. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14114. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14115. 0);
  14116. }
  14117. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14118. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14119. 0), 0);
  14120. }
  14121. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14122. /* Finalize and check tag matches. */
  14123. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14124. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14125. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14126. /* Check streaming encryption can be decrypted with one shot. */
  14127. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14128. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14129. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14130. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14131. wc_AesFree(aesEnc);
  14132. wc_AesFree(aesDec);
  14133. wc_FreeRng(rng);
  14134. res = TEST_RES_CHECK(ret == 0);
  14135. #endif
  14136. return res;
  14137. } /* END test_wc_AesGcmStream */
  14138. /*
  14139. * unit test for wc_Des3_SetIV()
  14140. */
  14141. static int test_wc_Des3_SetIV(void)
  14142. {
  14143. int res = TEST_SKIPPED;
  14144. #ifndef NO_DES3
  14145. Des3 des;
  14146. int ret = 0;
  14147. const byte key[] =
  14148. {
  14149. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14150. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14151. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14152. };
  14153. const byte iv[] =
  14154. {
  14155. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14156. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14157. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14158. };
  14159. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14160. if (ret != 0)
  14161. return ret;
  14162. /* DES_ENCRYPTION or DES_DECRYPTION */
  14163. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14164. if (ret == 0) {
  14165. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14166. ret = WOLFSSL_FATAL_ERROR;
  14167. }
  14168. }
  14169. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14170. /* Test explicitly wc_Des3_SetIV() */
  14171. if (ret == 0) {
  14172. ret = wc_Des3_SetIV(NULL, iv);
  14173. if (ret == BAD_FUNC_ARG) {
  14174. ret = wc_Des3_SetIV(&des, NULL);
  14175. }
  14176. else if (ret == 0) {
  14177. ret = WOLFSSL_FATAL_ERROR;
  14178. }
  14179. }
  14180. #endif
  14181. wc_Des3Free(&des);
  14182. res = TEST_RES_CHECK(ret == 0);
  14183. #endif
  14184. return res;
  14185. } /* END test_wc_Des3_SetIV */
  14186. /*
  14187. * unit test for wc_Des3_SetKey()
  14188. */
  14189. static int test_wc_Des3_SetKey(void)
  14190. {
  14191. int res = TEST_SKIPPED;
  14192. #ifndef NO_DES3
  14193. Des3 des;
  14194. int ret = 0;
  14195. const byte key[] =
  14196. {
  14197. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14198. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14199. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14200. };
  14201. const byte iv[] =
  14202. {
  14203. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14204. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14205. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14206. };
  14207. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14208. if (ret != 0)
  14209. return ret;
  14210. /* DES_ENCRYPTION or DES_DECRYPTION */
  14211. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14212. if (ret == 0) {
  14213. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14214. ret = WOLFSSL_FATAL_ERROR;
  14215. }
  14216. }
  14217. /* Test bad args. */
  14218. if (ret == 0) {
  14219. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  14220. if (ret == BAD_FUNC_ARG) {
  14221. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  14222. }
  14223. if (ret == BAD_FUNC_ARG) {
  14224. ret = wc_Des3_SetKey(&des, key, iv, -1);
  14225. }
  14226. if (ret == BAD_FUNC_ARG) {
  14227. /* Default case. Should return 0. */
  14228. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  14229. }
  14230. } /* END if ret != 0 */
  14231. wc_Des3Free(&des);
  14232. res = TEST_RES_CHECK(ret == 0);
  14233. #endif
  14234. return res;
  14235. } /* END test_wc_Des3_SetKey */
  14236. /*
  14237. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14238. */
  14239. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14240. {
  14241. int res = TEST_SKIPPED;
  14242. #ifndef NO_DES3
  14243. Des3 des;
  14244. int ret = 0;
  14245. byte cipher[24];
  14246. byte plain[24];
  14247. const byte key[] =
  14248. {
  14249. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14250. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14251. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14252. };
  14253. const byte iv[] =
  14254. {
  14255. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14256. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14257. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14258. };
  14259. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14260. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14261. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14262. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14263. };
  14264. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14265. if (ret != 0)
  14266. return ret;
  14267. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14268. if (ret == 0) {
  14269. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14270. if (ret == 0) {
  14271. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14272. }
  14273. if (ret == 0) {
  14274. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14275. }
  14276. }
  14277. if (ret == 0) {
  14278. if (XMEMCMP(plain, vector, 24) != 0) {
  14279. ret = WOLFSSL_FATAL_ERROR;
  14280. }
  14281. }
  14282. /* Pass in bad args. */
  14283. if (ret == 0) {
  14284. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14285. if (ret == BAD_FUNC_ARG) {
  14286. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14287. }
  14288. if (ret == BAD_FUNC_ARG) {
  14289. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14290. }
  14291. if (ret != BAD_FUNC_ARG) {
  14292. ret = WOLFSSL_FATAL_ERROR;
  14293. }
  14294. else {
  14295. ret = 0;
  14296. }
  14297. }
  14298. if (ret == 0) {
  14299. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14300. if (ret == BAD_FUNC_ARG) {
  14301. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14302. }
  14303. if (ret == BAD_FUNC_ARG) {
  14304. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14305. }
  14306. if (ret != BAD_FUNC_ARG) {
  14307. ret = WOLFSSL_FATAL_ERROR;
  14308. }
  14309. else {
  14310. ret = 0;
  14311. }
  14312. }
  14313. wc_Des3Free(&des);
  14314. res = TEST_RES_CHECK(ret == 0);
  14315. #endif
  14316. return res;
  14317. } /* END wc_Des3_CbcEncrypt */
  14318. /*
  14319. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14320. */
  14321. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14322. {
  14323. int res = TEST_SKIPPED;
  14324. #ifndef NO_DES3
  14325. int ret = 0;
  14326. word32 vectorSz, cipherSz;
  14327. byte cipher[24];
  14328. byte plain[24];
  14329. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14330. {
  14331. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14332. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14333. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14334. };
  14335. byte key[] =
  14336. {
  14337. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14338. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14339. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14340. };
  14341. byte iv[] =
  14342. {
  14343. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14344. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14345. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14346. };
  14347. vectorSz = sizeof(byte) * 24;
  14348. cipherSz = sizeof(byte) * 24;
  14349. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14350. if (ret == 0) {
  14351. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14352. if (ret == 0) {
  14353. if (XMEMCMP(plain, vector, 24) != 0) {
  14354. ret = WOLFSSL_FATAL_ERROR;
  14355. }
  14356. }
  14357. }
  14358. /* pass in bad args. */
  14359. if (ret == 0) {
  14360. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14361. if (ret == BAD_FUNC_ARG) {
  14362. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14363. }
  14364. if (ret == BAD_FUNC_ARG) {
  14365. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14366. }
  14367. if (ret == BAD_FUNC_ARG) {
  14368. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14369. key, NULL);
  14370. }
  14371. else {
  14372. /* Return code catch. */
  14373. ret = WOLFSSL_FAILURE;
  14374. }
  14375. }
  14376. if (ret == 0) {
  14377. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14378. if (ret == BAD_FUNC_ARG) {
  14379. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14380. }
  14381. if (ret == BAD_FUNC_ARG) {
  14382. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14383. }
  14384. if (ret == BAD_FUNC_ARG) {
  14385. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14386. }
  14387. else {
  14388. ret = WOLFSSL_FAILURE;
  14389. }
  14390. }
  14391. res = TEST_RES_CHECK(ret == 0);
  14392. #endif
  14393. return res;
  14394. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14395. /*
  14396. * Unit test for wc_Des3_EcbEncrypt
  14397. */
  14398. static int test_wc_Des3_EcbEncrypt(void)
  14399. {
  14400. int res = TEST_SKIPPED;
  14401. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14402. Des3 des;
  14403. int ret = 0;
  14404. byte cipher[24];
  14405. word32 cipherSz = sizeof(cipher);
  14406. const byte key[] =
  14407. {
  14408. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14409. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14410. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14411. };
  14412. const byte iv[] =
  14413. {
  14414. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14415. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14416. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14417. };
  14418. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14419. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14420. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14421. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14422. };
  14423. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14424. if (ret != 0) {
  14425. return ret;
  14426. }
  14427. if (ret == 0 ) {
  14428. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14429. }
  14430. /* Bad Cases */
  14431. if (ret == 0) {
  14432. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14433. if (ret == BAD_FUNC_ARG) {
  14434. ret = 0;
  14435. }
  14436. }
  14437. if (ret == 0) {
  14438. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14439. if (ret == BAD_FUNC_ARG) {
  14440. ret = 0;
  14441. }
  14442. }
  14443. if (ret == 0) {
  14444. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14445. if (ret == BAD_FUNC_ARG) {
  14446. ret = 0;
  14447. }
  14448. }
  14449. if (ret == 0) {
  14450. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  14451. if (ret == BAD_FUNC_ARG) {
  14452. ret = 0;
  14453. }
  14454. }
  14455. if (ret == 0) {
  14456. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  14457. if (ret == BAD_FUNC_ARG) {
  14458. ret = 0;
  14459. }
  14460. }
  14461. /* Good Cases */
  14462. if (ret == 0) {
  14463. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  14464. }
  14465. wc_Des3Free(&des);
  14466. res = TEST_RES_CHECK(ret == 0);
  14467. #endif
  14468. return res;
  14469. } /* END test_wc_Des3_EcbEncrypt */
  14470. /*
  14471. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14472. */
  14473. static int test_wc_Chacha_SetKey(void)
  14474. {
  14475. int res = TEST_SKIPPED;
  14476. #ifdef HAVE_CHACHA
  14477. ChaCha ctx;
  14478. const byte key[] =
  14479. {
  14480. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14484. };
  14485. byte cipher[128];
  14486. int ret = 0;
  14487. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14488. /* Test bad args. */
  14489. if (ret == 0) {
  14490. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14491. if (ret == BAD_FUNC_ARG) {
  14492. ret = wc_Chacha_SetKey(&ctx, key, 18);
  14493. }
  14494. if (ret == BAD_FUNC_ARG) {
  14495. ret = 0;
  14496. }
  14497. else {
  14498. ret = WOLFSSL_FATAL_ERROR;
  14499. }
  14500. }
  14501. if (ret == 0) {
  14502. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  14503. }
  14504. if (ret == 0) {
  14505. /* Test bad args. */
  14506. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  14507. if (ret == BAD_FUNC_ARG) {
  14508. ret = 0;
  14509. }
  14510. else {
  14511. ret = WOLFSSL_FAILURE;
  14512. }
  14513. }
  14514. res = TEST_RES_CHECK(ret == 0);
  14515. #endif
  14516. return res;
  14517. } /* END test_wc_Chacha_SetKey */
  14518. /*
  14519. * unit test for wc_Poly1305SetKey()
  14520. */
  14521. static int test_wc_Poly1305SetKey(void)
  14522. {
  14523. int res = TEST_SKIPPED;
  14524. #ifdef HAVE_POLY1305
  14525. Poly1305 ctx;
  14526. const byte key[] =
  14527. {
  14528. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14529. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14530. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14531. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14532. };
  14533. int ret = 0;
  14534. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14535. /* Test bad args. */
  14536. if (ret == 0) {
  14537. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14538. if (ret == BAD_FUNC_ARG) {
  14539. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  14540. }
  14541. if (ret == BAD_FUNC_ARG) {
  14542. ret = wc_Poly1305SetKey(&ctx, key, 18);
  14543. }
  14544. if (ret == BAD_FUNC_ARG) {
  14545. ret = 0;
  14546. }
  14547. else {
  14548. ret = WOLFSSL_FATAL_ERROR;
  14549. }
  14550. }
  14551. res = TEST_RES_CHECK(ret == 0);
  14552. #endif
  14553. return res;
  14554. } /* END test_wc_Poly1305_SetKey() */
  14555. /*
  14556. * Testing wc_Chacha_Process()
  14557. */
  14558. static int test_wc_Chacha_Process(void)
  14559. {
  14560. int res = TEST_SKIPPED;
  14561. #ifdef HAVE_CHACHA
  14562. ChaCha enc, dec;
  14563. byte cipher[128];
  14564. byte plain[128];
  14565. const byte key[] =
  14566. {
  14567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14568. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14569. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14570. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14571. };
  14572. const char* input = "Everybody gets Friday off.";
  14573. word32 keySz = sizeof(key)/sizeof(byte);
  14574. unsigned long int inlen = XSTRLEN(input);
  14575. int ret = 0;
  14576. /*Initialize stack varialbes.*/
  14577. XMEMSET(cipher, 0, 128);
  14578. XMEMSET(plain, 0, 128);
  14579. ret = wc_Chacha_SetKey(&enc, key, keySz);
  14580. AssertIntEQ(ret, 0);
  14581. ret = wc_Chacha_SetKey(&dec, key, keySz);
  14582. AssertIntEQ(ret, 0);
  14583. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14584. AssertIntEQ(ret, 0);
  14585. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14586. AssertIntEQ(ret, 0);
  14587. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  14588. AssertIntEQ(ret, 0);
  14589. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  14590. AssertIntEQ(ret, 0);
  14591. ret = XMEMCMP(input, plain, (int)inlen);
  14592. AssertIntEQ(ret, 0);
  14593. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14594. /* test checking and using leftovers, currently just in C code */
  14595. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14596. AssertIntEQ(ret, 0);
  14597. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14598. AssertIntEQ(ret, 0);
  14599. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  14600. AssertIntEQ(ret, 0);
  14601. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14602. (byte*)input + (inlen - 2), 2);
  14603. AssertIntEQ(ret, 0);
  14604. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  14605. AssertIntEQ(ret, 0);
  14606. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14607. (byte*)input + (inlen - 2), 2);
  14608. AssertIntEQ(ret, 0);
  14609. ret = XMEMCMP(input, plain, (int)inlen);
  14610. AssertIntEQ(ret, 0);
  14611. /* check edge cases with counter increment */
  14612. {
  14613. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14614. const byte expected[] = {
  14615. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14616. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14617. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14618. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14619. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14620. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14621. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14622. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14623. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14624. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14625. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14626. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14627. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14628. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14629. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14630. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14631. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14632. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14633. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14634. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14635. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14636. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14637. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14638. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14639. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14640. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14641. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14642. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14643. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14644. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14645. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14646. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14647. };
  14648. const byte iv2[] = {
  14649. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14650. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14651. };
  14652. byte input2[256];
  14653. int i;
  14654. for (i = 0; i < 256; i++)
  14655. input2[i] = i;
  14656. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  14657. AssertIntEQ(ret, 0);
  14658. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  14659. AssertIntEQ(ret, 0);
  14660. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14661. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  14662. AssertIntEQ(ret, 0);
  14663. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14664. /* partial */
  14665. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  14666. AssertIntEQ(ret, 0);
  14667. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14668. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  14669. AssertIntEQ(ret, 0);
  14670. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14671. }
  14672. #endif
  14673. /* Test bad args. */
  14674. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  14675. AssertIntEQ(ret, BAD_FUNC_ARG);
  14676. if (ret == BAD_FUNC_ARG) {
  14677. ret = 0;
  14678. }
  14679. res = TEST_RES_CHECK(ret == 0);
  14680. #endif
  14681. return res;
  14682. } /* END test_wc_Chacha_Process */
  14683. /*
  14684. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14685. */
  14686. static int test_wc_ChaCha20Poly1305_aead(void)
  14687. {
  14688. int res = TEST_SKIPPED;
  14689. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14690. const byte key[] = {
  14691. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14692. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14693. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14694. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14695. };
  14696. const byte plaintext[] = {
  14697. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14698. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14699. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14700. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14701. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14702. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14703. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14704. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14705. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14706. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14707. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14708. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14709. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14710. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14711. 0x74, 0x2e
  14712. };
  14713. const byte iv[] = {
  14714. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14715. 0x44, 0x45, 0x46, 0x47
  14716. };
  14717. const byte aad[] = { /* additional data */
  14718. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14719. 0xc4, 0xc5, 0xc6, 0xc7
  14720. };
  14721. const byte cipher[] = { /* expected output from operation */
  14722. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14723. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14724. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14725. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14726. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14727. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14728. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14729. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14730. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14731. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14732. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14733. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14734. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14735. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14736. 0x61, 0x16
  14737. };
  14738. const byte authTag[] = { /* expected output from operation */
  14739. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14740. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14741. };
  14742. byte generatedCiphertext[272];
  14743. byte generatedPlaintext[272];
  14744. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14745. int ret = 0;
  14746. /* Initialize stack variables. */
  14747. XMEMSET(generatedCiphertext, 0, 272);
  14748. XMEMSET(generatedPlaintext, 0, 272);
  14749. /* Test Encrypt */
  14750. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14751. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14752. AssertIntEQ(ret, 0);
  14753. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14754. AssertIntEQ(ret, 0);
  14755. /* Test bad args. */
  14756. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14757. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14758. AssertIntEQ(ret, BAD_FUNC_ARG);
  14759. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14760. plaintext, sizeof(plaintext),
  14761. generatedCiphertext, generatedAuthTag);
  14762. AssertIntEQ(ret, BAD_FUNC_ARG);
  14763. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14764. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14765. AssertIntEQ(ret, BAD_FUNC_ARG);
  14766. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14767. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14768. AssertIntEQ(ret, BAD_FUNC_ARG);
  14769. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14770. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14771. AssertIntEQ(ret, BAD_FUNC_ARG);
  14772. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14773. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14774. if (ret == BAD_FUNC_ARG) {
  14775. ret = 0;
  14776. (void)ret; /* suppress never read */
  14777. }
  14778. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14779. sizeof(cipher), authTag, generatedPlaintext);
  14780. AssertIntEQ(ret, 0);
  14781. ret = XMEMCMP(generatedPlaintext, plaintext,
  14782. sizeof(plaintext)/sizeof(byte));
  14783. AssertIntEQ(ret, 0);
  14784. /* Test bad args. */
  14785. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14786. sizeof(cipher), authTag, generatedPlaintext);
  14787. AssertIntEQ(ret, BAD_FUNC_ARG);
  14788. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14789. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14790. AssertIntEQ(ret, BAD_FUNC_ARG);
  14791. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14792. sizeof(cipher), authTag, generatedPlaintext);
  14793. AssertIntEQ(ret, BAD_FUNC_ARG);
  14794. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14795. sizeof(cipher), NULL, generatedPlaintext);
  14796. AssertIntEQ(ret, BAD_FUNC_ARG);
  14797. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14798. sizeof(cipher), authTag, NULL);
  14799. AssertIntEQ(ret, BAD_FUNC_ARG);
  14800. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14801. sizeof(cipher), authTag, generatedPlaintext);
  14802. AssertIntEQ(ret, BAD_FUNC_ARG);
  14803. if (ret == BAD_FUNC_ARG) {
  14804. ret = 0;
  14805. }
  14806. res = TEST_RES_CHECK(ret == 0);
  14807. #endif
  14808. return res;
  14809. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14810. /*
  14811. * Testing function for wc_Rc2SetKey().
  14812. */
  14813. static int test_wc_Rc2SetKey(void)
  14814. {
  14815. int res = TEST_SKIPPED;
  14816. #ifdef WC_RC2
  14817. Rc2 rc2;
  14818. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14819. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14820. int ret = 0;
  14821. /* valid key and IV */
  14822. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14823. iv, 40);
  14824. if (ret == 0) {
  14825. /* valid key, no IV */
  14826. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14827. NULL, 40);
  14828. }
  14829. /* bad arguments */
  14830. if (ret == 0) {
  14831. /* null Rc2 struct */
  14832. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14833. iv, 40);
  14834. if (ret == BAD_FUNC_ARG) {
  14835. ret = 0;
  14836. }
  14837. }
  14838. if (ret == 0) {
  14839. /* null key */
  14840. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14841. iv, 40);
  14842. if (ret == BAD_FUNC_ARG) {
  14843. ret = 0;
  14844. }
  14845. }
  14846. if (ret == 0) {
  14847. /* key size == 0 */
  14848. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14849. if (ret == WC_KEY_SIZE_E) {
  14850. ret = 0;
  14851. }
  14852. }
  14853. if (ret == 0) {
  14854. /* key size > 128 */
  14855. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14856. if (ret == WC_KEY_SIZE_E) {
  14857. ret = 0;
  14858. }
  14859. }
  14860. if (ret == 0) {
  14861. /* effective bits == 0 */
  14862. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14863. iv, 0);
  14864. if (ret == WC_KEY_SIZE_E) {
  14865. ret = 0;
  14866. }
  14867. }
  14868. if (ret == 0) {
  14869. /* effective bits > 1024 */
  14870. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14871. iv, 1025);
  14872. if (ret == WC_KEY_SIZE_E) {
  14873. ret = 0;
  14874. }
  14875. }
  14876. res = TEST_RES_CHECK(ret == 0);
  14877. #endif
  14878. return res;
  14879. } /* END test_wc_Rc2SetKey */
  14880. /*
  14881. * Testing function for wc_Rc2SetIV().
  14882. */
  14883. static int test_wc_Rc2SetIV(void)
  14884. {
  14885. int res = TEST_SKIPPED;
  14886. #ifdef WC_RC2
  14887. Rc2 rc2;
  14888. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14889. int ret = 0;
  14890. /* valid IV */
  14891. ret = wc_Rc2SetIV(&rc2, iv);
  14892. if (ret == 0) {
  14893. /* valid NULL IV */
  14894. ret = wc_Rc2SetIV(&rc2, NULL);
  14895. }
  14896. /* bad arguments */
  14897. if (ret == 0) {
  14898. ret = wc_Rc2SetIV(NULL, iv);
  14899. if (ret == BAD_FUNC_ARG) {
  14900. ret = 0;
  14901. }
  14902. }
  14903. res = TEST_RES_CHECK(ret == 0);
  14904. #endif
  14905. return res;
  14906. } /* END test_wc_Rc2SetKey */
  14907. /*
  14908. * Testing function for wc_Rc2EcbEncrypt().
  14909. */
  14910. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14911. {
  14912. int res = TEST_SKIPPED;
  14913. #ifdef WC_RC2
  14914. Rc2 rc2;
  14915. int ret = 0;
  14916. int effectiveKeyBits = 63;
  14917. byte cipher[RC2_BLOCK_SIZE];
  14918. byte plain[RC2_BLOCK_SIZE];
  14919. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14920. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14921. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14922. XMEMSET(cipher, 0, sizeof(cipher));
  14923. XMEMSET(plain, 0, sizeof(plain));
  14924. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14925. NULL, effectiveKeyBits);
  14926. if (ret == 0) {
  14927. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14928. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14929. ret = WOLFSSL_FATAL_ERROR;
  14930. }
  14931. if (ret == 0) {
  14932. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14933. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14934. ret = WOLFSSL_FATAL_ERROR;
  14935. }
  14936. }
  14937. }
  14938. /* Rc2EcbEncrypt bad arguments */
  14939. if (ret == 0) {
  14940. /* null Rc2 struct */
  14941. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14942. if (ret == BAD_FUNC_ARG) {
  14943. ret = 0;
  14944. }
  14945. }
  14946. if (ret == 0) {
  14947. /* null out buffer */
  14948. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14949. if (ret == BAD_FUNC_ARG) {
  14950. ret = 0;
  14951. }
  14952. }
  14953. if (ret == 0) {
  14954. /* null input buffer */
  14955. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14956. if (ret == BAD_FUNC_ARG) {
  14957. ret = 0;
  14958. }
  14959. }
  14960. if (ret == 0) {
  14961. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14962. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14963. if (ret == BUFFER_E) {
  14964. ret = 0;
  14965. }
  14966. }
  14967. /* Rc2EcbDecrypt bad arguments */
  14968. if (ret == 0) {
  14969. /* null Rc2 struct */
  14970. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14971. if (ret == BAD_FUNC_ARG) {
  14972. ret = 0;
  14973. }
  14974. }
  14975. if (ret == 0) {
  14976. /* null out buffer */
  14977. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14978. if (ret == BAD_FUNC_ARG) {
  14979. ret = 0;
  14980. }
  14981. }
  14982. if (ret == 0) {
  14983. /* null input buffer */
  14984. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14985. if (ret == BAD_FUNC_ARG) {
  14986. ret = 0;
  14987. }
  14988. }
  14989. if (ret == 0) {
  14990. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14991. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14992. if (ret == BUFFER_E) {
  14993. ret = 0;
  14994. }
  14995. }
  14996. res = TEST_RES_CHECK(ret == 0);
  14997. #endif
  14998. return res;
  14999. } /* END test_wc_Rc2SetKey */
  15000. /*
  15001. * Testing function for wc_Rc2CbcEncrypt().
  15002. */
  15003. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15004. {
  15005. int res = TEST_SKIPPED;
  15006. #ifdef WC_RC2
  15007. Rc2 rc2;
  15008. int ret = 0;
  15009. int effectiveKeyBits = 63;
  15010. byte cipher[RC2_BLOCK_SIZE*2];
  15011. byte plain[RC2_BLOCK_SIZE*2];
  15012. /* vector taken from test.c */
  15013. byte key[] = {
  15014. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15015. };
  15016. byte iv[] = {
  15017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15018. };
  15019. byte input[] = {
  15020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15021. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15022. };
  15023. byte output[] = {
  15024. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15025. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15026. };
  15027. XMEMSET(cipher, 0, sizeof(cipher));
  15028. XMEMSET(plain, 0, sizeof(plain));
  15029. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15030. iv, effectiveKeyBits);
  15031. if (ret == 0) {
  15032. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  15033. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  15034. ret = WOLFSSL_FATAL_ERROR;
  15035. }
  15036. else {
  15037. /* reset IV for decrypt */
  15038. ret = wc_Rc2SetIV(&rc2, iv);
  15039. }
  15040. if (ret == 0) {
  15041. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  15042. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  15043. ret = WOLFSSL_FATAL_ERROR;
  15044. }
  15045. }
  15046. }
  15047. /* Rc2CbcEncrypt bad arguments */
  15048. if (ret == 0) {
  15049. /* null Rc2 struct */
  15050. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  15051. if (ret == BAD_FUNC_ARG) {
  15052. ret = 0;
  15053. }
  15054. }
  15055. if (ret == 0) {
  15056. /* null out buffer */
  15057. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  15058. if (ret == BAD_FUNC_ARG) {
  15059. ret = 0;
  15060. }
  15061. }
  15062. if (ret == 0) {
  15063. /* null input buffer */
  15064. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  15065. if (ret == BAD_FUNC_ARG) {
  15066. ret = 0;
  15067. }
  15068. }
  15069. /* Rc2CbcDecrypt bad arguments */
  15070. if (ret == 0) {
  15071. /* in size is 0 */
  15072. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  15073. if (ret != 0) {
  15074. ret = WOLFSSL_FATAL_ERROR;
  15075. }
  15076. }
  15077. if (ret == 0) {
  15078. /* null Rc2 struct */
  15079. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  15080. if (ret == BAD_FUNC_ARG) {
  15081. ret = 0;
  15082. }
  15083. }
  15084. if (ret == 0) {
  15085. /* null out buffer */
  15086. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  15087. if (ret == BAD_FUNC_ARG) {
  15088. ret = 0;
  15089. }
  15090. }
  15091. if (ret == 0) {
  15092. /* null input buffer */
  15093. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  15094. if (ret == BAD_FUNC_ARG) {
  15095. ret = 0;
  15096. }
  15097. }
  15098. res = TEST_RES_CHECK(ret == 0);
  15099. #endif
  15100. return res;
  15101. } /* END test_wc_Rc2SetKey */
  15102. /*
  15103. * Testing function for wc_AesSetIV
  15104. */
  15105. static int test_wc_AesSetIV(void)
  15106. {
  15107. int res = TEST_SKIPPED;
  15108. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15109. Aes aes;
  15110. int ret = 0;
  15111. byte key16[] =
  15112. {
  15113. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15114. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15115. };
  15116. byte iv1[] = "1234567890abcdef";
  15117. byte iv2[] = "0987654321fedcba";
  15118. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15119. if (ret != 0)
  15120. return ret;
  15121. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15122. iv1, AES_ENCRYPTION);
  15123. if (ret == 0) {
  15124. ret = wc_AesSetIV(&aes, iv2);
  15125. }
  15126. /* Test bad args. */
  15127. if (ret == 0) {
  15128. ret = wc_AesSetIV(NULL, iv1);
  15129. if (ret == BAD_FUNC_ARG) {
  15130. /* NULL iv should return 0. */
  15131. ret = wc_AesSetIV(&aes, NULL);
  15132. }
  15133. else {
  15134. ret = WOLFSSL_FATAL_ERROR;
  15135. }
  15136. }
  15137. wc_AesFree(&aes);
  15138. res = TEST_RES_CHECK(ret == 0);
  15139. #endif
  15140. return res;
  15141. } /* test_wc_AesSetIV */
  15142. /*
  15143. * Testing function for wc_AesSetKey().
  15144. */
  15145. static int test_wc_AesSetKey(void)
  15146. {
  15147. int res = TEST_SKIPPED;
  15148. #ifndef NO_AES
  15149. Aes aes;
  15150. int ret = 0;
  15151. byte key16[] =
  15152. {
  15153. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15154. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15155. };
  15156. #ifdef WOLFSSL_AES_192
  15157. byte key24[] =
  15158. {
  15159. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15160. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15161. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15162. };
  15163. #endif
  15164. #ifdef WOLFSSL_AES_256
  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. #endif
  15173. byte badKey16[] =
  15174. {
  15175. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15176. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15177. };
  15178. byte iv[] = "1234567890abcdef";
  15179. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15180. if (ret != 0)
  15181. return ret;
  15182. #ifdef WOLFSSL_AES_128
  15183. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15184. iv, AES_ENCRYPTION);
  15185. #endif
  15186. #ifdef WOLFSSL_AES_192
  15187. if (ret == 0) {
  15188. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  15189. iv, AES_ENCRYPTION);
  15190. }
  15191. #endif
  15192. #ifdef WOLFSSL_AES_256
  15193. if (ret == 0) {
  15194. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  15195. iv, AES_ENCRYPTION);
  15196. }
  15197. #endif
  15198. /* Pass in bad args. */
  15199. if (ret == 0) {
  15200. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  15201. iv, AES_ENCRYPTION);
  15202. if (ret == BAD_FUNC_ARG) {
  15203. ret = wc_AesSetKey(&aes, badKey16,
  15204. (word32) sizeof(badKey16) / sizeof(byte),
  15205. iv, AES_ENCRYPTION);
  15206. }
  15207. if (ret == BAD_FUNC_ARG) {
  15208. ret = 0;
  15209. }
  15210. else {
  15211. ret = WOLFSSL_FATAL_ERROR;
  15212. }
  15213. }
  15214. wc_AesFree(&aes);
  15215. res = TEST_RES_CHECK(ret == 0);
  15216. #endif
  15217. return res;
  15218. } /* END test_wc_AesSetKey */
  15219. /*
  15220. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15221. * and wc_AesCbcDecryptWithKey()
  15222. */
  15223. static int test_wc_AesCbcEncryptDecrypt(void)
  15224. {
  15225. int res = TEST_SKIPPED;
  15226. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15227. defined(WOLFSSL_AES_256)
  15228. Aes aes;
  15229. int ret = 0;
  15230. byte key32[] =
  15231. {
  15232. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15233. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15234. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15235. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15236. };
  15237. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  15238. {
  15239. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15240. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15241. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  15242. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  15243. };
  15244. byte iv[] = "1234567890abcdef";
  15245. byte enc[sizeof(vector)];
  15246. byte dec[sizeof(vector)];
  15247. int cbcE = WOLFSSL_FATAL_ERROR;
  15248. int cbcD = WOLFSSL_FATAL_ERROR;
  15249. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15250. byte dec2[sizeof(vector)];
  15251. /* Init stack variables. */
  15252. XMEMSET(enc, 0, sizeof(enc));
  15253. XMEMSET(dec, 0, sizeof(vector));
  15254. XMEMSET(dec2, 0, sizeof(vector));
  15255. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15256. if (ret != 0)
  15257. return ret;
  15258. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15259. if (ret == 0) {
  15260. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15261. if (ret == 0) {
  15262. /* Re init for decrypt and set flag. */
  15263. cbcE = 0;
  15264. wc_AesFree(&aes);
  15265. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15266. iv, AES_DECRYPTION);
  15267. }
  15268. if (ret == 0) {
  15269. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15270. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15271. ret = WOLFSSL_FATAL_ERROR;
  15272. }
  15273. else {
  15274. /* Set flag. */
  15275. cbcD = 0;
  15276. }
  15277. }
  15278. }
  15279. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15280. if (ret == 0 || cbcE == 0) {
  15281. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15282. key32, sizeof(key32)/sizeof(byte), iv);
  15283. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15284. cbcDWK = 0;
  15285. }
  15286. }
  15287. /* Pass in bad args */
  15288. if (cbcE == 0) {
  15289. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15290. if (cbcE == BAD_FUNC_ARG) {
  15291. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15292. }
  15293. if (cbcE == BAD_FUNC_ARG) {
  15294. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15295. }
  15296. if (cbcE == BAD_FUNC_ARG) {
  15297. cbcE = 0;
  15298. }
  15299. else {
  15300. cbcE = WOLFSSL_FATAL_ERROR;
  15301. }
  15302. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15303. if (cbcE == 0) {
  15304. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15305. }
  15306. if (cbcE == BAD_LENGTH_E) {
  15307. cbcE = 0;
  15308. }
  15309. else {
  15310. cbcE = WOLFSSL_FATAL_ERROR;
  15311. }
  15312. #endif
  15313. }
  15314. if (cbcE == 0) {
  15315. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15316. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15317. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15318. "mode (v2), skip test");
  15319. #else
  15320. /* Test passing in size of 0 */
  15321. XMEMSET(enc, 0, sizeof(enc));
  15322. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15323. if (cbcE == 0) {
  15324. /* Check enc was not modified */
  15325. int i;
  15326. for (i = 0; i < (int)sizeof(enc); i++)
  15327. cbcE |= enc[i];
  15328. }
  15329. #endif
  15330. }
  15331. if (cbcE != 0) {
  15332. wc_AesFree(&aes);
  15333. return TEST_FAIL;
  15334. }
  15335. if (cbcD == 0) {
  15336. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15337. if (cbcD == BAD_FUNC_ARG) {
  15338. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15339. }
  15340. if (cbcD == BAD_FUNC_ARG) {
  15341. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15342. }
  15343. if (cbcD == BAD_FUNC_ARG) {
  15344. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15345. }
  15346. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15347. if (cbcD == BAD_LENGTH_E) {
  15348. cbcD = 0;
  15349. }
  15350. else {
  15351. cbcD = WOLFSSL_FATAL_ERROR;
  15352. }
  15353. #else
  15354. if (cbcD == BAD_FUNC_ARG) {
  15355. cbcD = 0;
  15356. }
  15357. else {
  15358. cbcD = WOLFSSL_FATAL_ERROR;
  15359. }
  15360. #endif
  15361. }
  15362. if (cbcD == 0) {
  15363. /* Test passing in size of 0 */
  15364. XMEMSET(dec, 0, sizeof(dec));
  15365. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15366. if (cbcD == 0) {
  15367. /* Check dec was not modified */
  15368. int i;
  15369. for (i = 0; i < (int)sizeof(dec); i++)
  15370. cbcD |= dec[i];
  15371. }
  15372. }
  15373. if (cbcD != 0) {
  15374. wc_AesFree(&aes);
  15375. return TEST_FAIL;
  15376. }
  15377. if (cbcDWK == 0) {
  15378. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15379. key32, sizeof(key32)/sizeof(byte), iv);
  15380. if (cbcDWK == BAD_FUNC_ARG) {
  15381. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15382. key32, sizeof(key32)/sizeof(byte), iv);
  15383. }
  15384. if (cbcDWK == BAD_FUNC_ARG) {
  15385. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15386. NULL, sizeof(key32)/sizeof(byte), iv);
  15387. }
  15388. if (cbcDWK == BAD_FUNC_ARG) {
  15389. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15390. key32, sizeof(key32)/sizeof(byte), NULL);
  15391. }
  15392. if (cbcDWK == BAD_FUNC_ARG) {
  15393. cbcDWK = 0;
  15394. }
  15395. else {
  15396. cbcDWK = WOLFSSL_FATAL_ERROR;
  15397. }
  15398. }
  15399. wc_AesFree(&aes);
  15400. res = TEST_RES_CHECK(cbcDWK == 0);
  15401. #endif
  15402. return res;
  15403. } /* END test_wc_AesCbcEncryptDecrypt */
  15404. /*
  15405. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15406. */
  15407. static int test_wc_AesCtrEncryptDecrypt(void)
  15408. {
  15409. int res = TEST_SKIPPED;
  15410. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15411. Aes aesEnc, aesDec;
  15412. int ret = 0;
  15413. byte key32[] =
  15414. {
  15415. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15416. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15417. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15418. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15419. };
  15420. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15421. {
  15422. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15423. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15424. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15425. };
  15426. byte iv[] = "1234567890abcdef";
  15427. byte enc[AES_BLOCK_SIZE * 2];
  15428. byte dec[AES_BLOCK_SIZE * 2];
  15429. /* Init stack variables. */
  15430. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15431. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15432. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15433. if (ret != 0)
  15434. return ret;
  15435. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15436. if (ret != 0) {
  15437. wc_AesFree(&aesEnc);
  15438. return ret;
  15439. }
  15440. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15441. iv, AES_ENCRYPTION);
  15442. if (ret == 0) {
  15443. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15444. sizeof(vector)/sizeof(byte));
  15445. if (ret == 0) {
  15446. /* Decrypt with wc_AesCtrEncrypt() */
  15447. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  15448. iv, AES_ENCRYPTION);
  15449. }
  15450. if (ret == 0) {
  15451. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  15452. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  15453. ret = WOLFSSL_FATAL_ERROR;
  15454. }
  15455. }
  15456. }
  15457. /* Test bad args. */
  15458. if (ret == 0) {
  15459. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  15460. if (ret == BAD_FUNC_ARG) {
  15461. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  15462. }
  15463. if (ret == BAD_FUNC_ARG) {
  15464. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  15465. }
  15466. if (ret == BAD_FUNC_ARG) {
  15467. ret = 0;
  15468. }
  15469. else {
  15470. ret = WOLFSSL_FATAL_ERROR;
  15471. }
  15472. }
  15473. wc_AesFree(&aesEnc);
  15474. wc_AesFree(&aesDec);
  15475. res = TEST_RES_CHECK(ret == 0);
  15476. #endif
  15477. return res;
  15478. } /* END test_wc_AesCtrEncryptDecrypt */
  15479. /*
  15480. * test function for wc_AesGcmSetKey()
  15481. */
  15482. static int test_wc_AesGcmSetKey(void)
  15483. {
  15484. int res = TEST_SKIPPED;
  15485. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15486. Aes aes;
  15487. int ret = 0;
  15488. #ifdef WOLFSSL_AES_128
  15489. byte key16[] =
  15490. {
  15491. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15492. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15493. };
  15494. #endif
  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, 0x65
  15516. };
  15517. byte badKey24[] =
  15518. {
  15519. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15520. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15521. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15522. };
  15523. byte badKey32[] =
  15524. {
  15525. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15526. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15527. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15528. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15529. };
  15530. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15531. if (ret != 0)
  15532. return ret;
  15533. #ifdef WOLFSSL_AES_128
  15534. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  15535. #endif
  15536. #ifdef WOLFSSL_AES_192
  15537. if (ret == 0) {
  15538. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  15539. }
  15540. #endif
  15541. #ifdef WOLFSSL_AES_256
  15542. if (ret == 0) {
  15543. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15544. }
  15545. #endif
  15546. /* Pass in bad args. */
  15547. if (ret == 0) {
  15548. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  15549. if (ret == BAD_FUNC_ARG) {
  15550. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  15551. }
  15552. if (ret == BAD_FUNC_ARG) {
  15553. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  15554. }
  15555. if (ret == BAD_FUNC_ARG) {
  15556. ret = 0;
  15557. }
  15558. else {
  15559. ret = WOLFSSL_FATAL_ERROR;
  15560. }
  15561. }
  15562. wc_AesFree(&aes);
  15563. res = TEST_RES_CHECK(ret == 0);
  15564. #endif
  15565. return res;
  15566. } /* END test_wc_AesGcmSetKey */
  15567. /*
  15568. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15569. */
  15570. static int test_wc_AesGcmEncryptDecrypt(void)
  15571. {
  15572. int res = TEST_SKIPPED;
  15573. /* WOLFSSL_AFALG requires 12 byte IV */
  15574. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15575. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15576. Aes aes;
  15577. byte key32[] =
  15578. {
  15579. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15580. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15581. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15582. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15583. };
  15584. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15585. {
  15586. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15587. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15588. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15589. };
  15590. const byte a[] =
  15591. {
  15592. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15593. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15594. 0xab, 0xad, 0xda, 0xd2
  15595. };
  15596. byte iv[] = "1234567890a";
  15597. byte longIV[] = "1234567890abcdefghij";
  15598. byte enc[sizeof(vector)];
  15599. byte resultT[AES_BLOCK_SIZE];
  15600. byte dec[sizeof(vector)];
  15601. int gcmD = WOLFSSL_FATAL_ERROR;
  15602. int gcmE = WOLFSSL_FATAL_ERROR;
  15603. int ret = 0;
  15604. /* Init stack variables. */
  15605. XMEMSET(enc, 0, sizeof(vector));
  15606. XMEMSET(dec, 0, sizeof(vector));
  15607. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15608. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15609. if (ret != 0)
  15610. return ret;
  15611. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15612. if (ret == 0) {
  15613. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  15614. iv, sizeof(iv)/sizeof(byte), resultT,
  15615. sizeof(resultT), a, sizeof(a));
  15616. }
  15617. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  15618. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  15619. iv, sizeof(iv)/sizeof(byte), resultT,
  15620. sizeof(resultT), a, sizeof(a));
  15621. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  15622. gcmD = WOLFSSL_FATAL_ERROR;
  15623. }
  15624. }
  15625. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15626. if (gcmE == 0) {
  15627. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  15628. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  15629. a, sizeof(a));
  15630. if (gcmE == BAD_FUNC_ARG) {
  15631. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15632. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15633. resultT, sizeof(resultT) + 1, a, sizeof(a));
  15634. }
  15635. if (gcmE == BAD_FUNC_ARG) {
  15636. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15637. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15638. resultT, sizeof(resultT) - 5, a, sizeof(a));
  15639. }
  15640. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15641. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15642. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15643. /* FIPS does not check the lower bound of ivSz */
  15644. #else
  15645. if (gcmE == BAD_FUNC_ARG) {
  15646. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15647. sizeof(vector), iv, 0,
  15648. resultT, sizeof(resultT), a, sizeof(a));
  15649. }
  15650. #endif
  15651. if (gcmE == BAD_FUNC_ARG) {
  15652. gcmE = 0;
  15653. }
  15654. else {
  15655. gcmE = WOLFSSL_FATAL_ERROR;
  15656. }
  15657. }
  15658. /* This case is now considered good. Long IVs are now allowed.
  15659. * Except for the original FIPS release, it still has an upper
  15660. * bound on the IV length. */
  15661. #if (!defined(HAVE_FIPS) || \
  15662. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15663. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15664. if (gcmE == 0) {
  15665. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15666. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  15667. a, sizeof(a));
  15668. }
  15669. #else
  15670. (void)longIV;
  15671. #endif /* Old FIPS */
  15672. /* END wc_AesGcmEncrypt */
  15673. if (gcmE != 0) {
  15674. wc_AesFree(&aes);
  15675. return TEST_FAIL;
  15676. }
  15677. #ifdef HAVE_AES_DECRYPT
  15678. if (gcmD == 0) {
  15679. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  15680. iv, sizeof(iv)/sizeof(byte), resultT,
  15681. sizeof(resultT), a, sizeof(a));
  15682. if (gcmD == BAD_FUNC_ARG) {
  15683. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  15684. iv, sizeof(iv)/sizeof(byte), resultT,
  15685. sizeof(resultT), a, sizeof(a));
  15686. }
  15687. if (gcmD == BAD_FUNC_ARG) {
  15688. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  15689. iv, sizeof(iv)/sizeof(byte), resultT,
  15690. sizeof(resultT), a, sizeof(a));
  15691. }
  15692. if (gcmD == BAD_FUNC_ARG) {
  15693. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15694. NULL, sizeof(iv)/sizeof(byte), resultT,
  15695. sizeof(resultT), a, sizeof(a));
  15696. }
  15697. if (gcmD == BAD_FUNC_ARG) {
  15698. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15699. iv, sizeof(iv)/sizeof(byte), NULL,
  15700. sizeof(resultT), a, sizeof(a));
  15701. }
  15702. if (gcmD == BAD_FUNC_ARG) {
  15703. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15704. iv, sizeof(iv)/sizeof(byte), resultT,
  15705. sizeof(resultT) + 1, a, sizeof(a));
  15706. }
  15707. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15708. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15709. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15710. /* FIPS does not check the lower bound of ivSz */
  15711. #else
  15712. if (gcmD == BAD_FUNC_ARG) {
  15713. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15714. iv, 0, resultT,
  15715. sizeof(resultT), a, sizeof(a));
  15716. }
  15717. #endif
  15718. if (gcmD == BAD_FUNC_ARG) {
  15719. gcmD = 0;
  15720. }
  15721. else {
  15722. gcmD = WOLFSSL_FATAL_ERROR;
  15723. }
  15724. res = TEST_RES_CHECK(gcmD == 0);
  15725. } /* END wc_AesGcmDecrypt */
  15726. #endif /* HAVE_AES_DECRYPT */
  15727. wc_AesFree(&aes);
  15728. #endif
  15729. return res;
  15730. } /* END test_wc_AesGcmEncryptDecrypt */
  15731. /*
  15732. * unit test for wc_GmacSetKey()
  15733. */
  15734. static int test_wc_GmacSetKey(void)
  15735. {
  15736. int res = TEST_SKIPPED;
  15737. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15738. Gmac gmac;
  15739. byte key16[] =
  15740. {
  15741. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15742. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15743. };
  15744. #ifdef WOLFSSL_AES_192
  15745. byte key24[] =
  15746. {
  15747. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15748. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15749. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15750. };
  15751. #endif
  15752. #ifdef WOLFSSL_AES_256
  15753. byte key32[] =
  15754. {
  15755. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15756. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15757. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15758. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15759. };
  15760. #endif
  15761. byte badKey16[] =
  15762. {
  15763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15764. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15765. };
  15766. byte badKey24[] =
  15767. {
  15768. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15769. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15770. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15771. };
  15772. byte badKey32[] =
  15773. {
  15774. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15775. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15776. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15777. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15778. };
  15779. int ret = 0;
  15780. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15781. if (ret != 0)
  15782. return ret;
  15783. #ifdef WOLFSSL_AES_128
  15784. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15785. #endif
  15786. #ifdef WOLFSSL_AES_192
  15787. if (ret == 0) {
  15788. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15789. }
  15790. #endif
  15791. #ifdef WOLFSSL_AES_256
  15792. if (ret == 0) {
  15793. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15794. }
  15795. #endif
  15796. /* Pass in bad args. */
  15797. if (ret == 0) {
  15798. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15799. if (ret == BAD_FUNC_ARG) {
  15800. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15801. }
  15802. if (ret == BAD_FUNC_ARG) {
  15803. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15804. }
  15805. if (ret == BAD_FUNC_ARG) {
  15806. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15807. }
  15808. if (ret == BAD_FUNC_ARG) {
  15809. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15810. }
  15811. if (ret == BAD_FUNC_ARG) {
  15812. ret = 0;
  15813. }
  15814. else {
  15815. ret = WOLFSSL_FATAL_ERROR;
  15816. }
  15817. }
  15818. wc_AesFree(&gmac.aes);
  15819. res = TEST_RES_CHECK(ret == 0);
  15820. #endif
  15821. return res;
  15822. } /* END test_wc_GmacSetKey */
  15823. /*
  15824. * unit test for wc_GmacUpdate
  15825. */
  15826. static int test_wc_GmacUpdate(void)
  15827. {
  15828. int res = TEST_SKIPPED;
  15829. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15830. Gmac gmac;
  15831. #ifdef WOLFSSL_AES_128
  15832. const byte key16[] =
  15833. {
  15834. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15835. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15836. };
  15837. #endif
  15838. #ifdef WOLFSSL_AES_192
  15839. byte key24[] =
  15840. {
  15841. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15842. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15843. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15844. };
  15845. #endif
  15846. #ifdef WOLFSSL_AES_256
  15847. byte key32[] =
  15848. {
  15849. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15850. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15851. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15852. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15853. };
  15854. #endif
  15855. #ifdef WOLFSSL_AES_128
  15856. const byte authIn[] =
  15857. {
  15858. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15859. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15860. };
  15861. #endif
  15862. #ifdef WOLFSSL_AES_192
  15863. const byte authIn2[] =
  15864. {
  15865. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15866. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15867. };
  15868. #endif
  15869. const byte authIn3[] =
  15870. {
  15871. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15872. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15873. };
  15874. #ifdef WOLFSSL_AES_128
  15875. const byte tag1[] = /* Known. */
  15876. {
  15877. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15878. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15879. };
  15880. #endif
  15881. #ifdef WOLFSSL_AES_192
  15882. const byte tag2[] = /* Known */
  15883. {
  15884. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15885. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15886. };
  15887. #endif
  15888. const byte tag3[] = /* Known */
  15889. {
  15890. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15891. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15892. };
  15893. #ifdef WOLFSSL_AES_128
  15894. const byte iv[] =
  15895. {
  15896. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15897. 0xe2, 0x8c, 0x8f, 0x16
  15898. };
  15899. #endif
  15900. #ifdef WOLFSSL_AES_192
  15901. const byte iv2[] =
  15902. {
  15903. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15904. 0x7e, 0x1a, 0x6f, 0xbc
  15905. };
  15906. #endif
  15907. const byte iv3[] =
  15908. {
  15909. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15910. 0xc3, 0xfb, 0x6c, 0x8a
  15911. };
  15912. byte tagOut[16];
  15913. byte tagOut2[24];
  15914. byte tagOut3[32];
  15915. int ret = 0;
  15916. /* Init stack variables. */
  15917. XMEMSET(tagOut, 0, sizeof(tagOut));
  15918. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15919. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15920. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15921. if (ret != 0)
  15922. return ret;
  15923. #ifdef WOLFSSL_AES_128
  15924. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15925. if (ret == 0) {
  15926. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15927. tagOut, sizeof(tag1));
  15928. if (ret == 0) {
  15929. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15930. }
  15931. wc_AesFree(&gmac.aes);
  15932. }
  15933. #endif
  15934. #ifdef WOLFSSL_AES_192
  15935. if (ret == 0) {
  15936. XMEMSET(&gmac, 0, sizeof(Gmac));
  15937. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15938. }
  15939. if (ret == 0) {
  15940. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15941. sizeof(authIn2), tagOut2, sizeof(tag2));
  15942. }
  15943. if (ret == 0) {
  15944. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15945. wc_AesFree(&gmac.aes);
  15946. }
  15947. #endif
  15948. #ifdef WOLFSSL_AES_256
  15949. if (ret == 0) {
  15950. XMEMSET(&gmac, 0, sizeof(Gmac));
  15951. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15952. }
  15953. if (ret == 0) {
  15954. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15955. sizeof(authIn3), tagOut3, sizeof(tag3));
  15956. }
  15957. if (ret == 0) {
  15958. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15959. }
  15960. #endif
  15961. /*Pass bad args. */
  15962. if (ret == 0) {
  15963. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15964. sizeof(authIn3), tagOut3, sizeof(tag3));
  15965. if (ret == BAD_FUNC_ARG) {
  15966. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15967. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15968. }
  15969. if (ret == BAD_FUNC_ARG) {
  15970. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15971. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15972. }
  15973. if (ret == BAD_FUNC_ARG) {
  15974. ret = 0;
  15975. }
  15976. else {
  15977. ret = WOLFSSL_FATAL_ERROR;
  15978. }
  15979. }
  15980. wc_AesFree(&gmac.aes);
  15981. res = TEST_RES_CHECK(ret == 0);
  15982. #endif
  15983. return res;
  15984. } /* END test_wc_GmacUpdate */
  15985. /*
  15986. * testing wc_CamelliaSetKey
  15987. */
  15988. static int test_wc_CamelliaSetKey(void)
  15989. {
  15990. int res = TEST_SKIPPED;
  15991. #ifdef HAVE_CAMELLIA
  15992. Camellia camellia;
  15993. /*128-bit key*/
  15994. static const byte key16[] =
  15995. {
  15996. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15997. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15998. };
  15999. /* 192-bit key */
  16000. static const byte key24[] =
  16001. {
  16002. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16003. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16004. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16005. };
  16006. /* 256-bit key */
  16007. static const byte key32[] =
  16008. {
  16009. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16010. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16011. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16012. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16013. };
  16014. static const byte iv[] =
  16015. {
  16016. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16017. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16018. };
  16019. int ret = 0;
  16020. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  16021. if (ret == 0) {
  16022. ret = wc_CamelliaSetKey(&camellia, key16,
  16023. (word32)sizeof(key16), NULL);
  16024. if (ret == 0) {
  16025. ret = wc_CamelliaSetKey(&camellia, key24,
  16026. (word32)sizeof(key24), iv);
  16027. }
  16028. if (ret == 0) {
  16029. ret = wc_CamelliaSetKey(&camellia, key24,
  16030. (word32)sizeof(key24), NULL);
  16031. }
  16032. if (ret == 0) {
  16033. ret = wc_CamelliaSetKey(&camellia, key32,
  16034. (word32)sizeof(key32), iv);
  16035. }
  16036. if (ret == 0) {
  16037. ret = wc_CamelliaSetKey(&camellia, key32,
  16038. (word32)sizeof(key32), NULL);
  16039. }
  16040. }
  16041. /* Bad args. */
  16042. if (ret == 0) {
  16043. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  16044. if (ret != BAD_FUNC_ARG) {
  16045. ret = WOLFSSL_FATAL_ERROR;
  16046. }
  16047. else {
  16048. ret = 0;
  16049. }
  16050. } /* END bad args. */
  16051. res = TEST_RES_CHECK(ret == 0);
  16052. #endif
  16053. return res;
  16054. } /* END test_wc_CammeliaSetKey */
  16055. /*
  16056. * Testing wc_CamelliaSetIV()
  16057. */
  16058. static int test_wc_CamelliaSetIV(void)
  16059. {
  16060. int res = TEST_SKIPPED;
  16061. #ifdef HAVE_CAMELLIA
  16062. Camellia camellia;
  16063. static const byte iv[] =
  16064. {
  16065. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16066. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16067. };
  16068. int ret = 0;
  16069. ret = wc_CamelliaSetIV(&camellia, iv);
  16070. if (ret == 0) {
  16071. ret = wc_CamelliaSetIV(&camellia, NULL);
  16072. }
  16073. /* Bad args. */
  16074. if (ret == 0) {
  16075. ret = wc_CamelliaSetIV(NULL, NULL);
  16076. if (ret != BAD_FUNC_ARG) {
  16077. ret = WOLFSSL_FATAL_ERROR;
  16078. }
  16079. else {
  16080. ret = 0;
  16081. }
  16082. }
  16083. res = TEST_RES_CHECK(ret == 0);
  16084. #endif
  16085. return res;
  16086. } /*END test_wc_CamelliaSetIV*/
  16087. /*
  16088. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16089. */
  16090. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16091. {
  16092. int res = TEST_SKIPPED;
  16093. #ifdef HAVE_CAMELLIA
  16094. Camellia camellia;
  16095. static const byte key24[] =
  16096. {
  16097. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16098. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16099. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16100. };
  16101. static const byte iv[] =
  16102. {
  16103. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16104. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16105. };
  16106. static const byte plainT[] =
  16107. {
  16108. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16109. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16110. };
  16111. byte enc[sizeof(plainT)];
  16112. byte dec[sizeof(enc)];
  16113. int camE = WOLFSSL_FATAL_ERROR;
  16114. int camD = WOLFSSL_FATAL_ERROR;
  16115. int ret = 0;
  16116. /*Init stack variables.*/
  16117. XMEMSET(enc, 0, 16);
  16118. XMEMSET(enc, 0, 16);
  16119. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  16120. if (ret == 0) {
  16121. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  16122. if (ret == 0) {
  16123. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  16124. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16125. ret = WOLFSSL_FATAL_ERROR;
  16126. }
  16127. }
  16128. }
  16129. /* Pass bad args. */
  16130. if (ret == 0) {
  16131. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  16132. if (camE == BAD_FUNC_ARG) {
  16133. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  16134. }
  16135. if (camE == BAD_FUNC_ARG) {
  16136. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  16137. }
  16138. if (camE == BAD_FUNC_ARG) {
  16139. camE = 0;
  16140. }
  16141. else {
  16142. camE = WOLFSSL_FATAL_ERROR;
  16143. }
  16144. }
  16145. if (camE != 0) {
  16146. return TEST_FAIL;
  16147. }
  16148. if (ret == 0) {
  16149. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  16150. if (camD == BAD_FUNC_ARG) {
  16151. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  16152. }
  16153. if (camD == BAD_FUNC_ARG) {
  16154. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  16155. }
  16156. if (camD == BAD_FUNC_ARG) {
  16157. camD = 0;
  16158. }
  16159. else {
  16160. camD = WOLFSSL_FATAL_ERROR;
  16161. }
  16162. }
  16163. res = TEST_RES_CHECK(camD == 0);
  16164. #endif
  16165. return res;
  16166. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16167. /*
  16168. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16169. */
  16170. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16171. {
  16172. int res = TEST_SKIPPED;
  16173. #ifdef HAVE_CAMELLIA
  16174. Camellia camellia;
  16175. static const byte key24[] =
  16176. {
  16177. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16178. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16179. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16180. };
  16181. static const byte plainT[] =
  16182. {
  16183. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16184. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16185. };
  16186. byte enc[CAMELLIA_BLOCK_SIZE];
  16187. byte dec[CAMELLIA_BLOCK_SIZE];
  16188. int camCbcE = WOLFSSL_FATAL_ERROR;
  16189. int camCbcD = WOLFSSL_FATAL_ERROR;
  16190. int ret = 0;
  16191. /* Init stack variables. */
  16192. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16193. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16194. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16195. if (ret == 0) {
  16196. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16197. if (ret != 0) {
  16198. ret = WOLFSSL_FATAL_ERROR;
  16199. }
  16200. }
  16201. if (ret == 0) {
  16202. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16203. if (ret == 0) {
  16204. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  16205. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16206. ret = WOLFSSL_FATAL_ERROR;
  16207. }
  16208. }
  16209. }
  16210. /* Pass in bad args. */
  16211. if (ret == 0) {
  16212. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16213. if (camCbcE == BAD_FUNC_ARG) {
  16214. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16215. CAMELLIA_BLOCK_SIZE);
  16216. }
  16217. if (camCbcE == BAD_FUNC_ARG) {
  16218. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16219. CAMELLIA_BLOCK_SIZE);
  16220. }
  16221. if (camCbcE == BAD_FUNC_ARG) {
  16222. camCbcE = 0;
  16223. }
  16224. else {
  16225. camCbcE = WOLFSSL_FATAL_ERROR;
  16226. }
  16227. }
  16228. if (camCbcE != 0) {
  16229. return TEST_FAIL;
  16230. }
  16231. if (ret == 0) {
  16232. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  16233. if (camCbcD == BAD_FUNC_ARG) {
  16234. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16235. CAMELLIA_BLOCK_SIZE);
  16236. }
  16237. if (camCbcD == BAD_FUNC_ARG) {
  16238. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16239. CAMELLIA_BLOCK_SIZE);
  16240. }
  16241. if (camCbcD == BAD_FUNC_ARG) {
  16242. camCbcD = 0;
  16243. }
  16244. else {
  16245. camCbcD = WOLFSSL_FATAL_ERROR;
  16246. }
  16247. } /* END bad args. */
  16248. res = TEST_RES_CHECK(camCbcD == 0);
  16249. #endif
  16250. return res;
  16251. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16252. /*
  16253. * Testing wc_Arc4SetKey()
  16254. */
  16255. static int test_wc_Arc4SetKey(void)
  16256. {
  16257. int res = TEST_SKIPPED;
  16258. #ifndef NO_RC4
  16259. Arc4 arc;
  16260. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16261. int keyLen = 8;
  16262. int ret = 0;
  16263. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16264. /* Test bad args. */
  16265. if (ret == 0) {
  16266. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16267. if (ret == BAD_FUNC_ARG)
  16268. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16269. if (ret == BAD_FUNC_ARG)
  16270. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16271. if (ret == BAD_FUNC_ARG)
  16272. ret = WOLFSSL_ERROR_NONE;
  16273. else
  16274. ret = WOLFSSL_FATAL_ERROR;
  16275. } /* END test bad args. */
  16276. res = TEST_RES_CHECK(ret == 0);
  16277. #endif
  16278. return res;
  16279. } /* END test_wc_Arc4SetKey */
  16280. /*
  16281. * Testing wc_Arc4Process for ENC/DEC.
  16282. */
  16283. static int test_wc_Arc4Process(void)
  16284. {
  16285. int res = TEST_SKIPPED;
  16286. #ifndef NO_RC4
  16287. Arc4 enc, dec;
  16288. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16289. int keyLen = 8;
  16290. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16291. byte cipher[8];
  16292. byte plain[8];
  16293. int ret;
  16294. /* Init stack variables */
  16295. XMEMSET(cipher, 0, sizeof(cipher));
  16296. XMEMSET(plain, 0, sizeof(plain));
  16297. /* Use for async. */
  16298. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16299. if (ret == 0) {
  16300. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16301. }
  16302. if (ret == 0) {
  16303. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16304. }
  16305. if (ret == 0) {
  16306. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16307. }
  16308. if (ret == 0) {
  16309. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16310. }
  16311. if (ret == 0) {
  16312. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16313. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16314. ret = WOLFSSL_FATAL_ERROR;
  16315. }
  16316. else {
  16317. ret = 0;
  16318. }
  16319. }
  16320. /* Bad args. */
  16321. if (ret == 0) {
  16322. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16323. if (ret == BAD_FUNC_ARG) {
  16324. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16325. }
  16326. if (ret == BAD_FUNC_ARG) {
  16327. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16328. }
  16329. if (ret == BAD_FUNC_ARG) {
  16330. ret = 0;
  16331. }
  16332. else {
  16333. ret = WOLFSSL_FATAL_ERROR;
  16334. }
  16335. }
  16336. wc_Arc4Free(&enc);
  16337. wc_Arc4Free(&dec);
  16338. res = TEST_RES_CHECK(ret == 0);
  16339. #endif
  16340. return res;
  16341. }/* END test_wc_Arc4Process */
  16342. /*
  16343. * Testing wc_Init RsaKey()
  16344. */
  16345. static int test_wc_InitRsaKey(void)
  16346. {
  16347. int res = TEST_SKIPPED;
  16348. #ifndef NO_RSA
  16349. RsaKey key;
  16350. int ret = 0;
  16351. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16352. /* Test bad args. */
  16353. if (ret == 0) {
  16354. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16355. #ifndef HAVE_USER_RSA
  16356. if (ret == BAD_FUNC_ARG) {
  16357. ret = 0;
  16358. }
  16359. else {
  16360. #else
  16361. if (ret == USER_CRYPTO_ERROR) {
  16362. ret = 0;
  16363. }
  16364. else {
  16365. #endif
  16366. ret = WOLFSSL_FATAL_ERROR;
  16367. }
  16368. } /* end if */
  16369. if (wc_FreeRsaKey(&key) || ret != 0) {
  16370. ret = WOLFSSL_FATAL_ERROR;
  16371. }
  16372. res = TEST_RES_CHECK(ret == 0);
  16373. #endif
  16374. return res;
  16375. } /* END test_wc_InitRsaKey */
  16376. /*
  16377. * Testing wc_RsaPrivateKeyDecode()
  16378. */
  16379. static int test_wc_RsaPrivateKeyDecode(void)
  16380. {
  16381. int res = TEST_SKIPPED;
  16382. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16383. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16384. RsaKey key;
  16385. byte* tmp;
  16386. word32 idx = 0;
  16387. int bytes = 0;
  16388. int ret = 0;
  16389. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16390. if (tmp == NULL) {
  16391. ret = WOLFSSL_FATAL_ERROR;
  16392. }
  16393. if (ret == 0) {
  16394. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16395. }
  16396. if (ret == 0) {
  16397. #ifdef USE_CERT_BUFFERS_1024
  16398. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16399. bytes = sizeof_client_key_der_1024;
  16400. #else
  16401. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16402. bytes = sizeof_client_key_der_2048;
  16403. #endif /* Use cert buffers. */
  16404. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  16405. }
  16406. #ifndef HAVE_USER_RSA
  16407. /* Test bad args. */
  16408. if (ret == 0) {
  16409. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16410. if (ret == BAD_FUNC_ARG) {
  16411. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16412. }
  16413. if (ret == BAD_FUNC_ARG) {
  16414. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16415. }
  16416. if (ret == BAD_FUNC_ARG) {
  16417. ret = 0;
  16418. }
  16419. else {
  16420. ret = WOLFSSL_FATAL_ERROR;
  16421. }
  16422. }
  16423. #else
  16424. /* Test bad args. User RSA. */
  16425. if (ret == 0) {
  16426. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16427. if (ret == USER_CRYPTO_ERROR) {
  16428. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16429. }
  16430. if (ret == USER_CRYPTO_ERROR) {
  16431. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16432. }
  16433. if (ret == USER_CRYPTO_ERROR) {
  16434. ret = 0;
  16435. }
  16436. else {
  16437. ret = WOLFSSL_FATAL_ERROR;
  16438. }
  16439. }
  16440. #endif
  16441. if (tmp != NULL) {
  16442. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16443. }
  16444. if (wc_FreeRsaKey(&key) || ret != 0) {
  16445. ret = WOLFSSL_FATAL_ERROR;
  16446. }
  16447. res = TEST_RES_CHECK(ret == 0);
  16448. #endif
  16449. return res;
  16450. } /* END test_wc_RsaPrivateKeyDecode */
  16451. /*
  16452. * Testing wc_RsaPublicKeyDecode()
  16453. */
  16454. static int test_wc_RsaPublicKeyDecode(void)
  16455. {
  16456. int res = TEST_SKIPPED;
  16457. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16458. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16459. RsaKey keyPub;
  16460. byte* tmp;
  16461. word32 idx = 0;
  16462. int bytes = 0;
  16463. word32 keySz = 0;
  16464. word32 tstKeySz = 0;
  16465. int ret = 0;
  16466. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16467. XFILE f;
  16468. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16469. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16470. byte buf[4096];
  16471. #endif
  16472. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16473. if (tmp == NULL) {
  16474. ret = WOLFSSL_FATAL_ERROR;
  16475. }
  16476. if (ret == 0) {
  16477. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  16478. }
  16479. if (ret == 0) {
  16480. #ifdef USE_CERT_BUFFERS_1024
  16481. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16482. bytes = sizeof_client_keypub_der_1024;
  16483. keySz = 1024;
  16484. #else
  16485. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16486. bytes = sizeof_client_keypub_der_2048;
  16487. keySz = 2048;
  16488. #endif
  16489. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  16490. }
  16491. #ifndef HAVE_USER_RSA
  16492. /* Pass in bad args. */
  16493. if (ret == 0) {
  16494. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16495. if (ret == BAD_FUNC_ARG) {
  16496. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16497. }
  16498. if (ret == BAD_FUNC_ARG) {
  16499. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16500. }
  16501. if (ret == BAD_FUNC_ARG) {
  16502. ret = 0;
  16503. }
  16504. else {
  16505. ret = WOLFSSL_FATAL_ERROR;
  16506. }
  16507. }
  16508. #else
  16509. /* Pass in bad args. */
  16510. if (ret == 0) {
  16511. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16512. if (ret == USER_CRYPTO_ERROR) {
  16513. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16514. }
  16515. if (ret == USER_CRYPTO_ERROR) {
  16516. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16517. }
  16518. if (ret == USER_CRYPTO_ERROR) {
  16519. ret = 0;
  16520. }
  16521. else {
  16522. ret = WOLFSSL_FATAL_ERROR;
  16523. }
  16524. }
  16525. #endif
  16526. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  16527. ret = WOLFSSL_FATAL_ERROR;
  16528. }
  16529. if (ret == 0) {
  16530. /* Test for getting modulus key size */
  16531. idx = 0;
  16532. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16533. &tstKeySz, NULL, NULL);
  16534. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  16535. }
  16536. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16537. f = XFOPEN(rsaPssPubKey, "rb");
  16538. AssertTrue((f != XBADFILE));
  16539. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16540. XFCLOSE(f);
  16541. idx = 0;
  16542. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16543. NULL), 0);
  16544. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  16545. AssertTrue((f != XBADFILE));
  16546. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16547. XFCLOSE(f);
  16548. idx = 0;
  16549. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16550. NULL), 0);
  16551. #endif
  16552. if (tmp != NULL) {
  16553. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16554. }
  16555. res = TEST_RES_CHECK(ret == 0);
  16556. #endif
  16557. return res;
  16558. } /* END test_wc_RsaPublicKeyDecode */
  16559. /*
  16560. * Testing wc_RsaPublicKeyDecodeRaw()
  16561. */
  16562. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16563. {
  16564. int res = TEST_SKIPPED;
  16565. #if !defined(NO_RSA)
  16566. RsaKey key;
  16567. const byte n = 0x23;
  16568. const byte e = 0x03;
  16569. int nSz = sizeof(n);
  16570. int eSz = sizeof(e);
  16571. int ret;
  16572. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16573. if (ret == 0) {
  16574. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  16575. }
  16576. #ifndef HAVE_USER_RSA
  16577. /* Pass in bad args. */
  16578. if (ret == 0) {
  16579. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16580. if (ret == BAD_FUNC_ARG) {
  16581. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16582. }
  16583. if (ret == BAD_FUNC_ARG) {
  16584. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16585. }
  16586. if (ret == BAD_FUNC_ARG) {
  16587. ret = 0;
  16588. }
  16589. else {
  16590. ret = WOLFSSL_FATAL_ERROR;
  16591. }
  16592. }
  16593. #else
  16594. /* Pass in bad args. User RSA. */
  16595. if (ret == 0) {
  16596. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16597. if (ret == USER_CRYPTO_ERROR) {
  16598. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16599. }
  16600. if (ret == USER_CRYPTO_ERROR) {
  16601. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16602. }
  16603. if (ret == USER_CRYPTO_ERROR) {
  16604. ret = 0;
  16605. }
  16606. else {
  16607. ret = WOLFSSL_FATAL_ERROR;
  16608. }
  16609. }
  16610. #endif
  16611. if (wc_FreeRsaKey(&key) || ret != 0) {
  16612. ret = WOLFSSL_FATAL_ERROR;
  16613. }
  16614. res = TEST_RES_CHECK(ret == 0);
  16615. #endif
  16616. return res;
  16617. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16618. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16619. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16620. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16621. * trying until it gets a probable prime. */
  16622. #ifdef HAVE_FIPS
  16623. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16624. {
  16625. int ret;
  16626. for (;;) {
  16627. ret = wc_MakeRsaKey(key, size, e, rng);
  16628. if (ret != PRIME_GEN_E) break;
  16629. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16630. "trying again.\n");
  16631. }
  16632. return ret;
  16633. }
  16634. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16635. #else
  16636. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16637. #endif
  16638. #endif
  16639. /*
  16640. * Testing wc_MakeRsaKey()
  16641. */
  16642. static int test_wc_MakeRsaKey(void)
  16643. {
  16644. int res = TEST_SKIPPED;
  16645. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16646. RsaKey genKey;
  16647. WC_RNG rng;
  16648. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16649. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16650. int bits = 1024;
  16651. #else
  16652. int bits = 2048;
  16653. #endif
  16654. int ret = 0;
  16655. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16656. if (ret == 0) {
  16657. ret = wc_InitRng(&rng);
  16658. if (ret == 0) {
  16659. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16660. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  16661. ret = WOLFSSL_FATAL_ERROR;
  16662. }
  16663. }
  16664. }
  16665. #ifndef HAVE_USER_RSA
  16666. /* Test bad args. */
  16667. if (ret == 0) {
  16668. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16669. if (ret == BAD_FUNC_ARG) {
  16670. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16671. }
  16672. if (ret == BAD_FUNC_ARG) {
  16673. /* e < 3 */
  16674. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16675. }
  16676. if (ret == BAD_FUNC_ARG) {
  16677. /* e & 1 == 0 */
  16678. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16679. }
  16680. if (ret == BAD_FUNC_ARG) {
  16681. ret = 0;
  16682. }
  16683. else {
  16684. ret = WOLFSSL_FATAL_ERROR;
  16685. }
  16686. }
  16687. #else
  16688. /* Test bad args. */
  16689. if (ret == 0) {
  16690. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16691. if (ret == USER_CRYPTO_ERROR) {
  16692. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16693. }
  16694. if (ret == USER_CRYPTO_ERROR) {
  16695. /* e < 3 */
  16696. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16697. }
  16698. if (ret == USER_CRYPTO_ERROR) {
  16699. /* e & 1 == 0 */
  16700. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16701. }
  16702. if (ret == USER_CRYPTO_ERROR) {
  16703. ret = 0;
  16704. }
  16705. else {
  16706. ret = WOLFSSL_FATAL_ERROR;
  16707. }
  16708. }
  16709. #endif
  16710. if (wc_FreeRng(&rng) || ret != 0) {
  16711. ret = WOLFSSL_FATAL_ERROR;
  16712. }
  16713. res = TEST_RES_CHECK(ret == 0);
  16714. #endif
  16715. return res;
  16716. } /* END test_wc_MakeRsaKey */
  16717. /*
  16718. * Test the bounds checking on the cipher text versus the key modulus.
  16719. * 1. Make a new RSA key.
  16720. * 2. Set c to 1.
  16721. * 3. Decrypt c into k. (error)
  16722. * 4. Copy the key modulus to c and sub 1 from the copy.
  16723. * 5. Decrypt c into k. (error)
  16724. * Valid bounds test cases are covered by all the other RSA tests.
  16725. */
  16726. static int test_RsaDecryptBoundsCheck(void)
  16727. {
  16728. int res = TEST_SKIPPED;
  16729. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16730. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16731. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16732. WC_RNG rng;
  16733. RsaKey key;
  16734. byte flatC[256];
  16735. word32 flatCSz;
  16736. byte out[256];
  16737. word32 outSz = sizeof(out);
  16738. int ret;
  16739. XMEMSET(&rng, 0, sizeof(rng));
  16740. ret = wc_InitRng(&rng);
  16741. if (ret == 0)
  16742. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16743. if (ret == 0) {
  16744. const byte* derKey;
  16745. word32 derKeySz;
  16746. word32 idx = 0;
  16747. #ifdef USE_CERT_BUFFERS_1024
  16748. derKey = server_key_der_1024;
  16749. derKeySz = (word32)sizeof_server_key_der_1024;
  16750. flatCSz = 128;
  16751. #else
  16752. derKey = server_key_der_2048;
  16753. derKeySz = (word32)sizeof_server_key_der_2048;
  16754. flatCSz = 256;
  16755. #endif
  16756. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16757. }
  16758. if (ret == 0) {
  16759. XMEMSET(flatC, 0, flatCSz);
  16760. flatC[flatCSz-1] = 1;
  16761. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16762. RSA_PRIVATE_DECRYPT, &rng);
  16763. if (ret == RSA_OUT_OF_RANGE_E) {
  16764. mp_int c;
  16765. mp_init_copy(&c, &key.n);
  16766. mp_sub_d(&c, 1, &c);
  16767. mp_to_unsigned_bin(&c, flatC);
  16768. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16769. RSA_PRIVATE_DECRYPT, NULL);
  16770. mp_clear(&c);
  16771. }
  16772. if (ret == RSA_OUT_OF_RANGE_E)
  16773. ret = 0;
  16774. else
  16775. ret = WOLFSSL_FATAL_ERROR;
  16776. }
  16777. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16778. ret = WOLFSSL_FATAL_ERROR;
  16779. res = TEST_RES_CHECK(ret == 0);
  16780. #endif
  16781. return res;
  16782. } /* END test_wc_RsaDecryptBoundsCheck */
  16783. /*
  16784. * Testing wc_SetKeyUsage()
  16785. */
  16786. static int test_wc_SetKeyUsage(void)
  16787. {
  16788. int res = TEST_SKIPPED;
  16789. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16790. Cert myCert;
  16791. int ret = 0;
  16792. ret = wc_InitCert(&myCert);
  16793. if (ret == 0) {
  16794. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16795. if (ret == 0) {
  16796. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16797. }
  16798. if (ret == 0) {
  16799. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16800. }
  16801. if (ret == 0) {
  16802. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16803. }
  16804. if (ret == 0) {
  16805. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16806. }
  16807. }
  16808. /* Test bad args. */
  16809. if (ret == 0) {
  16810. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16811. if (ret == BAD_FUNC_ARG) {
  16812. ret = wc_SetKeyUsage(&myCert, NULL);
  16813. }
  16814. if (ret == BAD_FUNC_ARG) {
  16815. ret = wc_SetKeyUsage(&myCert, "");
  16816. }
  16817. if (ret == KEYUSAGE_E) {
  16818. ret = wc_SetKeyUsage(&myCert, ",");
  16819. }
  16820. if (ret == KEYUSAGE_E) {
  16821. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16822. }
  16823. if (ret == KEYUSAGE_E) {
  16824. ret = 0;
  16825. }
  16826. else {
  16827. ret = WOLFSSL_FATAL_ERROR;
  16828. }
  16829. }
  16830. res = TEST_RES_CHECK(ret == 0);
  16831. #endif
  16832. return res;
  16833. } /* END test_wc_SetKeyUsage */
  16834. /*
  16835. * Testing wc_CheckProbablePrime()
  16836. */
  16837. static int test_wc_CheckProbablePrime(void)
  16838. {
  16839. int res = TEST_SKIPPED;
  16840. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16841. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16842. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16843. RsaKey key;
  16844. WC_RNG rng;
  16845. byte e[3];
  16846. word32 eSz = (word32)sizeof(e);
  16847. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16848. word32 nSz = (word32)sizeof(n);
  16849. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16850. word32 dSz = (word32)sizeof(d);
  16851. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16852. word32 pSz = (word32)sizeof(p);
  16853. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16854. word32 qSz = (word32)sizeof(q);
  16855. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16856. int ret = 0;
  16857. int* isPrime;
  16858. int test[5];
  16859. isPrime = test;
  16860. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16861. if (ret == 0) {
  16862. ret = wc_InitRng(&rng);
  16863. }
  16864. if (ret == 0) {
  16865. ret = wc_RsaSetRNG(&key, &rng);
  16866. }
  16867. if (ret == 0) {
  16868. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16869. }
  16870. if (ret == 0) {
  16871. PRIVATE_KEY_UNLOCK();
  16872. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16873. p, &pSz, q, &qSz);
  16874. PRIVATE_KEY_LOCK();
  16875. }
  16876. /* Bad cases */
  16877. if (ret == 0) {
  16878. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16879. nlen, isPrime);
  16880. if (ret == BAD_FUNC_ARG) {
  16881. ret = 0;
  16882. }
  16883. }
  16884. if (ret == 0) {
  16885. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16886. nlen, isPrime);
  16887. if (ret == BAD_FUNC_ARG) {
  16888. ret = 0;
  16889. }
  16890. }
  16891. if (ret == 0) {
  16892. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16893. nlen, isPrime);
  16894. if (ret == BAD_FUNC_ARG) {
  16895. ret = 0;
  16896. }
  16897. }
  16898. if (ret == 0) {
  16899. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16900. nlen, isPrime);
  16901. if (ret == BAD_FUNC_ARG) {
  16902. ret = 0;
  16903. }
  16904. }
  16905. if (ret == 0) {
  16906. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16907. nlen, isPrime);
  16908. if (ret == BAD_FUNC_ARG) {
  16909. ret = 0;
  16910. }
  16911. }
  16912. if (ret == 0) {
  16913. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16914. nlen, isPrime);
  16915. if (ret == BAD_FUNC_ARG) {
  16916. ret = 0;
  16917. }
  16918. }
  16919. if (ret == 0) {
  16920. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16921. nlen, isPrime);
  16922. if (ret == BAD_FUNC_ARG) {
  16923. ret = 0;
  16924. }
  16925. }
  16926. /* Good case */
  16927. if (ret == 0) {
  16928. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16929. nlen, isPrime);
  16930. }
  16931. wc_FreeRsaKey(&key);
  16932. wc_FreeRng(&rng);
  16933. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16934. res = TEST_RES_CHECK(ret == 0);
  16935. #endif
  16936. return res;
  16937. } /* END test_wc_CheckProbablePrime */
  16938. /*
  16939. * Testing wc_RsaPSS_Verify()
  16940. */
  16941. static int test_wc_RsaPSS_Verify(void)
  16942. {
  16943. int res = TEST_SKIPPED;
  16944. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16945. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16946. RsaKey key;
  16947. WC_RNG rng;
  16948. int sz = 256;
  16949. byte* pt;
  16950. const char* szMessage = "This is the string to be signed";
  16951. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16952. unsigned char pDecrypted[2048/8];
  16953. word32 outLen = sizeof(pDecrypted);
  16954. int ret = 0;
  16955. pt = pDecrypted;
  16956. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16957. if (ret == 0) {
  16958. ret = wc_InitRng(&rng);
  16959. }
  16960. if (ret == 0) {
  16961. ret = wc_RsaSetRNG(&key, &rng);
  16962. }
  16963. if (ret == 0) {
  16964. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16965. }
  16966. if (ret == 0) {
  16967. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16968. pSignature, sizeof(pSignature),
  16969. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16970. if (ret > 0) {
  16971. sz = ret;
  16972. ret = 0;
  16973. }
  16974. }
  16975. /* Bad cases */
  16976. if (ret == 0) {
  16977. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16978. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16979. if (ret == BAD_FUNC_ARG) {
  16980. ret = 0;
  16981. }
  16982. }
  16983. if (ret == 0) {
  16984. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16985. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16986. if (ret == BAD_FUNC_ARG) {
  16987. ret = 0;
  16988. }
  16989. }
  16990. if (ret == 0) {
  16991. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16992. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16993. if (ret == BAD_FUNC_ARG) {
  16994. ret = 0;
  16995. }
  16996. }
  16997. if (ret == 0) {
  16998. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16999. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17000. if (ret == BAD_FUNC_ARG) {
  17001. ret = 0;
  17002. }
  17003. }
  17004. /* Good case */
  17005. if (ret == 0) {
  17006. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17007. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17008. if (ret > 0) {
  17009. ret = 0;
  17010. }
  17011. }
  17012. wc_FreeRsaKey(&key);
  17013. wc_FreeRng(&rng);
  17014. res = TEST_RES_CHECK(ret == 0);
  17015. #endif
  17016. return res;
  17017. } /* END test_wc_RsaPSS_Verify */
  17018. /*
  17019. * Testing wc_RsaPSS_VerifyCheck()
  17020. */
  17021. static int test_wc_RsaPSS_VerifyCheck(void)
  17022. {
  17023. int res = TEST_SKIPPED;
  17024. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17025. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17026. RsaKey key;
  17027. WC_RNG rng;
  17028. int sz = 256; /* 2048/8 */
  17029. byte* pt;
  17030. byte digest[32];
  17031. word32 digestSz = sizeof(digest);
  17032. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17033. word32 pSignatureSz = sizeof(pSignature);
  17034. unsigned char pDecrypted[2048/8];
  17035. word32 outLen = sizeof(pDecrypted);
  17036. int ret = 0;
  17037. pt = pDecrypted;
  17038. XMEMSET(digest, 0, sizeof(digest));
  17039. XMEMSET(pSignature, 0, sizeof(pSignature));
  17040. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17041. if (ret == 0) {
  17042. ret = wc_InitRng(&rng);
  17043. }
  17044. if (ret == 0) {
  17045. ret = wc_RsaSetRNG(&key, &rng);
  17046. }
  17047. if (ret == 0) {
  17048. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17049. }
  17050. if (ret == 0) {
  17051. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17052. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17053. }
  17054. if (ret == 0) {
  17055. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17056. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17057. if (ret > 0) {
  17058. sz = ret;
  17059. ret = 0;
  17060. }
  17061. }
  17062. /* Bad cases */
  17063. if (ret == 0) {
  17064. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  17065. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17066. if (ret == BAD_FUNC_ARG) {
  17067. ret = 0;
  17068. }
  17069. }
  17070. if (ret == 0) {
  17071. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  17072. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17073. if (ret == BAD_FUNC_ARG) {
  17074. ret = 0;
  17075. }
  17076. }
  17077. if (ret == 0) {
  17078. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  17079. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17080. if (ret == BAD_FUNC_ARG) {
  17081. ret = 0;
  17082. }
  17083. }
  17084. if (ret == 0) {
  17085. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  17086. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17087. if (ret == BAD_FUNC_ARG) {
  17088. ret = 0;
  17089. }
  17090. }
  17091. /* Good case */
  17092. if (ret == 0) {
  17093. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  17094. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17095. if (ret > 0) {
  17096. ret = 0;
  17097. }
  17098. }
  17099. wc_FreeRsaKey(&key);
  17100. wc_FreeRng(&rng);
  17101. res = TEST_RES_CHECK(ret == 0);
  17102. #endif
  17103. return res;
  17104. } /* END test_wc_RsaPSS_VerifyCheck */
  17105. /*
  17106. * Testing wc_RsaPSS_VerifyCheckInline()
  17107. */
  17108. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17109. {
  17110. int res = TEST_SKIPPED;
  17111. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17112. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17113. RsaKey key;
  17114. WC_RNG rng;
  17115. int sz = 256;
  17116. byte* pt;
  17117. byte digest[32];
  17118. word32 digestSz = sizeof(digest);
  17119. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17120. unsigned char pDecrypted[2048/8];
  17121. int ret;
  17122. pt = pDecrypted;
  17123. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17124. XMEMSET(digest, 0, sizeof(digest));
  17125. XMEMSET(pSignature, 0, sizeof(pSignature));
  17126. if (ret == 0) {
  17127. ret = wc_InitRng(&rng);
  17128. }
  17129. if (ret == 0) {
  17130. ret = wc_RsaSetRNG(&key, &rng);
  17131. }
  17132. if (ret == 0) {
  17133. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17134. }
  17135. if (ret == 0) {
  17136. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17137. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17138. }
  17139. if (ret == 0) {
  17140. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  17141. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17142. if (ret > 0) {
  17143. sz = ret;
  17144. ret = 0;
  17145. }
  17146. }
  17147. /* Bad Cases */
  17148. if (ret == 0) {
  17149. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  17150. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17151. if (ret == BAD_FUNC_ARG) {
  17152. ret = 0;
  17153. }
  17154. }
  17155. if (ret == 0) {
  17156. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  17157. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17158. if (ret == BAD_FUNC_ARG) {
  17159. ret = 0;
  17160. }
  17161. }
  17162. if (ret == 0) {
  17163. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  17164. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17165. if (ret == BAD_FUNC_ARG) {
  17166. ret = 0;
  17167. }
  17168. }
  17169. if (ret == 0) {
  17170. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17171. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  17172. if (ret == BAD_FUNC_ARG) {
  17173. ret = 0;
  17174. }
  17175. }
  17176. /* Good case */
  17177. if (ret == 0) {
  17178. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17179. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17180. if (ret > 0) {
  17181. ret = 0;
  17182. }
  17183. }
  17184. wc_FreeRsaKey(&key);
  17185. wc_FreeRng(&rng);
  17186. res = TEST_RES_CHECK(ret == 0);
  17187. #endif
  17188. return res;
  17189. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17190. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17191. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17192. {
  17193. (void)flag;
  17194. (void)type;
  17195. (void)file;
  17196. (void)line;
  17197. }
  17198. #endif
  17199. /*
  17200. * Testing wc_LockMutex_ex
  17201. */
  17202. static int test_wc_LockMutex_ex(void)
  17203. {
  17204. int res = TEST_SKIPPED;
  17205. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17206. int ret = 0;
  17207. int flag = CRYPTO_LOCK;
  17208. int type = 0;
  17209. const char* file = "./test-LockMutex_ex.txt";
  17210. int line = 0;
  17211. /* without SetMutexCb */
  17212. ret = wc_LockMutex_ex(flag, type, file, line);
  17213. if (ret == BAD_STATE_E) {
  17214. ret = 0;
  17215. }
  17216. /* with SetMutexCb */
  17217. if (ret == 0) {
  17218. ret = wc_SetMutexCb(sample_mutex_cb);
  17219. if (ret == 0) {
  17220. ret = wc_LockMutex_ex(flag, type, file, line);
  17221. }
  17222. }
  17223. res = TEST_RES_CHECK(ret == 0);
  17224. #endif
  17225. return res;
  17226. }/*End test_wc_LockMutex_ex*/
  17227. /*
  17228. * Testing wc_SetMutexCb
  17229. */
  17230. static int test_wc_SetMutexCb(void)
  17231. {
  17232. int res = TEST_SKIPPED;
  17233. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17234. int ret = wc_SetMutexCb(sample_mutex_cb);
  17235. res = TEST_RES_CHECK(ret == 0);
  17236. #endif
  17237. return res;
  17238. }/*End test_wc_SetMutexCb*/
  17239. /*
  17240. * Testing wc_RsaKeyToDer()
  17241. */
  17242. static int test_wc_RsaKeyToDer(void)
  17243. {
  17244. int res = TEST_SKIPPED;
  17245. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17246. RsaKey genKey;
  17247. WC_RNG rng;
  17248. byte* der;
  17249. int ret = 0;
  17250. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17251. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17252. int bits = 1024;
  17253. word32 derSz = 611;
  17254. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17255. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17256. #else
  17257. int bits = 2048;
  17258. word32 derSz = 1196;
  17259. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17260. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17261. #endif
  17262. XMEMSET(&rng, 0, sizeof(rng));
  17263. XMEMSET(&genKey, 0, sizeof(genKey));
  17264. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17265. if (der == NULL) {
  17266. ret = WOLFSSL_FATAL_ERROR;
  17267. }
  17268. /* Init structures. */
  17269. if (ret == 0) {
  17270. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17271. }
  17272. if (ret == 0) {
  17273. ret = wc_InitRng(&rng);
  17274. }
  17275. /* Make key. */
  17276. if (ret == 0) {
  17277. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17278. if (ret != 0) {
  17279. ret = WOLFSSL_FATAL_ERROR;
  17280. }
  17281. }
  17282. if (ret == 0) {
  17283. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17284. if (ret > 0) {
  17285. ret = 0;
  17286. }
  17287. else {
  17288. ret = WOLFSSL_FATAL_ERROR;
  17289. }
  17290. }
  17291. #ifndef HAVE_USER_RSA
  17292. /* Pass good/bad args. */
  17293. if (ret == 0) {
  17294. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17295. if (ret == BAD_FUNC_ARG) {
  17296. /* Get just the output length */
  17297. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17298. }
  17299. if (ret > 0) {
  17300. /* Try Public Key. */
  17301. genKey.type = 0;
  17302. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17303. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17304. /* Put back to Private Key */
  17305. genKey.type = 1;
  17306. #endif
  17307. }
  17308. if (ret == BAD_FUNC_ARG) {
  17309. ret = 0;
  17310. }
  17311. else {
  17312. ret = WOLFSSL_FATAL_ERROR;
  17313. }
  17314. }
  17315. #else
  17316. /* Pass good/bad args. */
  17317. if (ret == 0) {
  17318. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17319. if (ret == USER_CRYPTO_ERROR) {
  17320. /* Get just the output length */
  17321. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17322. }
  17323. if (ret > 0) {
  17324. /* Try Public Key. */
  17325. genKey.type = 0;
  17326. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17327. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17328. /* Put back to Private Key */
  17329. genKey.type = 1;
  17330. #endif
  17331. }
  17332. if (ret == USER_CRYPTO_ERROR) {
  17333. ret = 0;
  17334. }
  17335. else {
  17336. ret = WOLFSSL_FATAL_ERROR;
  17337. }
  17338. }
  17339. #endif
  17340. if (der != NULL) {
  17341. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17342. }
  17343. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  17344. ret = WOLFSSL_FATAL_ERROR;
  17345. }
  17346. if (wc_FreeRng(&rng) || ret != 0) {
  17347. ret = WOLFSSL_FATAL_ERROR;
  17348. }
  17349. res = TEST_RES_CHECK(ret == 0);
  17350. #endif
  17351. return res;
  17352. } /* END test_wc_RsaKeyToDer */
  17353. /*
  17354. * Testing wc_RsaKeyToPublicDer()
  17355. */
  17356. static int test_wc_RsaKeyToPublicDer(void)
  17357. {
  17358. int res = TEST_SKIPPED;
  17359. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17360. RsaKey key;
  17361. WC_RNG rng;
  17362. byte* der;
  17363. int ret = 0;
  17364. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17365. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17366. int bits = 1024;
  17367. word32 derLen = 162;
  17368. #else
  17369. int bits = 2048;
  17370. word32 derLen = 294;
  17371. #endif
  17372. XMEMSET(&rng, 0, sizeof(rng));
  17373. XMEMSET(&key, 0, sizeof(key));
  17374. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17375. if (der == NULL) {
  17376. ret = WOLFSSL_FATAL_ERROR;
  17377. }
  17378. if (ret == 0) {
  17379. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17380. }
  17381. if (ret == 0) {
  17382. ret = wc_InitRng(&rng);
  17383. }
  17384. if (ret == 0) {
  17385. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17386. }
  17387. if (ret == 0) {
  17388. /* test getting size only */
  17389. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17390. if (ret >= 0)
  17391. ret = 0;
  17392. }
  17393. if (ret == 0) {
  17394. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  17395. if (ret >= 0) {
  17396. ret = 0;
  17397. }
  17398. else {
  17399. ret = WOLFSSL_FATAL_ERROR;
  17400. }
  17401. }
  17402. if (ret == 0) {
  17403. /* test getting size only */
  17404. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  17405. if (ret >= 0)
  17406. ret = 0;
  17407. }
  17408. if (ret == 0) {
  17409. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  17410. if (ret >= 0) {
  17411. ret = 0;
  17412. }
  17413. else {
  17414. ret = WOLFSSL_FATAL_ERROR;
  17415. }
  17416. }
  17417. #ifndef HAVE_USER_RSA
  17418. /* Pass in bad args. */
  17419. if (ret == 0) {
  17420. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17421. if (ret == BAD_FUNC_ARG) {
  17422. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17423. }
  17424. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  17425. ret = 0;
  17426. }
  17427. else {
  17428. ret = WOLFSSL_FATAL_ERROR;
  17429. }
  17430. }
  17431. #else
  17432. /* Pass in bad args. */
  17433. if (ret == 0) {
  17434. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17435. if (ret == USER_CRYPTO_ERROR) {
  17436. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17437. }
  17438. if (ret == USER_CRYPTO_ERROR) {
  17439. ret = 0;
  17440. }
  17441. else {
  17442. ret = WOLFSSL_FATAL_ERROR;
  17443. }
  17444. }
  17445. #endif
  17446. if (der != NULL) {
  17447. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17448. }
  17449. if (wc_FreeRsaKey(&key) || ret != 0) {
  17450. ret = WOLFSSL_FATAL_ERROR;
  17451. }
  17452. if (wc_FreeRng(&rng) || ret != 0) {
  17453. ret = WOLFSSL_FATAL_ERROR;
  17454. }
  17455. res = TEST_RES_CHECK(ret == 0);
  17456. #endif
  17457. return res;
  17458. } /* END test_wc_RsaKeyToPublicDer */
  17459. /*
  17460. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17461. */
  17462. static int test_wc_RsaPublicEncryptDecrypt(void)
  17463. {
  17464. int res = TEST_SKIPPED;
  17465. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17466. RsaKey key;
  17467. WC_RNG rng;
  17468. int ret = 0;
  17469. const char inStr[] = TEST_STRING;
  17470. const word32 plainLen = (word32)TEST_STRING_SZ;
  17471. const word32 inLen = (word32)TEST_STRING_SZ;
  17472. int bits = TEST_RSA_BITS;
  17473. const word32 cipherLen = TEST_RSA_BYTES;
  17474. word32 cipherLenResult = cipherLen;
  17475. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17476. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17477. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17478. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17479. if (in == NULL || plain == NULL || cipher == NULL) {
  17480. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  17481. return MEMORY_E;
  17482. }
  17483. #endif
  17484. XMEMCPY(in, inStr, inLen);
  17485. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17486. if (ret == 0) {
  17487. ret = wc_InitRng(&rng);
  17488. }
  17489. if (ret == 0) {
  17490. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17491. }
  17492. /* Encrypt. */
  17493. if (ret == 0) {
  17494. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  17495. if (ret >= 0) {
  17496. cipherLenResult = ret;
  17497. ret = 0;
  17498. }
  17499. else {
  17500. ret = WOLFSSL_FATAL_ERROR;
  17501. }
  17502. }
  17503. /* Pass bad args. */
  17504. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  17505. if (ret != 0) {
  17506. return TEST_FAIL;
  17507. }
  17508. /* Decrypt */
  17509. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17510. /* Bind rng */
  17511. if (ret == 0) {
  17512. ret = wc_RsaSetRNG(&key, &rng);
  17513. }
  17514. #endif
  17515. if (ret == 0) {
  17516. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  17517. }
  17518. if (ret >= 0) {
  17519. ret = XMEMCMP(plain, inStr, plainLen);
  17520. }
  17521. /* Pass in bad args. */
  17522. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  17523. WC_FREE_VAR(in, NULL);
  17524. WC_FREE_VAR(plain, NULL);
  17525. WC_FREE_VAR(cipher, NULL);
  17526. if (wc_FreeRsaKey(&key) || ret != 0) {
  17527. ret = WOLFSSL_FATAL_ERROR;
  17528. }
  17529. if (wc_FreeRng(&rng) || ret != 0) {
  17530. ret = WOLFSSL_FATAL_ERROR;
  17531. }
  17532. res = TEST_RES_CHECK(ret == 0);
  17533. #endif
  17534. return res;
  17535. } /* END test_wc_RsaPublicEncryptDecrypt */
  17536. /*
  17537. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17538. */
  17539. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17540. {
  17541. int result = TEST_SKIPPED;
  17542. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17543. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  17544. && !defined(NO_SHA256)
  17545. RsaKey key;
  17546. WC_RNG rng;
  17547. int ret;
  17548. const char inStr[] = TEST_STRING;
  17549. const word32 inLen = (word32)TEST_STRING_SZ;
  17550. const word32 plainSz = (word32)TEST_STRING_SZ;
  17551. byte* res = NULL;
  17552. int idx = 0;
  17553. int bits = TEST_RSA_BITS;
  17554. const word32 cipherSz = TEST_RSA_BYTES;
  17555. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17556. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17557. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17558. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17559. if (in == NULL || plain == NULL || cipher == NULL) {
  17560. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  17561. return TEST_FAIL;
  17562. }
  17563. #endif
  17564. XMEMCPY(in, inStr, inLen);
  17565. /* Initialize stack structures. */
  17566. XMEMSET(&rng, 0, sizeof(rng));
  17567. XMEMSET(&key, 0, sizeof(key));
  17568. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  17569. if (ret == 0) {
  17570. ret = wc_InitRng(&rng);
  17571. }
  17572. if (ret == 0) {
  17573. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17574. }
  17575. /* Encrypt */
  17576. if (ret == 0) {
  17577. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  17578. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  17579. if (ret >= 0) {
  17580. idx = ret;
  17581. ret = 0;
  17582. }
  17583. else {
  17584. ret = WOLFSSL_FATAL_ERROR;
  17585. }
  17586. }
  17587. /* Pass bad args. */
  17588. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  17589. if (ret != 0) {
  17590. return TEST_FAIL;
  17591. }
  17592. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17593. /* Decrypt */
  17594. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17595. if (ret == 0) {
  17596. ret = wc_RsaSetRNG(&key, &rng);
  17597. }
  17598. #endif
  17599. if (ret == 0) {
  17600. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  17601. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  17602. WC_MGF1SHA256, NULL, 0);
  17603. }
  17604. if (ret >= 0) {
  17605. if (!XMEMCMP(plain, inStr, plainSz)) {
  17606. ret = 0;
  17607. }
  17608. else {
  17609. ret = WOLFSSL_FATAL_ERROR;
  17610. }
  17611. }
  17612. /*Pass bad args.*/
  17613. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  17614. if (ret != 0) {
  17615. return TEST_FAIL;
  17616. }
  17617. if (ret == 0) {
  17618. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  17619. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  17620. WC_MGF1SHA256, NULL, 0);
  17621. if (ret >= 0) {
  17622. if (!XMEMCMP(inStr, res, plainSz)) {
  17623. ret = 0;
  17624. }
  17625. else {
  17626. ret = WOLFSSL_FATAL_ERROR;
  17627. }
  17628. }
  17629. }
  17630. #endif
  17631. WC_FREE_VAR(in, NULL);
  17632. WC_FREE_VAR(plain, NULL);
  17633. WC_FREE_VAR(cipher, NULL);
  17634. if (wc_FreeRsaKey(&key) || ret != 0) {
  17635. ret = WOLFSSL_FATAL_ERROR;
  17636. }
  17637. if (wc_FreeRng(&rng) || ret != 0) {
  17638. ret = WOLFSSL_FATAL_ERROR;
  17639. }
  17640. result = TEST_RES_CHECK(ret == 0);
  17641. #endif
  17642. return result;
  17643. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17644. /*
  17645. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17646. */
  17647. static int test_wc_RsaSSL_SignVerify(void)
  17648. {
  17649. int res = TEST_SKIPPED;
  17650. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17651. RsaKey key;
  17652. WC_RNG rng;
  17653. int ret = 0;
  17654. const char inStr[] = TEST_STRING;
  17655. const word32 plainSz = (word32)TEST_STRING_SZ;
  17656. const word32 inLen = (word32)TEST_STRING_SZ;
  17657. word32 idx = 0;
  17658. int bits = TEST_RSA_BITS;
  17659. const word32 outSz = TEST_RSA_BYTES;
  17660. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17661. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17662. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17663. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17664. if (in == NULL || out == NULL || plain == NULL) {
  17665. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  17666. return TEST_FAIL;
  17667. }
  17668. #endif
  17669. XMEMCPY(in, inStr, inLen);
  17670. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17671. if (ret == 0) {
  17672. ret = wc_InitRng(&rng);
  17673. }
  17674. if (ret == 0) {
  17675. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17676. }
  17677. /* Sign. */
  17678. if (ret == 0) {
  17679. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  17680. if (ret == (int)outSz) {
  17681. idx = ret;
  17682. ret = 0;
  17683. }
  17684. else {
  17685. ret = WOLFSSL_FATAL_ERROR;
  17686. }
  17687. }
  17688. #ifndef HAVE_USER_RSA
  17689. /* Test bad args. */
  17690. if (ret == 0) {
  17691. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17692. if (ret == BAD_FUNC_ARG) {
  17693. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17694. }
  17695. if (ret == BAD_FUNC_ARG) {
  17696. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17697. }
  17698. if (ret == BAD_FUNC_ARG) {
  17699. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17700. }
  17701. if (ret == BAD_FUNC_ARG) {
  17702. ret = 0;
  17703. }
  17704. else {
  17705. ret = WOLFSSL_FATAL_ERROR;
  17706. }
  17707. }
  17708. #else
  17709. /* Test bad args. */
  17710. if (ret == 0) {
  17711. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17712. if (ret == USER_CRYPTO_ERROR) {
  17713. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17714. }
  17715. if (ret == USER_CRYPTO_ERROR) {
  17716. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17717. }
  17718. if (ret == USER_CRYPTO_ERROR) {
  17719. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17720. }
  17721. if (ret == USER_CRYPTO_ERROR) {
  17722. ret = 0;
  17723. }
  17724. else {
  17725. ret = WOLFSSL_FATAL_ERROR;
  17726. }
  17727. }
  17728. #endif
  17729. if (ret != 0) {
  17730. return TEST_FAIL;
  17731. }
  17732. /* Verify. */
  17733. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  17734. if (ret == (int)inLen) {
  17735. ret = 0;
  17736. }
  17737. else {
  17738. ret = WOLFSSL_FATAL_ERROR;
  17739. }
  17740. #ifndef HAVE_USER_RSA
  17741. /* Pass bad args. */
  17742. if (ret == 0) {
  17743. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17744. if (ret == BAD_FUNC_ARG) {
  17745. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17746. }
  17747. if (ret == BAD_FUNC_ARG) {
  17748. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17749. }
  17750. if (ret == BAD_FUNC_ARG) {
  17751. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17752. }
  17753. if (ret == BAD_FUNC_ARG) {
  17754. ret = 0;
  17755. }
  17756. else {
  17757. ret = WOLFSSL_FATAL_ERROR;
  17758. }
  17759. }
  17760. #else
  17761. /* Pass bad args. */
  17762. if (ret == 0) {
  17763. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17764. if (ret == USER_CRYPTO_ERROR) {
  17765. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17766. }
  17767. if (ret == USER_CRYPTO_ERROR) {
  17768. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17769. }
  17770. if (ret == USER_CRYPTO_ERROR) {
  17771. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17772. }
  17773. if (ret == USER_CRYPTO_ERROR) {
  17774. ret = 0;
  17775. }
  17776. else {
  17777. ret = WOLFSSL_FATAL_ERROR;
  17778. }
  17779. }
  17780. #endif
  17781. WC_FREE_VAR(in, NULL);
  17782. WC_FREE_VAR(out, NULL);
  17783. WC_FREE_VAR(plain, NULL);
  17784. if (wc_FreeRsaKey(&key) || ret != 0) {
  17785. ret = WOLFSSL_FATAL_ERROR;
  17786. }
  17787. if (wc_FreeRng(&rng) || ret != 0) {
  17788. ret = WOLFSSL_FATAL_ERROR;
  17789. }
  17790. res = TEST_RES_CHECK(ret == 0);
  17791. #endif
  17792. return res;
  17793. } /* END test_wc_RsaSSL_SignVerify */
  17794. /*
  17795. * Testing wc_RsaEncryptSize()
  17796. */
  17797. static int test_wc_RsaEncryptSize(void)
  17798. {
  17799. int res = TEST_SKIPPED;
  17800. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17801. RsaKey key;
  17802. WC_RNG rng;
  17803. int ret;
  17804. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17805. if (ret == 0) {
  17806. ret = wc_InitRng(&rng);
  17807. }
  17808. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17809. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17810. if (ret == 0) {
  17811. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17812. if (ret == 0) {
  17813. ret = wc_RsaEncryptSize(&key);
  17814. }
  17815. if (ret == 128) {
  17816. ret = 0;
  17817. }
  17818. else {
  17819. ret = WOLFSSL_FATAL_ERROR;
  17820. }
  17821. }
  17822. if (wc_FreeRsaKey(&key) || ret != 0) {
  17823. ret = WOLFSSL_FATAL_ERROR;
  17824. }
  17825. else {
  17826. ret = 0;
  17827. }
  17828. #endif
  17829. if (ret == 0) {
  17830. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17831. if (ret == 0) {
  17832. ret = wc_RsaEncryptSize(&key);
  17833. }
  17834. if (ret == 256) {
  17835. ret = 0;
  17836. }
  17837. else {
  17838. ret = WOLFSSL_FATAL_ERROR;
  17839. }
  17840. }
  17841. /* Pass in bad arg. */
  17842. if (ret == 0) {
  17843. ret = wc_RsaEncryptSize(NULL);
  17844. #ifndef HAVE_USER_RSA
  17845. if (ret == BAD_FUNC_ARG) {
  17846. ret = 0;
  17847. }
  17848. else {
  17849. ret = WOLFSSL_FATAL_ERROR;
  17850. }
  17851. #endif
  17852. }
  17853. if (wc_FreeRsaKey(&key) || ret != 0) {
  17854. ret = WOLFSSL_FATAL_ERROR;
  17855. }
  17856. if (wc_FreeRng(&rng) || ret != 0) {
  17857. ret = WOLFSSL_FATAL_ERROR;
  17858. }
  17859. res = TEST_RES_CHECK(ret == 0);
  17860. #endif
  17861. return res;
  17862. } /* END test_wc_RsaEncryptSize*/
  17863. /*
  17864. * Testing wc_RsaFlattenPublicKey()
  17865. */
  17866. static int test_wc_RsaFlattenPublicKey(void)
  17867. {
  17868. int res = TEST_SKIPPED;
  17869. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17870. RsaKey key;
  17871. WC_RNG rng;
  17872. int ret = 0;
  17873. byte e[256];
  17874. byte n[256];
  17875. word32 eSz = sizeof(e);
  17876. word32 nSz = sizeof(n);
  17877. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17878. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17879. int bits = 1024;
  17880. #else
  17881. int bits = 2048;
  17882. #endif
  17883. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17884. if (ret == 0) {
  17885. ret = wc_InitRng(&rng);
  17886. }
  17887. if (ret == 0) {
  17888. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17889. if (ret >= 0) {
  17890. ret = 0;
  17891. }
  17892. else {
  17893. ret = WOLFSSL_FATAL_ERROR;
  17894. }
  17895. }
  17896. if (ret == 0) {
  17897. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17898. }
  17899. #ifndef HAVE_USER_RSA
  17900. /* Pass bad args. */
  17901. if (ret == 0) {
  17902. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17903. if (ret == BAD_FUNC_ARG) {
  17904. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17905. }
  17906. if (ret == BAD_FUNC_ARG) {
  17907. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17908. }
  17909. if (ret == BAD_FUNC_ARG) {
  17910. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17911. }
  17912. if (ret == BAD_FUNC_ARG) {
  17913. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17914. }
  17915. if (ret == BAD_FUNC_ARG) {
  17916. ret = 0;
  17917. }
  17918. else {
  17919. ret = WOLFSSL_FATAL_ERROR;
  17920. }
  17921. }
  17922. #else
  17923. /* Pass bad args. */
  17924. if (ret == 0) {
  17925. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17926. if (ret == USER_CRYPTO_ERROR) {
  17927. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17928. }
  17929. if (ret == USER_CRYPTO_ERROR) {
  17930. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17931. }
  17932. if (ret == USER_CRYPTO_ERROR) {
  17933. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17934. }
  17935. if (ret == USER_CRYPTO_ERROR) {
  17936. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17937. }
  17938. if (ret == USER_CRYPTO_ERROR) {
  17939. ret = 0;
  17940. }
  17941. else {
  17942. ret = WOLFSSL_FATAL_ERROR;
  17943. }
  17944. }
  17945. #endif
  17946. if (wc_FreeRsaKey(&key) || ret != 0) {
  17947. ret = WOLFSSL_FATAL_ERROR;
  17948. }
  17949. if (wc_FreeRng(&rng) || ret != 0) {
  17950. ret = WOLFSSL_FATAL_ERROR;
  17951. }
  17952. res = TEST_RES_CHECK(ret == 0);
  17953. #endif
  17954. return res;
  17955. } /* END test_wc_RsaFlattenPublicKey */
  17956. /*
  17957. * unit test for wc_AesCcmSetKey
  17958. */
  17959. static int test_wc_AesCcmSetKey(void)
  17960. {
  17961. int res = TEST_SKIPPED;
  17962. #ifdef HAVE_AESCCM
  17963. Aes aes;
  17964. int ret = 0;
  17965. const byte key16[] =
  17966. {
  17967. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17968. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17969. };
  17970. const byte key24[] =
  17971. {
  17972. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17973. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17974. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17975. };
  17976. const byte key32[] =
  17977. {
  17978. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17979. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17980. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17981. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17982. };
  17983. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17984. if (ret != 0)
  17985. return ret;
  17986. #ifdef WOLFSSL_AES_128
  17987. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17988. #endif
  17989. #ifdef WOLFSSL_AES_192
  17990. if (ret == 0) {
  17991. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  17992. }
  17993. #endif
  17994. #ifdef WOLFSSL_AES_256
  17995. if (ret == 0) {
  17996. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  17997. }
  17998. #endif
  17999. /* Test bad args. */
  18000. if (ret == 0) {
  18001. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  18002. if (ret == BAD_FUNC_ARG) {
  18003. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  18004. }
  18005. if (ret == BAD_FUNC_ARG) {
  18006. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  18007. }
  18008. if (ret != BAD_FUNC_ARG) {
  18009. ret = WOLFSSL_FATAL_ERROR;
  18010. }
  18011. else {
  18012. ret = 0;
  18013. }
  18014. }
  18015. wc_AesFree(&aes);
  18016. res = TEST_RES_CHECK(ret == 0);
  18017. #endif
  18018. return res;
  18019. } /* END test_wc_AesCcmSetKey */
  18020. /*
  18021. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18022. */
  18023. static int test_wc_AesCcmEncryptDecrypt(void)
  18024. {
  18025. int res = TEST_SKIPPED;
  18026. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18027. Aes aes;
  18028. int ret = 0;
  18029. const byte key16[] =
  18030. {
  18031. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18032. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18033. };
  18034. /* plaintext */
  18035. const byte plainT[] =
  18036. {
  18037. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18038. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18039. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18040. };
  18041. /* nonce */
  18042. const byte iv[] =
  18043. {
  18044. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18045. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18046. };
  18047. const byte c[] = /* cipher text. */
  18048. {
  18049. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18050. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18051. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18052. };
  18053. const byte t[] = /* Auth tag */
  18054. {
  18055. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18056. };
  18057. const byte authIn[] =
  18058. {
  18059. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18060. };
  18061. byte cipherOut[sizeof(plainT)];
  18062. byte authTag[sizeof(t)];
  18063. int ccmE = WOLFSSL_FATAL_ERROR;
  18064. #ifdef HAVE_AES_DECRYPT
  18065. int ccmD = WOLFSSL_FATAL_ERROR;
  18066. byte plainOut[sizeof(cipherOut)];
  18067. #endif
  18068. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18069. if (ret != 0)
  18070. return ret;
  18071. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18072. if (ret == 0) {
  18073. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18074. iv, sizeof(iv), authTag, sizeof(authTag),
  18075. authIn , sizeof(authIn));
  18076. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  18077. XMEMCMP(t, authTag, sizeof(t))) {
  18078. ccmE = WOLFSSL_FATAL_ERROR;
  18079. ret = WOLFSSL_FATAL_ERROR;
  18080. }
  18081. #ifdef HAVE_AES_DECRYPT
  18082. if (ret == 0) {
  18083. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18084. sizeof(plainOut), iv, sizeof(iv),
  18085. authTag, sizeof(authTag),
  18086. authIn, sizeof(authIn));
  18087. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  18088. ccmD = WOLFSSL_FATAL_ERROR;
  18089. }
  18090. }
  18091. #endif
  18092. }
  18093. /* Pass in bad args. Encrypt*/
  18094. if (ret == 0 && ccmE == 0) {
  18095. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18096. iv, sizeof(iv), authTag, sizeof(authTag),
  18097. authIn , sizeof(authIn));
  18098. if (ccmE == BAD_FUNC_ARG) {
  18099. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18100. iv, sizeof(iv), authTag, sizeof(authTag),
  18101. authIn , sizeof(authIn));
  18102. }
  18103. if (ccmE == BAD_FUNC_ARG) {
  18104. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18105. iv, sizeof(iv), authTag, sizeof(authTag),
  18106. authIn , sizeof(authIn));
  18107. }
  18108. if (ccmE == BAD_FUNC_ARG) {
  18109. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18110. NULL, sizeof(iv), authTag, sizeof(authTag),
  18111. authIn , sizeof(authIn));
  18112. }
  18113. if (ccmE == BAD_FUNC_ARG) {
  18114. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18115. iv, sizeof(iv), NULL, sizeof(authTag),
  18116. authIn , sizeof(authIn));
  18117. }
  18118. if (ccmE == BAD_FUNC_ARG) {
  18119. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18120. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  18121. authIn , sizeof(authIn));
  18122. }
  18123. if (ccmE == BAD_FUNC_ARG) {
  18124. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18125. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  18126. authIn , sizeof(authIn));
  18127. }
  18128. if (ccmE != BAD_FUNC_ARG) {
  18129. ccmE = WOLFSSL_FATAL_ERROR;
  18130. }
  18131. else {
  18132. ccmE = 0;
  18133. }
  18134. } /* End Encrypt */
  18135. if (ccmE != 0) {
  18136. wc_AesFree(&aes);
  18137. return TEST_FAIL;
  18138. }
  18139. #ifdef HAVE_AES_DECRYPT
  18140. /* Pass in bad args. Decrypt*/
  18141. if (ret == 0 && ccmD == 0) {
  18142. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18143. iv, sizeof(iv), authTag, sizeof(authTag),
  18144. authIn, sizeof(authIn));
  18145. if (ccmD == BAD_FUNC_ARG) {
  18146. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18147. iv, sizeof(iv), authTag, sizeof(authTag),
  18148. authIn, sizeof(authIn));
  18149. }
  18150. if (ccmD == BAD_FUNC_ARG) {
  18151. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18152. iv, sizeof(iv), authTag, sizeof(authTag),
  18153. authIn, sizeof(authIn));
  18154. }
  18155. if (ccmD == BAD_FUNC_ARG) {
  18156. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18157. sizeof(plainOut), NULL, sizeof(iv),
  18158. authTag, sizeof(authTag),
  18159. authIn, sizeof(authIn));
  18160. }
  18161. if (ccmD == BAD_FUNC_ARG) {
  18162. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18163. sizeof(plainOut), iv, sizeof(iv), NULL,
  18164. sizeof(authTag), authIn, sizeof(authIn));
  18165. }
  18166. if (ccmD == BAD_FUNC_ARG) {
  18167. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18168. sizeof(plainOut), iv, sizeof(iv) + 1,
  18169. authTag, sizeof(authTag),
  18170. authIn, sizeof(authIn));
  18171. }
  18172. if (ccmD == BAD_FUNC_ARG) {
  18173. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18174. sizeof(plainOut), iv, sizeof(iv) - 7,
  18175. authTag, sizeof(authTag),
  18176. authIn, sizeof(authIn));
  18177. }
  18178. if (ccmD != BAD_FUNC_ARG) {
  18179. ccmD = WOLFSSL_FATAL_ERROR;
  18180. }
  18181. else {
  18182. ccmD = 0;
  18183. }
  18184. } /* END Decrypt */
  18185. res = TEST_RES_CHECK(ccmD == 0);
  18186. #endif
  18187. wc_AesFree(&aes);
  18188. #endif /* HAVE_AESCCM */
  18189. return res;
  18190. } /* END test_wc_AesCcmEncryptDecrypt */
  18191. /*
  18192. * Testing wc_InitDsaKey()
  18193. */
  18194. static int test_wc_InitDsaKey(void)
  18195. {
  18196. int res = TEST_SKIPPED;
  18197. #ifndef NO_DSA
  18198. DsaKey key;
  18199. int ret = 0;
  18200. ret = wc_InitDsaKey(&key);
  18201. /* Pass in bad args. */
  18202. if (ret == 0) {
  18203. ret = wc_InitDsaKey(NULL);
  18204. if (ret == BAD_FUNC_ARG) {
  18205. ret = 0;
  18206. }
  18207. else {
  18208. ret = WOLFSSL_FATAL_ERROR;
  18209. }
  18210. }
  18211. wc_FreeDsaKey(&key);
  18212. res = TEST_RES_CHECK(ret == 0);
  18213. #endif
  18214. return res;
  18215. } /* END test_wc_InitDsaKey */
  18216. /*
  18217. * Testing wc_DsaSign() and wc_DsaVerify()
  18218. */
  18219. static int test_wc_DsaSignVerify(void)
  18220. {
  18221. int res = TEST_SKIPPED;
  18222. #if !defined(NO_DSA)
  18223. DsaKey key;
  18224. WC_RNG rng;
  18225. wc_Sha sha;
  18226. int ret = 0;
  18227. byte signature[DSA_SIG_SIZE];
  18228. byte hash[WC_SHA_DIGEST_SIZE];
  18229. word32 idx = 0;
  18230. word32 bytes;
  18231. int answer;
  18232. #ifdef USE_CERT_BUFFERS_1024
  18233. byte tmp[ONEK_BUF];
  18234. XMEMSET(tmp, 0, sizeof(tmp));
  18235. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18236. bytes = sizeof_dsa_key_der_1024;
  18237. #elif defined(USE_CERT_BUFFERS_2048)
  18238. byte tmp[TWOK_BUF];
  18239. XMEMSET(tmp, 0, sizeof(tmp));
  18240. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18241. bytes = sizeof_dsa_key_der_2048;
  18242. #else
  18243. byte tmp[TWOK_BUF];
  18244. XMEMSET(tmp, 0, sizeof(tmp));
  18245. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18246. if (fp == XBADFILE) {
  18247. return WOLFSSL_BAD_FILE;
  18248. }
  18249. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18250. XFCLOSE(fp);
  18251. #endif /* END USE_CERT_BUFFERS_1024 */
  18252. ret = wc_InitSha(&sha);
  18253. if (ret == 0) {
  18254. ret = wc_ShaUpdate(&sha, tmp, bytes);
  18255. if (ret == 0) {
  18256. ret = wc_ShaFinal(&sha, hash);
  18257. }
  18258. if (ret == 0) {
  18259. ret = wc_InitDsaKey(&key);
  18260. }
  18261. if (ret == 0) {
  18262. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18263. }
  18264. if (ret == 0) {
  18265. ret = wc_InitRng(&rng);
  18266. }
  18267. }
  18268. /* Sign. */
  18269. if (ret == 0) {
  18270. ret = wc_DsaSign(hash, signature, &key, &rng);
  18271. }
  18272. /* Test bad args. */
  18273. if (ret == 0) {
  18274. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18275. if (ret == BAD_FUNC_ARG) {
  18276. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18277. }
  18278. if (ret == BAD_FUNC_ARG) {
  18279. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18280. }
  18281. if (ret == BAD_FUNC_ARG) {
  18282. ret = wc_DsaSign(hash, signature, &key, NULL);
  18283. }
  18284. if (ret == BAD_FUNC_ARG) {
  18285. ret = 0;
  18286. }
  18287. else {
  18288. ret = WOLFSSL_FATAL_ERROR;
  18289. }
  18290. }
  18291. if (ret == 0) {
  18292. /* Verify. */
  18293. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18294. if (ret != 0 || answer != 1) {
  18295. ret = WOLFSSL_FATAL_ERROR;
  18296. }
  18297. else {
  18298. ret = 0;
  18299. }
  18300. }
  18301. /* Pass in bad args. */
  18302. if (ret == 0) {
  18303. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18304. if (ret == BAD_FUNC_ARG) {
  18305. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18306. }
  18307. if (ret == BAD_FUNC_ARG) {
  18308. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18309. }
  18310. if (ret == BAD_FUNC_ARG) {
  18311. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18312. }
  18313. if (ret == BAD_FUNC_ARG) {
  18314. ret = 0;
  18315. }
  18316. else {
  18317. ret = WOLFSSL_FATAL_ERROR;
  18318. }
  18319. }
  18320. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18321. /* hard set q to 0 and test fail case */
  18322. mp_free(&key.q);
  18323. mp_init(&key.q);
  18324. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18325. mp_set(&key.q, 1);
  18326. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18327. #endif
  18328. if (wc_FreeRng(&rng) && ret == 0) {
  18329. ret = WOLFSSL_FATAL_ERROR;
  18330. }
  18331. wc_FreeDsaKey(&key);
  18332. wc_ShaFree(&sha);
  18333. res = TEST_RES_CHECK(ret == 0);
  18334. #endif
  18335. return res;
  18336. } /* END test_wc_DsaSign */
  18337. /*
  18338. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18339. */
  18340. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18341. {
  18342. int res = TEST_SKIPPED;
  18343. #if !defined(NO_DSA)
  18344. DsaKey key;
  18345. word32 bytes;
  18346. word32 idx = 0;
  18347. int priv = 0;
  18348. int pub = 0;
  18349. int ret = 0;
  18350. #ifdef USE_CERT_BUFFERS_1024
  18351. byte tmp[ONEK_BUF];
  18352. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18353. bytes = sizeof_dsa_key_der_1024;
  18354. #elif defined(USE_CERT_BUFFERS_2048)
  18355. byte tmp[TWOK_BUF];
  18356. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18357. bytes = sizeof_dsa_key_der_2048;
  18358. #else
  18359. byte tmp[TWOK_BUF];
  18360. XMEMSET(tmp, 0, sizeof(tmp));
  18361. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18362. if (fp == XBADFILE)
  18363. {
  18364. return WOLFSSL_BAD_FILE;
  18365. }
  18366. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18367. XFCLOSE(fp);
  18368. #endif /* END USE_CERT_BUFFERS_1024 */
  18369. ret = wc_InitDsaKey(&key);
  18370. if (ret == 0) {
  18371. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18372. /* Test bad args. */
  18373. if (priv == 0) {
  18374. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18375. if (priv == BAD_FUNC_ARG) {
  18376. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18377. }
  18378. if (priv == BAD_FUNC_ARG) {
  18379. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18380. }
  18381. if (priv == BAD_FUNC_ARG) {
  18382. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18383. }
  18384. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  18385. priv = 0;
  18386. }
  18387. else {
  18388. priv = WOLFSSL_FATAL_ERROR;
  18389. }
  18390. }
  18391. wc_FreeDsaKey(&key);
  18392. ret = wc_InitDsaKey(&key);
  18393. }
  18394. if (ret == 0) {
  18395. idx = 0; /* Reset */
  18396. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18397. /* Test bad args. */
  18398. if (pub == 0) {
  18399. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  18400. if (pub == BAD_FUNC_ARG) {
  18401. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  18402. }
  18403. if (pub == BAD_FUNC_ARG) {
  18404. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  18405. }
  18406. if (pub == BAD_FUNC_ARG) {
  18407. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18408. }
  18409. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  18410. pub = 0;
  18411. }
  18412. else {
  18413. pub = WOLFSSL_FATAL_ERROR;
  18414. }
  18415. }
  18416. } /* END Public Key */
  18417. wc_FreeDsaKey(&key);
  18418. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  18419. #endif /* !NO_DSA */
  18420. return res;
  18421. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18422. /*
  18423. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18424. */
  18425. static int test_wc_MakeDsaKey(void)
  18426. {
  18427. int res = TEST_SKIPPED;
  18428. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18429. DsaKey genKey;
  18430. WC_RNG rng;
  18431. int ret = 0;
  18432. XMEMSET(&rng, 0, sizeof(rng));
  18433. XMEMSET(&genKey, 0, sizeof(genKey));
  18434. ret = wc_InitRng(&rng);
  18435. if (ret == 0) {
  18436. ret = wc_InitDsaKey(&genKey);
  18437. }
  18438. if (ret == 0) {
  18439. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18440. }
  18441. /* Test bad args. */
  18442. if (ret == 0) {
  18443. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  18444. if (ret == BAD_FUNC_ARG) {
  18445. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  18446. }
  18447. if (ret == BAD_FUNC_ARG) {
  18448. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  18449. }
  18450. if (ret == BAD_FUNC_ARG) {
  18451. ret = 0;
  18452. }
  18453. else {
  18454. ret = WOLFSSL_FATAL_ERROR;
  18455. }
  18456. }
  18457. if (ret == 0) {
  18458. ret = wc_MakeDsaKey(&rng, &genKey);
  18459. }
  18460. /* Test bad args. */
  18461. if (ret == 0) {
  18462. ret = wc_MakeDsaKey(NULL, &genKey);
  18463. if (ret == BAD_FUNC_ARG) {
  18464. ret = wc_MakeDsaKey(&rng, NULL);
  18465. }
  18466. if (ret == BAD_FUNC_ARG) {
  18467. ret = 0;
  18468. }
  18469. else {
  18470. ret = WOLFSSL_FATAL_ERROR;
  18471. }
  18472. }
  18473. if (wc_FreeRng(&rng) && ret == 0) {
  18474. ret = WOLFSSL_FAILURE;
  18475. }
  18476. wc_FreeDsaKey(&genKey);
  18477. res = TEST_RES_CHECK(ret == 0);
  18478. #endif
  18479. return res;
  18480. } /* END test_wc_MakeDsaKey */
  18481. /*
  18482. * Testing wc_DsaKeyToDer()
  18483. */
  18484. static int test_wc_DsaKeyToDer(void)
  18485. {
  18486. int res = TEST_SKIPPED;
  18487. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18488. DsaKey genKey;
  18489. WC_RNG rng;
  18490. word32 bytes;
  18491. word32 idx = 0;
  18492. int ret = 0;
  18493. #ifdef USE_CERT_BUFFERS_1024
  18494. byte tmp[ONEK_BUF];
  18495. byte der[ONEK_BUF];
  18496. XMEMSET(tmp, 0, sizeof(tmp));
  18497. XMEMSET(der, 0, sizeof(der));
  18498. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18499. bytes = sizeof_dsa_key_der_1024;
  18500. #elif defined(USE_CERT_BUFFERS_2048)
  18501. byte tmp[TWOK_BUF];
  18502. byte der[TWOK_BUF];
  18503. XMEMSET(tmp, 0, sizeof(tmp));
  18504. XMEMSET(der, 0, sizeof(der));
  18505. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18506. bytes = sizeof_dsa_key_der_2048;
  18507. #else
  18508. byte tmp[TWOK_BUF];
  18509. byte der[TWOK_BUF];
  18510. XMEMSET(tmp, 0, sizeof(tmp));
  18511. XMEMSET(der, 0, sizeof(der));
  18512. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18513. if (fp == XBADFILE) {
  18514. return WOLFSSL_BAD_FILE;
  18515. }
  18516. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18517. XFCLOSE(fp);
  18518. #endif /* END USE_CERT_BUFFERS_1024 */
  18519. XMEMSET(&rng, 0, sizeof(rng));
  18520. XMEMSET(&genKey, 0, sizeof(genKey));
  18521. ret = wc_InitRng(&rng);
  18522. if (ret == 0) {
  18523. ret = wc_InitDsaKey(&genKey);
  18524. }
  18525. if (ret == 0) {
  18526. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  18527. if (ret == 0) {
  18528. wc_FreeDsaKey(&genKey);
  18529. ret = wc_InitDsaKey(&genKey);
  18530. }
  18531. }
  18532. if (ret == 0) {
  18533. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  18534. }
  18535. if (ret == 0) {
  18536. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  18537. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  18538. ret = 0;
  18539. }
  18540. }
  18541. /* Test bad args. */
  18542. if (ret == 0) {
  18543. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  18544. if (ret == BAD_FUNC_ARG) {
  18545. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  18546. }
  18547. if (ret == BAD_FUNC_ARG) {
  18548. ret = 0;
  18549. }
  18550. else {
  18551. ret = WOLFSSL_FATAL_ERROR;
  18552. }
  18553. }
  18554. if (wc_FreeRng(&rng) && ret == 0) {
  18555. ret = WOLFSSL_FATAL_ERROR;
  18556. }
  18557. wc_FreeDsaKey(&genKey);
  18558. res = TEST_RES_CHECK(ret == 0);
  18559. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18560. return res;
  18561. } /* END test_wc_DsaKeyToDer */
  18562. /*
  18563. * Testing wc_DsaKeyToPublicDer()
  18564. * (indirectly testing setDsaPublicKey())
  18565. */
  18566. static int test_wc_DsaKeyToPublicDer(void)
  18567. {
  18568. int res = TEST_SKIPPED;
  18569. #ifndef HAVE_SELFTEST
  18570. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18571. DsaKey genKey;
  18572. WC_RNG rng;
  18573. byte* der;
  18574. word32 sz;
  18575. int ret = 0;
  18576. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18577. if (der == NULL) {
  18578. ret = WOLFSSL_FATAL_ERROR;
  18579. }
  18580. if (ret == 0) {
  18581. ret = wc_InitDsaKey(&genKey);
  18582. }
  18583. if (ret == 0) {
  18584. ret = wc_InitRng(&rng);
  18585. }
  18586. if (ret == 0) {
  18587. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18588. }
  18589. if (ret == 0) {
  18590. ret = wc_MakeDsaKey(&rng, &genKey);
  18591. }
  18592. if (ret == 0) {
  18593. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  18594. if (ret >= 0) {
  18595. sz = ret;
  18596. ret = 0;
  18597. }
  18598. else {
  18599. ret = WOLFSSL_FATAL_ERROR;
  18600. }
  18601. }
  18602. if (ret == 0) {
  18603. word32 idx = 0;
  18604. wc_FreeDsaKey(&genKey);
  18605. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18606. }
  18607. /* Test without the SubjectPublicKeyInfo header */
  18608. if (ret == 0) {
  18609. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  18610. if (ret >= 0) {
  18611. sz = ret;
  18612. ret = 0;
  18613. }
  18614. else {
  18615. ret = WOLFSSL_FATAL_ERROR;
  18616. }
  18617. }
  18618. if (ret == 0) {
  18619. word32 idx = 0;
  18620. wc_FreeDsaKey(&genKey);
  18621. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18622. }
  18623. /* Test bad args. */
  18624. if (ret == 0) {
  18625. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  18626. if (ret == BAD_FUNC_ARG) {
  18627. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  18628. }
  18629. if (ret == BAD_FUNC_ARG) {
  18630. ret = 0;
  18631. }
  18632. else {
  18633. ret = WOLFSSL_FATAL_ERROR;
  18634. }
  18635. }
  18636. if (wc_FreeRng(&rng) && ret == 0) {
  18637. ret = WOLFSSL_FATAL_ERROR;
  18638. }
  18639. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18640. wc_FreeDsaKey(&genKey);
  18641. res = TEST_RES_CHECK(ret == 0);
  18642. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18643. #endif /* !HAVE_SELFTEST */
  18644. return res;
  18645. } /* END test_wc_DsaKeyToPublicDer */
  18646. /*
  18647. * Testing wc_DsaImportParamsRaw()
  18648. */
  18649. static int test_wc_DsaImportParamsRaw(void)
  18650. {
  18651. int res = TEST_SKIPPED;
  18652. #if !defined(NO_DSA)
  18653. DsaKey key;
  18654. int ret = 0;
  18655. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18656. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18657. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18658. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18659. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18660. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18661. "80a0093113a8bd73";
  18662. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18663. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18664. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18665. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18666. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18667. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18668. "76341a7e7d9";
  18669. /* invalid p and q parameters */
  18670. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18671. const char* invalidQ = "96c5390a";
  18672. ret = wc_InitDsaKey(&key);
  18673. if (ret == 0) {
  18674. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18675. }
  18676. /* test bad args */
  18677. if (ret == 0) {
  18678. /* null key struct */
  18679. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  18680. if (ret == BAD_FUNC_ARG) {
  18681. /* null param pointers */
  18682. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  18683. }
  18684. if (ret == BAD_FUNC_ARG) {
  18685. /* illegal p length */
  18686. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  18687. }
  18688. if (ret == BAD_FUNC_ARG) {
  18689. /* illegal q length */
  18690. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  18691. if (ret == BAD_FUNC_ARG)
  18692. ret = 0;
  18693. }
  18694. }
  18695. wc_FreeDsaKey(&key);
  18696. res = TEST_RES_CHECK(ret == 0);
  18697. #endif
  18698. return res;
  18699. } /* END test_wc_DsaImportParamsRaw */
  18700. /*
  18701. * Testing wc_DsaImportParamsRawCheck()
  18702. */
  18703. static int test_wc_DsaImportParamsRawCheck(void)
  18704. {
  18705. int res = TEST_SKIPPED;
  18706. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18707. DsaKey key;
  18708. int ret = 0;
  18709. int trusted = 0;
  18710. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18711. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18712. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18713. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18714. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18715. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18716. "80a0093113a8bd73";
  18717. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18718. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18719. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18720. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18721. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18722. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18723. "76341a7e7d9";
  18724. /* invalid p and q parameters */
  18725. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18726. const char* invalidQ = "96c5390a";
  18727. ret = wc_InitDsaKey(&key);
  18728. if (ret == 0) {
  18729. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  18730. }
  18731. /* test bad args */
  18732. if (ret == 0) {
  18733. /* null key struct */
  18734. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  18735. if (ret == BAD_FUNC_ARG) {
  18736. /* null param pointers */
  18737. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  18738. }
  18739. if (ret == BAD_FUNC_ARG) {
  18740. /* illegal p length */
  18741. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  18742. }
  18743. if (ret == BAD_FUNC_ARG) {
  18744. /* illegal q length */
  18745. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  18746. if (ret == BAD_FUNC_ARG)
  18747. ret = 0;
  18748. }
  18749. }
  18750. wc_FreeDsaKey(&key);
  18751. res = TEST_RES_CHECK(ret == 0);
  18752. #endif
  18753. return res;
  18754. } /* END test_wc_DsaImportParamsRawCheck */
  18755. /*
  18756. * Testing wc_DsaExportParamsRaw()
  18757. */
  18758. static int test_wc_DsaExportParamsRaw(void)
  18759. {
  18760. int res = TEST_SKIPPED;
  18761. #if !defined(NO_DSA)
  18762. DsaKey key;
  18763. int ret = 0;
  18764. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18765. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18766. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18767. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18768. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18769. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18770. "80a0093113a8bd73";
  18771. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18772. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18773. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18774. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18775. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18776. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18777. "76341a7e7d9";
  18778. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18779. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18780. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18781. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18782. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18783. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18784. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18785. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18786. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18787. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18788. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18789. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18790. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18791. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18792. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18793. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18794. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18795. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18796. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18797. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18798. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18799. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18800. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18801. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18802. byte pOut[MAX_DSA_PARAM_SIZE];
  18803. byte qOut[MAX_DSA_PARAM_SIZE];
  18804. byte gOut[MAX_DSA_PARAM_SIZE];
  18805. word32 pOutSz, qOutSz, gOutSz;
  18806. ret = wc_InitDsaKey(&key);
  18807. if (ret == 0) {
  18808. /* first test using imported raw parameters, for expected */
  18809. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18810. }
  18811. if (ret == 0) {
  18812. pOutSz = sizeof(pOut);
  18813. qOutSz = sizeof(qOut);
  18814. gOutSz = sizeof(gOut);
  18815. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18816. gOut, &gOutSz);
  18817. }
  18818. if (ret == 0) {
  18819. /* validate exported parameters are correct */
  18820. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18821. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18822. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18823. ret = -1;
  18824. }
  18825. }
  18826. /* test bad args */
  18827. if (ret == 0) {
  18828. /* null key struct */
  18829. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18830. gOut, &gOutSz);
  18831. if (ret == BAD_FUNC_ARG) {
  18832. /* null output pointers */
  18833. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18834. NULL, &gOutSz);
  18835. }
  18836. if (ret == LENGTH_ONLY_E) {
  18837. /* null output size pointers */
  18838. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18839. gOut, NULL);
  18840. }
  18841. if (ret == BAD_FUNC_ARG) {
  18842. /* p output buffer size too small */
  18843. pOutSz = 1;
  18844. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18845. gOut, &gOutSz);
  18846. pOutSz = sizeof(pOut);
  18847. }
  18848. if (ret == BUFFER_E) {
  18849. /* q output buffer size too small */
  18850. qOutSz = 1;
  18851. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18852. gOut, &gOutSz);
  18853. qOutSz = sizeof(qOut);
  18854. }
  18855. if (ret == BUFFER_E) {
  18856. /* g output buffer size too small */
  18857. gOutSz = 1;
  18858. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18859. gOut, &gOutSz);
  18860. if (ret == BUFFER_E)
  18861. ret = 0;
  18862. }
  18863. }
  18864. wc_FreeDsaKey(&key);
  18865. res = TEST_RES_CHECK(ret == 0);
  18866. #endif
  18867. return res;
  18868. } /* END test_wc_DsaExportParamsRaw */
  18869. /*
  18870. * Testing wc_DsaExportKeyRaw()
  18871. */
  18872. static int test_wc_DsaExportKeyRaw(void)
  18873. {
  18874. int res = TEST_SKIPPED;
  18875. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18876. DsaKey key;
  18877. WC_RNG rng;
  18878. int ret = 0;
  18879. byte xOut[MAX_DSA_PARAM_SIZE];
  18880. byte yOut[MAX_DSA_PARAM_SIZE];
  18881. word32 xOutSz, yOutSz;
  18882. XMEMSET(&rng, 0, sizeof(rng));
  18883. XMEMSET(&key, 0, sizeof(key));
  18884. ret = wc_InitRng(&rng);
  18885. if (ret == 0) {
  18886. ret = wc_InitDsaKey(&key);
  18887. }
  18888. if (ret == 0) {
  18889. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18890. if (ret == 0) {
  18891. ret = wc_MakeDsaKey(&rng, &key);
  18892. }
  18893. }
  18894. /* try successful export */
  18895. if (ret == 0) {
  18896. xOutSz = sizeof(xOut);
  18897. yOutSz = sizeof(yOut);
  18898. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18899. }
  18900. /* test bad args */
  18901. if (ret == 0) {
  18902. /* null key struct */
  18903. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18904. if (ret == BAD_FUNC_ARG) {
  18905. /* null output pointers */
  18906. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18907. }
  18908. if (ret == LENGTH_ONLY_E) {
  18909. /* null output size pointers */
  18910. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18911. }
  18912. if (ret == BAD_FUNC_ARG) {
  18913. /* x output buffer size too small */
  18914. xOutSz = 1;
  18915. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18916. xOutSz = sizeof(xOut);
  18917. }
  18918. if (ret == BUFFER_E) {
  18919. /* y output buffer size too small */
  18920. yOutSz = 1;
  18921. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18922. if (ret == BUFFER_E)
  18923. ret = 0;
  18924. }
  18925. }
  18926. wc_FreeDsaKey(&key);
  18927. wc_FreeRng(&rng);
  18928. res = TEST_RES_CHECK(ret == 0);
  18929. #endif
  18930. return res;
  18931. } /* END test_wc_DsaExportParamsRaw */
  18932. /*
  18933. * Testing wc_ed25519_make_key().
  18934. */
  18935. static int test_wc_ed25519_make_key(void)
  18936. {
  18937. int res = TEST_SKIPPED;
  18938. #if defined(HAVE_ED25519)
  18939. ed25519_key key;
  18940. WC_RNG rng;
  18941. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  18942. int ret = 0;
  18943. ret = wc_InitRng(&rng);
  18944. if (ret == 0) {
  18945. ret = wc_ed25519_init(&key);
  18946. }
  18947. if (ret == 0) {
  18948. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  18949. if (ret == ECC_PRIV_KEY_E) {
  18950. ret = 0;
  18951. }
  18952. else if (ret == 0) {
  18953. ret = -1;
  18954. }
  18955. }
  18956. if (ret == 0) {
  18957. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18958. }
  18959. /* Test bad args. */
  18960. if (ret == 0) {
  18961. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18962. if (ret == BAD_FUNC_ARG) {
  18963. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18964. }
  18965. if (ret == BAD_FUNC_ARG) {
  18966. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18967. }
  18968. if (ret == BAD_FUNC_ARG) {
  18969. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18970. }
  18971. if (ret == BAD_FUNC_ARG) {
  18972. ret = 0;
  18973. }
  18974. else if (ret == 0) {
  18975. ret = WOLFSSL_FATAL_ERROR;
  18976. }
  18977. }
  18978. if (wc_FreeRng(&rng) && ret == 0) {
  18979. ret = WOLFSSL_FATAL_ERROR;
  18980. }
  18981. wc_ed25519_free(&key);
  18982. res = TEST_RES_CHECK(ret == 0);
  18983. #endif
  18984. return res;
  18985. } /* END test_wc_ed25519_make_key */
  18986. /*
  18987. * Testing wc_ed25519_init()
  18988. */
  18989. static int test_wc_ed25519_init(void)
  18990. {
  18991. int res = TEST_SKIPPED;
  18992. #if defined(HAVE_ED25519)
  18993. ed25519_key key;
  18994. int ret = 0;
  18995. ret = wc_ed25519_init(&key);
  18996. /* Test bad args. */
  18997. if (ret == 0) {
  18998. ret = wc_ed25519_init(NULL);
  18999. if (ret == BAD_FUNC_ARG) {
  19000. ret = 0;
  19001. }
  19002. else if (ret == 0) {
  19003. ret = WOLFSSL_FATAL_ERROR;
  19004. }
  19005. }
  19006. wc_ed25519_free(&key);
  19007. res = TEST_RES_CHECK(ret == 0);
  19008. #endif
  19009. return res;
  19010. } /* END test_wc_ed25519_init */
  19011. /*
  19012. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19013. */
  19014. static int test_wc_ed25519_sign_msg(void)
  19015. {
  19016. int res = TEST_SKIPPED;
  19017. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19018. WC_RNG rng;
  19019. ed25519_key key;
  19020. int ret = 0;
  19021. byte msg[] = "Everybody gets Friday off.\n";
  19022. byte sig[ED25519_SIG_SIZE];
  19023. word32 msglen = sizeof(msg);
  19024. word32 siglen = sizeof(sig);
  19025. word32 badSigLen = sizeof(sig) - 1;
  19026. #ifdef HAVE_ED25519_VERIFY
  19027. int verify_ok = 0; /*1 = Verify success.*/
  19028. #endif
  19029. /* Initialize stack variables. */
  19030. XMEMSET(sig, 0, siglen);
  19031. /* Initialize key. */
  19032. ret = wc_InitRng(&rng);
  19033. if (ret == 0) {
  19034. ret = wc_ed25519_init(&key);
  19035. if (ret == 0) {
  19036. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19037. }
  19038. }
  19039. if (ret == 0) {
  19040. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  19041. }
  19042. /* Test bad args. */
  19043. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  19044. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  19045. if (ret == BAD_FUNC_ARG) {
  19046. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  19047. }
  19048. if (ret == BAD_FUNC_ARG) {
  19049. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  19050. }
  19051. if (ret == BAD_FUNC_ARG) {
  19052. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  19053. }
  19054. if (ret == BAD_FUNC_ARG) {
  19055. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  19056. }
  19057. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  19058. badSigLen -= 1;
  19059. ret = 0;
  19060. }
  19061. else if (ret == 0) {
  19062. ret = WOLFSSL_FATAL_ERROR;
  19063. }
  19064. } /* END sign */
  19065. #ifdef HAVE_ED25519_VERIFY
  19066. if (ret == 0) {
  19067. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  19068. if (ret == 0 && verify_ok == 1) {
  19069. ret = 0;
  19070. }
  19071. else if (ret == 0) {
  19072. ret = WOLFSSL_FATAL_ERROR;
  19073. }
  19074. /* Test bad args. */
  19075. if (ret == 0) {
  19076. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  19077. msglen, &verify_ok, &key),
  19078. BAD_FUNC_ARG);
  19079. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  19080. msglen, &verify_ok, &key),
  19081. BAD_FUNC_ARG);
  19082. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19083. &key);
  19084. if (ret == BAD_FUNC_ARG) {
  19085. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  19086. &verify_ok, &key);
  19087. }
  19088. if (ret == BAD_FUNC_ARG) {
  19089. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19090. NULL, &key);
  19091. }
  19092. if (ret == BAD_FUNC_ARG) {
  19093. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19094. &verify_ok, NULL);
  19095. }
  19096. if (ret == BAD_FUNC_ARG) {
  19097. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  19098. &verify_ok, &key);
  19099. }
  19100. if (ret == BAD_FUNC_ARG) {
  19101. ret = 0;
  19102. }
  19103. else if (ret == 0) {
  19104. ret = WOLFSSL_FATAL_ERROR;
  19105. }
  19106. }
  19107. } /* END verify. */
  19108. #endif /* Verify. */
  19109. if (wc_FreeRng(&rng) && ret == 0) {
  19110. ret = WOLFSSL_FATAL_ERROR;
  19111. }
  19112. wc_ed25519_free(&key);
  19113. res = TEST_RES_CHECK(ret == 0);
  19114. #endif
  19115. return res;
  19116. } /* END test_wc_ed25519_sign_msg */
  19117. /*
  19118. * Testing wc_ed25519_import_public()
  19119. */
  19120. static int test_wc_ed25519_import_public(void)
  19121. {
  19122. int res = TEST_SKIPPED;
  19123. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19124. WC_RNG rng;
  19125. ed25519_key pubKey;
  19126. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19127. word32 inlen = sizeof(in);
  19128. int ret = 0;
  19129. ret = wc_InitRng(&rng);
  19130. if (ret == 0) {
  19131. ret = wc_ed25519_init(&pubKey);
  19132. if (ret == 0) {
  19133. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  19134. }
  19135. }
  19136. if (ret == 0) {
  19137. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  19138. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19139. ret = 0;
  19140. }
  19141. else {
  19142. ret = WOLFSSL_FATAL_ERROR;
  19143. }
  19144. /* Test bad args. */
  19145. if (ret == 0) {
  19146. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  19147. if (ret == BAD_FUNC_ARG) {
  19148. ret = wc_ed25519_import_public(in, inlen, NULL);
  19149. }
  19150. if (ret == BAD_FUNC_ARG) {
  19151. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  19152. }
  19153. if (ret == BAD_FUNC_ARG) {
  19154. ret = 0;
  19155. }
  19156. else if (ret == 0) {
  19157. ret = WOLFSSL_FATAL_ERROR;
  19158. }
  19159. }
  19160. }
  19161. if (wc_FreeRng(&rng) && ret == 0) {
  19162. ret = WOLFSSL_FATAL_ERROR;
  19163. }
  19164. wc_ed25519_free(&pubKey);
  19165. res = TEST_RES_CHECK(ret == 0);
  19166. #endif
  19167. return res;
  19168. } /* END wc_ed25519_import_public */
  19169. /*
  19170. * Testing wc_ed25519_import_private_key()
  19171. */
  19172. static int test_wc_ed25519_import_private_key(void)
  19173. {
  19174. int res = TEST_SKIPPED;
  19175. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19176. WC_RNG rng;
  19177. ed25519_key key;
  19178. int ret;
  19179. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19180. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19181. word32 privKeySz = sizeof(privKey);
  19182. word32 pubKeySz = sizeof(pubKey);
  19183. #ifdef HAVE_ED25519_KEY_EXPORT
  19184. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19185. word32 bothKeysSz = sizeof(bothKeys);
  19186. #endif
  19187. ret = wc_InitRng(&rng);
  19188. if (ret != 0) {
  19189. return ret;
  19190. }
  19191. ret = wc_ed25519_init(&key);
  19192. if (ret != 0) {
  19193. wc_FreeRng(&rng);
  19194. return ret;
  19195. }
  19196. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19197. if (ret == 0) {
  19198. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19199. pubKeySz, &key, 1);
  19200. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19201. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19202. ret = WOLFSSL_FATAL_ERROR;
  19203. }
  19204. }
  19205. #ifdef HAVE_ED25519_KEY_EXPORT
  19206. if (ret == 0)
  19207. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  19208. if (ret == 0) {
  19209. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19210. &key, 1);
  19211. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19212. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19213. ret = WOLFSSL_FATAL_ERROR;
  19214. }
  19215. }
  19216. #endif
  19217. /* Test bad args. */
  19218. if (ret == 0) {
  19219. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19220. &key);
  19221. if (ret == BAD_FUNC_ARG) {
  19222. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19223. pubKeySz, &key);
  19224. }
  19225. if (ret == BAD_FUNC_ARG) {
  19226. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19227. pubKeySz, NULL);
  19228. }
  19229. if (ret == BAD_FUNC_ARG) {
  19230. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19231. pubKeySz, &key);
  19232. }
  19233. if (ret == BAD_FUNC_ARG) {
  19234. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19235. pubKeySz - 1, &key);
  19236. }
  19237. if (ret == BAD_FUNC_ARG) {
  19238. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19239. 0, &key);
  19240. }
  19241. if (ret == BAD_FUNC_ARG) {
  19242. ret = 0;
  19243. }
  19244. else if (ret == 0) {
  19245. ret = WOLFSSL_FATAL_ERROR;
  19246. }
  19247. }
  19248. if (wc_FreeRng(&rng) && ret == 0) {
  19249. ret = WOLFSSL_FATAL_ERROR;
  19250. }
  19251. wc_ed25519_free(&key);
  19252. res = TEST_RES_CHECK(ret == 0);
  19253. #endif
  19254. return res;
  19255. } /* END test_wc_ed25519_import_private_key */
  19256. /*
  19257. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19258. */
  19259. static int test_wc_ed25519_export(void)
  19260. {
  19261. int res = TEST_SKIPPED;
  19262. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19263. WC_RNG rng;
  19264. ed25519_key key;
  19265. int ret = 0;
  19266. byte priv[ED25519_PRV_KEY_SIZE];
  19267. byte pub[ED25519_PUB_KEY_SIZE];
  19268. word32 privSz = sizeof(priv);
  19269. word32 pubSz = sizeof(pub);
  19270. ret = wc_InitRng(&rng);
  19271. if (ret != 0) {
  19272. return ret;
  19273. }
  19274. ret = wc_ed25519_init(&key);
  19275. if (ret != 0) {
  19276. wc_FreeRng(&rng);
  19277. return ret;
  19278. }
  19279. if (ret == 0) {
  19280. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19281. }
  19282. if (ret == 0) {
  19283. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19284. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19285. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19286. ret = WOLFSSL_FATAL_ERROR;
  19287. }
  19288. if (ret == 0) {
  19289. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19290. if (ret == BAD_FUNC_ARG) {
  19291. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19292. }
  19293. if (ret == BAD_FUNC_ARG) {
  19294. ret = wc_ed25519_export_public(&key, pub, NULL);
  19295. }
  19296. if (ret == BAD_FUNC_ARG) {
  19297. ret = 0;
  19298. }
  19299. else if (ret == 0) {
  19300. ret = WOLFSSL_FATAL_ERROR;
  19301. }
  19302. }
  19303. }
  19304. if (ret == 0) {
  19305. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19306. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19307. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19308. ret = WOLFSSL_FATAL_ERROR;
  19309. }
  19310. if (ret == 0) {
  19311. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19312. if (ret == BAD_FUNC_ARG) {
  19313. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19314. }
  19315. if (ret == BAD_FUNC_ARG) {
  19316. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19317. }
  19318. if (ret == BAD_FUNC_ARG) {
  19319. ret = 0;
  19320. }
  19321. else if (ret == 0) {
  19322. ret = WOLFSSL_FATAL_ERROR;
  19323. }
  19324. }
  19325. }
  19326. if (wc_FreeRng(&rng) && ret == 0) {
  19327. ret = WOLFSSL_FATAL_ERROR;
  19328. }
  19329. wc_ed25519_free(&key);
  19330. res = TEST_RES_CHECK(ret == 0);
  19331. #endif
  19332. return res;
  19333. } /* END test_wc_ed25519_export */
  19334. /*
  19335. * Testing wc_ed25519_size()
  19336. */
  19337. static int test_wc_ed25519_size(void)
  19338. {
  19339. int res = TEST_SKIPPED;
  19340. #if defined(HAVE_ED25519)
  19341. WC_RNG rng;
  19342. ed25519_key key;
  19343. int ret;
  19344. ret = wc_InitRng(&rng);
  19345. if (ret != 0) {
  19346. return ret;
  19347. }
  19348. ret = wc_ed25519_init(&key);
  19349. if (ret != 0) {
  19350. wc_FreeRng(&rng);
  19351. return ret;
  19352. }
  19353. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19354. if (ret != 0) {
  19355. wc_FreeRng(&rng);
  19356. wc_ed25519_free(&key);
  19357. return ret;
  19358. }
  19359. ret = wc_ed25519_size(&key);
  19360. /* Test bad args. */
  19361. if (ret == ED25519_KEY_SIZE) {
  19362. ret = wc_ed25519_size(NULL);
  19363. if (ret == BAD_FUNC_ARG) {
  19364. ret = 0;
  19365. }
  19366. }
  19367. if (ret == 0) {
  19368. ret = wc_ed25519_sig_size(&key);
  19369. if (ret == ED25519_SIG_SIZE) {
  19370. ret = 0;
  19371. }
  19372. /* Test bad args. */
  19373. if (ret == 0) {
  19374. ret = wc_ed25519_sig_size(NULL);
  19375. if (ret == BAD_FUNC_ARG) {
  19376. ret = 0;
  19377. }
  19378. }
  19379. } /* END wc_ed25519_sig_size() */
  19380. if (ret == 0) {
  19381. ret = wc_ed25519_pub_size(&key);
  19382. if (ret == ED25519_PUB_KEY_SIZE) {
  19383. ret = 0;
  19384. }
  19385. if (ret == 0) {
  19386. ret = wc_ed25519_pub_size(NULL);
  19387. if (ret == BAD_FUNC_ARG) {
  19388. ret = 0;
  19389. }
  19390. }
  19391. } /* END wc_ed25519_pub_size */
  19392. if (ret == 0) {
  19393. ret = wc_ed25519_priv_size(&key);
  19394. if (ret == ED25519_PRV_KEY_SIZE) {
  19395. ret = 0;
  19396. }
  19397. if (ret == 0) {
  19398. ret = wc_ed25519_priv_size(NULL);
  19399. if (ret == BAD_FUNC_ARG) {
  19400. ret = 0;
  19401. }
  19402. }
  19403. } /* END wc_ed25519_pub_size */
  19404. if (wc_FreeRng(&rng) && ret == 0) {
  19405. ret = WOLFSSL_FATAL_ERROR;
  19406. }
  19407. wc_ed25519_free(&key);
  19408. res = TEST_RES_CHECK(ret == 0);
  19409. #endif
  19410. return res;
  19411. } /* END test_wc_ed25519_size */
  19412. /*
  19413. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19414. */
  19415. static int test_wc_ed25519_exportKey(void)
  19416. {
  19417. int res = TEST_SKIPPED;
  19418. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19419. WC_RNG rng;
  19420. ed25519_key key;
  19421. int ret = 0;
  19422. byte priv[ED25519_PRV_KEY_SIZE];
  19423. byte pub[ED25519_PUB_KEY_SIZE];
  19424. byte privOnly[ED25519_PRV_KEY_SIZE];
  19425. word32 privSz = sizeof(priv);
  19426. word32 pubSz = sizeof(pub);
  19427. word32 privOnlySz = sizeof(privOnly);
  19428. ret = wc_InitRng(&rng);
  19429. if (ret != 0) {
  19430. return TEST_FAIL;
  19431. }
  19432. ret = wc_ed25519_init(&key);
  19433. if (ret != 0) {
  19434. wc_FreeRng(&rng);
  19435. return TEST_FAIL;
  19436. }
  19437. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19438. if (ret != 0) {
  19439. wc_FreeRng(&rng);
  19440. wc_ed25519_free(&key);
  19441. return TEST_FAIL;
  19442. }
  19443. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  19444. if (ret == 0) {
  19445. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  19446. if (ret == BAD_FUNC_ARG) {
  19447. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  19448. }
  19449. if (ret == BAD_FUNC_ARG) {
  19450. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  19451. }
  19452. if (ret == BAD_FUNC_ARG) {
  19453. ret = 0;
  19454. }
  19455. else if (ret == 0) {
  19456. ret = WOLFSSL_FATAL_ERROR;
  19457. }
  19458. }
  19459. if (ret == 0) {
  19460. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  19461. if (ret == 0) {
  19462. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  19463. if (ret == BAD_FUNC_ARG) {
  19464. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  19465. }
  19466. if (ret == BAD_FUNC_ARG) {
  19467. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  19468. }
  19469. if (ret == BAD_FUNC_ARG) {
  19470. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  19471. }
  19472. if (ret == BAD_FUNC_ARG) {
  19473. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  19474. }
  19475. if (ret == BAD_FUNC_ARG) {
  19476. ret = 0;
  19477. }
  19478. else if (ret == 0) {
  19479. ret = WOLFSSL_FATAL_ERROR;
  19480. }
  19481. }
  19482. } /* END wc_ed25519_export_key() */
  19483. /* Cross check output. */
  19484. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19485. ret = WOLFSSL_FATAL_ERROR;
  19486. }
  19487. if (wc_FreeRng(&rng) && ret == 0) {
  19488. ret = WOLFSSL_FATAL_ERROR;
  19489. }
  19490. wc_ed25519_free(&key);
  19491. res = TEST_RES_CHECK(ret == 0);
  19492. #endif
  19493. return res;
  19494. } /* END test_wc_ed25519_exportKey */
  19495. /*
  19496. * Testing wc_Ed25519PublicKeyToDer
  19497. */
  19498. static int test_wc_Ed25519PublicKeyToDer(void)
  19499. {
  19500. int res = TEST_SKIPPED;
  19501. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19502. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19503. int tmp;
  19504. ed25519_key key;
  19505. byte derBuf[1024];
  19506. int ret = 0;
  19507. /* Test bad args */
  19508. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  19509. if (tmp != BAD_FUNC_ARG) {
  19510. ret = WOLFSSL_FATAL_ERROR;
  19511. }
  19512. if (ret == 0) {
  19513. wc_ed25519_init(&key);
  19514. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  19515. if (tmp != BUFFER_E) {
  19516. ret = WOLFSSL_FATAL_ERROR;
  19517. }
  19518. wc_ed25519_free(&key);
  19519. }
  19520. /* Test good args */
  19521. if (ret == 0) {
  19522. WC_RNG rng;
  19523. ret = wc_InitRng(&rng);
  19524. if (ret != 0) {
  19525. return TEST_FAIL;
  19526. }
  19527. ret = wc_ed25519_init(&key);
  19528. if (ret != 0) {
  19529. wc_FreeRng(&rng);
  19530. return TEST_FAIL;
  19531. }
  19532. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19533. if (ret != 0) {
  19534. wc_FreeRng(&rng);
  19535. wc_ed25519_free(&key);
  19536. return TEST_FAIL;
  19537. }
  19538. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  19539. if (tmp <= 0) {
  19540. ret = WOLFSSL_FATAL_ERROR;
  19541. }
  19542. wc_FreeRng(&rng);
  19543. wc_ed25519_free(&key);
  19544. }
  19545. res = TEST_RES_CHECK(ret == 0);
  19546. #endif
  19547. return res;
  19548. } /* END testing wc_Ed25519PublicKeyToDer */
  19549. /*
  19550. * Testing wc_curve25519_init and wc_curve25519_free.
  19551. */
  19552. static int test_wc_curve25519_init(void)
  19553. {
  19554. int res = TEST_SKIPPED;
  19555. #if defined(HAVE_CURVE25519)
  19556. curve25519_key key;
  19557. int ret = 0;
  19558. ret = wc_curve25519_init(&key);
  19559. /* Test bad args for wc_curve25519_init */
  19560. if (ret == 0) {
  19561. ret = wc_curve25519_init(NULL);
  19562. if (ret == BAD_FUNC_ARG) {
  19563. ret = 0;
  19564. }
  19565. else if (ret == 0) {
  19566. ret = WOLFSSL_FATAL_ERROR;
  19567. }
  19568. }
  19569. /* Test good args for wc_curve_25519_free */
  19570. wc_curve25519_free(&key);
  19571. wc_curve25519_free(NULL);
  19572. res = TEST_RES_CHECK(ret == 0);
  19573. #endif
  19574. return res;
  19575. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19576. /*
  19577. * Testing test_wc_curve25519_size.
  19578. */
  19579. static int test_wc_curve25519_size(void)
  19580. {
  19581. int res = TEST_SKIPPED;
  19582. #if defined(HAVE_CURVE25519)
  19583. curve25519_key key;
  19584. int ret = 0;
  19585. ret = wc_curve25519_init(&key);
  19586. /* Test good args for wc_curve25519_size */
  19587. if (ret == 0) {
  19588. ret = wc_curve25519_size(&key);
  19589. }
  19590. /* Test bad args for wc_curve25519_size */
  19591. if (ret != 0) {
  19592. ret = wc_curve25519_size(NULL);
  19593. }
  19594. wc_curve25519_free(&key);
  19595. res = TEST_RES_CHECK(ret == 0);
  19596. #endif
  19597. return res;
  19598. } /* END test_wc_curve25519_size*/
  19599. /*
  19600. * Testing test_wc_curve25519_export_key_raw().
  19601. */
  19602. static int test_wc_curve25519_export_key_raw(void)
  19603. {
  19604. int res = TEST_SKIPPED;
  19605. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19606. curve25519_key key;
  19607. WC_RNG rng;
  19608. int ret = 0;
  19609. byte privateKey[CURVE25519_KEYSIZE];
  19610. byte publicKey[CURVE25519_KEYSIZE];
  19611. word32 prvkSz;
  19612. word32 pubkSz;
  19613. byte prik[CURVE25519_KEYSIZE];
  19614. byte pubk[CURVE25519_KEYSIZE];
  19615. word32 prksz;
  19616. word32 pbksz;
  19617. if (0 != wc_InitRng(&rng)) {
  19618. return TEST_FAIL;
  19619. }
  19620. if (0 != wc_curve25519_init(&key)) {
  19621. wc_FreeRng(&rng);
  19622. return TEST_FAIL;
  19623. }
  19624. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19625. /*
  19626. bad-argument-test cases
  19627. target function sould return BAD_FUNC_ARG
  19628. */
  19629. if (ret == 0) {
  19630. prvkSz = CURVE25519_KEYSIZE;
  19631. pubkSz = CURVE25519_KEYSIZE;
  19632. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19633. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  19634. ret = -1;
  19635. }
  19636. }
  19637. if (ret == 0) {
  19638. prvkSz = CURVE25519_KEYSIZE;
  19639. pubkSz = CURVE25519_KEYSIZE;
  19640. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19641. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  19642. ret = -1;
  19643. }
  19644. }
  19645. if (ret == 0) {
  19646. prvkSz = CURVE25519_KEYSIZE;
  19647. pubkSz = CURVE25519_KEYSIZE;
  19648. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19649. &key, privateKey, NULL, publicKey, &pubkSz)) {
  19650. ret = -1;
  19651. }
  19652. }
  19653. if (ret == 0) {
  19654. /* prvkSz = CURVE25519_KEYSIZE; */
  19655. pubkSz = CURVE25519_KEYSIZE;
  19656. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19657. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  19658. ret = -1;
  19659. }
  19660. }
  19661. if (ret == 0) {
  19662. prvkSz = CURVE25519_KEYSIZE;
  19663. pubkSz = CURVE25519_KEYSIZE;
  19664. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19665. &key, privateKey, &prvkSz, publicKey, NULL )) {
  19666. ret = -1;
  19667. }
  19668. }
  19669. /*
  19670. cross-testing
  19671. */
  19672. if (ret == 0) {
  19673. prksz = CURVE25519_KEYSIZE;
  19674. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  19675. }
  19676. if (ret == 0) {
  19677. pbksz = CURVE25519_KEYSIZE;
  19678. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  19679. }
  19680. if (ret == 0) {
  19681. prvkSz = CURVE25519_KEYSIZE;
  19682. /* pubkSz = CURVE25519_KEYSIZE; */
  19683. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19684. publicKey, &pubkSz);
  19685. }
  19686. if (ret == 0) {
  19687. if ((prksz == CURVE25519_KEYSIZE) &&
  19688. (pbksz == CURVE25519_KEYSIZE) &&
  19689. (prvkSz == CURVE25519_KEYSIZE) &&
  19690. (pubkSz == CURVE25519_KEYSIZE)) {
  19691. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19692. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  19693. ret = -1;
  19694. }
  19695. }
  19696. }
  19697. wc_curve25519_free(&key);
  19698. wc_FreeRng(&rng);
  19699. res = TEST_RES_CHECK(ret == 0);
  19700. #endif
  19701. return res;
  19702. } /* end of test_wc_curve25519_export_key_raw */
  19703. /*
  19704. * Testing test_wc_curve25519_export_key_raw_ex().
  19705. */
  19706. static int test_wc_curve25519_export_key_raw_ex(void)
  19707. {
  19708. int res = TEST_SKIPPED;
  19709. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19710. curve25519_key key;
  19711. WC_RNG rng;
  19712. int ret;
  19713. byte privateKey[CURVE25519_KEYSIZE];
  19714. byte publicKey[CURVE25519_KEYSIZE];
  19715. word32 prvkSz;
  19716. word32 pubkSz;
  19717. byte prik[CURVE25519_KEYSIZE];
  19718. byte pubk[CURVE25519_KEYSIZE];
  19719. word32 prksz;
  19720. word32 pbksz;
  19721. if (0 != wc_InitRng(&rng)) {
  19722. return TEST_FAIL;
  19723. }
  19724. if (0 != wc_curve25519_init(&key)) {
  19725. wc_FreeRng(&rng);
  19726. return TEST_FAIL;
  19727. }
  19728. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19729. /*
  19730. bad-argument-test cases
  19731. target function sould return BAD_FUNC_ARG
  19732. */
  19733. if (ret == 0) {
  19734. prvkSz = CURVE25519_KEYSIZE;
  19735. pubkSz = CURVE25519_KEYSIZE;
  19736. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19737. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19738. ret = -1;
  19739. }
  19740. }
  19741. if (ret == 0) {
  19742. prvkSz = CURVE25519_KEYSIZE;
  19743. pubkSz = CURVE25519_KEYSIZE;
  19744. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19745. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19746. ret = -1;
  19747. }
  19748. }
  19749. if (ret == 0) {
  19750. prvkSz = CURVE25519_KEYSIZE;
  19751. pubkSz = CURVE25519_KEYSIZE;
  19752. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19753. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19754. ret = -1;
  19755. }
  19756. }
  19757. if (ret == 0) {
  19758. /* prvkSz = CURVE25519_KEYSIZE; */
  19759. pubkSz = CURVE25519_KEYSIZE;
  19760. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19761. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  19762. ret = -1;
  19763. }
  19764. }
  19765. if (ret == 0) {
  19766. prvkSz = CURVE25519_KEYSIZE;
  19767. pubkSz = CURVE25519_KEYSIZE;
  19768. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19769. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  19770. ret = -1;
  19771. }
  19772. }
  19773. if (ret == 0) {
  19774. prvkSz = CURVE25519_KEYSIZE;
  19775. /* pubkSz = CURVE25519_KEYSIZE; */
  19776. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19777. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19778. ret = -1;
  19779. }
  19780. }
  19781. if (ret == 0) {
  19782. prvkSz = CURVE25519_KEYSIZE;
  19783. pubkSz = CURVE25519_KEYSIZE;
  19784. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  19785. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19786. ret = -1;
  19787. }
  19788. }
  19789. if (ret == 0) {
  19790. prvkSz = CURVE25519_KEYSIZE;
  19791. pubkSz = CURVE25519_KEYSIZE;
  19792. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19793. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19794. ret = -1;
  19795. }
  19796. }
  19797. if (ret == 0) {
  19798. /* prvkSz = CURVE25519_KEYSIZE; */
  19799. pubkSz = CURVE25519_KEYSIZE;
  19800. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19801. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  19802. ret = -1;
  19803. }
  19804. }
  19805. if (ret == 0) {
  19806. prvkSz = CURVE25519_KEYSIZE;
  19807. pubkSz = CURVE25519_KEYSIZE;
  19808. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19809. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  19810. ret = -1;
  19811. }
  19812. }
  19813. /* illegal value for endien */
  19814. if (ret == 0) {
  19815. prvkSz = CURVE25519_KEYSIZE;
  19816. /* pubkSz = CURVE25519_KEYSIZE; */
  19817. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  19818. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  19819. ret = -1;
  19820. }
  19821. }
  19822. /*
  19823. cross-testing
  19824. */
  19825. if (ret == 0) {
  19826. prksz = CURVE25519_KEYSIZE;
  19827. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  19828. }
  19829. if (ret == 0) {
  19830. pbksz = CURVE25519_KEYSIZE;
  19831. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  19832. }
  19833. if (ret == 0) {
  19834. prvkSz = CURVE25519_KEYSIZE;
  19835. /* pubkSz = CURVE25519_KEYSIZE; */
  19836. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19837. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19838. }
  19839. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  19840. pbksz != CURVE25519_KEYSIZE ||
  19841. prvkSz != CURVE25519_KEYSIZE ||
  19842. pubkSz != CURVE25519_KEYSIZE)) {
  19843. ret = -1;
  19844. }
  19845. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  19846. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  19847. ret = -1;
  19848. }
  19849. if (ret == 0) {
  19850. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19851. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  19852. }
  19853. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19854. pubkSz != CURVE25519_KEYSIZE)) {
  19855. ret = -1;
  19856. }
  19857. /*
  19858. try once with another endian
  19859. */
  19860. if (ret == 0) {
  19861. prvkSz = CURVE25519_KEYSIZE;
  19862. pubkSz = CURVE25519_KEYSIZE;
  19863. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19864. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  19865. }
  19866. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  19867. pubkSz != CURVE25519_KEYSIZE)) {
  19868. ret = -1;
  19869. }
  19870. wc_curve25519_free(&key);
  19871. wc_FreeRng(&rng);
  19872. res = TEST_RES_CHECK(ret == 0);
  19873. #endif
  19874. return res;
  19875. } /* end of test_wc_curve25519_export_key_raw_ex */
  19876. /*
  19877. * Testing wc_curve25519_make_key
  19878. */
  19879. static int test_wc_curve25519_make_key(void)
  19880. {
  19881. int res = TEST_SKIPPED;
  19882. #if defined(HAVE_CURVE25519)
  19883. WC_RNG rng;
  19884. curve25519_key key;
  19885. int keysize;
  19886. int ret;
  19887. ret = wc_curve25519_init(&key);
  19888. if (ret == 0) {
  19889. ret = wc_InitRng(&rng);
  19890. }
  19891. if (ret == 0) {
  19892. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19893. if (ret == 0) {
  19894. keysize = wc_curve25519_size(&key);
  19895. if (keysize != CURVE25519_KEYSIZE) {
  19896. ret = WOLFSSL_FATAL_ERROR;
  19897. }
  19898. }
  19899. if (ret == 0) {
  19900. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19901. }
  19902. }
  19903. /*test bad cases*/
  19904. if (ret == 0) {
  19905. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19906. if (ret == BAD_FUNC_ARG) {
  19907. ret = 0;
  19908. }
  19909. }
  19910. if (ret == 0) {
  19911. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19912. if (ret == BAD_FUNC_ARG) {
  19913. ret = 0;
  19914. }
  19915. }
  19916. if (ret == 0) {
  19917. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19918. if (ret == BAD_FUNC_ARG) {
  19919. ret = 0;
  19920. }
  19921. }
  19922. if (ret == 0) {
  19923. ret = wc_curve25519_make_key(&rng, 0, &key);
  19924. if (ret == ECC_BAD_ARG_E) {
  19925. ret = 0;
  19926. }
  19927. }
  19928. wc_curve25519_free(&key);
  19929. wc_FreeRng(&rng);
  19930. res = TEST_RES_CHECK(ret == 0);
  19931. #endif
  19932. return res;
  19933. } /*END test_wc_curve25519_make_key*/
  19934. /*
  19935. * Testing wc_curve25519_shared_secret_ex
  19936. */
  19937. static int test_wc_curve25519_shared_secret_ex(void)
  19938. {
  19939. int res = TEST_SKIPPED;
  19940. #if defined(HAVE_CURVE25519)
  19941. WC_RNG rng;
  19942. curve25519_key private_key, public_key;
  19943. byte out[CURVE25519_KEYSIZE];
  19944. word32 outLen = sizeof(out);
  19945. int endian = EC25519_BIG_ENDIAN;
  19946. int ret;
  19947. ret = wc_curve25519_init(&private_key);
  19948. if (ret == 0) {
  19949. ret = wc_curve25519_init(&public_key);
  19950. }
  19951. if (ret == 0) {
  19952. ret = wc_InitRng(&rng);
  19953. }
  19954. if (ret == 0) {
  19955. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19956. }
  19957. if (ret == 0) {
  19958. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19959. }
  19960. if (ret == 0) {
  19961. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19962. &outLen, endian);
  19963. }
  19964. /*test bad cases*/
  19965. if (ret == 0) {
  19966. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19967. 0, endian);
  19968. if (ret == 0) {
  19969. ret = -1;
  19970. }
  19971. if (ret == BAD_FUNC_ARG) {
  19972. ret = 0;
  19973. }
  19974. }
  19975. if (ret == 0) {
  19976. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19977. &outLen, endian);
  19978. if (ret == 0) {
  19979. ret = -1;
  19980. }
  19981. else if (ret == BAD_FUNC_ARG) {
  19982. ret = 0;
  19983. }
  19984. }
  19985. if (ret == 0) {
  19986. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19987. &outLen, endian);
  19988. if (ret == 0) {
  19989. ret = -1;
  19990. }
  19991. else if (ret == BAD_FUNC_ARG) {
  19992. ret = 0;
  19993. }
  19994. }
  19995. if (ret == 0) {
  19996. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19997. &outLen, endian);
  19998. if (ret == 0) {
  19999. ret = -1;
  20000. }
  20001. else if (ret == BAD_FUNC_ARG) {
  20002. ret = 0;
  20003. }
  20004. }
  20005. if (ret == 0) {
  20006. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20007. NULL, endian);
  20008. if (ret == 0) {
  20009. ret = -1;
  20010. }
  20011. else if (ret == BAD_FUNC_ARG) {
  20012. ret = 0;
  20013. }
  20014. }
  20015. if (ret == 0) {
  20016. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  20017. *increasing to 0x8f checks for error being returned*/
  20018. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20019. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20020. &outLen, endian);
  20021. if (ret == 0) {
  20022. ret = -1;
  20023. }
  20024. else if (ret == ECC_BAD_ARG_E) {
  20025. ret = 0;
  20026. }
  20027. }
  20028. outLen = outLen - 2;
  20029. if (ret == 0) {
  20030. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20031. &outLen, endian);
  20032. if (ret == 0) {
  20033. ret = -1;
  20034. }
  20035. else if (ret == BAD_FUNC_ARG) {
  20036. ret = 0;
  20037. }
  20038. }
  20039. wc_curve25519_free(&private_key);
  20040. wc_curve25519_free(&public_key);
  20041. wc_FreeRng(&rng);
  20042. res = TEST_RES_CHECK(ret == 0);
  20043. #endif
  20044. return res;
  20045. } /*END test_wc_curve25519_shared_secret_ex*/
  20046. /*
  20047. * Testing wc_curve25519_make_pub
  20048. */
  20049. static int test_wc_curve25519_make_pub(void)
  20050. {
  20051. int res = TEST_SKIPPED;
  20052. #ifdef HAVE_CURVE25519
  20053. WC_RNG rng;
  20054. curve25519_key key;
  20055. byte out[CURVE25519_KEYSIZE];
  20056. int ret;
  20057. ret = wc_curve25519_init(&key);
  20058. if (ret == 0) {
  20059. ret = wc_InitRng(&rng);
  20060. if (ret == 0) {
  20061. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20062. }
  20063. }
  20064. if (ret == 0) {
  20065. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  20066. }
  20067. /*test bad cases*/
  20068. if (ret == 0) {
  20069. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  20070. if (ret == ECC_BAD_ARG_E) {
  20071. ret = 0;
  20072. }
  20073. }
  20074. if (ret == 0) {
  20075. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  20076. if (ret == ECC_BAD_ARG_E) {
  20077. ret = 0;
  20078. }
  20079. }
  20080. if (ret == 0) {
  20081. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  20082. if (ret == ECC_BAD_ARG_E) {
  20083. ret = 0;
  20084. }
  20085. }
  20086. if (ret == 0) {
  20087. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  20088. if (ret == ECC_BAD_ARG_E) {
  20089. ret = 0;
  20090. }
  20091. }
  20092. if (ret == 0) {
  20093. /* verify clamping test */
  20094. key.k[0] |= ~248;
  20095. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20096. if (ret == ECC_BAD_ARG_E) {
  20097. ret = 0;
  20098. }
  20099. key.k[0] &= 248;
  20100. }
  20101. /* repeat the expected-to-succeed test. */
  20102. if (ret == 0) {
  20103. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20104. }
  20105. wc_curve25519_free(&key);
  20106. wc_FreeRng(&rng);
  20107. res = TEST_RES_CHECK(ret == 0);
  20108. #endif
  20109. return res;
  20110. } /*END test_wc_curve25519_make_pub */
  20111. /*
  20112. * Testing test_wc_curve25519_export_public_ex
  20113. */
  20114. static int test_wc_curve25519_export_public_ex(void)
  20115. {
  20116. int res = TEST_SKIPPED;
  20117. #if defined(HAVE_CURVE25519)
  20118. WC_RNG rng;
  20119. curve25519_key key;
  20120. byte out[CURVE25519_KEYSIZE];
  20121. word32 outLen = sizeof(out);
  20122. int endian = EC25519_BIG_ENDIAN;
  20123. int ret;
  20124. ret = wc_curve25519_init(&key);
  20125. if (ret == 0) {
  20126. ret = wc_InitRng(&rng);
  20127. }
  20128. if (ret == 0) {
  20129. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20130. if (ret == 0) {
  20131. ret = wc_curve25519_export_public(&key, out, &outLen);
  20132. }
  20133. if (ret == 0) {
  20134. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20135. }
  20136. }
  20137. /*test bad cases*/
  20138. if (ret == 0) {
  20139. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  20140. if (ret == BAD_FUNC_ARG) {
  20141. ret = 0;
  20142. }
  20143. }
  20144. if (ret == 0) {
  20145. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20146. if (ret == BAD_FUNC_ARG) {
  20147. ret = 0;
  20148. }
  20149. }
  20150. if (ret == 0) {
  20151. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20152. if (ret == BAD_FUNC_ARG) {
  20153. ret = 0;
  20154. }
  20155. }
  20156. if (ret == 0) {
  20157. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20158. if (ret == BAD_FUNC_ARG) {
  20159. ret = 0;
  20160. }
  20161. }
  20162. outLen = outLen - 2;
  20163. if (ret == 0) {
  20164. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20165. if (ret == ECC_BAD_ARG_E) {
  20166. ret = 0;
  20167. }
  20168. }
  20169. wc_curve25519_free(&key);
  20170. wc_FreeRng(&rng);
  20171. res = TEST_RES_CHECK(ret == 0);
  20172. #endif
  20173. return res;
  20174. } /*END test_wc_curve25519_export_public_ex*/
  20175. /*
  20176. * Testing test_wc_curve25519_import_private_raw_ex
  20177. */
  20178. static int test_wc_curve25519_import_private_raw_ex(void)
  20179. {
  20180. int res = TEST_SKIPPED;
  20181. #if defined(HAVE_CURVE25519)
  20182. WC_RNG rng;
  20183. curve25519_key key;
  20184. byte priv[CURVE25519_KEYSIZE];
  20185. byte pub[CURVE25519_KEYSIZE];
  20186. word32 privSz = sizeof(priv);
  20187. word32 pubSz = sizeof(pub);
  20188. int endian = EC25519_BIG_ENDIAN;
  20189. int ret;
  20190. ret = wc_curve25519_init(&key);
  20191. if (ret == 0) {
  20192. ret = wc_InitRng(&rng);
  20193. }
  20194. if (ret == 0) {
  20195. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20196. if (ret == 0) {
  20197. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20198. }
  20199. if (ret == 0) {
  20200. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20201. }
  20202. if (ret == 0) {
  20203. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20204. &key, endian);
  20205. }
  20206. }
  20207. /*test bad cases*/
  20208. if (ret == 0) {
  20209. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20210. endian);
  20211. if (ret == BAD_FUNC_ARG) {
  20212. ret = 0;
  20213. }
  20214. }
  20215. if (ret == 0) {
  20216. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20217. &key, endian);
  20218. if (ret == BAD_FUNC_ARG) {
  20219. ret = 0;
  20220. }
  20221. }
  20222. if (ret == 0) {
  20223. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20224. &key, endian);
  20225. if (ret == BAD_FUNC_ARG) {
  20226. ret = 0;
  20227. }
  20228. }
  20229. if (ret == 0) {
  20230. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20231. NULL, endian);
  20232. if (ret == BAD_FUNC_ARG) {
  20233. ret = 0;
  20234. }
  20235. }
  20236. if (ret == 0) {
  20237. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20238. &key, endian);
  20239. if (ret == ECC_BAD_ARG_E) {
  20240. ret = 0;
  20241. }
  20242. }
  20243. if (ret == 0) {
  20244. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20245. &key, endian);
  20246. if (ret == ECC_BAD_ARG_E) {
  20247. ret = 0;
  20248. }
  20249. }
  20250. if (ret == 0) {
  20251. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20252. &key, EC25519_LITTLE_ENDIAN);
  20253. }
  20254. wc_curve25519_free(&key);
  20255. wc_FreeRng(&rng);
  20256. res = TEST_RES_CHECK(ret == 0);
  20257. #endif
  20258. return res;
  20259. } /*END test_wc_curve25519_import_private_raw_ex*/
  20260. /*
  20261. * Testing test_wc_curve25519_import_private
  20262. */
  20263. static int test_wc_curve25519_import_private(void)
  20264. {
  20265. int res = TEST_SKIPPED;
  20266. #if defined(HAVE_CURVE25519)
  20267. curve25519_key key;
  20268. WC_RNG rng;
  20269. byte priv[CURVE25519_KEYSIZE];
  20270. word32 privSz = sizeof(priv);
  20271. int ret;
  20272. ret = wc_curve25519_init(&key);
  20273. if (ret == 0) {
  20274. ret = wc_InitRng(&rng);
  20275. }
  20276. if (ret == 0) {
  20277. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20278. if (ret == 0) {
  20279. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20280. }
  20281. }
  20282. if (ret == 0) {
  20283. ret = wc_curve25519_import_private(priv, privSz, &key);
  20284. }
  20285. wc_curve25519_free(&key);
  20286. wc_FreeRng(&rng);
  20287. res = TEST_RES_CHECK(ret == 0);
  20288. #endif
  20289. return res;
  20290. } /*END test_wc_curve25519_import*/
  20291. /*
  20292. * Testing test_wc_curve25519_export_private_raw_ex
  20293. */
  20294. static int test_wc_curve25519_export_private_raw_ex(void)
  20295. {
  20296. int res = TEST_SKIPPED;
  20297. #if defined(HAVE_CURVE25519)
  20298. curve25519_key key;
  20299. byte out[CURVE25519_KEYSIZE];
  20300. word32 outLen = sizeof(out);
  20301. int endian = EC25519_BIG_ENDIAN;
  20302. int ret;
  20303. ret = wc_curve25519_init(&key);
  20304. if (ret == 0) {
  20305. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20306. }
  20307. /*test bad cases*/
  20308. if (ret == 0) {
  20309. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20310. if (ret == BAD_FUNC_ARG) {
  20311. ret = 0;
  20312. }
  20313. }
  20314. if (ret == 0) {
  20315. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20316. if (ret == BAD_FUNC_ARG) {
  20317. ret = 0;
  20318. }
  20319. }
  20320. if (ret == 0) {
  20321. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20322. if (ret == BAD_FUNC_ARG) {
  20323. ret = 0;
  20324. }
  20325. }
  20326. if (ret == 0) {
  20327. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20328. if (ret == BAD_FUNC_ARG) {
  20329. ret = 0;
  20330. }
  20331. }
  20332. if (ret == 0) {
  20333. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20334. EC25519_LITTLE_ENDIAN);
  20335. }
  20336. outLen = outLen - 2;
  20337. if (ret == 0) {
  20338. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20339. if (ret == ECC_BAD_ARG_E) {
  20340. ret = 0;
  20341. }
  20342. }
  20343. wc_curve25519_free(&key);
  20344. res = TEST_RES_CHECK(ret == 0);
  20345. #endif
  20346. return res;
  20347. }/*END test_wc_curve25519_export_private_raw_ex*/
  20348. /*
  20349. * Testing wc_ed448_make_key().
  20350. */
  20351. static int test_wc_ed448_make_key(void)
  20352. {
  20353. int res = TEST_SKIPPED;
  20354. #if defined(HAVE_ED448)
  20355. ed448_key key;
  20356. WC_RNG rng;
  20357. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20358. int ret;
  20359. ret = wc_InitRng(&rng);
  20360. if (ret == 0) {
  20361. ret = wc_ed448_init(&key);
  20362. }
  20363. if (ret == 0) {
  20364. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20365. if (ret == ECC_PRIV_KEY_E) {
  20366. ret = 0;
  20367. }
  20368. else if (ret == 0) {
  20369. ret = -1;
  20370. }
  20371. }
  20372. if (ret == 0) {
  20373. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20374. }
  20375. /* Test bad args. */
  20376. if (ret == 0) {
  20377. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20378. if (ret == BAD_FUNC_ARG) {
  20379. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20380. }
  20381. if (ret == BAD_FUNC_ARG) {
  20382. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20383. }
  20384. if (ret == BAD_FUNC_ARG) {
  20385. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20386. }
  20387. if (ret == BAD_FUNC_ARG) {
  20388. ret = 0;
  20389. }
  20390. else if (ret == 0) {
  20391. ret = WOLFSSL_FATAL_ERROR;
  20392. }
  20393. }
  20394. if (wc_FreeRng(&rng) && ret == 0) {
  20395. ret = WOLFSSL_FATAL_ERROR;
  20396. }
  20397. wc_ed448_free(&key);
  20398. res = TEST_RES_CHECK(ret == 0);
  20399. #endif
  20400. return res;
  20401. } /* END test_wc_ed448_make_key */
  20402. /*
  20403. * Testing wc_ed448_init()
  20404. */
  20405. static int test_wc_ed448_init(void)
  20406. {
  20407. int res = TEST_SKIPPED;
  20408. #if defined(HAVE_ED448)
  20409. ed448_key key;
  20410. int ret;
  20411. ret = wc_ed448_init(&key);
  20412. /* Test bad args. */
  20413. if (ret == 0) {
  20414. ret = wc_ed448_init(NULL);
  20415. if (ret == BAD_FUNC_ARG) {
  20416. ret = 0;
  20417. }
  20418. else if (ret == 0) {
  20419. ret = WOLFSSL_FATAL_ERROR;
  20420. }
  20421. }
  20422. wc_ed448_free(&key);
  20423. res = TEST_RES_CHECK(ret == 0);
  20424. #endif
  20425. return res;
  20426. } /* END test_wc_ed448_init */
  20427. /*
  20428. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20429. */
  20430. static int test_wc_ed448_sign_msg(void)
  20431. {
  20432. int res = TEST_SKIPPED;
  20433. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20434. WC_RNG rng;
  20435. ed448_key key;
  20436. byte msg[] = "Everybody gets Friday off.\n";
  20437. byte sig[ED448_SIG_SIZE];
  20438. word32 msglen = sizeof(msg);
  20439. word32 siglen = sizeof(sig);
  20440. word32 badSigLen = sizeof(sig) - 1;
  20441. #ifdef HAVE_ED448_VERIFY
  20442. int verify_ok = 0; /*1 = Verify success.*/
  20443. #endif
  20444. int ret;
  20445. /* Initialize stack variables. */
  20446. XMEMSET(sig, 0, siglen);
  20447. /* Initialize key. */
  20448. ret = wc_InitRng(&rng);
  20449. if (ret == 0) {
  20450. ret = wc_ed448_init(&key);
  20451. if (ret == 0) {
  20452. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20453. }
  20454. }
  20455. if (ret == 0) {
  20456. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  20457. }
  20458. /* Test bad args. */
  20459. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  20460. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  20461. if (ret == BAD_FUNC_ARG) {
  20462. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  20463. }
  20464. if (ret == BAD_FUNC_ARG) {
  20465. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  20466. }
  20467. if (ret == BAD_FUNC_ARG) {
  20468. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  20469. }
  20470. if (ret == BAD_FUNC_ARG) {
  20471. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  20472. NULL, 0);
  20473. }
  20474. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  20475. badSigLen -= 1;
  20476. ret = 0;
  20477. }
  20478. else if (ret == 0) {
  20479. ret = WOLFSSL_FATAL_ERROR;
  20480. }
  20481. } /* END sign */
  20482. #ifdef HAVE_ED448_VERIFY
  20483. if (ret == 0) {
  20484. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20485. &key, NULL, 0);
  20486. if (ret == 0 && verify_ok == 1) {
  20487. ret = 0;
  20488. }
  20489. else if (ret == 0) {
  20490. ret = WOLFSSL_FATAL_ERROR;
  20491. }
  20492. /* Test bad args. */
  20493. if (ret == 0) {
  20494. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  20495. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20496. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  20497. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  20498. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20499. &key, NULL, 0);
  20500. if (ret == BAD_FUNC_ARG) {
  20501. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  20502. &verify_ok, &key, NULL, 0);
  20503. }
  20504. if (ret == BAD_FUNC_ARG) {
  20505. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20506. NULL, &key, NULL, 0);
  20507. }
  20508. if (ret == BAD_FUNC_ARG) {
  20509. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20510. &verify_ok, NULL, NULL, 0);
  20511. }
  20512. if (ret == BAD_FUNC_ARG) {
  20513. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  20514. &verify_ok, &key, NULL, 0);
  20515. }
  20516. if (ret == BAD_FUNC_ARG) {
  20517. ret = 0;
  20518. }
  20519. else if (ret == 0) {
  20520. ret = WOLFSSL_FATAL_ERROR;
  20521. }
  20522. }
  20523. } /* END verify. */
  20524. #endif /* Verify. */
  20525. if (wc_FreeRng(&rng) && ret == 0) {
  20526. ret = WOLFSSL_FATAL_ERROR;
  20527. }
  20528. wc_ed448_free(&key);
  20529. res = TEST_RES_CHECK(ret == 0);
  20530. #endif
  20531. return res;
  20532. } /* END test_wc_ed448_sign_msg */
  20533. /*
  20534. * Testing wc_ed448_import_public()
  20535. */
  20536. static int test_wc_ed448_import_public(void)
  20537. {
  20538. int res = TEST_SKIPPED;
  20539. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20540. WC_RNG rng;
  20541. ed448_key pubKey;
  20542. const byte in[] =
  20543. "Ed448PublicKeyUnitTest.................................\n";
  20544. word32 inlen = sizeof(in);
  20545. int ret = 0;
  20546. ret = wc_InitRng(&rng);
  20547. if (ret == 0) {
  20548. ret = wc_ed448_init(&pubKey);
  20549. if (ret == 0) {
  20550. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  20551. }
  20552. }
  20553. if (ret == 0) {
  20554. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  20555. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  20556. ret = 0;
  20557. }
  20558. else {
  20559. ret = WOLFSSL_FATAL_ERROR;
  20560. }
  20561. /* Test bad args. */
  20562. if (ret == 0) {
  20563. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  20564. if (ret == BAD_FUNC_ARG) {
  20565. ret = wc_ed448_import_public(in, inlen, NULL);
  20566. }
  20567. if (ret == BAD_FUNC_ARG) {
  20568. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  20569. }
  20570. if (ret == BAD_FUNC_ARG) {
  20571. ret = 0;
  20572. }
  20573. else if (ret == 0) {
  20574. ret = WOLFSSL_FATAL_ERROR;
  20575. }
  20576. }
  20577. }
  20578. if (wc_FreeRng(&rng) && ret == 0) {
  20579. ret = WOLFSSL_FATAL_ERROR;
  20580. }
  20581. wc_ed448_free(&pubKey);
  20582. res = TEST_RES_CHECK(ret == 0);
  20583. #endif
  20584. return res;
  20585. } /* END wc_ed448_import_public */
  20586. /*
  20587. * Testing wc_ed448_import_private_key()
  20588. */
  20589. static int test_wc_ed448_import_private_key(void)
  20590. {
  20591. int res = TEST_SKIPPED;
  20592. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20593. WC_RNG rng;
  20594. ed448_key key;
  20595. const byte privKey[] =
  20596. "Ed448PrivateKeyUnitTest................................\n";
  20597. const byte pubKey[] =
  20598. "Ed448PublicKeyUnitTest.................................\n";
  20599. word32 privKeySz = sizeof(privKey);
  20600. word32 pubKeySz = sizeof(pubKey);
  20601. #ifdef HAVE_ED448_KEY_EXPORT
  20602. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20603. word32 bothKeysSz = sizeof(bothKeys);
  20604. #endif
  20605. int ret;
  20606. ret = wc_InitRng(&rng);
  20607. if (ret != 0) {
  20608. return TEST_FAIL;
  20609. }
  20610. ret = wc_ed448_init(&key);
  20611. if (ret != 0) {
  20612. wc_FreeRng(&rng);
  20613. return TEST_FAIL;
  20614. }
  20615. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20616. if (ret == 0) {
  20617. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20618. pubKeySz, &key, 1);
  20619. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20620. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20621. ret = WOLFSSL_FATAL_ERROR;
  20622. }
  20623. }
  20624. #ifdef HAVE_ED448_KEY_EXPORT
  20625. if (ret == 0)
  20626. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  20627. if (ret == 0) {
  20628. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20629. &key, 1);
  20630. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20631. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20632. ret = WOLFSSL_FATAL_ERROR;
  20633. }
  20634. }
  20635. #endif
  20636. /* Test bad args. */
  20637. if (ret == 0) {
  20638. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20639. &key);
  20640. if (ret == BAD_FUNC_ARG) {
  20641. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20642. pubKeySz, &key);
  20643. }
  20644. if (ret == BAD_FUNC_ARG) {
  20645. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20646. pubKeySz, NULL);
  20647. }
  20648. if (ret == BAD_FUNC_ARG) {
  20649. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20650. pubKeySz, &key);
  20651. }
  20652. if (ret == BAD_FUNC_ARG) {
  20653. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20654. pubKeySz - 1, &key);
  20655. }
  20656. if (ret == BAD_FUNC_ARG) {
  20657. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20658. 0, &key);
  20659. }
  20660. if (ret == BAD_FUNC_ARG) {
  20661. ret = 0;
  20662. }
  20663. else if (ret == 0) {
  20664. ret = WOLFSSL_FATAL_ERROR;
  20665. }
  20666. }
  20667. if (wc_FreeRng(&rng) && ret == 0) {
  20668. ret = WOLFSSL_FATAL_ERROR;
  20669. }
  20670. wc_ed448_free(&key);
  20671. res = TEST_RES_CHECK(ret == 0);
  20672. #endif
  20673. return res;
  20674. } /* END test_wc_ed448_import_private_key */
  20675. /*
  20676. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20677. */
  20678. static int test_wc_ed448_export(void)
  20679. {
  20680. int res = TEST_SKIPPED;
  20681. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20682. WC_RNG rng;
  20683. ed448_key key;
  20684. byte priv[ED448_PRV_KEY_SIZE];
  20685. byte pub[ED448_PUB_KEY_SIZE];
  20686. word32 privSz = sizeof(priv);
  20687. word32 pubSz = sizeof(pub);
  20688. int ret;
  20689. ret = wc_InitRng(&rng);
  20690. if (ret != 0) {
  20691. return TEST_FAIL;
  20692. }
  20693. ret = wc_ed448_init(&key);
  20694. if (ret != 0) {
  20695. wc_FreeRng(&rng);
  20696. return TEST_FAIL;
  20697. }
  20698. if (ret == 0) {
  20699. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20700. }
  20701. if (ret == 0) {
  20702. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20703. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20704. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20705. ret = WOLFSSL_FATAL_ERROR;
  20706. }
  20707. if (ret == 0) {
  20708. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20709. if (ret == BAD_FUNC_ARG) {
  20710. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20711. }
  20712. if (ret == BAD_FUNC_ARG) {
  20713. ret = wc_ed448_export_public(&key, pub, NULL);
  20714. }
  20715. if (ret == BAD_FUNC_ARG) {
  20716. ret = 0;
  20717. }
  20718. else if (ret == 0) {
  20719. ret = WOLFSSL_FATAL_ERROR;
  20720. }
  20721. }
  20722. }
  20723. if (ret == 0) {
  20724. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20725. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20726. XMEMCMP(key.k, priv, privSz) != 0)) {
  20727. ret = WOLFSSL_FATAL_ERROR;
  20728. }
  20729. if (ret == 0) {
  20730. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20731. if (ret == BAD_FUNC_ARG) {
  20732. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20733. }
  20734. if (ret == BAD_FUNC_ARG) {
  20735. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20736. }
  20737. if (ret == BAD_FUNC_ARG) {
  20738. ret = 0;
  20739. }
  20740. else if (ret == 0) {
  20741. ret = WOLFSSL_FATAL_ERROR;
  20742. }
  20743. }
  20744. }
  20745. if (wc_FreeRng(&rng) && ret == 0) {
  20746. ret = WOLFSSL_FATAL_ERROR;
  20747. }
  20748. wc_ed448_free(&key);
  20749. res = TEST_RES_CHECK(ret == 0);
  20750. #endif
  20751. return res;
  20752. } /* END test_wc_ed448_export */
  20753. /*
  20754. * Testing wc_ed448_size()
  20755. */
  20756. static int test_wc_ed448_size(void)
  20757. {
  20758. int res = TEST_SKIPPED;
  20759. #if defined(HAVE_ED448)
  20760. WC_RNG rng;
  20761. ed448_key key;
  20762. int ret = 0;
  20763. ret = wc_InitRng(&rng);
  20764. if (ret != 0) {
  20765. return TEST_FAIL;
  20766. }
  20767. ret = wc_ed448_init(&key);
  20768. if (ret != 0) {
  20769. wc_FreeRng(&rng);
  20770. return TEST_FAIL;
  20771. }
  20772. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20773. if (ret != 0) {
  20774. wc_FreeRng(&rng);
  20775. wc_ed448_free(&key);
  20776. return TEST_FAIL;
  20777. }
  20778. ret = wc_ed448_size(&key);
  20779. /* Test bad args. */
  20780. if (ret == ED448_KEY_SIZE) {
  20781. ret = wc_ed448_size(NULL);
  20782. if (ret == BAD_FUNC_ARG) {
  20783. ret = 0;
  20784. }
  20785. }
  20786. if (ret == 0) {
  20787. ret = wc_ed448_sig_size(&key);
  20788. if (ret == ED448_SIG_SIZE) {
  20789. ret = 0;
  20790. }
  20791. /* Test bad args. */
  20792. if (ret == 0) {
  20793. ret = wc_ed448_sig_size(NULL);
  20794. if (ret == BAD_FUNC_ARG) {
  20795. ret = 0;
  20796. }
  20797. }
  20798. } /* END wc_ed448_sig_size() */
  20799. if (ret == 0) {
  20800. ret = wc_ed448_pub_size(&key);
  20801. if (ret == ED448_PUB_KEY_SIZE) {
  20802. ret = 0;
  20803. }
  20804. if (ret == 0) {
  20805. ret = wc_ed448_pub_size(NULL);
  20806. if (ret == BAD_FUNC_ARG) {
  20807. ret = 0;
  20808. }
  20809. }
  20810. } /* END wc_ed448_pub_size */
  20811. if (ret == 0) {
  20812. ret = wc_ed448_priv_size(&key);
  20813. if (ret == ED448_PRV_KEY_SIZE) {
  20814. ret = 0;
  20815. }
  20816. if (ret == 0) {
  20817. ret = wc_ed448_priv_size(NULL);
  20818. if (ret == BAD_FUNC_ARG) {
  20819. ret = 0;
  20820. }
  20821. }
  20822. } /* END wc_ed448_pub_size */
  20823. if (wc_FreeRng(&rng) && ret == 0) {
  20824. ret = WOLFSSL_FATAL_ERROR;
  20825. }
  20826. wc_ed448_free(&key);
  20827. res = TEST_RES_CHECK(ret == 0);
  20828. #endif
  20829. return res;
  20830. } /* END test_wc_ed448_size */
  20831. /*
  20832. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20833. */
  20834. static int test_wc_ed448_exportKey(void)
  20835. {
  20836. int res = TEST_SKIPPED;
  20837. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20838. WC_RNG rng;
  20839. ed448_key key;
  20840. byte priv[ED448_PRV_KEY_SIZE];
  20841. byte pub[ED448_PUB_KEY_SIZE];
  20842. byte privOnly[ED448_PRV_KEY_SIZE];
  20843. word32 privSz = sizeof(priv);
  20844. word32 pubSz = sizeof(pub);
  20845. word32 privOnlySz = sizeof(privOnly);
  20846. int ret;
  20847. ret = wc_InitRng(&rng);
  20848. if (ret != 0) {
  20849. return TEST_FAIL;
  20850. }
  20851. ret = wc_ed448_init(&key);
  20852. if (ret != 0) {
  20853. wc_FreeRng(&rng);
  20854. return TEST_FAIL;
  20855. }
  20856. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20857. if (ret != 0) {
  20858. wc_FreeRng(&rng);
  20859. wc_ed448_free(&key);
  20860. return TEST_FAIL;
  20861. }
  20862. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20863. if (ret == 0) {
  20864. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20865. if (ret == BAD_FUNC_ARG) {
  20866. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20867. }
  20868. if (ret == BAD_FUNC_ARG) {
  20869. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20870. }
  20871. if (ret == BAD_FUNC_ARG) {
  20872. ret = 0;
  20873. }
  20874. else if (ret == 0) {
  20875. ret = WOLFSSL_FATAL_ERROR;
  20876. }
  20877. }
  20878. if (ret == 0) {
  20879. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20880. if (ret == 0) {
  20881. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20882. if (ret == BAD_FUNC_ARG) {
  20883. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20884. }
  20885. if (ret == BAD_FUNC_ARG) {
  20886. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20887. }
  20888. if (ret == BAD_FUNC_ARG) {
  20889. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20890. }
  20891. if (ret == BAD_FUNC_ARG) {
  20892. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20893. }
  20894. if (ret == BAD_FUNC_ARG) {
  20895. ret = 0;
  20896. }
  20897. else if (ret == 0) {
  20898. ret = WOLFSSL_FATAL_ERROR;
  20899. }
  20900. }
  20901. } /* END wc_ed448_export_key() */
  20902. /* Cross check output. */
  20903. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20904. ret = WOLFSSL_FATAL_ERROR;
  20905. }
  20906. if (wc_FreeRng(&rng) && ret == 0) {
  20907. ret = WOLFSSL_FATAL_ERROR;
  20908. }
  20909. wc_ed448_free(&key);
  20910. res = TEST_RES_CHECK(ret == 0);
  20911. #endif
  20912. return res;
  20913. } /* END test_wc_ed448_exportKey */
  20914. /*
  20915. * Testing wc_Ed448PublicKeyToDer
  20916. */
  20917. static int test_wc_Ed448PublicKeyToDer(void)
  20918. {
  20919. int res = TEST_SKIPPED;
  20920. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20921. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20922. int tmp;
  20923. ed448_key key;
  20924. byte derBuf[1024];
  20925. int ret = 0;
  20926. /* Test bad args */
  20927. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20928. if (tmp != BAD_FUNC_ARG) {
  20929. ret = WOLFSSL_FATAL_ERROR;
  20930. }
  20931. if (ret == 0) {
  20932. wc_ed448_init(&key);
  20933. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20934. if (tmp != BUFFER_E) {
  20935. ret = WOLFSSL_FATAL_ERROR;
  20936. }
  20937. wc_ed448_free(&key);
  20938. }
  20939. /* Test good args */
  20940. if (ret == 0) {
  20941. WC_RNG rng;
  20942. ret = wc_InitRng(&rng);
  20943. if (ret != 0) {
  20944. return TEST_FAIL;
  20945. }
  20946. ret = wc_ed448_init(&key);
  20947. if (ret != 0) {
  20948. wc_FreeRng(&rng);
  20949. return TEST_FAIL;
  20950. }
  20951. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20952. if (ret != 0) {
  20953. wc_FreeRng(&rng);
  20954. wc_ed448_free(&key);
  20955. return TEST_FAIL;
  20956. }
  20957. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20958. if (tmp <= 0) {
  20959. ret = WOLFSSL_FATAL_ERROR;
  20960. }
  20961. wc_FreeRng(&rng);
  20962. wc_ed448_free(&key);
  20963. }
  20964. res = TEST_RES_CHECK(ret == 0);
  20965. #endif
  20966. return res;
  20967. } /* END testing wc_Ed448PublicKeyToDer */
  20968. /*
  20969. * Testing wc_curve448_init and wc_curve448_free.
  20970. */
  20971. static int test_wc_curve448_init(void)
  20972. {
  20973. int res = TEST_SKIPPED;
  20974. #if defined(HAVE_CURVE448)
  20975. curve448_key key;
  20976. int ret = 0;
  20977. ret = wc_curve448_init(&key);
  20978. /* Test bad args for wc_curve448_init */
  20979. if (ret == 0) {
  20980. ret = wc_curve448_init(NULL);
  20981. if (ret == BAD_FUNC_ARG) {
  20982. ret = 0;
  20983. }
  20984. else if (ret == 0) {
  20985. ret = WOLFSSL_FATAL_ERROR;
  20986. }
  20987. }
  20988. /* Test good args for wc_curve_448_free */
  20989. wc_curve448_free(&key);
  20990. wc_curve448_free(NULL);
  20991. res = TEST_RES_CHECK(ret == 0);
  20992. #endif
  20993. return res;
  20994. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20995. /*
  20996. * Testing wc_curve448_make_key
  20997. */
  20998. static int test_wc_curve448_make_key(void)
  20999. {
  21000. int res = TEST_SKIPPED;
  21001. #if defined(HAVE_CURVE448)
  21002. WC_RNG rng;
  21003. curve448_key key;
  21004. int keysize;
  21005. int ret;
  21006. ret = wc_curve448_init(&key);
  21007. if (ret == 0) {
  21008. ret = wc_InitRng(&rng);
  21009. }
  21010. if (ret == 0) {
  21011. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21012. if (ret == 0) {
  21013. keysize = wc_curve448_size(&key);
  21014. if (keysize != CURVE448_KEY_SIZE) {
  21015. ret = WOLFSSL_FATAL_ERROR;
  21016. }
  21017. }
  21018. if (ret == 0) {
  21019. ret = wc_curve448_make_key(&rng, keysize, &key);
  21020. }
  21021. }
  21022. /* test bad cases */
  21023. if (ret == 0) {
  21024. ret = wc_curve448_make_key(NULL, 0, NULL);
  21025. if (ret == BAD_FUNC_ARG) {
  21026. ret = 0;
  21027. }
  21028. }
  21029. if (ret == 0) {
  21030. ret = wc_curve448_make_key(&rng, keysize, NULL);
  21031. if (ret == BAD_FUNC_ARG) {
  21032. ret = 0;
  21033. }
  21034. }
  21035. if (ret == 0) {
  21036. ret = wc_curve448_make_key(NULL, keysize, &key);
  21037. if (ret == BAD_FUNC_ARG) {
  21038. ret = 0;
  21039. }
  21040. }
  21041. if (ret == 0) {
  21042. ret = wc_curve448_make_key(&rng, 0, &key);
  21043. if (ret == ECC_BAD_ARG_E) {
  21044. ret = 0;
  21045. }
  21046. }
  21047. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21048. ret = WOLFSSL_FATAL_ERROR;
  21049. }
  21050. wc_curve448_free(&key);
  21051. res = TEST_RES_CHECK(ret == 0);
  21052. #endif
  21053. return res;
  21054. } /*END test_wc_curve448_make_key*/
  21055. /*
  21056. * Testing test_wc_curve448_shared_secret_ex
  21057. */
  21058. static int test_wc_curve448_shared_secret_ex(void)
  21059. {
  21060. int res = TEST_SKIPPED;
  21061. #if defined(HAVE_CURVE448)
  21062. WC_RNG rng;
  21063. curve448_key private_key, public_key;
  21064. byte out[CURVE448_KEY_SIZE];
  21065. word32 outLen = sizeof(out);
  21066. int endian = EC448_BIG_ENDIAN;
  21067. int ret;
  21068. ret = wc_curve448_init(&private_key);
  21069. if (ret == 0) {
  21070. ret = wc_InitRng(&rng);
  21071. if (ret == 0) {
  21072. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  21073. }
  21074. }
  21075. if (ret == 0) {
  21076. ret = wc_curve448_init(&public_key);
  21077. }
  21078. if (ret == 0) {
  21079. if (ret == 0) {
  21080. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  21081. }
  21082. }
  21083. if (ret == 0) {
  21084. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21085. &outLen, endian);
  21086. }
  21087. /* test bad cases */
  21088. if (ret == 0) {
  21089. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  21090. if (ret == BAD_FUNC_ARG) {
  21091. ret = 0;
  21092. }
  21093. }
  21094. if (ret == 0) {
  21095. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  21096. &outLen, endian);
  21097. if (ret == BAD_FUNC_ARG) {
  21098. ret = 0;
  21099. }
  21100. }
  21101. if (ret == 0) {
  21102. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  21103. &outLen, endian);
  21104. if (ret == BAD_FUNC_ARG) {
  21105. ret = 0;
  21106. }
  21107. }
  21108. if (ret == 0) {
  21109. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21110. &outLen, endian);
  21111. if (ret == BAD_FUNC_ARG) {
  21112. ret = 0;
  21113. }
  21114. }
  21115. if (ret == 0) {
  21116. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21117. NULL, endian);
  21118. if (ret == BAD_FUNC_ARG) {
  21119. ret = 0;
  21120. }
  21121. }
  21122. outLen = outLen - 2;
  21123. if (ret == 0) {
  21124. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21125. &outLen, endian);
  21126. if (ret == BAD_FUNC_ARG) {
  21127. ret = 0;
  21128. }
  21129. }
  21130. wc_curve448_free(&private_key);
  21131. wc_curve448_free(&public_key);
  21132. wc_FreeRng(&rng);
  21133. res = TEST_RES_CHECK(ret == 0);
  21134. #endif
  21135. return res;
  21136. } /*END test_wc_curve448_shared_secret_ex*/
  21137. /*
  21138. * Testing test_wc_curve448_export_public_ex
  21139. */
  21140. static int test_wc_curve448_export_public_ex(void)
  21141. {
  21142. int res = TEST_SKIPPED;
  21143. #if defined(HAVE_CURVE448)
  21144. WC_RNG rng;
  21145. curve448_key key;
  21146. byte out[CURVE448_KEY_SIZE];
  21147. word32 outLen = sizeof(out);
  21148. int endian = EC448_BIG_ENDIAN;
  21149. int ret;
  21150. ret = wc_curve448_init(&key);
  21151. if (ret == 0) {
  21152. ret = wc_InitRng(&rng);
  21153. }
  21154. if (ret == 0) {
  21155. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21156. if (ret == 0) {
  21157. ret = wc_curve448_export_public(&key, out, &outLen);
  21158. }
  21159. if (ret == 0) {
  21160. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21161. }
  21162. }
  21163. /*test bad cases*/
  21164. if (ret == 0) {
  21165. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21166. if (ret == BAD_FUNC_ARG) {
  21167. ret = 0;
  21168. }
  21169. }
  21170. if (ret == 0) {
  21171. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21172. if (ret == BAD_FUNC_ARG) {
  21173. ret = 0;
  21174. }
  21175. }
  21176. if (ret == 0) {
  21177. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21178. if (ret == BAD_FUNC_ARG) {
  21179. ret = 0;
  21180. }
  21181. }
  21182. if (ret == 0) {
  21183. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21184. if (ret == BAD_FUNC_ARG) {
  21185. ret = 0;
  21186. }
  21187. }
  21188. outLen = outLen - 2;
  21189. if (ret == 0) {
  21190. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21191. if (ret == ECC_BAD_ARG_E) {
  21192. ret = 0;
  21193. }
  21194. }
  21195. wc_curve448_free(&key);
  21196. wc_FreeRng(&rng);
  21197. res = TEST_RES_CHECK(ret == 0);
  21198. #endif
  21199. return res;
  21200. } /*END test_wc_curve448_export_public_ex*/
  21201. /*
  21202. * Testing test_wc_curve448_export_private_raw_ex
  21203. */
  21204. static int test_wc_curve448_export_private_raw_ex(void)
  21205. {
  21206. int res = TEST_SKIPPED;
  21207. #if defined(HAVE_CURVE448)
  21208. curve448_key key;
  21209. byte out[CURVE448_KEY_SIZE];
  21210. word32 outLen = sizeof(out);
  21211. int endian = EC448_BIG_ENDIAN;
  21212. int ret;
  21213. ret = wc_curve448_init(&key);
  21214. if (ret == 0) {
  21215. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21216. }
  21217. /*test bad cases*/
  21218. if (ret == 0) {
  21219. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21220. if (ret == BAD_FUNC_ARG) {
  21221. ret = 0;
  21222. }
  21223. }
  21224. if (ret == 0) {
  21225. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21226. if (ret == BAD_FUNC_ARG) {
  21227. ret = 0;
  21228. }
  21229. }
  21230. if (ret == 0) {
  21231. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21232. if (ret == BAD_FUNC_ARG) {
  21233. ret = 0;
  21234. }
  21235. }
  21236. if (ret == 0) {
  21237. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21238. if (ret == BAD_FUNC_ARG) {
  21239. ret = 0;
  21240. }
  21241. }
  21242. if (ret == 0) {
  21243. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21244. EC448_LITTLE_ENDIAN);
  21245. }
  21246. outLen = outLen - 2;
  21247. if (ret == 0) {
  21248. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21249. if (ret == ECC_BAD_ARG_E) {
  21250. ret = 0;
  21251. }
  21252. }
  21253. wc_curve448_free(&key);
  21254. res = TEST_RES_CHECK(ret == 0);
  21255. #endif
  21256. return res;
  21257. }/*END test_wc_curve448_export_private_raw_ex*/
  21258. /*
  21259. * Testing test_wc_curve448_import_private_raw_ex
  21260. */
  21261. static int test_wc_curve448_import_private_raw_ex(void)
  21262. {
  21263. int res = TEST_SKIPPED;
  21264. #if defined(HAVE_CURVE448)
  21265. WC_RNG rng;
  21266. curve448_key key;
  21267. byte priv[CURVE448_KEY_SIZE];
  21268. byte pub[CURVE448_KEY_SIZE];
  21269. word32 privSz = sizeof(priv);
  21270. word32 pubSz = sizeof(pub);
  21271. int endian = EC448_BIG_ENDIAN;
  21272. int ret;
  21273. ret = wc_curve448_init(&key);
  21274. if (ret == 0) {
  21275. ret = wc_InitRng(&rng);
  21276. }
  21277. if (ret == 0) {
  21278. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21279. if (ret == 0) {
  21280. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21281. }
  21282. if (ret == 0) {
  21283. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21284. }
  21285. if (ret == 0) {
  21286. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21287. &key, endian);
  21288. }
  21289. }
  21290. /* test bad cases */
  21291. if (ret == 0) {
  21292. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21293. if (ret == BAD_FUNC_ARG) {
  21294. ret = 0;
  21295. }
  21296. }
  21297. if (ret == 0) {
  21298. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21299. &key, endian);
  21300. if (ret == BAD_FUNC_ARG) {
  21301. ret = 0;
  21302. }
  21303. }
  21304. if (ret == 0) {
  21305. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21306. &key, endian);
  21307. if (ret == BAD_FUNC_ARG) {
  21308. ret = 0;
  21309. }
  21310. }
  21311. if (ret == 0) {
  21312. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21313. NULL, endian);
  21314. if (ret == BAD_FUNC_ARG) {
  21315. ret = 0;
  21316. }
  21317. }
  21318. if (ret == 0) {
  21319. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21320. &key, endian);
  21321. if (ret == ECC_BAD_ARG_E) {
  21322. ret = 0;
  21323. }
  21324. }
  21325. if (ret == 0) {
  21326. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21327. &key, endian);
  21328. if (ret == ECC_BAD_ARG_E) {
  21329. ret = 0;
  21330. }
  21331. }
  21332. if (ret == 0) {
  21333. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21334. &key, EC448_LITTLE_ENDIAN);
  21335. }
  21336. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21337. ret = WOLFSSL_FATAL_ERROR;
  21338. }
  21339. wc_curve448_free(&key);
  21340. res = TEST_RES_CHECK(ret == 0);
  21341. #endif
  21342. return res;
  21343. } /*END test_wc_curve448_import_private_raw_ex*/
  21344. /*
  21345. * Testing test_curve448_export_key_raw
  21346. */
  21347. static int test_wc_curve448_export_key_raw(void)
  21348. {
  21349. int res = TEST_SKIPPED;
  21350. #if defined(HAVE_CURVE448)
  21351. WC_RNG rng;
  21352. curve448_key key;
  21353. byte priv[CURVE448_KEY_SIZE];
  21354. byte pub[CURVE448_KEY_SIZE];
  21355. word32 privSz = sizeof(priv);
  21356. word32 pubSz = sizeof(pub);
  21357. int ret;
  21358. ret = wc_curve448_init(&key);
  21359. if (ret == 0) {
  21360. ret = wc_InitRng(&rng);
  21361. }
  21362. if (ret == 0) {
  21363. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21364. if (ret == 0) {
  21365. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21366. }
  21367. if (ret == 0) {
  21368. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21369. }
  21370. if (ret == 0) {
  21371. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21372. }
  21373. }
  21374. wc_curve448_free(&key);
  21375. wc_FreeRng(&rng);
  21376. res = TEST_RES_CHECK(ret == 0);
  21377. #endif
  21378. return res;
  21379. }/*END test_wc_curve448_import_private_raw_ex*/
  21380. /*
  21381. * Testing test_wc_curve448_import_private
  21382. */
  21383. static int test_wc_curve448_import_private(void)
  21384. {
  21385. int res = TEST_SKIPPED;
  21386. #if defined(HAVE_CURVE448)
  21387. curve448_key key;
  21388. WC_RNG rng;
  21389. byte priv[CURVE448_KEY_SIZE];
  21390. word32 privSz = sizeof(priv);
  21391. int ret;
  21392. ret = wc_curve448_init(&key);
  21393. if (ret == 0) {
  21394. ret = wc_InitRng(&rng);
  21395. }
  21396. if (ret == 0) {
  21397. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21398. if (ret == 0) {
  21399. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21400. }
  21401. }
  21402. if (ret == 0) {
  21403. ret = wc_curve448_import_private(priv, privSz, &key);
  21404. }
  21405. wc_curve448_free(&key);
  21406. wc_FreeRng(&rng);
  21407. res = TEST_RES_CHECK(ret == 0);
  21408. #endif
  21409. return res;
  21410. } /*END test_wc_curve448_import*/
  21411. /*
  21412. * Testing test_wc_curve448_size.
  21413. */
  21414. static int test_wc_curve448_size(void)
  21415. {
  21416. int res = TEST_SKIPPED;
  21417. #if defined(HAVE_CURVE448)
  21418. curve448_key key;
  21419. int ret = 0;
  21420. ret = wc_curve448_init(&key);
  21421. /* Test good args for wc_curve448_size */
  21422. if (ret == 0) {
  21423. ret = wc_curve448_size(&key);
  21424. }
  21425. /* Test bad args for wc_curve448_size */
  21426. if (ret != 0) {
  21427. ret = wc_curve448_size(NULL);
  21428. }
  21429. wc_curve448_free(&key);
  21430. res = TEST_RES_CHECK(ret == 0);
  21431. #endif
  21432. return res;
  21433. } /* END test_wc_curve448_size*/
  21434. /*
  21435. * Testing wc_ecc_make_key.
  21436. */
  21437. static int test_wc_ecc_make_key(void)
  21438. {
  21439. int res = TEST_SKIPPED;
  21440. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21441. WC_RNG rng;
  21442. ecc_key key;
  21443. int ret;
  21444. ret = wc_InitRng(&rng);
  21445. if (ret != 0)
  21446. return TEST_FAIL;
  21447. ret = wc_ecc_init(&key);
  21448. if (ret == 0) {
  21449. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21450. #if defined(WOLFSSL_ASYNC_CRYPT)
  21451. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21452. #endif
  21453. /* Pass in bad args. */
  21454. if (ret == 0) {
  21455. ret = wc_ecc_make_key(NULL, KEY14, &key);
  21456. if (ret == BAD_FUNC_ARG) {
  21457. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  21458. }
  21459. if (ret == BAD_FUNC_ARG) {
  21460. ret = 0;
  21461. }
  21462. else if (ret == 0) {
  21463. ret = WOLFSSL_FATAL_ERROR;
  21464. }
  21465. }
  21466. wc_ecc_free(&key);
  21467. }
  21468. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21469. ret = WOLFSSL_FATAL_ERROR;
  21470. }
  21471. #ifdef FP_ECC
  21472. wc_ecc_fp_free();
  21473. #endif
  21474. res = TEST_RES_CHECK(ret == 0);
  21475. #endif
  21476. return res;
  21477. } /* END test_wc_ecc_make_key */
  21478. /*
  21479. * Testing wc_ecc_init()
  21480. */
  21481. static int test_wc_ecc_init(void)
  21482. {
  21483. int res = TEST_SKIPPED;
  21484. #ifdef HAVE_ECC
  21485. ecc_key key;
  21486. int ret;
  21487. ret = wc_ecc_init(&key);
  21488. /* Pass in bad args. */
  21489. if (ret == 0) {
  21490. ret = wc_ecc_init(NULL);
  21491. if (ret == BAD_FUNC_ARG) {
  21492. ret = 0;
  21493. }
  21494. else if (ret == 0) {
  21495. ret = WOLFSSL_FATAL_ERROR;
  21496. }
  21497. }
  21498. wc_ecc_free(&key);
  21499. res = TEST_RES_CHECK(ret == 0);
  21500. #endif
  21501. return res;
  21502. } /* END test_wc_ecc_init */
  21503. /*
  21504. * Testing wc_ecc_check_key()
  21505. */
  21506. static int test_wc_ecc_check_key(void)
  21507. {
  21508. int res = TEST_SKIPPED;
  21509. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21510. WC_RNG rng;
  21511. ecc_key key;
  21512. int ret;
  21513. XMEMSET(&rng, 0, sizeof(rng));
  21514. XMEMSET(&key, 0, sizeof(key));
  21515. ret = wc_InitRng(&rng);
  21516. if (ret == 0) {
  21517. ret = wc_ecc_init(&key);
  21518. if (ret == 0) {
  21519. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21520. #if defined(WOLFSSL_ASYNC_CRYPT)
  21521. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21522. #endif
  21523. }
  21524. }
  21525. if (ret == 0) {
  21526. ret = wc_ecc_check_key(&key);
  21527. }
  21528. /* Pass in bad args. */
  21529. if (ret == 0) {
  21530. ret = wc_ecc_check_key(NULL);
  21531. if (ret == BAD_FUNC_ARG) {
  21532. ret = 0;
  21533. }
  21534. else if (ret == 0) {
  21535. ret = WOLFSSL_FATAL_ERROR;
  21536. }
  21537. }
  21538. if (wc_FreeRng(&rng) && ret == 0) {
  21539. ret = WOLFSSL_FATAL_ERROR;
  21540. }
  21541. wc_ecc_free(&key);
  21542. #ifdef FP_ECC
  21543. wc_ecc_fp_free();
  21544. #endif
  21545. res = TEST_RES_CHECK(ret == 0);
  21546. #endif
  21547. return res;
  21548. } /* END test_wc_ecc_check_key */
  21549. /*
  21550. * Testing wc_ecc_get_generator()
  21551. */
  21552. static int test_wc_ecc_get_generator(void)
  21553. {
  21554. int res = TEST_SKIPPED;
  21555. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21556. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21557. ecc_point* pt;
  21558. int ret = 0;
  21559. pt = wc_ecc_new_point();
  21560. if (!pt) {
  21561. ret = WOLFSSL_FATAL_ERROR;
  21562. }
  21563. if (ret == 0) {
  21564. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21565. }
  21566. /* Test bad args. */
  21567. if (ret == MP_OKAY) {
  21568. /* Returns Zero for bad arg. */
  21569. ret = wc_ecc_get_generator(pt, -1);
  21570. if (ret != MP_OKAY)
  21571. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21572. if (ret != MP_OKAY)
  21573. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  21574. * increase this number */
  21575. if (ret != MP_OKAY)
  21576. wc_ecc_get_generator(NULL, -1);
  21577. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  21578. }
  21579. wc_ecc_del_point(pt);
  21580. res = TEST_RES_CHECK(ret == 0);
  21581. #endif
  21582. return res;
  21583. } /* END test_wc_ecc_get_generator */
  21584. /*
  21585. * Testing wc_ecc_size()
  21586. */
  21587. static int test_wc_ecc_size(void)
  21588. {
  21589. int res = TEST_SKIPPED;
  21590. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21591. WC_RNG rng;
  21592. ecc_key key;
  21593. int ret;
  21594. XMEMSET(&rng, 0, sizeof(rng));
  21595. XMEMSET(&key, 0, sizeof(key));
  21596. ret = wc_InitRng(&rng);
  21597. if (ret == 0) {
  21598. ret = wc_ecc_init(&key);
  21599. if (ret == 0) {
  21600. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21601. #if defined(WOLFSSL_ASYNC_CRYPT)
  21602. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21603. #endif
  21604. }
  21605. }
  21606. if (ret == 0) {
  21607. ret = wc_ecc_size(&key);
  21608. if (ret == KEY14) {
  21609. ret = 0;
  21610. }
  21611. else if (ret == 0) {
  21612. ret = WOLFSSL_FATAL_ERROR;
  21613. }
  21614. }
  21615. /* Test bad args. */
  21616. if (ret == 0) {
  21617. /* Returns Zero for bad arg. */
  21618. ret = wc_ecc_size(NULL);
  21619. }
  21620. if (wc_FreeRng(&rng) && ret == 0) {
  21621. ret = WOLFSSL_FATAL_ERROR;
  21622. }
  21623. wc_ecc_free(&key);
  21624. res = TEST_RES_CHECK(ret == 0);
  21625. #endif
  21626. return res;
  21627. } /* END test_wc_ecc_size */
  21628. static int test_wc_ecc_params(void)
  21629. {
  21630. int res = TEST_SKIPPED;
  21631. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21632. It was added after certifications */
  21633. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21634. const ecc_set_type* ecc_set;
  21635. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21636. /* Test for SECP256R1 curve */
  21637. int curve_id = ECC_SECP256R1;
  21638. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21639. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21640. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21641. AssertNotNull(ecc_set);
  21642. AssertIntEQ(ecc_set->id, curve_id);
  21643. #endif
  21644. /* Test case when SECP256R1 is not enabled */
  21645. /* Test that we get curve params for index 0 */
  21646. ecc_set = wc_ecc_get_curve_params(0);
  21647. AssertNotNull(ecc_set);
  21648. res = TEST_RES_CHECK(1);
  21649. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21650. return res;
  21651. }
  21652. /*
  21653. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21654. */
  21655. static int test_wc_ecc_signVerify_hash(void)
  21656. {
  21657. int res = TEST_SKIPPED;
  21658. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21659. WC_RNG rng;
  21660. ecc_key key;
  21661. int ret;
  21662. int signH = WOLFSSL_FATAL_ERROR;
  21663. #ifdef HAVE_ECC_VERIFY
  21664. int verifyH = WOLFSSL_FATAL_ERROR;
  21665. int verify = 0;
  21666. #endif
  21667. word32 siglen = ECC_BUFSIZE;
  21668. byte sig[ECC_BUFSIZE];
  21669. byte adjustedSig[ECC_BUFSIZE+1];
  21670. byte digest[] = TEST_STRING;
  21671. word32 digestlen = (word32)TEST_STRING_SZ;
  21672. /* Init stack var */
  21673. XMEMSET(sig, 0, siglen);
  21674. XMEMSET(&key, 0, sizeof(key));
  21675. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21676. /* Init structs. */
  21677. ret = wc_InitRng(&rng);
  21678. if (ret == 0) {
  21679. ret = wc_ecc_init(&key);
  21680. if (ret == 0) {
  21681. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21682. #if defined(WOLFSSL_ASYNC_CRYPT)
  21683. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21684. #endif
  21685. }
  21686. }
  21687. if (ret == 0) {
  21688. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21689. }
  21690. /* Check bad args. */
  21691. if (ret == 0) {
  21692. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21693. if (signH == ECC_BAD_ARG_E) {
  21694. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21695. &rng, &key);
  21696. }
  21697. if (signH == ECC_BAD_ARG_E) {
  21698. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21699. &rng, &key);
  21700. }
  21701. if (signH == ECC_BAD_ARG_E) {
  21702. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21703. NULL, &key);
  21704. }
  21705. if (signH == ECC_BAD_ARG_E) {
  21706. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21707. &rng, NULL);
  21708. }
  21709. if (signH == ECC_BAD_ARG_E) {
  21710. signH = 0;
  21711. }
  21712. else if (ret == 0) {
  21713. signH = WOLFSSL_FATAL_ERROR;
  21714. }
  21715. }
  21716. #ifdef HAVE_ECC_VERIFY
  21717. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21718. if (verify != 1 && ret == 0) {
  21719. ret = WOLFSSL_FATAL_ERROR;
  21720. }
  21721. /* test check on length of signature passed in */
  21722. XMEMCPY(adjustedSig, sig, siglen);
  21723. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21724. #ifndef NO_STRICT_ECDSA_LEN
  21725. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21726. &verify, &key), 0);
  21727. #else
  21728. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21729. * is allowed */
  21730. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21731. &verify, &key), 0);
  21732. #endif
  21733. /* Test bad args. */
  21734. if (ret == 0) {
  21735. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21736. &verify, &key);
  21737. if (verifyH == ECC_BAD_ARG_E) {
  21738. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21739. &verify, &key);
  21740. }
  21741. if (verifyH == ECC_BAD_ARG_E) {
  21742. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21743. NULL, &key);
  21744. }
  21745. if (verifyH == ECC_BAD_ARG_E) {
  21746. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21747. &verify, NULL);
  21748. }
  21749. if (verifyH == ECC_BAD_ARG_E) {
  21750. verifyH = 0;
  21751. }
  21752. else if (ret == 0) {
  21753. verifyH = WOLFSSL_FATAL_ERROR;
  21754. }
  21755. }
  21756. #endif /* HAVE_ECC_VERIFY */
  21757. if (wc_FreeRng(&rng) && ret == 0) {
  21758. ret = WOLFSSL_FATAL_ERROR;
  21759. }
  21760. wc_ecc_free(&key);
  21761. #ifdef FP_ECC
  21762. wc_ecc_fp_free();
  21763. #endif
  21764. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  21765. #endif
  21766. return res;
  21767. } /* END test_wc_ecc_sign_hash */
  21768. /*
  21769. * Testing wc_ecc_shared_secret()
  21770. */
  21771. static int test_wc_ecc_shared_secret(void)
  21772. {
  21773. int res = TEST_SKIPPED;
  21774. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21775. ecc_key key, pubKey;
  21776. WC_RNG rng;
  21777. int ret;
  21778. byte out[KEY32];
  21779. int keySz = sizeof(out);
  21780. word32 outlen = (word32)sizeof(out);
  21781. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21782. const char* qx =
  21783. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21784. const char* qy =
  21785. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21786. const char* d =
  21787. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21788. const char* curveName = "SECP256R1";
  21789. const byte expected_shared_secret[] =
  21790. {
  21791. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21792. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21793. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21794. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21795. };
  21796. #endif
  21797. PRIVATE_KEY_UNLOCK();
  21798. /* Initialize variables. */
  21799. XMEMSET(out, 0, keySz);
  21800. XMEMSET(&rng, 0, sizeof(rng));
  21801. XMEMSET(&key, 0, sizeof(key));
  21802. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21803. ret = wc_InitRng(&rng);
  21804. if (ret == 0) {
  21805. ret = wc_ecc_init(&key);
  21806. if (ret == 0) {
  21807. ret = wc_ecc_init(&pubKey);
  21808. }
  21809. }
  21810. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21811. if (ret == 0) {
  21812. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21813. }
  21814. if (ret == 0) {
  21815. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21816. }
  21817. #else
  21818. if (ret == 0) {
  21819. ret = wc_ecc_make_key(&rng, keySz, &key);
  21820. #if defined(WOLFSSL_ASYNC_CRYPT)
  21821. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21822. #endif
  21823. }
  21824. if (ret == 0) {
  21825. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21826. #if defined(WOLFSSL_ASYNC_CRYPT)
  21827. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21828. #endif
  21829. }
  21830. #endif
  21831. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21832. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21833. !defined(HAVE_SELFTEST)
  21834. if (ret == 0) {
  21835. ret = wc_ecc_set_rng(&key, &rng);
  21836. }
  21837. #endif
  21838. if (ret == 0) {
  21839. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21840. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21841. if (ret == 0) {
  21842. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21843. ret = WOLFSSL_FATAL_ERROR;
  21844. }
  21845. }
  21846. #endif
  21847. /* Test bad args. */
  21848. if (ret == 0) {
  21849. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21850. if (ret == BAD_FUNC_ARG) {
  21851. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21852. }
  21853. if (ret == BAD_FUNC_ARG) {
  21854. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21855. }
  21856. if (ret == BAD_FUNC_ARG) {
  21857. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21858. }
  21859. if (ret == BAD_FUNC_ARG) {
  21860. /* Invalid length */
  21861. outlen = 1;
  21862. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21863. }
  21864. if (ret == BUFFER_E) {
  21865. ret = 0;
  21866. }
  21867. else if (ret == 0) {
  21868. ret = WOLFSSL_FATAL_ERROR;
  21869. }
  21870. }
  21871. }
  21872. if (wc_FreeRng(&rng) && ret == 0) {
  21873. ret = WOLFSSL_FATAL_ERROR;
  21874. }
  21875. wc_ecc_free(&key);
  21876. wc_ecc_free(&pubKey);
  21877. #ifdef FP_ECC
  21878. wc_ecc_fp_free();
  21879. #endif
  21880. PRIVATE_KEY_LOCK();
  21881. res = TEST_RES_CHECK(ret == 0);
  21882. #endif
  21883. return res;
  21884. } /* END tests_wc_ecc_shared_secret */
  21885. /*
  21886. * testint wc_ecc_export_x963()
  21887. */
  21888. static int test_wc_ecc_export_x963(void)
  21889. {
  21890. int res = TEST_SKIPPED;
  21891. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21892. ecc_key key;
  21893. WC_RNG rng;
  21894. byte out[ECC_ASN963_MAX_BUF_SZ];
  21895. word32 outlen = sizeof(out);
  21896. int ret = 0;
  21897. PRIVATE_KEY_UNLOCK();
  21898. /* Initialize variables. */
  21899. XMEMSET(out, 0, outlen);
  21900. XMEMSET(&rng, 0, sizeof(rng));
  21901. XMEMSET(&key, 0, sizeof(key));
  21902. ret = wc_InitRng(&rng);
  21903. if (ret == 0) {
  21904. ret = wc_ecc_init(&key);
  21905. if (ret == 0) {
  21906. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21907. #if defined(WOLFSSL_ASYNC_CRYPT)
  21908. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21909. #endif
  21910. }
  21911. }
  21912. if (ret == 0) {
  21913. ret = wc_ecc_export_x963(&key, out, &outlen);
  21914. }
  21915. /* Test bad args. */
  21916. if (ret == 0) {
  21917. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21918. if (ret == ECC_BAD_ARG_E) {
  21919. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21920. }
  21921. if (ret == LENGTH_ONLY_E) {
  21922. ret = wc_ecc_export_x963(&key, out, NULL);
  21923. }
  21924. if (ret == ECC_BAD_ARG_E) {
  21925. key.idx = -4;
  21926. ret = wc_ecc_export_x963(&key, out, &outlen);
  21927. }
  21928. if (ret == ECC_BAD_ARG_E) {
  21929. ret = 0;
  21930. }
  21931. else {
  21932. ret = WOLFSSL_FATAL_ERROR;
  21933. }
  21934. }
  21935. if (wc_FreeRng(&rng) && ret == 0) {
  21936. ret = WOLFSSL_FATAL_ERROR;
  21937. }
  21938. wc_ecc_free(&key);
  21939. #ifdef FP_ECC
  21940. wc_ecc_fp_free();
  21941. #endif
  21942. PRIVATE_KEY_LOCK();
  21943. res = TEST_RES_CHECK(ret == 0);
  21944. #endif
  21945. return res;
  21946. } /* END test_wc_ecc_export_x963 */
  21947. /*
  21948. * Testing wc_ecc_export_x963_ex()
  21949. * compile with --enable-compkey will use compression.
  21950. */
  21951. static int test_wc_ecc_export_x963_ex(void)
  21952. {
  21953. int res = TEST_SKIPPED;
  21954. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21955. ecc_key key;
  21956. WC_RNG rng;
  21957. int ret = 0;
  21958. byte out[ECC_ASN963_MAX_BUF_SZ];
  21959. word32 outlen = sizeof(out);
  21960. #ifdef HAVE_COMP_KEY
  21961. word32 badOutLen = 5;
  21962. #endif
  21963. /* Init stack variables. */
  21964. XMEMSET(out, 0, outlen);
  21965. XMEMSET(&rng, 0, sizeof(rng));
  21966. XMEMSET(&key, 0, sizeof(key));
  21967. ret = wc_InitRng(&rng);
  21968. if (ret == 0) {
  21969. ret = wc_ecc_init(&key);
  21970. if (ret == 0) {
  21971. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21972. #if defined(WOLFSSL_ASYNC_CRYPT)
  21973. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21974. #endif
  21975. }
  21976. }
  21977. #ifdef HAVE_COMP_KEY
  21978. if (ret == 0) {
  21979. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21980. }
  21981. #else
  21982. if (ret == 0) {
  21983. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21984. }
  21985. #endif
  21986. /* Test bad args. */
  21987. #ifdef HAVE_COMP_KEY
  21988. if (ret == 0) {
  21989. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21990. if (ret == BAD_FUNC_ARG) {
  21991. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21992. }
  21993. if (ret == BAD_FUNC_ARG) {
  21994. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21995. }
  21996. if (ret == BAD_FUNC_ARG) {
  21997. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21998. }
  21999. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  22000. if (ret == BUFFER_E)
  22001. #else
  22002. if (ret == LENGTH_ONLY_E)
  22003. #endif
  22004. {
  22005. key.idx = -4;
  22006. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22007. }
  22008. if (ret == ECC_BAD_ARG_E) {
  22009. ret = 0;
  22010. }
  22011. else {
  22012. ret = WOLFSSL_FATAL_ERROR;
  22013. }
  22014. }
  22015. #else
  22016. if (ret == 0) {
  22017. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  22018. if (ret == BAD_FUNC_ARG) {
  22019. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  22020. }
  22021. if (ret == BAD_FUNC_ARG) {
  22022. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  22023. }
  22024. if (ret == NOT_COMPILED_IN) {
  22025. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  22026. }
  22027. if (ret == BAD_FUNC_ARG) {
  22028. key.idx = -4;
  22029. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22030. }
  22031. if (ret == ECC_BAD_ARG_E) {
  22032. ret = 0;
  22033. }
  22034. else if (ret == 0) {
  22035. ret = WOLFSSL_FATAL_ERROR;
  22036. }
  22037. }
  22038. #endif
  22039. if (wc_FreeRng(&rng) && ret == 0) {
  22040. ret = WOLFSSL_FATAL_ERROR;
  22041. }
  22042. wc_ecc_free(&key);
  22043. #ifdef FP_ECC
  22044. wc_ecc_fp_free();
  22045. #endif
  22046. res = TEST_RES_CHECK(ret == 0);
  22047. #endif
  22048. return res;
  22049. } /* END test_wc_ecc_export_x963_ex */
  22050. /*
  22051. * testing wc_ecc_import_x963()
  22052. */
  22053. static int test_wc_ecc_import_x963(void)
  22054. {
  22055. int res = TEST_SKIPPED;
  22056. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22057. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22058. ecc_key pubKey, key;
  22059. WC_RNG rng;
  22060. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22061. word32 x963Len = (word32)sizeof(x963);
  22062. int ret;
  22063. /* Init stack variables. */
  22064. XMEMSET(x963, 0, x963Len);
  22065. XMEMSET(&rng, 0, sizeof(rng));
  22066. XMEMSET(&key, 0, sizeof(key));
  22067. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22068. ret = wc_InitRng(&rng);
  22069. if (ret == 0) {
  22070. ret = wc_ecc_init(&pubKey);
  22071. if (ret == 0) {
  22072. ret = wc_ecc_init(&key);
  22073. }
  22074. if (ret == 0) {
  22075. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22076. #if defined(WOLFSSL_ASYNC_CRYPT)
  22077. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22078. #endif
  22079. }
  22080. if (ret == 0) {
  22081. PRIVATE_KEY_UNLOCK();
  22082. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  22083. PRIVATE_KEY_LOCK();
  22084. }
  22085. }
  22086. if (ret == 0) {
  22087. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  22088. }
  22089. /* Test bad args. */
  22090. if (ret == 0) {
  22091. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  22092. if (ret == BAD_FUNC_ARG) {
  22093. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  22094. }
  22095. if (ret == BAD_FUNC_ARG) {
  22096. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  22097. }
  22098. if (ret == ECC_BAD_ARG_E) {
  22099. ret = 0;
  22100. }
  22101. else if (ret == 0) {
  22102. ret = WOLFSSL_FATAL_ERROR;
  22103. }
  22104. }
  22105. if (wc_FreeRng(&rng) && ret == 0) {
  22106. ret = WOLFSSL_FATAL_ERROR;
  22107. }
  22108. wc_ecc_free(&key);
  22109. wc_ecc_free(&pubKey);
  22110. #ifdef FP_ECC
  22111. wc_ecc_fp_free();
  22112. #endif
  22113. res = TEST_RES_CHECK(ret == 0);
  22114. #endif
  22115. return res;
  22116. } /* END wc_ecc_import_x963 */
  22117. /*
  22118. * testing wc_ecc_import_private_key()
  22119. */
  22120. static int ecc_import_private_key(void)
  22121. {
  22122. int res = TEST_SKIPPED;
  22123. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22124. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22125. ecc_key key, keyImp;
  22126. WC_RNG rng;
  22127. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22128. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22129. word32 privKeySz = (word32)sizeof(privKey);
  22130. word32 x963KeySz = (word32)sizeof(x963Key);
  22131. int ret;
  22132. /* Init stack variables. */
  22133. XMEMSET(privKey, 0, privKeySz);
  22134. XMEMSET(x963Key, 0, x963KeySz);
  22135. XMEMSET(&rng, 0, sizeof(rng));
  22136. XMEMSET(&key, 0, sizeof(key));
  22137. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22138. ret = wc_InitRng(&rng);
  22139. if (ret == 0) {
  22140. ret = wc_ecc_init(&key);
  22141. if (ret == 0) {
  22142. ret = wc_ecc_init(&keyImp);
  22143. }
  22144. if (ret == 0) {
  22145. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22146. #if defined(WOLFSSL_ASYNC_CRYPT)
  22147. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22148. #endif
  22149. }
  22150. if (ret == 0) {
  22151. PRIVATE_KEY_UNLOCK();
  22152. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22153. PRIVATE_KEY_LOCK();
  22154. }
  22155. if (ret == 0) {
  22156. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22157. }
  22158. }
  22159. if (ret == 0) {
  22160. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22161. x963KeySz, &keyImp);
  22162. }
  22163. /* Pass in bad args. */
  22164. if (ret == 0) {
  22165. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22166. x963KeySz, NULL);
  22167. if (ret == BAD_FUNC_ARG) {
  22168. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22169. x963KeySz, &keyImp);
  22170. }
  22171. if (ret == BAD_FUNC_ARG) {
  22172. ret = 0;
  22173. }
  22174. else if (ret == 0) {
  22175. ret = WOLFSSL_FATAL_ERROR;
  22176. }
  22177. }
  22178. if (wc_FreeRng(&rng) && ret == 0) {
  22179. ret = WOLFSSL_FATAL_ERROR;
  22180. }
  22181. wc_ecc_free(&key);
  22182. wc_ecc_free(&keyImp);
  22183. #ifdef FP_ECC
  22184. wc_ecc_fp_free();
  22185. #endif
  22186. res = TEST_RES_CHECK(ret == 0);
  22187. #endif
  22188. return res;
  22189. } /* END wc_ecc_import_private_key */
  22190. /*
  22191. * Testing wc_ecc_export_private_only()
  22192. */
  22193. static int test_wc_ecc_export_private_only(void)
  22194. {
  22195. int res = TEST_SKIPPED;
  22196. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22197. ecc_key key;
  22198. WC_RNG rng;
  22199. byte out[ECC_PRIV_KEY_BUF];
  22200. word32 outlen = sizeof(out);
  22201. int ret;
  22202. /* Init stack variables. */
  22203. XMEMSET(out, 0, outlen);
  22204. XMEMSET(&rng, 0, sizeof(rng));
  22205. XMEMSET(&key, 0, sizeof(key));
  22206. ret = wc_InitRng(&rng);
  22207. if (ret == 0) {
  22208. ret = wc_ecc_init(&key);
  22209. if (ret == 0) {
  22210. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22211. #if defined(WOLFSSL_ASYNC_CRYPT)
  22212. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22213. #endif
  22214. }
  22215. }
  22216. if (ret == 0) {
  22217. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22218. }
  22219. /* Pass in bad args. */
  22220. if (ret == 0) {
  22221. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22222. if (ret == BAD_FUNC_ARG) {
  22223. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22224. }
  22225. if (ret == BAD_FUNC_ARG) {
  22226. ret = wc_ecc_export_private_only(&key, out, NULL);
  22227. }
  22228. if (ret == BAD_FUNC_ARG) {
  22229. ret = 0;
  22230. }
  22231. else if (ret == 0) {
  22232. ret = WOLFSSL_FATAL_ERROR;
  22233. }
  22234. }
  22235. if (wc_FreeRng(&rng) && ret == 0) {
  22236. ret = WOLFSSL_FATAL_ERROR;
  22237. }
  22238. wc_ecc_free(&key);
  22239. #ifdef FP_ECC
  22240. wc_ecc_fp_free();
  22241. #endif
  22242. res = TEST_RES_CHECK(ret == 0);
  22243. #endif
  22244. return res;
  22245. } /* END test_wc_ecc_export_private_only */
  22246. /*
  22247. * Testing wc_ecc_rs_to_sig()
  22248. */
  22249. static int test_wc_ecc_rs_to_sig(void)
  22250. {
  22251. int res = TEST_SKIPPED;
  22252. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22253. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22254. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22255. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22256. const char* zeroStr = "0";
  22257. byte sig[ECC_MAX_SIG_SIZE];
  22258. word32 siglen = (word32)sizeof(sig);
  22259. /*R and S max size is the order of curve. 2^192.*/
  22260. int keySz = KEY24;
  22261. byte r[KEY24];
  22262. byte s[KEY24];
  22263. word32 rlen = (word32)sizeof(r);
  22264. word32 slen = (word32)sizeof(s);
  22265. int ret;
  22266. /* Init stack variables. */
  22267. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22268. XMEMSET(r, 0, keySz);
  22269. XMEMSET(s, 0, keySz);
  22270. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22271. /* Test bad args. */
  22272. if (ret == 0) {
  22273. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22274. if (ret == ECC_BAD_ARG_E) {
  22275. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22276. }
  22277. if (ret == ECC_BAD_ARG_E) {
  22278. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22279. }
  22280. if (ret == ECC_BAD_ARG_E) {
  22281. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22282. }
  22283. if (ret == ECC_BAD_ARG_E) {
  22284. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22285. }
  22286. if (ret == MP_ZERO_E) {
  22287. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22288. }
  22289. if (ret == MP_ZERO_E) {
  22290. ret = 0;
  22291. }
  22292. else {
  22293. ret = WOLFSSL_FATAL_ERROR;
  22294. }
  22295. }
  22296. if (ret == 0) {
  22297. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22298. }
  22299. /* Test bad args. */
  22300. if (ret == 0) {
  22301. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22302. if (ret == ECC_BAD_ARG_E) {
  22303. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22304. }
  22305. if (ret == ECC_BAD_ARG_E) {
  22306. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22307. }
  22308. if (ret == ECC_BAD_ARG_E) {
  22309. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22310. }
  22311. if (ret == ECC_BAD_ARG_E) {
  22312. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22313. }
  22314. if (ret == ECC_BAD_ARG_E) {
  22315. ret = 0;
  22316. }
  22317. else if (ret == 0) {
  22318. ret = WOLFSSL_FATAL_ERROR;
  22319. }
  22320. }
  22321. res = TEST_RES_CHECK(ret == 0);
  22322. #endif
  22323. return res;
  22324. } /* END test_wc_ecc_rs_to_sig */
  22325. static int test_wc_ecc_import_raw(void)
  22326. {
  22327. int res = TEST_SKIPPED;
  22328. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22329. ecc_key key;
  22330. int ret = 0;
  22331. const char* qx =
  22332. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22333. const char* qy =
  22334. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22335. const char* d =
  22336. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22337. const char* curveName = "SECP256R1";
  22338. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22339. const char* kNullStr = "";
  22340. #endif
  22341. ret = wc_ecc_init(&key);
  22342. /* Test good import */
  22343. if (ret == 0) {
  22344. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22345. }
  22346. /* Test bad args. */
  22347. if (ret == 0) {
  22348. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22349. if (ret == BAD_FUNC_ARG) {
  22350. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22351. }
  22352. if (ret == BAD_FUNC_ARG) {
  22353. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22354. }
  22355. if (ret == BAD_FUNC_ARG) {
  22356. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22357. }
  22358. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22359. if (ret == BAD_FUNC_ARG) {
  22360. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22361. wc_ecc_free(&key);
  22362. #endif
  22363. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22364. if (ret == ECC_INF_E)
  22365. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22366. }
  22367. #endif
  22368. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22369. if (ret == BAD_FUNC_ARG) {
  22370. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22371. wc_ecc_free(&key);
  22372. #endif
  22373. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22374. /* Note: SP math "is point" failure returns MP_VAL */
  22375. if (ret == ECC_INF_E || ret == MP_VAL) {
  22376. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22377. }
  22378. }
  22379. if (ret == BAD_FUNC_ARG) {
  22380. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22381. wc_ecc_free(&key);
  22382. #endif
  22383. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22384. /* Note: SP math "is point" failure returns MP_VAL */
  22385. if (ret == ECC_INF_E || ret == MP_VAL) {
  22386. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22387. }
  22388. }
  22389. #endif
  22390. if (ret == BAD_FUNC_ARG) {
  22391. ret = 0;
  22392. }
  22393. }
  22394. wc_ecc_free(&key);
  22395. res = TEST_RES_CHECK(ret == 0);
  22396. #endif
  22397. return res;
  22398. } /* END test_wc_ecc_import_raw */
  22399. static int test_wc_ecc_import_unsigned(void)
  22400. {
  22401. int res = TEST_SKIPPED;
  22402. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22403. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  22404. ecc_key key;
  22405. const byte qx[] = {
  22406. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22407. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22408. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22409. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22410. };
  22411. const byte qy[] = {
  22412. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22413. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22414. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22415. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22416. };
  22417. const byte d[] = {
  22418. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22419. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22420. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22421. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22422. };
  22423. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22424. const byte nullBytes[32] = {0};
  22425. #endif
  22426. int curveId = ECC_SECP256R1;
  22427. int ret;
  22428. ret = wc_ecc_init(&key);
  22429. if (ret == 0) {
  22430. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22431. curveId);
  22432. }
  22433. /* Test bad args. */
  22434. if (ret == 0) {
  22435. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22436. curveId);
  22437. if (ret == BAD_FUNC_ARG) {
  22438. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22439. curveId);
  22440. }
  22441. if (ret == BAD_FUNC_ARG) {
  22442. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22443. curveId);
  22444. }
  22445. if (ret == BAD_FUNC_ARG) {
  22446. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22447. ECC_CURVE_INVALID);
  22448. }
  22449. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22450. if (ret == BAD_FUNC_ARG) {
  22451. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22452. (byte*)nullBytes, (byte*)nullBytes, curveId);
  22453. }
  22454. #endif
  22455. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  22456. ret = 0;
  22457. }
  22458. }
  22459. wc_ecc_free(&key);
  22460. res = TEST_RES_CHECK(ret == 0);
  22461. #endif
  22462. return res;
  22463. } /* END test_wc_ecc_import_unsigned */
  22464. /*
  22465. * Testing wc_ecc_sig_size()
  22466. */
  22467. static int test_wc_ecc_sig_size(void)
  22468. {
  22469. int res = TEST_SKIPPED;
  22470. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22471. ecc_key key;
  22472. WC_RNG rng;
  22473. int keySz = KEY16;
  22474. int ret = 0;
  22475. XMEMSET(&rng, 0, sizeof(rng));
  22476. XMEMSET(&key, 0, sizeof(key));
  22477. ret = wc_InitRng(&rng);
  22478. if (ret == 0) {
  22479. ret = wc_ecc_init(&key);
  22480. if (ret == 0) {
  22481. ret = wc_ecc_make_key(&rng, keySz, &key);
  22482. #if defined(WOLFSSL_ASYNC_CRYPT)
  22483. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22484. #endif
  22485. }
  22486. }
  22487. if (ret == 0) {
  22488. ret = wc_ecc_sig_size(&key);
  22489. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  22490. ret = 0;
  22491. }
  22492. }
  22493. if (wc_FreeRng(&rng) && ret == 0) {
  22494. ret = WOLFSSL_FATAL_ERROR;
  22495. }
  22496. wc_ecc_free(&key);
  22497. res = TEST_RES_CHECK(ret == 0);
  22498. #endif
  22499. return res;
  22500. } /* END test_wc_ecc_sig_size */
  22501. /*
  22502. * Testing wc_ecc_ctx_new()
  22503. */
  22504. static int test_wc_ecc_ctx_new(void)
  22505. {
  22506. int res = TEST_SKIPPED;
  22507. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22508. WC_RNG rng;
  22509. int ret = 0;
  22510. ecEncCtx* cli = NULL;
  22511. ecEncCtx* srv = NULL;
  22512. ret = wc_InitRng(&rng);
  22513. if (ret == 0) {
  22514. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  22515. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  22516. }
  22517. if (ret == 0 && (cli == NULL || srv == NULL)) {
  22518. ret = WOLFSSL_FATAL_ERROR;
  22519. }
  22520. wc_ecc_ctx_free(cli);
  22521. wc_ecc_ctx_free(srv);
  22522. /* Test bad args. */
  22523. if (ret == 0) {
  22524. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22525. cli = wc_ecc_ctx_new(0, &rng);
  22526. if (cli != NULL) {
  22527. ret = WOLFSSL_FATAL_ERROR;
  22528. }
  22529. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  22530. if (cli != NULL) {
  22531. ret = WOLFSSL_FATAL_ERROR;
  22532. }
  22533. }
  22534. if (wc_FreeRng(&rng) && ret == 0) {
  22535. ret = WOLFSSL_FATAL_ERROR;
  22536. }
  22537. wc_ecc_ctx_free(cli);
  22538. res = TEST_RES_CHECK(ret == 0);
  22539. #endif
  22540. return res;
  22541. } /* END test_wc_ecc_ctx_new */
  22542. /*
  22543. * Tesing wc_ecc_reset()
  22544. */
  22545. static int test_wc_ecc_ctx_reset(void)
  22546. {
  22547. int res = TEST_SKIPPED;
  22548. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22549. ecEncCtx* ctx = NULL;
  22550. WC_RNG rng;
  22551. int ret = 0;
  22552. ret = wc_InitRng(&rng);
  22553. if (ret == 0) {
  22554. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  22555. ret = WOLFSSL_FATAL_ERROR;
  22556. }
  22557. }
  22558. if (ret == 0) {
  22559. ret = wc_ecc_ctx_reset(ctx, &rng);
  22560. }
  22561. /* Pass in bad args. */
  22562. if (ret == 0) {
  22563. ret = wc_ecc_ctx_reset(NULL, &rng);
  22564. if (ret == BAD_FUNC_ARG) {
  22565. ret = wc_ecc_ctx_reset(ctx, NULL);
  22566. }
  22567. if (ret == BAD_FUNC_ARG) {
  22568. ret = 0;
  22569. }
  22570. else if (ret == 0) {
  22571. ret = WOLFSSL_FATAL_ERROR;
  22572. }
  22573. }
  22574. if (wc_FreeRng(&rng) && ret == 0) {
  22575. ret = WOLFSSL_FATAL_ERROR;
  22576. }
  22577. wc_ecc_ctx_free(ctx);
  22578. res = TEST_RES_CHECK(ret == 0);
  22579. #endif
  22580. return res;
  22581. } /* END test_wc_ecc_ctx_reset */
  22582. /*
  22583. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22584. */
  22585. static int test_wc_ecc_ctx_set_peer_salt(void)
  22586. {
  22587. int res = TEST_SKIPPED;
  22588. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22589. WC_RNG rng;
  22590. ecEncCtx* cliCtx = NULL;
  22591. ecEncCtx* servCtx = NULL;
  22592. const byte* cliSalt = NULL;
  22593. const byte* servSalt = NULL;
  22594. int ret = 0;
  22595. ret = wc_InitRng(&rng);
  22596. if (ret == 0) {
  22597. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  22598. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  22599. ret = WOLFSSL_FATAL_ERROR;
  22600. }
  22601. }
  22602. /* Test bad args. */
  22603. if (ret == 0) {
  22604. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  22605. if (cliSalt != NULL) {
  22606. ret = WOLFSSL_FATAL_ERROR;
  22607. }
  22608. }
  22609. if (ret == 0) {
  22610. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  22611. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  22612. if (cliSalt == NULL || servSalt == NULL) {
  22613. ret = WOLFSSL_FATAL_ERROR;
  22614. }
  22615. }
  22616. if (ret == 0) {
  22617. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  22618. }
  22619. /* Test bad args. */
  22620. if (ret == 0) {
  22621. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  22622. if (ret == BAD_FUNC_ARG) {
  22623. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22624. }
  22625. if (ret == BAD_FUNC_ARG) {
  22626. ret = 0;
  22627. }
  22628. else if (ret == 0) {
  22629. ret = WOLFSSL_FATAL_ERROR;
  22630. }
  22631. }
  22632. if (wc_FreeRng(&rng) && ret == 0) {
  22633. ret = WOLFSSL_FATAL_ERROR;
  22634. }
  22635. wc_ecc_ctx_free(cliCtx);
  22636. wc_ecc_ctx_free(servCtx);
  22637. res = TEST_RES_CHECK(ret == 0);
  22638. #endif
  22639. return res;
  22640. } /* END test_wc_ecc_ctx_set_peer_salt */
  22641. /*
  22642. * Testing wc_ecc_ctx_set_info()
  22643. */
  22644. static int test_wc_ecc_ctx_set_info(void)
  22645. {
  22646. int res = TEST_SKIPPED;
  22647. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22648. ecEncCtx* ctx = NULL;
  22649. WC_RNG rng;
  22650. int ret;
  22651. const char* optInfo = "Optional Test Info.";
  22652. int optInfoSz = (int)XSTRLEN(optInfo);
  22653. const char* badOptInfo = NULL;
  22654. ret = wc_InitRng(&rng);
  22655. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22656. ret = WOLFSSL_FATAL_ERROR;
  22657. }
  22658. if (ret == 0) {
  22659. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22660. }
  22661. /* Test bad args. */
  22662. if (ret == 0) {
  22663. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22664. if (ret == BAD_FUNC_ARG) {
  22665. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22666. }
  22667. if (ret == BAD_FUNC_ARG) {
  22668. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22669. }
  22670. if (ret == BAD_FUNC_ARG) {
  22671. ret = 0;
  22672. }
  22673. else if (ret == 0) {
  22674. ret = WOLFSSL_FATAL_ERROR;
  22675. }
  22676. }
  22677. if (wc_FreeRng(&rng) && ret == 0) {
  22678. ret = WOLFSSL_FATAL_ERROR;
  22679. }
  22680. wc_ecc_ctx_free(ctx);
  22681. res = TEST_RES_CHECK(ret == 0);
  22682. #endif
  22683. return res;
  22684. } /* END test_wc_ecc_ctx_set_info */
  22685. /*
  22686. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22687. */
  22688. static int test_wc_ecc_encryptDecrypt(void)
  22689. {
  22690. int res = TEST_SKIPPED;
  22691. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22692. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22693. ecc_key srvKey, cliKey, tmpKey;
  22694. WC_RNG rng;
  22695. int ret;
  22696. const char* msg = "EccBlock Size 16";
  22697. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22698. #ifdef WOLFSSL_ECIES_OLD
  22699. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22700. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22701. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22702. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22703. #else
  22704. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22705. #endif
  22706. word32 outSz = (word32)sizeof(out);
  22707. byte plain[sizeof("EccBlock Size 16")];
  22708. word32 plainSz = (word32)sizeof(plain);
  22709. int keySz = KEY20;
  22710. /* Init stack variables. */
  22711. XMEMSET(out, 0, outSz);
  22712. XMEMSET(plain, 0, plainSz);
  22713. XMEMSET(&rng, 0, sizeof(rng));
  22714. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22715. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22716. ret = wc_InitRng(&rng);
  22717. if (ret == 0) {
  22718. ret = wc_ecc_init(&cliKey);
  22719. if (ret == 0) {
  22720. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22721. #if defined(WOLFSSL_ASYNC_CRYPT)
  22722. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22723. #endif
  22724. }
  22725. if (ret == 0) {
  22726. ret = wc_ecc_init(&srvKey);
  22727. }
  22728. if (ret == 0) {
  22729. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22730. #if defined(WOLFSSL_ASYNC_CRYPT)
  22731. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22732. #endif
  22733. }
  22734. if (ret == 0) {
  22735. ret = wc_ecc_init(&tmpKey);
  22736. }
  22737. }
  22738. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22739. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22740. !defined(HAVE_SELFTEST)
  22741. if (ret == 0) {
  22742. ret = wc_ecc_set_rng(&srvKey, &rng);
  22743. }
  22744. if (ret == 0) {
  22745. ret = wc_ecc_set_rng(&cliKey, &rng);
  22746. }
  22747. #endif
  22748. if (ret == 0) {
  22749. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22750. &outSz, NULL);
  22751. }
  22752. if (ret == 0) {
  22753. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22754. &outSz, NULL);
  22755. if (ret == BAD_FUNC_ARG) {
  22756. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22757. &outSz, NULL);
  22758. }
  22759. if (ret == BAD_FUNC_ARG) {
  22760. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22761. &outSz, NULL);
  22762. }
  22763. if (ret == BAD_FUNC_ARG) {
  22764. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22765. &outSz, NULL);
  22766. }
  22767. if (ret == BAD_FUNC_ARG) {
  22768. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22769. NULL, NULL);
  22770. }
  22771. if (ret == BAD_FUNC_ARG) {
  22772. ret = 0;
  22773. }
  22774. else if (ret == 0) {
  22775. ret = WOLFSSL_FATAL_ERROR;
  22776. }
  22777. }
  22778. #ifdef WOLFSSL_ECIES_OLD
  22779. if (ret == 0) {
  22780. tmpKey.dp = cliKey.dp;
  22781. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22782. }
  22783. #endif
  22784. if (ret == 0) {
  22785. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22786. &plainSz, NULL);
  22787. }
  22788. if (ret == 0) {
  22789. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22790. &plainSz, NULL);
  22791. #ifdef WOLFSSL_ECIES_OLD
  22792. /* NULL parameter allowed in new implementations - public key comes from
  22793. * the message. */
  22794. if (ret == BAD_FUNC_ARG) {
  22795. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22796. &plainSz, NULL);
  22797. }
  22798. #endif
  22799. if (ret == BAD_FUNC_ARG) {
  22800. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22801. &plainSz, NULL);
  22802. }
  22803. if (ret == BAD_FUNC_ARG) {
  22804. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22805. &plainSz, NULL);
  22806. }
  22807. if (ret == BAD_FUNC_ARG) {
  22808. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22809. plain, NULL, NULL);
  22810. }
  22811. if (ret == BAD_FUNC_ARG) {
  22812. ret = 0;
  22813. }
  22814. else if (ret == 0) {
  22815. ret = WOLFSSL_FATAL_ERROR;
  22816. }
  22817. }
  22818. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22819. ret = WOLFSSL_FATAL_ERROR;
  22820. }
  22821. if (wc_FreeRng(&rng) && ret == 0) {
  22822. ret = WOLFSSL_FATAL_ERROR;
  22823. }
  22824. wc_ecc_free(&tmpKey);
  22825. wc_ecc_free(&cliKey);
  22826. wc_ecc_free(&srvKey);
  22827. res = TEST_RES_CHECK(ret == 0);
  22828. #endif
  22829. return res;
  22830. } /* END test_wc_ecc_encryptDecrypt */
  22831. /*
  22832. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22833. */
  22834. static int test_wc_ecc_del_point(void)
  22835. {
  22836. int res = TEST_SKIPPED;
  22837. #if defined(HAVE_ECC)
  22838. ecc_point* pt;
  22839. pt = wc_ecc_new_point();
  22840. wc_ecc_del_point(pt);
  22841. res = TEST_RES_CHECK(pt != NULL);
  22842. #endif
  22843. return res;
  22844. } /* END test_wc_ecc_del_point */
  22845. /*
  22846. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22847. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22848. * and wc_ecc_cmp_point()
  22849. */
  22850. static int test_wc_ecc_pointFns(void)
  22851. {
  22852. int res = TEST_SKIPPED;
  22853. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22854. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22855. !defined(WOLFSSL_ATECC608A)
  22856. ecc_key key;
  22857. WC_RNG rng;
  22858. int ret;
  22859. ecc_point* point = NULL;
  22860. ecc_point* cpypt = NULL;
  22861. int idx = 0;
  22862. int keySz = KEY32;
  22863. byte der[DER_SZ(KEY32)];
  22864. word32 derlenChk = 0;
  22865. word32 derSz = DER_SZ(KEY32);
  22866. /* Init stack variables. */
  22867. XMEMSET(der, 0, derSz);
  22868. XMEMSET(&rng, 0, sizeof(rng));
  22869. XMEMSET(&key, 0, sizeof(key));
  22870. ret = wc_InitRng(&rng);
  22871. if (ret == 0) {
  22872. ret = wc_ecc_init(&key);
  22873. if (ret == 0) {
  22874. ret = wc_ecc_make_key(&rng, keySz, &key);
  22875. #if defined(WOLFSSL_ASYNC_CRYPT)
  22876. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22877. #endif
  22878. }
  22879. }
  22880. if (ret == 0) {
  22881. point = wc_ecc_new_point();
  22882. if (!point) {
  22883. ret = WOLFSSL_FATAL_ERROR;
  22884. }
  22885. }
  22886. if (ret == 0) {
  22887. cpypt = wc_ecc_new_point();
  22888. if (!cpypt) {
  22889. ret = WOLFSSL_FATAL_ERROR;
  22890. }
  22891. }
  22892. /* Export */
  22893. if (ret == 0) {
  22894. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22895. NULL, &derlenChk);
  22896. /* Check length value. */
  22897. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22898. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22899. der, &derSz);
  22900. }
  22901. }
  22902. /* Test bad args. */
  22903. if (ret == 0) {
  22904. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22905. if (ret == ECC_BAD_ARG_E) {
  22906. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22907. }
  22908. if (ret == ECC_BAD_ARG_E) {
  22909. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22910. der, NULL);
  22911. }
  22912. if (ret == ECC_BAD_ARG_E) {
  22913. ret = 0;
  22914. }
  22915. else if (ret == 0) {
  22916. ret = WOLFSSL_FATAL_ERROR;
  22917. }
  22918. }
  22919. /* Import */
  22920. if (ret == 0) {
  22921. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22922. /* Condition double checks wc_ecc_cmp_point(). */
  22923. if (ret == 0 &&
  22924. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22925. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22926. }
  22927. }
  22928. /* Test bad args. */
  22929. if (ret == 0) {
  22930. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22931. if (ret == ECC_BAD_ARG_E) {
  22932. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22933. }
  22934. if (ret == ECC_BAD_ARG_E) {
  22935. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22936. }
  22937. if (ret == ECC_BAD_ARG_E) {
  22938. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22939. }
  22940. if (ret == ECC_BAD_ARG_E) {
  22941. ret = 0;
  22942. }
  22943. else if (ret == 0) {
  22944. ret = WOLFSSL_FATAL_ERROR;
  22945. }
  22946. }
  22947. /* Copy */
  22948. if (ret == 0) {
  22949. ret = wc_ecc_copy_point(point, cpypt);
  22950. }
  22951. /* Test bad args. */
  22952. if (ret == 0) {
  22953. ret = wc_ecc_copy_point(NULL, cpypt);
  22954. if (ret == ECC_BAD_ARG_E) {
  22955. ret = wc_ecc_copy_point(point, NULL);
  22956. }
  22957. if (ret == ECC_BAD_ARG_E) {
  22958. ret = 0;
  22959. }
  22960. else if (ret == 0) {
  22961. ret = WOLFSSL_FATAL_ERROR;
  22962. }
  22963. }
  22964. /* Compare point */
  22965. if (ret == 0) {
  22966. ret = wc_ecc_cmp_point(point, cpypt);
  22967. }
  22968. /* Test bad args. */
  22969. if (ret == 0) {
  22970. ret = wc_ecc_cmp_point(NULL, cpypt);
  22971. if (ret == BAD_FUNC_ARG) {
  22972. ret = wc_ecc_cmp_point(point, NULL);
  22973. }
  22974. if (ret == BAD_FUNC_ARG) {
  22975. ret = 0;
  22976. }
  22977. else if (ret == 0) {
  22978. ret = WOLFSSL_FATAL_ERROR;
  22979. }
  22980. }
  22981. /* At infinity if return == 1, otherwise return == 0. */
  22982. if (ret == 0) {
  22983. ret = wc_ecc_point_is_at_infinity(point);
  22984. }
  22985. /* Test bad args. */
  22986. if (ret == 0) {
  22987. ret = wc_ecc_point_is_at_infinity(NULL);
  22988. if (ret == BAD_FUNC_ARG) {
  22989. ret = 0;
  22990. }
  22991. else if (ret == 0) {
  22992. ret = WOLFSSL_FATAL_ERROR;
  22993. }
  22994. }
  22995. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22996. #ifdef USE_ECC_B_PARAM
  22997. /* On curve if ret == 0 */
  22998. if (ret == 0) {
  22999. ret = wc_ecc_point_is_on_curve(point, idx);
  23000. }
  23001. /* Test bad args. */
  23002. if (ret == 0) {
  23003. ret = wc_ecc_point_is_on_curve(NULL, idx);
  23004. if (ret == BAD_FUNC_ARG) {
  23005. ret = wc_ecc_point_is_on_curve(point, 1000);
  23006. }
  23007. if (ret == ECC_BAD_ARG_E) {
  23008. ret = 0;
  23009. }
  23010. else if (ret == 0) {
  23011. ret = WOLFSSL_FATAL_ERROR;
  23012. }
  23013. }
  23014. #endif /* USE_ECC_B_PARAM */
  23015. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  23016. /* Free */
  23017. wc_ecc_del_point(point);
  23018. wc_ecc_del_point(cpypt);
  23019. wc_ecc_free(&key);
  23020. if (wc_FreeRng(&rng) && ret == 0) {
  23021. ret = WOLFSSL_FATAL_ERROR;
  23022. }
  23023. res = TEST_RES_CHECK(ret == 0);
  23024. #endif
  23025. return res;
  23026. } /* END test_wc_ecc_pointFns */
  23027. /*
  23028. * Testing wc_ecc_sahred_secret_ssh()
  23029. */
  23030. static int test_wc_ecc_shared_secret_ssh(void)
  23031. {
  23032. int res = TEST_SKIPPED;
  23033. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  23034. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23035. !defined(WOLFSSL_ATECC608A)
  23036. ecc_key key, key2;
  23037. WC_RNG rng;
  23038. int ret;
  23039. int keySz = KEY32;
  23040. int key2Sz = KEY24;
  23041. byte secret[KEY32];
  23042. word32 secretLen = keySz;
  23043. /* Init stack variables. */
  23044. XMEMSET(secret, 0, secretLen);
  23045. XMEMSET(&rng, 0, sizeof(rng));
  23046. XMEMSET(&key, 0, sizeof(key));
  23047. XMEMSET(&key2, 0, sizeof(key2));
  23048. /* Make keys */
  23049. ret = wc_InitRng(&rng);
  23050. if (ret == 0) {
  23051. ret = wc_ecc_init(&key);
  23052. if (ret == 0) {
  23053. ret = wc_ecc_make_key(&rng, keySz, &key);
  23054. #if defined(WOLFSSL_ASYNC_CRYPT)
  23055. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23056. #endif
  23057. }
  23058. if (wc_FreeRng(&rng) && ret == 0) {
  23059. ret = WOLFSSL_FATAL_ERROR;
  23060. }
  23061. }
  23062. if (ret == 0) {
  23063. ret = wc_InitRng(&rng);
  23064. if (ret == 0) {
  23065. ret = wc_ecc_init(&key2);
  23066. }
  23067. if (ret == 0) {
  23068. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  23069. #if defined(WOLFSSL_ASYNC_CRYPT)
  23070. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  23071. #endif
  23072. }
  23073. }
  23074. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23075. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23076. !defined(HAVE_SELFTEST)
  23077. if (ret == 0) {
  23078. ret = wc_ecc_set_rng(&key, &rng);
  23079. }
  23080. #endif
  23081. if (ret == 0) {
  23082. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23083. }
  23084. /* Pass in bad args. */
  23085. if (ret == 0) {
  23086. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  23087. if (ret == BAD_FUNC_ARG) {
  23088. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  23089. }
  23090. if (ret == BAD_FUNC_ARG) {
  23091. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  23092. }
  23093. if (ret == BAD_FUNC_ARG) {
  23094. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  23095. }
  23096. if (ret == BAD_FUNC_ARG) {
  23097. key.type = ECC_PUBLICKEY;
  23098. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23099. if (ret == ECC_BAD_ARG_E) {
  23100. ret = 0;
  23101. }
  23102. else if (ret == 0) {
  23103. ret = WOLFSSL_FATAL_ERROR;
  23104. }
  23105. }
  23106. else if (ret == 0) {
  23107. ret = WOLFSSL_FATAL_ERROR;
  23108. }
  23109. }
  23110. if (wc_FreeRng(&rng) && ret == 0) {
  23111. ret = WOLFSSL_FATAL_ERROR;
  23112. }
  23113. wc_ecc_free(&key);
  23114. wc_ecc_free(&key2);
  23115. #ifdef FP_ECC
  23116. wc_ecc_fp_free();
  23117. #endif
  23118. res = TEST_RES_CHECK(ret == 0);
  23119. #endif
  23120. return res;
  23121. } /* END test_wc_ecc_shared_secret_ssh */
  23122. /*
  23123. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23124. */
  23125. static int test_wc_ecc_verify_hash_ex(void)
  23126. {
  23127. int res = TEST_SKIPPED;
  23128. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23129. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23130. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23131. ecc_key key;
  23132. WC_RNG rng;
  23133. int ret;
  23134. mp_int r;
  23135. mp_int s;
  23136. mp_int z;
  23137. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23138. unsigned char iHash[] = "Everyone gets Friday off.......";
  23139. unsigned char shortHash[] = TEST_STRING;
  23140. word32 hashlen = sizeof(hash);
  23141. word32 iHashLen = sizeof(iHash);
  23142. word32 shortHashLen = sizeof(shortHash);
  23143. int keySz = KEY32;
  23144. int sig = WOLFSSL_FATAL_ERROR;
  23145. int ver = WOLFSSL_FATAL_ERROR;
  23146. int verify_ok = 0;
  23147. /* Initialize r and s. */
  23148. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23149. if (ret != MP_OKAY) {
  23150. return MP_INIT_E;
  23151. }
  23152. ret = wc_InitRng(&rng);
  23153. if (ret == 0) {
  23154. ret = wc_ecc_init(&key);
  23155. if (ret == 0) {
  23156. ret = wc_ecc_make_key(&rng, keySz, &key);
  23157. #if defined(WOLFSSL_ASYNC_CRYPT)
  23158. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23159. #endif
  23160. }
  23161. }
  23162. if (ret == 0) {
  23163. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23164. if (ret == 0) {
  23165. /* verify_ok should be 1. */
  23166. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23167. if (verify_ok != 1 && ret == 0) {
  23168. ret = WOLFSSL_FATAL_ERROR;
  23169. }
  23170. }
  23171. if (ret == 0) {
  23172. /* verify_ok should be 0 */
  23173. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23174. &verify_ok, &key);
  23175. if (verify_ok != 0 && ret == 0) {
  23176. ret = WOLFSSL_FATAL_ERROR;
  23177. }
  23178. }
  23179. if (ret == 0) {
  23180. /* verify_ok should be 0. */
  23181. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23182. &verify_ok, &key);
  23183. if (verify_ok != 0 && ret == 0) {
  23184. ret = WOLFSSL_FATAL_ERROR;
  23185. }
  23186. }
  23187. }
  23188. /* Test bad args. */
  23189. if (ret == 0) {
  23190. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23191. == ECC_BAD_ARG_E) {
  23192. sig = 0;
  23193. }
  23194. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23195. != ECC_BAD_ARG_E) {
  23196. sig = WOLFSSL_FATAL_ERROR;
  23197. }
  23198. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23199. != ECC_BAD_ARG_E) {
  23200. sig = WOLFSSL_FATAL_ERROR;
  23201. }
  23202. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23203. != ECC_BAD_ARG_E) {
  23204. sig = WOLFSSL_FATAL_ERROR;
  23205. }
  23206. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23207. != ECC_BAD_ARG_E) {
  23208. sig = WOLFSSL_FATAL_ERROR;
  23209. }
  23210. }
  23211. /* Test bad args. */
  23212. if (ret == 0) {
  23213. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23214. == ECC_BAD_ARG_E) {
  23215. ver = 0;
  23216. }
  23217. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23218. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23219. ver = WOLFSSL_FATAL_ERROR;
  23220. }
  23221. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23222. != MP_ZERO_E) {
  23223. ver = WOLFSSL_FATAL_ERROR;
  23224. }
  23225. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23226. != MP_ZERO_E) {
  23227. ver = WOLFSSL_FATAL_ERROR;
  23228. }
  23229. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23230. != MP_ZERO_E) {
  23231. ver = WOLFSSL_FATAL_ERROR;
  23232. }
  23233. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23234. &key) != ECC_BAD_ARG_E) {
  23235. ver = WOLFSSL_FATAL_ERROR;
  23236. }
  23237. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23238. NULL, &key) != ECC_BAD_ARG_E) {
  23239. ver = WOLFSSL_FATAL_ERROR;
  23240. }
  23241. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23242. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23243. ver = WOLFSSL_FATAL_ERROR;
  23244. }
  23245. }
  23246. wc_ecc_free(&key);
  23247. mp_free(&r);
  23248. mp_free(&s);
  23249. if (wc_FreeRng(&rng)) {
  23250. return WOLFSSL_FATAL_ERROR;
  23251. }
  23252. if (ret == 0 && (sig != 0 || ver != 0)) {
  23253. ret = WOLFSSL_FATAL_ERROR;
  23254. }
  23255. res = TEST_RES_CHECK(ret == 0);
  23256. #endif
  23257. return res;
  23258. } /* END test_wc_ecc_verify_hash_ex */
  23259. /*
  23260. * Testing wc_ecc_mulmod()
  23261. */
  23262. static int test_wc_ecc_mulmod(void)
  23263. {
  23264. int res = TEST_SKIPPED;
  23265. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23266. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23267. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23268. ecc_key key1, key2, key3;
  23269. WC_RNG rng;
  23270. int ret = 0;
  23271. ret = wc_InitRng(&rng);
  23272. if (ret == 0) {
  23273. ret = wc_ecc_init(&key1);
  23274. if (ret == 0) {
  23275. ret = wc_ecc_init(&key2);
  23276. }
  23277. if (ret == 0) {
  23278. ret = wc_ecc_init(&key3);
  23279. }
  23280. if (ret == 0) {
  23281. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23282. #if defined(WOLFSSL_ASYNC_CRYPT)
  23283. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23284. #endif
  23285. }
  23286. wc_FreeRng(&rng);
  23287. }
  23288. if (ret == 0) {
  23289. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23290. ECC_SECP256R1);
  23291. if (ret == 0) {
  23292. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23293. key1.dp->prime, ECC_SECP256R1);
  23294. }
  23295. }
  23296. if (ret == 0) {
  23297. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  23298. &key3.k, 1);
  23299. }
  23300. /* Test bad args. */
  23301. if (ret == 0) {
  23302. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  23303. &key3.k, 1);
  23304. if (ret == ECC_BAD_ARG_E) {
  23305. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  23306. &key3.k, 1);
  23307. }
  23308. if (ret == ECC_BAD_ARG_E) {
  23309. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  23310. &key3.k, 1);
  23311. }
  23312. if (ret == ECC_BAD_ARG_E) {
  23313. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  23314. &key2.k, NULL, 1);
  23315. }
  23316. if (ret == ECC_BAD_ARG_E) {
  23317. ret = 0;
  23318. }
  23319. else if (ret == 0) {
  23320. ret = WOLFSSL_FATAL_ERROR;
  23321. }
  23322. }
  23323. wc_ecc_free(&key1);
  23324. wc_ecc_free(&key2);
  23325. wc_ecc_free(&key3);
  23326. #ifdef FP_ECC
  23327. wc_ecc_fp_free();
  23328. #endif
  23329. res = TEST_RES_CHECK(ret == 0);
  23330. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23331. return res;
  23332. } /* END test_wc_ecc_mulmod */
  23333. /*
  23334. * Testing wc_ecc_is_valid_idx()
  23335. */
  23336. static int test_wc_ecc_is_valid_idx(void)
  23337. {
  23338. int res = TEST_SKIPPED;
  23339. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23340. ecc_key key;
  23341. WC_RNG rng;
  23342. int ret;
  23343. int iVal = -2;
  23344. int iVal2 = 3000;
  23345. XMEMSET(&rng, 0, sizeof(rng));
  23346. XMEMSET(&key, 0, sizeof(key));
  23347. ret = wc_InitRng(&rng);
  23348. if (ret == 0) {
  23349. ret = wc_ecc_init(&key);
  23350. if (ret == 0) {
  23351. ret = wc_ecc_make_key(&rng, 32, &key);
  23352. #if defined(WOLFSSL_ASYNC_CRYPT)
  23353. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23354. #endif
  23355. }
  23356. }
  23357. if (ret == 0) {
  23358. ret = wc_ecc_is_valid_idx(key.idx);
  23359. if (ret == 1) {
  23360. ret = 0;
  23361. }
  23362. else {
  23363. ret = WOLFSSL_FATAL_ERROR;
  23364. }
  23365. }
  23366. /* Test bad args. */
  23367. if (ret == 0) {
  23368. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23369. if (ret == 0) {
  23370. ret = wc_ecc_is_valid_idx(iVal2);
  23371. }
  23372. if (ret != 0) {
  23373. ret = WOLFSSL_FATAL_ERROR;
  23374. }
  23375. }
  23376. if (wc_FreeRng(&rng) && ret == 0) {
  23377. ret = WOLFSSL_FATAL_ERROR;
  23378. }
  23379. wc_ecc_free(&key);
  23380. #ifdef FP_ECC
  23381. wc_ecc_fp_free();
  23382. #endif
  23383. res = TEST_RES_CHECK(ret == 0);
  23384. #endif
  23385. return res;
  23386. } /* END test_wc_ecc_is_valid_idx */
  23387. /*
  23388. * Testing wc_ecc_get_curve_id_from_oid()
  23389. */
  23390. static int test_wc_ecc_get_curve_id_from_oid(void)
  23391. {
  23392. int res = TEST_SKIPPED;
  23393. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23394. !defined(HAVE_FIPS)
  23395. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23396. word32 len = sizeof(oid);
  23397. int ret;
  23398. /* Bad Cases */
  23399. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  23400. if (ret == BAD_FUNC_ARG) {
  23401. ret = 0;
  23402. }
  23403. if (ret == 0) {
  23404. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  23405. if (ret == ECC_CURVE_INVALID) {
  23406. ret = 0;
  23407. }
  23408. }
  23409. /* Good Case */
  23410. if (ret == 0) {
  23411. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  23412. if (ret == ECC_SECP256R1) {
  23413. ret = 0;
  23414. }
  23415. }
  23416. res = TEST_RES_CHECK(ret == 0);
  23417. #endif
  23418. return res;
  23419. }/* END test_wc_ecc_get_curve_id_from_oid */
  23420. /*
  23421. * Testing wc_ecc_sig_size_calc()
  23422. */
  23423. static int test_wc_ecc_sig_size_calc(void)
  23424. {
  23425. int res = TEST_SKIPPED;
  23426. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  23427. ecc_key key;
  23428. WC_RNG rng;
  23429. int sz = 0;
  23430. int ret = 0;
  23431. ret = wc_InitRng(&rng);
  23432. if (ret == 0) {
  23433. ret = wc_ecc_init(&key);
  23434. if (ret == 0) {
  23435. ret = wc_ecc_make_key(&rng, 16, &key);
  23436. #if defined(WOLFSSL_ASYNC_CRYPT)
  23437. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23438. #endif
  23439. }
  23440. sz = key.dp->size;
  23441. }
  23442. if (ret == 0) {
  23443. ret = wc_ecc_sig_size_calc(sz);
  23444. if (ret > 0) {
  23445. ret = 0;
  23446. }
  23447. }
  23448. wc_ecc_free(&key);
  23449. wc_FreeRng(&rng);
  23450. res = TEST_RES_CHECK(ret == 0);
  23451. #endif
  23452. return res;
  23453. } /* END test_wc_ecc_sig_size_calc */
  23454. /*
  23455. * Testing ToTraditional
  23456. */
  23457. static int test_ToTraditional(void)
  23458. {
  23459. int res = TEST_SKIPPED;
  23460. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23461. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23462. defined(OPENSSL_EXTRA_X509_SMALL))
  23463. XFILE f;
  23464. byte input[TWOK_BUF];
  23465. word32 sz;
  23466. int ret;
  23467. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  23468. AssertTrue((f != XBADFILE));
  23469. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  23470. XFCLOSE(f);
  23471. /* Good case */
  23472. ret = ToTraditional(input, sz);
  23473. if (ret > 0) {
  23474. ret = 0;
  23475. }
  23476. /* Bad cases */
  23477. if (ret == 0) {
  23478. ret = ToTraditional(NULL, 0);
  23479. if (ret == BAD_FUNC_ARG) {
  23480. ret = 0;
  23481. }
  23482. }
  23483. if (ret == 0) {
  23484. ret = ToTraditional(NULL, sz);
  23485. if (ret == BAD_FUNC_ARG) {
  23486. ret = 0;
  23487. }
  23488. }
  23489. if (ret == 0) {
  23490. ret = ToTraditional(input, 0);
  23491. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  23492. ret = 0;
  23493. }
  23494. }
  23495. res = TEST_RES_CHECK(ret == 0);
  23496. #endif
  23497. return res;
  23498. }/* End test_ToTraditional*/
  23499. /*
  23500. * Testing wc_EccPrivateKeyToDer
  23501. */
  23502. static int test_wc_EccPrivateKeyToDer(void)
  23503. {
  23504. int res = TEST_SKIPPED;
  23505. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23506. byte output[ONEK_BUF];
  23507. ecc_key eccKey;
  23508. WC_RNG rng;
  23509. word32 inLen;
  23510. int ret;
  23511. ret = wc_InitRng(&rng);
  23512. if (ret == 0) {
  23513. ret = wc_ecc_init(&eccKey);
  23514. if (ret == 0) {
  23515. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23516. #if defined(WOLFSSL_ASYNC_CRYPT)
  23517. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23518. #endif
  23519. }
  23520. inLen = (word32)sizeof(output);
  23521. /* Bad Cases */
  23522. if (ret == 0) {
  23523. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  23524. if (ret == BAD_FUNC_ARG) {
  23525. ret = 0;
  23526. }
  23527. }
  23528. if (ret == 0) {
  23529. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  23530. if (ret == BAD_FUNC_ARG) {
  23531. ret = 0;
  23532. }
  23533. }
  23534. if (ret == 0) {
  23535. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  23536. if (ret == LENGTH_ONLY_E) {
  23537. ret = 0;
  23538. }
  23539. }
  23540. if (ret == 0) {
  23541. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  23542. if (ret == BAD_FUNC_ARG) {
  23543. ret = 0;
  23544. }
  23545. }
  23546. /*Good Case */
  23547. if (ret == 0) {
  23548. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  23549. if (ret > 0) {
  23550. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23551. /* test importing private only into a PKEY struct */
  23552. EC_KEY* ec;
  23553. EVP_PKEY* pkey;
  23554. const unsigned char* der = output;
  23555. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  23556. AssertNotNull(pkey);
  23557. der = output;
  23558. ec = d2i_ECPrivateKey(NULL, &der, ret);
  23559. AssertNotNull(ec);
  23560. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23561. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23562. #endif
  23563. ret = 0;
  23564. }
  23565. }
  23566. wc_ecc_free(&eccKey);
  23567. }
  23568. wc_FreeRng(&rng);
  23569. res = TEST_RES_CHECK(ret == 0);
  23570. #endif
  23571. return res;
  23572. }/* End test_wc_EccPrivateKeyToDer*/
  23573. /*
  23574. * Testing wc_DhPublicKeyDecode
  23575. */
  23576. static int test_wc_DhPublicKeyDecode(void)
  23577. {
  23578. int res = TEST_SKIPPED;
  23579. #ifndef NO_DH
  23580. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23581. DhKey key;
  23582. word32 inOutIdx;
  23583. AssertIntEQ(wc_InitDhKey(&key), 0);
  23584. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  23585. BAD_FUNC_ARG);
  23586. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23587. BAD_FUNC_ARG);
  23588. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23589. BAD_FUNC_ARG);
  23590. inOutIdx = 0;
  23591. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23592. BAD_FUNC_ARG);
  23593. inOutIdx = 0;
  23594. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23595. BAD_FUNC_ARG);
  23596. inOutIdx = 0;
  23597. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23598. sizeof_dh_pub_key_der_2048), 0);
  23599. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  23600. key.pub.used != 0 && key.priv.used == 0);
  23601. wc_FreeDhKey(&key);
  23602. res = TEST_RES_CHECK(1);
  23603. #endif
  23604. #endif /* !NO_DH */
  23605. return res;
  23606. }
  23607. /*
  23608. * Testing wc_Ed25519KeyToDer
  23609. */
  23610. static int test_wc_Ed25519KeyToDer(void)
  23611. {
  23612. int res = TEST_SKIPPED;
  23613. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23614. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23615. byte output[ONEK_BUF];
  23616. ed25519_key ed25519Key;
  23617. WC_RNG rng;
  23618. word32 inLen;
  23619. int ret;
  23620. ret = wc_InitRng(&rng);
  23621. if (ret == 0) {
  23622. ret = wc_ed25519_init(&ed25519Key);
  23623. if (ret == 0) {
  23624. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23625. }
  23626. inLen = (word32)sizeof(output);
  23627. /* Bad Cases */
  23628. if (ret == 0) {
  23629. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23630. if (ret == BAD_FUNC_ARG) {
  23631. ret = 0;
  23632. }
  23633. }
  23634. if (ret == 0) {
  23635. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23636. if (ret == BAD_FUNC_ARG) {
  23637. ret = 0;
  23638. }
  23639. }
  23640. if (ret == 0) {
  23641. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23642. if (ret == BAD_FUNC_ARG) {
  23643. ret = 0;
  23644. }
  23645. }
  23646. /* Good Cases */
  23647. if (ret == 0) {
  23648. /* length only */
  23649. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23650. if (ret > 0) {
  23651. ret = 0;
  23652. }
  23653. }
  23654. if (ret == 0) {
  23655. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23656. if (ret > 0) {
  23657. ret = 0;
  23658. }
  23659. }
  23660. wc_ed25519_free(&ed25519Key);
  23661. }
  23662. wc_FreeRng(&rng);
  23663. res = TEST_RES_CHECK(ret == 0);
  23664. #endif
  23665. return res;
  23666. }/* End test_wc_Ed25519KeyToDer*/
  23667. /*
  23668. * Testing wc_Ed25519PrivateKeyToDer
  23669. */
  23670. static int test_wc_Ed25519PrivateKeyToDer(void)
  23671. {
  23672. int res = TEST_SKIPPED;
  23673. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23674. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23675. byte output[ONEK_BUF];
  23676. ed25519_key ed25519PrivKey;
  23677. WC_RNG rng;
  23678. word32 inLen;
  23679. int ret;
  23680. ret = wc_InitRng(&rng);
  23681. if (ret == 0) {
  23682. ret = wc_ed25519_init(&ed25519PrivKey);
  23683. if (ret == 0) {
  23684. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23685. }
  23686. inLen = (word32)sizeof(output);
  23687. /* Bad Cases */
  23688. if (ret == 0) {
  23689. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23690. if (ret == BAD_FUNC_ARG) {
  23691. ret = 0;
  23692. }
  23693. }
  23694. if (ret == 0) {
  23695. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23696. if (ret == BAD_FUNC_ARG) {
  23697. ret = 0;
  23698. }
  23699. }
  23700. if (ret == 0) {
  23701. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23702. if (ret == BAD_FUNC_ARG) {
  23703. ret = 0;
  23704. }
  23705. }
  23706. /* Good Cases */
  23707. if (ret == 0) {
  23708. /* length only */
  23709. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23710. if (ret > 0) {
  23711. ret = 0;
  23712. }
  23713. }
  23714. if (ret == 0) {
  23715. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23716. if (ret > 0) {
  23717. ret = 0;
  23718. }
  23719. }
  23720. wc_ed25519_free(&ed25519PrivKey);
  23721. }
  23722. wc_FreeRng(&rng);
  23723. res = TEST_RES_CHECK(ret == 0);
  23724. #endif
  23725. return res;
  23726. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23727. /*
  23728. * Testing wc_Ed448KeyToDer
  23729. */
  23730. static int test_wc_Ed448KeyToDer(void)
  23731. {
  23732. int res = TEST_SKIPPED;
  23733. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23734. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23735. byte output[ONEK_BUF];
  23736. ed448_key ed448Key;
  23737. WC_RNG rng;
  23738. word32 inLen;
  23739. int ret;
  23740. ret = wc_InitRng(&rng);
  23741. if (ret == 0) {
  23742. ret = wc_ed448_init(&ed448Key);
  23743. if (ret == 0) {
  23744. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23745. }
  23746. inLen = sizeof(output);
  23747. /* Bad Cases */
  23748. if (ret == 0) {
  23749. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23750. if (ret == BAD_FUNC_ARG) {
  23751. ret = 0;
  23752. }
  23753. }
  23754. if (ret == 0) {
  23755. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23756. if (ret == BAD_FUNC_ARG) {
  23757. ret = 0;
  23758. }
  23759. }
  23760. if (ret == 0) {
  23761. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23762. if (ret == BAD_FUNC_ARG) {
  23763. ret = 0;
  23764. }
  23765. }
  23766. /* Good Cases */
  23767. if (ret == 0) {
  23768. /* length only */
  23769. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23770. if (ret > 0) {
  23771. ret = 0;
  23772. }
  23773. }
  23774. if (ret == 0) {
  23775. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23776. if (ret > 0) {
  23777. ret = 0;
  23778. }
  23779. }
  23780. wc_ed448_free(&ed448Key);
  23781. }
  23782. wc_FreeRng(&rng);
  23783. res = TEST_RES_CHECK(ret == 0);
  23784. #endif
  23785. return res;
  23786. }/* End test_wc_Ed448KeyToDer*/
  23787. /*
  23788. * Testing wc_Ed448PrivateKeyToDer
  23789. */
  23790. static int test_wc_Ed448PrivateKeyToDer(void)
  23791. {
  23792. int res = TEST_SKIPPED;
  23793. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23794. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23795. byte output[ONEK_BUF];
  23796. ed448_key ed448PrivKey;
  23797. WC_RNG rng;
  23798. word32 inLen;
  23799. int ret;
  23800. ret = wc_InitRng(&rng);
  23801. if (ret == 0) {
  23802. ret = wc_ed448_init(&ed448PrivKey);
  23803. if (ret == 0) {
  23804. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23805. }
  23806. inLen = sizeof(output);
  23807. /* Bad Cases */
  23808. if (ret == 0) {
  23809. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23810. if (ret == BAD_FUNC_ARG) {
  23811. ret = 0;
  23812. }
  23813. }
  23814. if (ret == 0) {
  23815. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23816. if (ret == BAD_FUNC_ARG) {
  23817. ret = 0;
  23818. }
  23819. }
  23820. if (ret == 0) {
  23821. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23822. if (ret == BAD_FUNC_ARG) {
  23823. ret = 0;
  23824. }
  23825. }
  23826. /* Good cases */
  23827. if (ret == 0) {
  23828. /* length only */
  23829. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23830. if (ret > 0) {
  23831. ret = 0;
  23832. }
  23833. }
  23834. if (ret == 0) {
  23835. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23836. if (ret > 0) {
  23837. ret = 0;
  23838. }
  23839. }
  23840. wc_ed448_free(&ed448PrivKey);
  23841. }
  23842. wc_FreeRng(&rng);
  23843. res = TEST_RES_CHECK(ret == 0);
  23844. #endif
  23845. return res;
  23846. }/* End test_wc_Ed448PrivateKeyToDer*/
  23847. /*
  23848. * Testing wc_SetSubjectBuffer
  23849. */
  23850. static int test_wc_SetSubjectBuffer(void)
  23851. {
  23852. int res = TEST_SKIPPED;
  23853. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  23854. Cert cert;
  23855. FILE* file;
  23856. byte* der;
  23857. word32 derSz;
  23858. int ret = 0;
  23859. derSz = FOURK_BUF;
  23860. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23861. if (der == NULL) {
  23862. ret = -1;
  23863. }
  23864. if (ret == 0) {
  23865. file = XFOPEN("./certs/ca-cert.der", "rb");
  23866. if (file != NULL) {
  23867. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23868. XFCLOSE(file);
  23869. }
  23870. else {
  23871. ret = -1;
  23872. }
  23873. }
  23874. if (ret == 0) {
  23875. ret = wc_InitCert(&cert);
  23876. }
  23877. if (ret == 0) {
  23878. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23879. }
  23880. if (ret == 0) {
  23881. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23882. if (ret == BAD_FUNC_ARG) {
  23883. ret = 0;
  23884. }
  23885. }
  23886. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23887. res = TEST_RES_CHECK(ret == 0);
  23888. #endif
  23889. return res;
  23890. }/* End test_wc_SetSubjectBuffer*/
  23891. /*
  23892. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23893. */
  23894. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23895. {
  23896. int res = TEST_SKIPPED;
  23897. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23898. WC_RNG rng;
  23899. Cert cert;
  23900. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23901. ed25519_key ed25519Key;
  23902. #endif
  23903. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23904. RsaKey rsaKey;
  23905. int bits = 2048;
  23906. #endif
  23907. #if defined(HAVE_ECC)
  23908. ecc_key eccKey;
  23909. #endif
  23910. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23911. ed448_key ed448Key;
  23912. #endif
  23913. int ret = 0;
  23914. #ifndef HAVE_FIPS
  23915. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23916. #else
  23917. ret = wc_InitRng(&rng);
  23918. #endif
  23919. wc_InitCert(&cert);
  23920. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23921. if (ret == 0) { /*ED25519*/
  23922. ret = wc_ed25519_init(&ed25519Key);
  23923. if (ret == 0) {
  23924. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23925. }
  23926. if (ret == 0) {
  23927. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23928. &ed25519Key);
  23929. }
  23930. wc_ed25519_free(&ed25519Key);
  23931. }
  23932. #endif
  23933. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23934. if (ret == 0) { /*RSA*/
  23935. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23936. if (ret == 0) {
  23937. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23938. }
  23939. if (ret == 0) {
  23940. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23941. }
  23942. wc_FreeRsaKey(&rsaKey);
  23943. }
  23944. #endif
  23945. #if defined(HAVE_ECC)
  23946. if (ret == 0) { /*ECC*/
  23947. ret = wc_ecc_init(&eccKey);
  23948. if (ret == 0) {
  23949. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23950. #if defined(WOLFSSL_ASYNC_CRYPT)
  23951. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23952. #endif
  23953. }
  23954. if (ret == 0) {
  23955. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23956. }
  23957. wc_ecc_free(&eccKey);
  23958. }
  23959. #endif
  23960. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23961. if (ret == 0) { /*ED448*/
  23962. ret = wc_ed448_init(&ed448Key);
  23963. if (ret == 0) {
  23964. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23965. }
  23966. if (ret == 0) {
  23967. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23968. &ed448Key);
  23969. }
  23970. wc_ed448_free(&ed448Key);
  23971. }
  23972. #endif
  23973. wc_FreeRng(&rng);
  23974. res = TEST_RES_CHECK(ret == 0);
  23975. #endif
  23976. return res;
  23977. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23978. /*
  23979. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23980. */
  23981. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23982. {
  23983. int res = TEST_SKIPPED;
  23984. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23985. WC_RNG rng;
  23986. Cert cert;
  23987. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23988. ed25519_key ed25519Key;
  23989. #endif
  23990. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23991. RsaKey rsaKey;
  23992. int bits = 2048;
  23993. #endif
  23994. #if defined(HAVE_ECC)
  23995. ecc_key eccKey;
  23996. #endif
  23997. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23998. ed448_key ed448Key;
  23999. #endif
  24000. int ret = 0;
  24001. #ifndef HAVE_FIPS
  24002. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24003. #else
  24004. ret = wc_InitRng(&rng);
  24005. #endif
  24006. wc_InitCert(&cert);
  24007. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24008. if (ret == 0) { /*ED25519*/
  24009. ret = wc_ed25519_init(&ed25519Key);
  24010. if (ret == 0) {
  24011. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24012. }
  24013. if (ret == 0) {
  24014. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24015. &ed25519Key);
  24016. }
  24017. wc_ed25519_free(&ed25519Key);
  24018. }
  24019. #endif
  24020. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24021. if (ret == 0) { /*RSA*/
  24022. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24023. if (ret == 0) {
  24024. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24025. }
  24026. if (ret == 0) {
  24027. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24028. }
  24029. wc_FreeRsaKey(&rsaKey);
  24030. }
  24031. #endif
  24032. #if defined(HAVE_ECC)
  24033. if (ret == 0) { /*ECC*/
  24034. ret = wc_ecc_init(&eccKey);
  24035. if (ret == 0) {
  24036. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24037. #if defined(WOLFSSL_ASYNC_CRYPT)
  24038. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24039. #endif
  24040. }
  24041. if (ret == 0) {
  24042. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24043. }
  24044. wc_ecc_free(&eccKey);
  24045. }
  24046. #endif
  24047. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24048. if (ret == 0) { /*ED448*/
  24049. ret = wc_ed448_init(&ed448Key);
  24050. if (ret == 0) {
  24051. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24052. }
  24053. if (ret == 0) {
  24054. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24055. &ed448Key);
  24056. }
  24057. wc_ed448_free(&ed448Key);
  24058. }
  24059. #endif
  24060. wc_FreeRng(&rng);
  24061. res = TEST_RES_CHECK(ret == 0);
  24062. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  24063. return res;
  24064. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  24065. /*
  24066. * Testing wc_PKCS7_New()
  24067. */
  24068. static int test_wc_PKCS7_New(void)
  24069. {
  24070. int res = TEST_SKIPPED;
  24071. #if defined(HAVE_PKCS7)
  24072. PKCS7* pkcs7;
  24073. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  24074. wc_PKCS7_Free(pkcs7);
  24075. res = TEST_RES_CHECK(pkcs7 != NULL);
  24076. #endif
  24077. return res;
  24078. } /* END test-wc_PKCS7_New */
  24079. /*
  24080. * Testing wc_PKCS7_Init()
  24081. */
  24082. static int test_wc_PKCS7_Init(void)
  24083. {
  24084. int res = TEST_SKIPPED;
  24085. #if defined(HAVE_PKCS7)
  24086. PKCS7* pkcs7;
  24087. void* heap = NULL;
  24088. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24089. AssertNotNull(pkcs7);
  24090. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24091. /* Pass in bad args. */
  24092. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24093. wc_PKCS7_Free(pkcs7);
  24094. res = TEST_RES_CHECK(1);
  24095. #endif
  24096. return res;
  24097. } /* END test-wc_PKCS7_Init */
  24098. /*
  24099. * Testing wc_PKCS7_InitWithCert()
  24100. */
  24101. static int test_wc_PKCS7_InitWithCert(void)
  24102. {
  24103. int res = TEST_SKIPPED;
  24104. #if defined(HAVE_PKCS7)
  24105. PKCS7* pkcs7;
  24106. #ifndef NO_RSA
  24107. #if defined(USE_CERT_BUFFERS_2048)
  24108. unsigned char cert[sizeof(client_cert_der_2048)];
  24109. int certSz = (int)sizeof(cert);
  24110. XMEMSET(cert, 0, certSz);
  24111. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24112. #elif defined(USE_CERT_BUFFERS_1024)
  24113. unsigned char cert[sizeof(client_cert_der_1024)];
  24114. int certSz = (int)sizeof(cert);
  24115. XMEMSET(cert, 0, certSz);
  24116. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24117. #else
  24118. unsigned char cert[ONEK_BUF];
  24119. XFILE fp;
  24120. int certSz;
  24121. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24122. AssertTrue(fp != XBADFILE);
  24123. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24124. XFCLOSE(fp);
  24125. #endif
  24126. #elif defined(HAVE_ECC)
  24127. #if defined(USE_CERT_BUFFERS_256)
  24128. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24129. int certSz = (int)sizeof(cert);
  24130. XMEMSET(cert, 0, certSz);
  24131. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24132. #else
  24133. unsigned char cert[ONEK_BUF];
  24134. XFILE fp;
  24135. int certSz;
  24136. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24137. AssertTrue(fp != XBADFILE);
  24138. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24139. XFCLOSE(fp);
  24140. #endif
  24141. #else
  24142. #error PKCS7 requires ECC or RSA
  24143. #endif
  24144. #ifdef HAVE_ECC
  24145. {
  24146. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24147. static unsigned char certWithInvalidEccKey[] = {
  24148. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24149. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24150. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24151. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24152. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24153. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24154. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24155. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24156. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24157. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24158. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24159. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24160. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24161. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24162. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24163. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24164. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24165. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24166. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24167. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24168. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24169. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24170. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24171. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24172. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24173. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24174. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24175. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24176. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24177. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24178. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24179. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24180. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24181. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24182. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24183. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24184. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24185. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24186. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24187. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24188. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24189. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24190. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24191. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24192. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24193. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24194. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24195. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24196. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24197. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24198. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24199. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24200. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24201. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24202. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24203. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24204. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24205. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24206. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24207. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24208. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24209. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24210. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24211. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24212. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24213. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24214. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24215. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24216. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24217. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24218. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24219. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24220. 0x08, 0xA8, 0xB4};
  24221. #endif
  24222. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24223. /* If initialization is not successful, it's free'd in init func. */
  24224. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24225. wc_PKCS7_Free(pkcs7);
  24226. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24227. /* Valid initialization usage. */
  24228. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24229. /* Pass in bad args. No need free for null checks, free at end.*/
  24230. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24231. BAD_FUNC_ARG);
  24232. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24233. BAD_FUNC_ARG);
  24234. #ifdef HAVE_ECC
  24235. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24236. sizeof(certWithInvalidEccKey)), 0);
  24237. }
  24238. #endif
  24239. wc_PKCS7_Free(pkcs7);
  24240. res = TEST_RES_CHECK(1);
  24241. #endif
  24242. return res;
  24243. } /* END test_wc_PKCS7_InitWithCert */
  24244. /*
  24245. * Testing wc_PKCS7_EncodeData()
  24246. */
  24247. static int test_wc_PKCS7_EncodeData(void)
  24248. {
  24249. int res = TEST_SKIPPED;
  24250. #if defined(HAVE_PKCS7)
  24251. PKCS7* pkcs7;
  24252. byte output[FOURK_BUF];
  24253. byte data[] = "My encoded DER cert.";
  24254. #ifndef NO_RSA
  24255. #if defined(USE_CERT_BUFFERS_2048)
  24256. unsigned char cert[sizeof(client_cert_der_2048)];
  24257. unsigned char key[sizeof(client_key_der_2048)];
  24258. int certSz = (int)sizeof(cert);
  24259. int keySz = (int)sizeof(key);
  24260. XMEMSET(cert, 0, certSz);
  24261. XMEMSET(key, 0, keySz);
  24262. XMEMCPY(cert, client_cert_der_2048, certSz);
  24263. XMEMCPY(key, client_key_der_2048, keySz);
  24264. #elif defined(USE_CERT_BUFFERS_1024)
  24265. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24266. unsigned char key[sizeof_client_key_der_1024];
  24267. int certSz = (int)sizeof(cert);
  24268. int keySz = (int)sizeof(key);
  24269. XMEMSET(cert, 0, certSz);
  24270. XMEMSET(key, 0, keySz);
  24271. XMEMCPY(cert, client_cert_der_1024, certSz);
  24272. XMEMCPY(key, client_key_der_1024, keySz);
  24273. #else
  24274. unsigned char cert[ONEK_BUF];
  24275. unsigned char key[ONEK_BUF];
  24276. XFILE fp;
  24277. int certSz;
  24278. int keySz;
  24279. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24280. AssertTrue(fp != XBADFILE);
  24281. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24282. XFCLOSE(fp);
  24283. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24284. AssertTrue(fp != XBADFILE);
  24285. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24286. XFCLOSE(fp);
  24287. #endif
  24288. #elif defined(HAVE_ECC)
  24289. #if defined(USE_CERT_BUFFERS_256)
  24290. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24291. unsigned char key[sizeof(ecc_clikey_der_256)];
  24292. int certSz = (int)sizeof(cert);
  24293. int keySz = (int)sizeof(key);
  24294. XMEMSET(cert, 0, certSz);
  24295. XMEMSET(key, 0, keySz);
  24296. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24297. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24298. #else
  24299. unsigned char cert[ONEK_BUF];
  24300. unsigned char key[ONEK_BUF];
  24301. XFILE fp;
  24302. int certSz, keySz;
  24303. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24304. AssertTrue(fp != XBADFILE);
  24305. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24306. XFCLOSE(fp);
  24307. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24308. AssertTrue(fp != XBADFILE);
  24309. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24310. XFCLOSE(fp);
  24311. #endif
  24312. #endif
  24313. XMEMSET(output, 0, sizeof(output));
  24314. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24315. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24316. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24317. pkcs7->content = data;
  24318. pkcs7->contentSz = sizeof(data);
  24319. pkcs7->privateKey = key;
  24320. pkcs7->privateKeySz = keySz;
  24321. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24322. /* Test bad args. */
  24323. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24324. BAD_FUNC_ARG);
  24325. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24326. BAD_FUNC_ARG);
  24327. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24328. wc_PKCS7_Free(pkcs7);
  24329. res = TEST_RES_CHECK(1);
  24330. #endif
  24331. return res;
  24332. } /* END test_wc_PKCS7_EncodeData */
  24333. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24334. !defined(NO_RSA) && !defined(NO_SHA256)
  24335. /* RSA sign raw digest callback */
  24336. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24337. byte* out, word32 outSz, byte* privateKey,
  24338. word32 privateKeySz, int devid, int hashOID)
  24339. {
  24340. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24341. byte digInfoEncoding[] = {
  24342. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24343. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24344. 0x00, 0x04, 0x20
  24345. };
  24346. int ret;
  24347. byte digestInfo[ONEK_BUF];
  24348. byte sig[FOURK_BUF];
  24349. word32 digestInfoSz = 0;
  24350. word32 idx = 0;
  24351. RsaKey rsa;
  24352. /* SHA-256 required only for this example callback due to above
  24353. * digInfoEncoding[] */
  24354. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24355. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24356. (hashOID != SHA256h)) {
  24357. return -1;
  24358. }
  24359. /* build DigestInfo */
  24360. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24361. digestInfoSz += sizeof(digInfoEncoding);
  24362. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24363. digestInfoSz += digestSz;
  24364. /* set up RSA key */
  24365. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24366. if (ret != 0) {
  24367. return ret;
  24368. }
  24369. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24370. /* sign DigestInfo */
  24371. if (ret == 0) {
  24372. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24373. &rsa, pkcs7->rng);
  24374. if (ret > 0) {
  24375. if (ret > (int)outSz) {
  24376. /* output buffer too small */
  24377. ret = -1;
  24378. }
  24379. else {
  24380. /* success, ret holds sig size */
  24381. XMEMCPY(out, sig, ret);
  24382. }
  24383. }
  24384. }
  24385. wc_FreeRsaKey(&rsa);
  24386. return ret;
  24387. }
  24388. #endif
  24389. /*
  24390. * Testing wc_PKCS7_EncodeSignedData()
  24391. */
  24392. static int test_wc_PKCS7_EncodeSignedData(void)
  24393. {
  24394. int res = TEST_SKIPPED;
  24395. #if defined(HAVE_PKCS7)
  24396. PKCS7* pkcs7;
  24397. WC_RNG rng;
  24398. byte output[FOURK_BUF];
  24399. byte badOut[1];
  24400. word32 outputSz = (word32)sizeof(output);
  24401. word32 badOutSz = 0;
  24402. byte data[] = "Test data to encode.";
  24403. #ifndef NO_RSA
  24404. #if defined(USE_CERT_BUFFERS_2048)
  24405. byte key[sizeof(client_key_der_2048)];
  24406. byte cert[sizeof(client_cert_der_2048)];
  24407. word32 keySz = (word32)sizeof(key);
  24408. word32 certSz = (word32)sizeof(cert);
  24409. XMEMSET(key, 0, keySz);
  24410. XMEMSET(cert, 0, certSz);
  24411. XMEMCPY(key, client_key_der_2048, keySz);
  24412. XMEMCPY(cert, client_cert_der_2048, certSz);
  24413. #elif defined(USE_CERT_BUFFERS_1024)
  24414. byte key[sizeof_client_key_der_1024];
  24415. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24416. word32 keySz = (word32)sizeof(key);
  24417. word32 certSz = (word32)sizeof(cert);
  24418. XMEMSET(key, 0, keySz);
  24419. XMEMSET(cert, 0, certSz);
  24420. XMEMCPY(key, client_key_der_1024, keySz);
  24421. XMEMCPY(cert, client_cert_der_1024, certSz);
  24422. #else
  24423. unsigned char cert[ONEK_BUF];
  24424. unsigned char key[ONEK_BUF];
  24425. XFILE fp;
  24426. int certSz;
  24427. int keySz;
  24428. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24429. AssertTrue(fp != XBADFILE);
  24430. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24431. XFCLOSE(fp);
  24432. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24433. AssertTrue(fp != XBADFILE);
  24434. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24435. XFCLOSE(fp);
  24436. #endif
  24437. #elif defined(HAVE_ECC)
  24438. #if defined(USE_CERT_BUFFERS_256)
  24439. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24440. unsigned char key[sizeof(ecc_clikey_der_256)];
  24441. int certSz = (int)sizeof(cert);
  24442. int keySz = (int)sizeof(key);
  24443. XMEMSET(cert, 0, certSz);
  24444. XMEMSET(key, 0, keySz);
  24445. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24446. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24447. #else
  24448. unsigned char cert[ONEK_BUF];
  24449. unsigned char key[ONEK_BUF];
  24450. XFILE fp;
  24451. int certSz, keySz;
  24452. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  24453. AssertTrue(fp != XBADFILE);
  24454. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  24455. XFCLOSE(fp);
  24456. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24457. AssertTrue(fp != XBADFILE);
  24458. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  24459. XFCLOSE(fp);
  24460. #endif
  24461. #endif
  24462. XMEMSET(output, 0, outputSz);
  24463. AssertIntEQ(wc_InitRng(&rng), 0);
  24464. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24465. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24466. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24467. pkcs7->content = data;
  24468. pkcs7->contentSz = (word32)sizeof(data);
  24469. pkcs7->privateKey = key;
  24470. pkcs7->privateKeySz = (word32)sizeof(key);
  24471. pkcs7->encryptOID = RSAk;
  24472. #ifdef NO_SHA
  24473. pkcs7->hashOID = SHA256h;
  24474. #else
  24475. pkcs7->hashOID = SHAh;
  24476. #endif
  24477. pkcs7->rng = &rng;
  24478. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24479. wc_PKCS7_Free(pkcs7);
  24480. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24481. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24482. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24483. /* Pass in bad args. */
  24484. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24485. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24486. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24487. badOutSz), BAD_FUNC_ARG);
  24488. pkcs7->hashOID = 0; /* bad hashOID */
  24489. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  24490. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24491. !defined(NO_RSA) && !defined(NO_SHA256)
  24492. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24493. * Example callback assumes SHA-256, so only run test if compiled in. */
  24494. wc_PKCS7_Free(pkcs7);
  24495. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24496. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24497. pkcs7->content = data;
  24498. pkcs7->contentSz = (word32)sizeof(data);
  24499. pkcs7->privateKey = key;
  24500. pkcs7->privateKeySz = (word32)sizeof(key);
  24501. pkcs7->encryptOID = RSAk;
  24502. pkcs7->hashOID = SHA256h;
  24503. pkcs7->rng = &rng;
  24504. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24505. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24506. #endif
  24507. wc_PKCS7_Free(pkcs7);
  24508. wc_FreeRng(&rng);
  24509. res = TEST_RES_CHECK(1);
  24510. #endif
  24511. return res;
  24512. } /* END test_wc_PKCS7_EncodeSignedData */
  24513. /*
  24514. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24515. */
  24516. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24517. {
  24518. int res = TEST_SKIPPED;
  24519. #if defined(HAVE_PKCS7)
  24520. int ret, i;
  24521. PKCS7* pkcs7;
  24522. WC_RNG rng;
  24523. byte outputHead[FOURK_BUF/2];
  24524. byte outputFoot[FOURK_BUF/2];
  24525. word32 outputHeadSz = (word32)sizeof(outputHead);
  24526. word32 outputFootSz = (word32)sizeof(outputFoot);
  24527. byte data[FOURK_BUF];
  24528. wc_HashAlg hash;
  24529. #ifdef NO_SHA
  24530. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24531. #else
  24532. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24533. #endif
  24534. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24535. word32 hashSz = wc_HashGetDigestSize(hashType);
  24536. #ifndef NO_RSA
  24537. #if defined(USE_CERT_BUFFERS_2048)
  24538. byte key[sizeof(client_key_der_2048)];
  24539. byte cert[sizeof(client_cert_der_2048)];
  24540. word32 keySz = (word32)sizeof(key);
  24541. word32 certSz = (word32)sizeof(cert);
  24542. XMEMSET(key, 0, keySz);
  24543. XMEMSET(cert, 0, certSz);
  24544. XMEMCPY(key, client_key_der_2048, keySz);
  24545. XMEMCPY(cert, client_cert_der_2048, certSz);
  24546. #elif defined(USE_CERT_BUFFERS_1024)
  24547. byte key[sizeof_client_key_der_1024];
  24548. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24549. word32 keySz = (word32)sizeof(key);
  24550. word32 certSz = (word32)sizeof(cert);
  24551. XMEMSET(key, 0, keySz);
  24552. XMEMSET(cert, 0, certSz);
  24553. XMEMCPY(key, client_key_der_1024, keySz);
  24554. XMEMCPY(cert, client_cert_der_1024, certSz);
  24555. #else
  24556. unsigned char cert[ONEK_BUF];
  24557. unsigned char key[ONEK_BUF];
  24558. XFILE fp;
  24559. int certSz;
  24560. int keySz;
  24561. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24562. AssertTrue((fp != XBADFILE));
  24563. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24564. XFCLOSE(fp);
  24565. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24566. AssertTrue(fp != XBADFILE);
  24567. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24568. XFCLOSE(fp);
  24569. #endif
  24570. #elif defined(HAVE_ECC)
  24571. #if defined(USE_CERT_BUFFERS_256)
  24572. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24573. unsigned char key[sizeof(ecc_clikey_der_256)];
  24574. int certSz = (int)sizeof(cert);
  24575. int keySz = (int)sizeof(key);
  24576. XMEMSET(cert, 0, certSz);
  24577. XMEMSET(key, 0, keySz);
  24578. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24579. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24580. #else
  24581. unsigned char cert[ONEK_BUF];
  24582. unsigned char key[ONEK_BUF];
  24583. XFILE fp;
  24584. int certSz, keySz;
  24585. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24586. AssertTrue(fp != XBADFILE);
  24587. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24588. XFCLOSE(fp);
  24589. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24590. AssertTrue(fp != XBADFILE);
  24591. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24592. XFCLOSE(fp);
  24593. #endif
  24594. #endif
  24595. /* initialize large data with sequence */
  24596. for (i=0; i<(int)sizeof(data); i++)
  24597. data[i] = i & 0xff;
  24598. XMEMSET(outputHead, 0, outputHeadSz);
  24599. XMEMSET(outputFoot, 0, outputFootSz);
  24600. AssertIntEQ(wc_InitRng(&rng), 0);
  24601. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24602. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24603. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24604. pkcs7->content = NULL; /* not used for ex */
  24605. pkcs7->contentSz = (word32)sizeof(data);
  24606. pkcs7->privateKey = key;
  24607. pkcs7->privateKeySz = (word32)sizeof(key);
  24608. pkcs7->encryptOID = RSAk;
  24609. #ifdef NO_SHA
  24610. pkcs7->hashOID = SHA256h;
  24611. #else
  24612. pkcs7->hashOID = SHAh;
  24613. #endif
  24614. pkcs7->rng = &rng;
  24615. /* calculate hash for content */
  24616. ret = wc_HashInit(&hash, hashType);
  24617. if (ret == 0) {
  24618. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24619. if (ret == 0) {
  24620. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24621. }
  24622. wc_HashFree(&hash, hashType);
  24623. }
  24624. AssertIntEQ(ret, 0);
  24625. /* Perform PKCS7 sign using hash directly */
  24626. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24627. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24628. AssertIntGT(outputHeadSz, 0);
  24629. AssertIntGT(outputFootSz, 0);
  24630. wc_PKCS7_Free(pkcs7);
  24631. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24632. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24633. /* required parameter even on verify when using _ex, if using outputHead
  24634. * and outputFoot */
  24635. pkcs7->contentSz = (word32)sizeof(data);
  24636. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24637. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24638. wc_PKCS7_Free(pkcs7);
  24639. /* assembly complete PKCS7 sign and use normal verify */
  24640. {
  24641. byte* output = (byte*)XMALLOC(
  24642. outputHeadSz + sizeof(data) + outputFootSz,
  24643. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24644. word32 outputSz = 0;
  24645. AssertNotNull(output);
  24646. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24647. outputSz += outputHeadSz;
  24648. XMEMCPY(&output[outputSz], data, sizeof(data));
  24649. outputSz += sizeof(data);
  24650. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24651. outputSz += outputFootSz;
  24652. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24653. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24654. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24655. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24656. }
  24657. /* Pass in bad args. */
  24658. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24659. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24660. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24661. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24662. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24663. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24664. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24665. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24666. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24667. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24668. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24669. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24670. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24671. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24672. pkcs7->hashOID = 0; /* bad hashOID */
  24673. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24674. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24675. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24676. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24677. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24678. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24679. #ifndef NO_PKCS7_STREAM
  24680. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24681. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24682. #else
  24683. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24684. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24685. #endif
  24686. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24687. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24688. #ifndef NO_PKCS7_STREAM
  24689. /* can pass in 0 buffer length with streaming API */
  24690. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24691. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24692. #else
  24693. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24694. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24695. #endif
  24696. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24697. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24698. #ifndef NO_PKCS7_STREAM
  24699. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24700. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24701. #else
  24702. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24703. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24704. #endif
  24705. wc_PKCS7_Free(pkcs7);
  24706. wc_FreeRng(&rng);
  24707. res = TEST_RES_CHECK(1);
  24708. #endif
  24709. return res;
  24710. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24711. #if defined(HAVE_PKCS7)
  24712. /**
  24713. * Loads certs/keys from files or buffers into the argument buffers,
  24714. * helper function called by CreatePKCS7SignedData().
  24715. *
  24716. * Returns 0 on success, negative on error.
  24717. */
  24718. static int LoadPKCS7SignedDataCerts(
  24719. int useIntermediateCertChain, int pkAlgoType,
  24720. byte* intCARoot, word32* intCARootSz,
  24721. byte* intCA1, word32* intCA1Sz,
  24722. byte* intCA2, word32* intCA2Sz,
  24723. byte* cert, word32* certSz,
  24724. byte* key, word32* keySz)
  24725. {
  24726. int ret = 0;
  24727. FILE* fp = NULL;
  24728. #ifndef NO_RSA
  24729. const char* intCARootRSA = "./certs/ca-cert.der";
  24730. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24731. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24732. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24733. const char* intServKeyRSA = "./certs/server-key.der";
  24734. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24735. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24736. const char* cli1024Key = "./certs/1024/client-key.der";
  24737. #endif
  24738. #endif
  24739. #ifdef HAVE_ECC
  24740. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24741. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24742. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24743. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24744. const char* intServKeyECC = "./certs/ecc-key.der";
  24745. #ifndef USE_CERT_BUFFERS_256
  24746. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24747. const char* cliEccKey = "./certs/client-ecc-key.der";
  24748. #endif
  24749. #endif
  24750. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24751. ((useIntermediateCertChain == 1) &&
  24752. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24753. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24754. return BAD_FUNC_ARG;
  24755. }
  24756. /* Read/load certs and keys to use for signing based on PK type and chain */
  24757. switch (pkAlgoType) {
  24758. #ifndef NO_RSA
  24759. case RSA_TYPE:
  24760. if (useIntermediateCertChain == 1) {
  24761. fp = XFOPEN(intCARootRSA, "rb");
  24762. AssertNotNull(fp);
  24763. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24764. XFCLOSE(fp);
  24765. AssertIntGT(*intCARootSz, 0);
  24766. fp = XFOPEN(intCA1RSA, "rb");
  24767. AssertNotNull(fp);
  24768. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24769. XFCLOSE(fp);
  24770. AssertIntGT(*intCA1Sz, 0);
  24771. fp = XFOPEN(intCA2RSA, "rb");
  24772. AssertNotNull(fp);
  24773. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24774. XFCLOSE(fp);
  24775. AssertIntGT(*intCA2Sz, 0);
  24776. fp = XFOPEN(intServCertRSA, "rb");
  24777. AssertNotNull(fp);
  24778. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24779. XFCLOSE(fp);
  24780. AssertIntGT(*certSz, 0);
  24781. fp = XFOPEN(intServKeyRSA, "rb");
  24782. AssertNotNull(fp);
  24783. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24784. XFCLOSE(fp);
  24785. AssertIntGT(*keySz, 0);
  24786. }
  24787. else {
  24788. #if defined(USE_CERT_BUFFERS_2048)
  24789. *keySz = sizeof_client_key_der_2048;
  24790. *certSz = sizeof_client_cert_der_2048;
  24791. XMEMCPY(key, client_key_der_2048, *keySz);
  24792. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24793. #elif defined(USE_CERT_BUFFERS_1024)
  24794. *keySz = sizeof_client_key_der_1024;
  24795. *certSz = sizeof_client_cert_der_1024;
  24796. XMEMCPY(key, client_key_der_1024, *keySz);
  24797. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24798. #else
  24799. fp = XFOPEN(cli1024Key, "rb");
  24800. AssertNotNull(fp);
  24801. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24802. XFCLOSE(fp);
  24803. AssertIntGT(*keySz, 0);
  24804. fp = XFOPEN(cli1024Cert, "rb");
  24805. AssertNotNull(fp);
  24806. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24807. XFCLOSE(fp);
  24808. AssertIntGT(*certSz, 0);
  24809. #endif /* USE_CERT_BUFFERS_2048 */
  24810. }
  24811. break;
  24812. #endif /* !NO_RSA */
  24813. #ifdef HAVE_ECC
  24814. case ECC_TYPE:
  24815. if (useIntermediateCertChain == 1) {
  24816. fp = XFOPEN(intCARootECC, "rb");
  24817. AssertNotNull(fp);
  24818. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24819. XFCLOSE(fp);
  24820. AssertIntGT(*intCARootSz, 0);
  24821. fp = XFOPEN(intCA1ECC, "rb");
  24822. AssertNotNull(fp);
  24823. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24824. XFCLOSE(fp);
  24825. AssertIntGT(*intCA1Sz, 0);
  24826. fp = XFOPEN(intCA2ECC, "rb");
  24827. AssertNotNull(fp);
  24828. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24829. XFCLOSE(fp);
  24830. AssertIntGT(*intCA2Sz, 0);
  24831. fp = XFOPEN(intServCertECC, "rb");
  24832. AssertNotNull(fp);
  24833. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24834. XFCLOSE(fp);
  24835. AssertIntGT(*certSz, 0);
  24836. fp = XFOPEN(intServKeyECC, "rb");
  24837. AssertNotNull(fp);
  24838. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24839. XFCLOSE(fp);
  24840. AssertIntGT(*keySz, 0);
  24841. }
  24842. else {
  24843. #if defined(USE_CERT_BUFFERS_256)
  24844. *keySz = sizeof_ecc_clikey_der_256;
  24845. *certSz = sizeof_cliecc_cert_der_256;
  24846. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24847. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24848. #else
  24849. fp = XFOPEN(cliEccKey, "rb");
  24850. AssertNotNull(fp);
  24851. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24852. XFCLOSE(fp);
  24853. AssertIntGT(*keySz, 0);
  24854. fp = XFOPEN(cliEccCert, "rb");
  24855. AssertNotNull(fp);
  24856. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24857. XFCLOSE(fp);
  24858. AssertIntGT(*certSz, 0);
  24859. #endif /* USE_CERT_BUFFERS_256 */
  24860. }
  24861. break;
  24862. #endif /* HAVE_ECC */
  24863. default:
  24864. WOLFSSL_MSG("Unsupported SignedData PK type");
  24865. ret = BAD_FUNC_ARG;
  24866. break;
  24867. }
  24868. return ret;
  24869. }
  24870. /**
  24871. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24872. *
  24873. * output output buffer to place SignedData
  24874. * outputSz size of output buffer
  24875. * data data buffer to be signed
  24876. * dataSz size of data buffer
  24877. * withAttribs [1/0] include attributes in SignedData message
  24878. * detachedSig [1/0] create detached signature, no content
  24879. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24880. * root CAs in bundle
  24881. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24882. *
  24883. * Return size of bundle created on success, negative on error */
  24884. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24885. byte* data, word32 dataSz,
  24886. int withAttribs, int detachedSig,
  24887. int useIntermediateCertChain,
  24888. int pkAlgoType)
  24889. {
  24890. int ret = 0;
  24891. WC_RNG rng;
  24892. PKCS7* pkcs7 = NULL;
  24893. static byte messageTypeOid[] =
  24894. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24895. 0x09, 0x02 };
  24896. static byte messageType[] = { 0x13, 2, '1', '9' };
  24897. PKCS7Attrib attribs[] =
  24898. {
  24899. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24900. sizeof(messageType) }
  24901. };
  24902. byte intCARoot[TWOK_BUF];
  24903. byte intCA1[TWOK_BUF];
  24904. byte intCA2[TWOK_BUF];
  24905. byte cert[TWOK_BUF];
  24906. byte key[TWOK_BUF];
  24907. word32 intCARootSz = sizeof(intCARoot);
  24908. word32 intCA1Sz = sizeof(intCA1);
  24909. word32 intCA2Sz = sizeof(intCA2);
  24910. word32 certSz = sizeof(cert);
  24911. word32 keySz = sizeof(key);
  24912. XMEMSET(intCARoot, 0, intCARootSz);
  24913. XMEMSET(intCA1, 0, intCA1Sz);
  24914. XMEMSET(intCA2, 0, intCA2Sz);
  24915. XMEMSET(cert, 0, certSz);
  24916. XMEMSET(key, 0, keySz);
  24917. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24918. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24919. cert, &certSz, key, &keySz);
  24920. AssertIntEQ(ret, 0);
  24921. XMEMSET(output, 0, outputSz);
  24922. AssertIntEQ(wc_InitRng(&rng), 0);
  24923. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24924. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24925. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24926. if (useIntermediateCertChain == 1) {
  24927. /* Add intermediate and root CA certs into SignedData Certs SET */
  24928. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24929. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24930. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24931. }
  24932. pkcs7->content = data;
  24933. pkcs7->contentSz = dataSz;
  24934. pkcs7->privateKey = key;
  24935. pkcs7->privateKeySz = (word32)sizeof(key);
  24936. if (pkAlgoType == RSA_TYPE) {
  24937. pkcs7->encryptOID = RSAk;
  24938. }
  24939. else {
  24940. pkcs7->encryptOID = ECDSAk;
  24941. }
  24942. #ifdef NO_SHA
  24943. pkcs7->hashOID = SHA256h;
  24944. #else
  24945. pkcs7->hashOID = SHAh;
  24946. #endif
  24947. pkcs7->rng = &rng;
  24948. if (withAttribs) {
  24949. /* include a signed attribute */
  24950. pkcs7->signedAttribs = attribs;
  24951. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24952. }
  24953. if (detachedSig) {
  24954. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24955. }
  24956. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24957. AssertIntGT(outputSz, 0);
  24958. wc_PKCS7_Free(pkcs7);
  24959. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24960. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24961. if (detachedSig) {
  24962. pkcs7->content = data;
  24963. pkcs7->contentSz = dataSz;
  24964. }
  24965. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24966. wc_PKCS7_Free(pkcs7);
  24967. wc_FreeRng(&rng);
  24968. return outputSz;
  24969. }
  24970. #endif
  24971. /*
  24972. * Testing wc_PKCS_VerifySignedData()
  24973. */
  24974. static int test_wc_PKCS7_VerifySignedData(void)
  24975. {
  24976. int res = TEST_SKIPPED;
  24977. #if defined(HAVE_PKCS7)
  24978. PKCS7* pkcs7;
  24979. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24980. word32 outputSz = sizeof(output);
  24981. byte data[] = "Test data to encode.";
  24982. byte badOut[1];
  24983. word32 badOutSz = 0;
  24984. byte badContent[] = "This is different content than was signed";
  24985. int ret;
  24986. wc_HashAlg hash;
  24987. #ifdef NO_SHA
  24988. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24989. #else
  24990. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24991. #endif
  24992. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24993. word32 hashSz = wc_HashGetDigestSize(hashType);
  24994. #ifndef NO_RSA
  24995. /* Success test with RSA certs/key */
  24996. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24997. (word32)sizeof(data),
  24998. 0, 0, 0, RSA_TYPE)), 0);
  24999. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25000. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25001. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25002. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25003. #endif
  25004. #ifdef HAVE_ECC
  25005. #ifndef NO_RSA
  25006. wc_PKCS7_Free(pkcs7);
  25007. #endif
  25008. /* Success test with ECC certs/key */
  25009. outputSz = sizeof(output);
  25010. XMEMSET(output, 0, outputSz);
  25011. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25012. (word32)sizeof(data),
  25013. 0, 0, 0, ECC_TYPE)), 0);
  25014. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25015. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25016. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25017. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25018. #endif
  25019. /* Test bad args. */
  25020. #if !defined(NO_RSA) || defined(HAVE_ECC)
  25021. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25022. BAD_FUNC_ARG);
  25023. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25024. BAD_FUNC_ARG);
  25025. #ifndef NO_PKCS7_STREAM
  25026. /* can pass in 0 buffer length with streaming API */
  25027. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25028. badOutSz), WC_PKCS7_WANT_READ_E);
  25029. #else
  25030. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25031. badOutSz), BAD_FUNC_ARG);
  25032. #endif
  25033. wc_PKCS7_Free(pkcs7);
  25034. #endif /* !NO_RSA || HAVE_ECC */
  25035. /* Invalid content should error, use detached signature so we can
  25036. * easily change content */
  25037. #ifndef NO_RSA
  25038. /* Try RSA certs/key/sig first */
  25039. outputSz = sizeof(output);
  25040. XMEMSET(output, 0, outputSz);
  25041. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25042. (word32)sizeof(data),
  25043. 1, 1, 0, RSA_TYPE)), 0);
  25044. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25045. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25046. pkcs7->content = badContent;
  25047. pkcs7->contentSz = sizeof(badContent);
  25048. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25049. SIG_VERIFY_E);
  25050. wc_PKCS7_Free(pkcs7);
  25051. /* Test success case with detached signature and valid content */
  25052. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25053. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25054. pkcs7->content = data;
  25055. pkcs7->contentSz = sizeof(data);
  25056. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25057. wc_PKCS7_Free(pkcs7);
  25058. /* verify using pre-computed content digest only (no content) */
  25059. {
  25060. /* calculate hash for content */
  25061. ret = wc_HashInit(&hash, hashType);
  25062. if (ret == 0) {
  25063. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25064. if (ret == 0) {
  25065. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25066. }
  25067. wc_HashFree(&hash, hashType);
  25068. }
  25069. AssertIntEQ(ret, 0);
  25070. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25071. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25072. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25073. output, outputSz,
  25074. NULL, 0), 0);
  25075. wc_PKCS7_Free(pkcs7);
  25076. }
  25077. #endif /* !NO_RSA */
  25078. #ifdef HAVE_ECC
  25079. /* Try ECC certs/key/sig next */
  25080. outputSz = sizeof(output);
  25081. XMEMSET(output, 0, outputSz);
  25082. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25083. (word32)sizeof(data),
  25084. 1, 1, 0, ECC_TYPE)), 0);
  25085. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25086. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25087. pkcs7->content = badContent;
  25088. pkcs7->contentSz = sizeof(badContent);
  25089. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25090. SIG_VERIFY_E);
  25091. wc_PKCS7_Free(pkcs7);
  25092. /* Test success case with detached signature and valid content */
  25093. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25094. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25095. pkcs7->content = data;
  25096. pkcs7->contentSz = sizeof(data);
  25097. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25098. wc_PKCS7_Free(pkcs7);
  25099. /* verify using pre-computed content digest only (no content) */
  25100. {
  25101. /* calculate hash for content */
  25102. ret = wc_HashInit(&hash, hashType);
  25103. if (ret == 0) {
  25104. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25105. if (ret == 0) {
  25106. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25107. }
  25108. wc_HashFree(&hash, hashType);
  25109. }
  25110. AssertIntEQ(ret, 0);
  25111. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25112. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25113. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25114. output, outputSz,
  25115. NULL, 0), 0);
  25116. wc_PKCS7_Free(pkcs7);
  25117. }
  25118. #endif
  25119. /* Test verify on signedData containing intermediate/root CA certs */
  25120. #ifndef NO_RSA
  25121. outputSz = sizeof(output);
  25122. XMEMSET(output, 0, outputSz);
  25123. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25124. (word32)sizeof(data),
  25125. 0, 0, 1, RSA_TYPE)), 0);
  25126. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25127. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25128. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25129. wc_PKCS7_Free(pkcs7);
  25130. #endif /* !NO_RSA */
  25131. #ifdef HAVE_ECC
  25132. outputSz = sizeof(output);
  25133. XMEMSET(output, 0, outputSz);
  25134. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25135. (word32)sizeof(data),
  25136. 0, 0, 1, ECC_TYPE)), 0);
  25137. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25138. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25139. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25140. wc_PKCS7_Free(pkcs7);
  25141. #endif /* HAVE_ECC */
  25142. res = TEST_RES_CHECK(1);
  25143. #endif
  25144. return res;
  25145. } /* END test_wc_PKCS7_VerifySignedData() */
  25146. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25147. !defined(NO_AES_256)
  25148. static const byte defKey[] = {
  25149. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25150. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25151. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25152. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25153. };
  25154. static byte aesHandle[32]; /* simulated hardware key handle */
  25155. /* return 0 on success */
  25156. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25157. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25158. byte* in, int inSz, byte* out, void* usrCtx)
  25159. {
  25160. int ret;
  25161. Aes aes;
  25162. if (usrCtx == NULL) {
  25163. /* no simulated handle passed in */
  25164. return -1;
  25165. }
  25166. switch (encryptOID) {
  25167. case AES256CBCb:
  25168. if (ivSz != AES_BLOCK_SIZE)
  25169. return BAD_FUNC_ARG;
  25170. break;
  25171. default:
  25172. WOLFSSL_MSG("Unsupported content cipher type for test");
  25173. return ALGO_ID_E;
  25174. };
  25175. /* simulate using handle to get key */
  25176. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25177. if (ret == 0) {
  25178. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25179. if (ret == 0)
  25180. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25181. wc_AesFree(&aes);
  25182. }
  25183. (void)aad;
  25184. (void)aadSz;
  25185. (void)authTag;
  25186. (void)authTagSz;
  25187. (void)pkcs7;
  25188. return ret;
  25189. }
  25190. /* returns key size on success */
  25191. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25192. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25193. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25194. {
  25195. int ret = -1;
  25196. if (out == NULL)
  25197. return BAD_FUNC_ARG;
  25198. if (keyId[0] != 0x00) {
  25199. return -1;
  25200. }
  25201. if (type != (int)PKCS7_KEKRI) {
  25202. return -1;
  25203. }
  25204. switch (keyWrapAlgo) {
  25205. case AES256_WRAP:
  25206. /* simulate setting a handle for later decryption but use key
  25207. * as handle in the test case here */
  25208. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25209. aesHandle, sizeof(aesHandle), NULL);
  25210. if (ret < 0)
  25211. return ret;
  25212. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25213. if (ret < 0)
  25214. return ret;
  25215. /* return key size on success */
  25216. return sizeof(defKey);
  25217. default:
  25218. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25219. return BAD_KEYWRAP_ALG_E;
  25220. };
  25221. (void)cekSz;
  25222. (void)cek;
  25223. (void)outSz;
  25224. (void)keyIdSz;
  25225. (void)direction;
  25226. (void)orginKey; /* used with KAKRI */
  25227. (void)orginKeySz;
  25228. return ret;
  25229. }
  25230. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25231. /*
  25232. * Testing wc_PKCS7_EncodeEnvelopedData()
  25233. */
  25234. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25235. {
  25236. int res = TEST_SKIPPED;
  25237. #if defined(HAVE_PKCS7)
  25238. PKCS7* pkcs7;
  25239. #ifdef ECC_TIMING_RESISTANT
  25240. WC_RNG rng;
  25241. #endif
  25242. word32 tempWrd32 = 0;
  25243. byte* tmpBytePtr = NULL;
  25244. const char input[] = "Test data to encode.";
  25245. int i;
  25246. int testSz = 0;
  25247. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25248. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25249. byte* rsaCert = NULL;
  25250. byte* rsaPrivKey = NULL;
  25251. word32 rsaCertSz;
  25252. word32 rsaPrivKeySz;
  25253. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25254. !defined(USE_CERT_BUFFERS_2048) )
  25255. static const char* rsaClientCert = "./certs/client-cert.der";
  25256. static const char* rsaClientKey = "./certs/client-key.der";
  25257. rsaCertSz = (word32)sizeof(rsaClientCert);
  25258. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25259. #endif
  25260. #endif
  25261. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25262. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25263. byte* eccCert = NULL;
  25264. byte* eccPrivKey = NULL;
  25265. word32 eccCertSz;
  25266. word32 eccPrivKeySz;
  25267. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25268. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25269. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25270. #endif
  25271. #endif
  25272. /* Generic buffer size. */
  25273. byte output[ONEK_BUF];
  25274. byte decoded[sizeof(input)/sizeof(char)];
  25275. int decodedSz = 0;
  25276. #ifndef NO_FILESYSTEM
  25277. XFILE certFile;
  25278. XFILE keyFile;
  25279. #endif
  25280. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25281. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25282. /* RSA certs and keys. */
  25283. #if defined(USE_CERT_BUFFERS_1024)
  25284. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25285. /* Allocate buffer space. */
  25286. AssertNotNull(rsaCert =
  25287. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25288. /* Init buffer. */
  25289. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25290. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25291. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25292. DYNAMIC_TYPE_TMP_BUFFER));
  25293. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25294. #elif defined(USE_CERT_BUFFERS_2048)
  25295. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25296. /* Allocate buffer */
  25297. AssertNotNull(rsaCert =
  25298. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25299. /* Init buffer. */
  25300. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25301. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25302. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25303. DYNAMIC_TYPE_TMP_BUFFER));
  25304. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25305. #else
  25306. /* File system. */
  25307. certFile = XFOPEN(rsaClientCert, "rb");
  25308. AssertTrue(certFile != XBADFILE);
  25309. rsaCertSz = (word32)FOURK_BUF;
  25310. AssertNotNull(rsaCert =
  25311. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25312. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25313. XFCLOSE(certFile);
  25314. keyFile = XFOPEN(rsaClientKey, "rb");
  25315. AssertTrue(keyFile != XBADFILE);
  25316. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25317. DYNAMIC_TYPE_TMP_BUFFER));
  25318. rsaPrivKeySz = (word32)FOURK_BUF;
  25319. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  25320. XFCLOSE(keyFile);
  25321. #endif /* USE_CERT_BUFFERS */
  25322. #endif /* NO_RSA */
  25323. /* ECC */
  25324. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25325. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25326. #ifdef USE_CERT_BUFFERS_256
  25327. AssertNotNull(eccCert =
  25328. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25329. /* Init buffer. */
  25330. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25331. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25332. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25333. DYNAMIC_TYPE_TMP_BUFFER));
  25334. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25335. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25336. #else /* File system. */
  25337. certFile = XFOPEN(eccClientCert, "rb");
  25338. AssertTrue(certFile != XBADFILE);
  25339. eccCertSz = (word32)FOURK_BUF;
  25340. AssertNotNull(eccCert =
  25341. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25342. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  25343. XFCLOSE(certFile);
  25344. keyFile = XFOPEN(eccClientKey, "rb");
  25345. AssertTrue(keyFile != XBADFILE);
  25346. eccPrivKeySz = (word32)FOURK_BUF;
  25347. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25348. DYNAMIC_TYPE_TMP_BUFFER));
  25349. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  25350. XFCLOSE(keyFile);
  25351. #endif /* USE_CERT_BUFFERS_256 */
  25352. #endif /* END HAVE_ECC */
  25353. /* Silence. */
  25354. (void)keyFile;
  25355. (void)certFile;
  25356. {
  25357. const pkcs7EnvelopedVector testVectors[] = {
  25358. /* DATA is a global variable defined in the makefile. */
  25359. #if !defined(NO_RSA)
  25360. #ifndef NO_DES3
  25361. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25362. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25363. #endif /* NO_DES3 */
  25364. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25365. #ifndef NO_AES_128
  25366. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25367. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25368. #endif
  25369. #ifndef NO_AES_192
  25370. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25371. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25372. #endif
  25373. #ifndef NO_AES_256
  25374. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25375. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25376. #endif
  25377. #endif /* NO_AES && HAVE_AES_CBC */
  25378. #endif /* NO_RSA */
  25379. #if defined(HAVE_ECC)
  25380. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25381. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25382. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25383. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  25384. eccCertSz, eccPrivKey, eccPrivKeySz},
  25385. #endif
  25386. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25387. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25388. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  25389. eccCertSz, eccPrivKey, eccPrivKeySz},
  25390. #endif
  25391. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25392. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25393. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  25394. eccCertSz, eccPrivKey, eccPrivKeySz},
  25395. #endif
  25396. #endif /* NO_AES && HAVE_AES_CBC*/
  25397. #endif /* END HAVE_ECC */
  25398. }; /* END pkcs7EnvelopedVector */
  25399. #ifdef ECC_TIMING_RESISTANT
  25400. AssertIntEQ(wc_InitRng(&rng), 0);
  25401. #endif
  25402. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25403. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25404. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25405. for (i = 0; i < testSz; i++) {
  25406. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25407. (word32)(testVectors + i)->certSz), 0);
  25408. #ifdef ECC_TIMING_RESISTANT
  25409. pkcs7->rng = &rng;
  25410. #endif
  25411. pkcs7->content = (byte*)(testVectors + i)->content;
  25412. pkcs7->contentSz = (testVectors + i)->contentSz;
  25413. pkcs7->contentOID = (testVectors + i)->contentOID;
  25414. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25415. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25416. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25417. pkcs7->privateKey = (testVectors + i)->privateKey;
  25418. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25419. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25420. (word32)sizeof(output)), 0);
  25421. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25422. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25423. AssertIntGE(decodedSz, 0);
  25424. /* Verify the size of each buffer. */
  25425. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25426. /* Don't free the last time through the loop. */
  25427. if (i < testSz - 1) {
  25428. wc_PKCS7_Free(pkcs7);
  25429. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25430. }
  25431. } /* END test loop. */
  25432. }
  25433. /* Test bad args. */
  25434. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25435. (word32)sizeof(output)), BAD_FUNC_ARG);
  25436. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25437. (word32)sizeof(output)), BAD_FUNC_ARG);
  25438. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25439. /* Decode. */
  25440. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25441. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25442. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25443. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25444. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25445. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25446. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25447. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25448. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25449. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25450. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25451. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25452. /* only a failure for KARI test cases */
  25453. tempWrd32 = pkcs7->singleCertSz;
  25454. pkcs7->singleCertSz = 0;
  25455. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25456. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25457. pkcs7->singleCertSz = tempWrd32;
  25458. tmpBytePtr = pkcs7->singleCert;
  25459. pkcs7->singleCert = NULL;
  25460. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25461. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25462. pkcs7->singleCert = tmpBytePtr;
  25463. #endif
  25464. tempWrd32 = pkcs7->privateKeySz;
  25465. pkcs7->privateKeySz = 0;
  25466. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25467. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25468. pkcs7->privateKeySz = tempWrd32;
  25469. tmpBytePtr = pkcs7->privateKey;
  25470. pkcs7->privateKey = NULL;
  25471. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25472. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25473. pkcs7->privateKey = tmpBytePtr;
  25474. wc_PKCS7_Free(pkcs7);
  25475. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25476. /* test of decrypt callback with KEKRI enveloped data */
  25477. {
  25478. int envelopedSz;
  25479. const byte keyId[] = { 0x00 };
  25480. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25481. pkcs7->content = (byte*)input;
  25482. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25483. pkcs7->contentOID = DATA;
  25484. pkcs7->encryptOID = AES256CBCb;
  25485. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25486. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25487. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25488. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25489. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25490. (word32)sizeof(output))), 0);
  25491. wc_PKCS7_Free(pkcs7);
  25492. /* decode envelopedData */
  25493. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25494. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25495. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25496. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25497. envelopedSz, decoded, sizeof(decoded))), 0);
  25498. wc_PKCS7_Free(pkcs7);
  25499. }
  25500. #endif /* !NO_AES && !NO_AES_256 */
  25501. #ifndef NO_RSA
  25502. if (rsaCert) {
  25503. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25504. }
  25505. if (rsaPrivKey) {
  25506. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25507. }
  25508. #endif /*NO_RSA */
  25509. #ifdef HAVE_ECC
  25510. if (eccCert) {
  25511. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25512. }
  25513. if (eccPrivKey) {
  25514. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25515. }
  25516. #endif /* HAVE_ECC */
  25517. #ifdef ECC_TIMING_RESISTANT
  25518. wc_FreeRng(&rng);
  25519. #endif
  25520. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25521. !defined(NO_RSA) && !defined(NO_SHA)
  25522. {
  25523. byte out[7];
  25524. byte *cms;
  25525. word32 cmsSz;
  25526. XFILE cmsFile;
  25527. XMEMSET(out, 0, sizeof(out));
  25528. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25529. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  25530. AssertTrue(cmsFile != XBADFILE);
  25531. cmsSz = (word32)FOURK_BUF;
  25532. AssertNotNull(cms =
  25533. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25534. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  25535. XFCLOSE(cmsFile);
  25536. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25537. sizeof_client_cert_der_2048), 0);
  25538. pkcs7->privateKey = (byte*)client_key_der_2048;
  25539. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25540. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25541. 2), 0);
  25542. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25543. sizeof(out)), 0);
  25544. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25545. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  25546. wc_PKCS7_Free(pkcs7);
  25547. }
  25548. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25549. res = TEST_RES_CHECK(1);
  25550. #endif /* HAVE_PKCS7 */
  25551. return res;
  25552. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  25553. /*
  25554. * Testing wc_PKCS7_EncodeEncryptedData()
  25555. */
  25556. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25557. {
  25558. int res = TEST_SKIPPED;
  25559. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25560. PKCS7* pkcs7 = NULL;
  25561. byte* tmpBytePtr = NULL;
  25562. byte encrypted[TWOK_BUF];
  25563. byte decoded[TWOK_BUF];
  25564. word32 tmpWrd32 = 0;
  25565. int tmpInt = 0;
  25566. int decodedSz;
  25567. int encryptedSz;
  25568. int testSz;
  25569. int i;
  25570. const byte data[] = { /* Hello World */
  25571. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25572. 0x72,0x6c,0x64
  25573. };
  25574. #ifndef NO_DES3
  25575. byte desKey[] = {
  25576. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25577. };
  25578. byte des3Key[] = {
  25579. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25580. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25581. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25582. };
  25583. #endif
  25584. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25585. #ifndef NO_AES_128
  25586. byte aes128Key[] = {
  25587. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25588. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25589. };
  25590. #endif
  25591. #ifndef NO_AES_192
  25592. byte aes192Key[] = {
  25593. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25594. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25595. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25596. };
  25597. #endif
  25598. #ifndef NO_AES_256
  25599. byte aes256Key[] = {
  25600. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25601. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25602. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25603. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25604. };
  25605. #endif
  25606. #endif /* !NO_AES && HAVE_AES_CBC */
  25607. const pkcs7EncryptedVector testVectors[] =
  25608. {
  25609. #ifndef NO_DES3
  25610. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25611. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25612. #endif /* !NO_DES3 */
  25613. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25614. #ifndef NO_AES_128
  25615. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25616. sizeof(aes128Key)},
  25617. #endif
  25618. #ifndef NO_AES_192
  25619. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25620. sizeof(aes192Key)},
  25621. #endif
  25622. #ifndef NO_AES_256
  25623. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25624. sizeof(aes256Key)},
  25625. #endif
  25626. #endif /* !NO_AES && HAVE_AES_CBC */
  25627. };
  25628. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25629. for (i = 0; i < testSz; i++) {
  25630. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25631. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25632. pkcs7->content = (byte*)testVectors[i].content;
  25633. pkcs7->contentSz = testVectors[i].contentSz;
  25634. pkcs7->contentOID = testVectors[i].contentOID;
  25635. pkcs7->encryptOID = testVectors[i].encryptOID;
  25636. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25637. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25638. pkcs7->heap = HEAP_HINT;
  25639. /* encode encryptedData */
  25640. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25641. sizeof(encrypted));
  25642. AssertIntGT(encryptedSz, 0);
  25643. /* Decode encryptedData */
  25644. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25645. decoded, sizeof(decoded));
  25646. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25647. /* Keep values for last itr. */
  25648. if (i < testSz - 1) {
  25649. wc_PKCS7_Free(pkcs7);
  25650. }
  25651. }
  25652. if (pkcs7 == NULL || testSz == 0) {
  25653. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25654. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25655. }
  25656. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25657. sizeof(encrypted)),BAD_FUNC_ARG);
  25658. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25659. sizeof(encrypted)), BAD_FUNC_ARG);
  25660. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25661. 0), BAD_FUNC_ARG);
  25662. /* Testing the struct. */
  25663. tmpBytePtr = pkcs7->content;
  25664. pkcs7->content = NULL;
  25665. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25666. sizeof(encrypted)), BAD_FUNC_ARG);
  25667. pkcs7->content = tmpBytePtr;
  25668. tmpWrd32 = pkcs7->contentSz;
  25669. pkcs7->contentSz = 0;
  25670. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25671. sizeof(encrypted)), BAD_FUNC_ARG);
  25672. pkcs7->contentSz = tmpWrd32;
  25673. tmpInt = pkcs7->encryptOID;
  25674. pkcs7->encryptOID = 0;
  25675. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25676. sizeof(encrypted)), BAD_FUNC_ARG);
  25677. pkcs7->encryptOID = tmpInt;
  25678. tmpBytePtr = pkcs7->encryptionKey;
  25679. pkcs7->encryptionKey = NULL;
  25680. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25681. sizeof(encrypted)), BAD_FUNC_ARG);
  25682. pkcs7->encryptionKey = tmpBytePtr;
  25683. tmpWrd32 = pkcs7->encryptionKeySz;
  25684. pkcs7->encryptionKeySz = 0;
  25685. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25686. sizeof(encrypted)), BAD_FUNC_ARG);
  25687. pkcs7->encryptionKeySz = tmpWrd32;
  25688. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25689. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25690. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25691. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25692. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25693. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25694. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25695. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25696. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25697. decoded, 0), BAD_FUNC_ARG);
  25698. /* Test struct fields */
  25699. tmpBytePtr = pkcs7->encryptionKey;
  25700. pkcs7->encryptionKey = NULL;
  25701. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25702. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25703. pkcs7->encryptionKey = tmpBytePtr;
  25704. pkcs7->encryptionKeySz = 0;
  25705. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25706. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25707. wc_PKCS7_Free(pkcs7);
  25708. res = TEST_RES_CHECK(1);
  25709. #endif
  25710. return res;
  25711. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25712. /*
  25713. * Testing wc_PKCS7_Degenerate()
  25714. */
  25715. static int test_wc_PKCS7_Degenerate(void)
  25716. {
  25717. int res = TEST_SKIPPED;
  25718. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25719. PKCS7* pkcs7;
  25720. char fName[] = "./certs/test-degenerate.p7b";
  25721. XFILE f;
  25722. byte der[4096];
  25723. word32 derSz;
  25724. int ret;
  25725. AssertNotNull(f = XFOPEN(fName, "rb"));
  25726. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25727. derSz = (word32)ret;
  25728. XFCLOSE(f);
  25729. /* test degenerate success */
  25730. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25731. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25732. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25733. #ifndef NO_RSA
  25734. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25735. #else
  25736. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25737. #endif
  25738. wc_PKCS7_Free(pkcs7);
  25739. /* test with turning off degenerate cases */
  25740. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25741. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25742. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25743. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25744. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  25745. wc_PKCS7_Free(pkcs7);
  25746. res = TEST_RES_CHECK(1);
  25747. #endif
  25748. return res;
  25749. } /* END test_wc_PKCS7_Degenerate() */
  25750. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25751. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  25752. static byte berContent[] = {
  25753. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25754. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25755. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25756. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25757. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25758. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25759. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25760. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25761. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25762. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25763. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25764. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25765. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25766. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25767. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25768. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25769. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25770. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25771. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25772. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25773. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25774. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25775. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25776. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25777. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25778. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25779. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25780. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25781. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25782. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25783. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25784. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25785. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25786. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25787. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25788. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25789. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25790. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25791. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25792. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25793. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25794. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25795. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25796. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25797. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25798. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25799. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25800. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25801. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25802. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25803. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25804. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25805. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25806. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25807. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25808. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25809. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25810. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25811. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25812. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25813. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25814. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25815. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25816. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25817. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25818. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25819. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25820. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25821. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25822. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25823. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25824. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25825. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25826. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25827. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25828. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25829. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25830. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25831. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25832. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25833. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25834. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25835. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25836. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25837. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25838. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25839. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25840. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25841. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25842. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25843. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25844. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25845. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25846. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25847. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25848. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25849. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25850. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25851. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25852. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25853. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25854. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25855. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25856. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25857. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25858. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25859. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25860. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25861. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25862. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25863. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25864. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25865. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25866. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25867. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25868. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25869. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25870. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25871. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25872. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25873. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25874. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25875. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25876. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25877. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25878. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25879. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25880. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25881. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25882. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25883. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25884. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25885. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25886. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25887. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25888. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25889. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25890. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25891. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25892. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25893. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25894. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25895. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25896. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25897. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25898. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25899. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25900. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25901. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25902. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25903. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25904. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25905. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25906. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25907. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25908. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25909. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25910. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25911. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25912. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25913. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25914. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25915. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25916. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25917. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25918. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25919. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25920. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25921. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25922. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25923. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25924. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25925. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25926. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25927. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25928. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25929. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25930. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25931. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25932. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25933. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25934. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25935. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25936. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25937. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25938. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25939. 0x00, 0x00, 0x00, 0x00, 0x00
  25940. };
  25941. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25942. * !NO_DES3 && !NO_SHA
  25943. */
  25944. /*
  25945. * Testing wc_PKCS7_BER()
  25946. */
  25947. static int test_wc_PKCS7_BER(void)
  25948. {
  25949. int res = TEST_SKIPPED;
  25950. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25951. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25952. PKCS7* pkcs7;
  25953. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25954. XFILE f;
  25955. byte der[4096];
  25956. #ifndef NO_DES3
  25957. byte decoded[2048];
  25958. #endif
  25959. word32 derSz;
  25960. int ret;
  25961. AssertNotNull(f = XFOPEN(fName, "rb"));
  25962. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25963. derSz = (word32)ret;
  25964. XFCLOSE(f);
  25965. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25966. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25967. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25968. #ifndef NO_RSA
  25969. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25970. #else
  25971. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25972. #endif
  25973. wc_PKCS7_Free(pkcs7);
  25974. #ifndef NO_DES3
  25975. /* decode BER content */
  25976. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25977. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25978. derSz = (word32)ret;
  25979. XFCLOSE(f);
  25980. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25981. #ifndef NO_RSA
  25982. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25983. #else
  25984. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25985. #endif
  25986. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  25987. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25988. derSz = (word32)ret;
  25989. XFCLOSE(f);
  25990. pkcs7->privateKey = der;
  25991. pkcs7->privateKeySz = derSz;
  25992. #ifndef NO_RSA
  25993. #ifdef WOLFSSL_SP_MATH
  25994. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25995. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25996. #else
  25997. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25998. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25999. #endif
  26000. #else
  26001. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26002. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26003. #endif
  26004. wc_PKCS7_Free(pkcs7);
  26005. #endif /* !NO_DES3 */
  26006. res = TEST_RES_CHECK(1);
  26007. #endif
  26008. return res;
  26009. } /* END test_wc_PKCS7_BER() */
  26010. static int test_PKCS7_signed_enveloped(void)
  26011. {
  26012. int res = TEST_SKIPPED;
  26013. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26014. !defined(NO_FILESYSTEM)
  26015. XFILE f;
  26016. PKCS7* pkcs7;
  26017. #ifdef HAVE_AES_CBC
  26018. PKCS7* inner;
  26019. #endif
  26020. void* pt;
  26021. WC_RNG rng;
  26022. unsigned char key[FOURK_BUF/2];
  26023. unsigned char cert[FOURK_BUF/2];
  26024. unsigned char env[FOURK_BUF];
  26025. int envSz = FOURK_BUF;
  26026. int keySz;
  26027. int certSz;
  26028. unsigned char sig[FOURK_BUF * 2];
  26029. int sigSz = FOURK_BUF * 2;
  26030. #ifdef HAVE_AES_CBC
  26031. unsigned char decoded[FOURK_BUF];
  26032. int decodedSz = FOURK_BUF;
  26033. #endif
  26034. /* load cert */
  26035. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  26036. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26037. XFCLOSE(f);
  26038. /* load key */
  26039. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  26040. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26041. XFCLOSE(f);
  26042. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  26043. /* sign cert for envelope */
  26044. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26045. AssertIntEQ(wc_InitRng(&rng), 0);
  26046. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26047. pkcs7->content = cert;
  26048. pkcs7->contentSz = certSz;
  26049. pkcs7->contentOID = DATA;
  26050. pkcs7->privateKey = key;
  26051. pkcs7->privateKeySz = keySz;
  26052. pkcs7->encryptOID = RSAk;
  26053. pkcs7->hashOID = SHA256h;
  26054. pkcs7->rng = &rng;
  26055. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26056. wc_PKCS7_Free(pkcs7);
  26057. wc_FreeRng(&rng);
  26058. #ifdef HAVE_AES_CBC
  26059. /* create envelope */
  26060. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26061. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26062. pkcs7->content = sig;
  26063. pkcs7->contentSz = sigSz;
  26064. pkcs7->contentOID = DATA;
  26065. pkcs7->encryptOID = AES256CBCb;
  26066. pkcs7->privateKey = key;
  26067. pkcs7->privateKeySz = keySz;
  26068. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26069. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26070. wc_PKCS7_Free(pkcs7);
  26071. #endif
  26072. /* create bad signed enveloped data */
  26073. sigSz = FOURK_BUF * 2;
  26074. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26075. AssertIntEQ(wc_InitRng(&rng), 0);
  26076. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26077. pkcs7->content = env;
  26078. pkcs7->contentSz = envSz;
  26079. pkcs7->contentOID = DATA;
  26080. pkcs7->privateKey = key;
  26081. pkcs7->privateKeySz = keySz;
  26082. pkcs7->encryptOID = RSAk;
  26083. pkcs7->hashOID = SHA256h;
  26084. pkcs7->rng = &rng;
  26085. /* Set no certs in bundle for this test. Hang on to the pointer though to
  26086. * free it later. */
  26087. pt = (void*)pkcs7->certList;
  26088. pkcs7->certList = NULL; /* no certs in bundle */
  26089. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26090. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  26091. wc_PKCS7_Free(pkcs7);
  26092. /* check verify fails */
  26093. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26094. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26095. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26096. PKCS7_SIGNEEDS_CHECK);
  26097. /* try verifying the signature manually */
  26098. {
  26099. RsaKey rKey;
  26100. word32 idx = 0;
  26101. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26102. WC_MAX_DIGEST_SIZE];
  26103. int digestSz;
  26104. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26105. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26106. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26107. digest, sizeof(digest), &rKey);
  26108. AssertIntGT(digestSz, 0);
  26109. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26110. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26111. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26112. /* verify was success */
  26113. }
  26114. wc_PKCS7_Free(pkcs7);
  26115. /* initializing the PKCS7 struct with the signing certificate should pass */
  26116. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26117. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26118. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26119. wc_PKCS7_Free(pkcs7);
  26120. /* create valid degenerate bundle */
  26121. sigSz = FOURK_BUF * 2;
  26122. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26123. pkcs7->content = env;
  26124. pkcs7->contentSz = envSz;
  26125. pkcs7->contentOID = DATA;
  26126. pkcs7->privateKey = key;
  26127. pkcs7->privateKeySz = keySz;
  26128. pkcs7->encryptOID = RSAk;
  26129. pkcs7->hashOID = SHA256h;
  26130. pkcs7->rng = &rng;
  26131. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26132. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26133. wc_PKCS7_Free(pkcs7);
  26134. wc_FreeRng(&rng);
  26135. /* check verify */
  26136. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26137. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26138. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26139. AssertNotNull(pkcs7->content);
  26140. #ifdef HAVE_AES_CBC
  26141. /* check decode */
  26142. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26143. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26144. inner->privateKey = key;
  26145. inner->privateKeySz = keySz;
  26146. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26147. pkcs7->contentSz, decoded, decodedSz)), 0);
  26148. wc_PKCS7_Free(inner);
  26149. #endif
  26150. wc_PKCS7_Free(pkcs7);
  26151. #ifdef HAVE_AES_CBC
  26152. /* check cert set */
  26153. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26154. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26155. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26156. AssertNotNull(pkcs7->singleCert);
  26157. AssertIntNE(pkcs7->singleCertSz, 0);
  26158. wc_PKCS7_Free(pkcs7);
  26159. #endif
  26160. res = TEST_RES_CHECK(1);
  26161. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26162. return res;
  26163. }
  26164. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26165. {
  26166. int res = TEST_SKIPPED;
  26167. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26168. && !defined(NO_AES)
  26169. PKCS7* pkcs7;
  26170. void* heap = NULL;
  26171. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26172. AssertNotNull(pkcs7);
  26173. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26174. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26175. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26176. wc_PKCS7_Free(pkcs7);
  26177. res = TEST_RES_CHECK(1);
  26178. #endif
  26179. return res;
  26180. }
  26181. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26182. {
  26183. int res = TEST_SKIPPED;
  26184. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26185. && !defined(NO_AES)
  26186. PKCS7* pkcs7;
  26187. void* heap = NULL;
  26188. WOLFSSL_CTX* ctx;
  26189. ctx = NULL;
  26190. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26191. AssertNotNull(pkcs7);
  26192. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26193. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26194. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26195. wc_PKCS7_Free(pkcs7);
  26196. res = TEST_RES_CHECK(1);
  26197. #endif
  26198. return res;
  26199. }
  26200. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26201. {
  26202. int res = TEST_SKIPPED;
  26203. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26204. && !defined(NO_AES)
  26205. PKCS7* pkcs7;
  26206. void* heap = NULL;
  26207. WOLFSSL_CTX* ctx;
  26208. ctx = NULL;
  26209. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26210. AssertNotNull(pkcs7);
  26211. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26212. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26213. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26214. wc_PKCS7_Free(pkcs7);
  26215. res = TEST_RES_CHECK(1);
  26216. #endif
  26217. return res;
  26218. }
  26219. static int test_wc_PKCS7_DecodeCompressedData(void)
  26220. {
  26221. int res = TEST_SKIPPED;
  26222. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26223. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26224. PKCS7* pkcs7;
  26225. void* heap = NULL;
  26226. byte out[4096];
  26227. byte *decompressed;
  26228. int outSz, decompressedSz;
  26229. const char* cert = "./certs/client-cert.pem";
  26230. byte* cert_buf = NULL;
  26231. size_t cert_sz = 0;
  26232. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26233. AssertNotNull((decompressed =
  26234. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26235. decompressedSz = (int)cert_sz;
  26236. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26237. pkcs7->content = (byte*)cert_buf;
  26238. pkcs7->contentSz = (word32)cert_sz;
  26239. pkcs7->contentOID = DATA;
  26240. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26241. sizeof(out))), 0);
  26242. wc_PKCS7_Free(pkcs7);
  26243. /* compressed key should be smaller than when started */
  26244. AssertIntLT(outSz, cert_sz);
  26245. /* test decompression */
  26246. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26247. AssertIntEQ(pkcs7->contentOID, 0);
  26248. /* fail case with out buffer too small */
  26249. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26250. decompressed, outSz), 0);
  26251. /* success case */
  26252. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26253. decompressed, decompressedSz), cert_sz);
  26254. AssertIntEQ(pkcs7->contentOID, DATA);
  26255. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26256. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26257. decompressed = NULL;
  26258. /* test decompression function with different 'max' inputs */
  26259. outSz = sizeof(out);
  26260. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26261. 0);
  26262. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26263. out, outSz, 0, heap), 0);
  26264. AssertNull(decompressed);
  26265. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26266. out, outSz, 0, heap), 0);
  26267. AssertNotNull(decompressed);
  26268. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26269. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26270. decompressed = NULL;
  26271. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26272. out, outSz, 0, heap), 0);
  26273. AssertNotNull(decompressed);
  26274. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26275. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26276. if (cert_buf)
  26277. free(cert_buf);
  26278. wc_PKCS7_Free(pkcs7);
  26279. res = TEST_RES_CHECK(1);
  26280. #endif
  26281. return res;
  26282. }
  26283. static int test_wc_i2d_PKCS12(void)
  26284. {
  26285. int res = TEST_SKIPPED;
  26286. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26287. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26288. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26289. WC_PKCS12* pkcs12 = NULL;
  26290. unsigned char der[FOURK_BUF * 2];
  26291. unsigned char* pt;
  26292. int derSz;
  26293. unsigned char out[FOURK_BUF * 2];
  26294. int outSz = FOURK_BUF * 2;
  26295. const char p12_f[] = "./certs/test-servercert.p12";
  26296. XFILE f;
  26297. f = XFOPEN(p12_f, "rb");
  26298. AssertNotNull(f);
  26299. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26300. AssertIntGT(derSz, 0);
  26301. XFCLOSE(f);
  26302. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26303. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26304. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26305. AssertIntEQ(outSz, derSz);
  26306. outSz = derSz - 1;
  26307. pt = out;
  26308. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26309. outSz = derSz;
  26310. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26311. AssertIntEQ((pt == out), 0);
  26312. pt = NULL;
  26313. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26314. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26315. wc_PKCS12_free(pkcs12);
  26316. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26317. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26318. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26319. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26320. AssertIntEQ(outSz, derSz);
  26321. wc_PKCS12_free(pkcs12);
  26322. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26323. pkcs12 = NULL;
  26324. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26325. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26326. AssertIntEQ(outSz, derSz);
  26327. wc_PKCS12_free(pkcs12);
  26328. res = TEST_RES_CHECK(1);
  26329. #endif
  26330. return res;
  26331. }
  26332. /* Testing wc_SignatureGetSize() for signature type ECC */
  26333. static int test_wc_SignatureGetSize_ecc(void)
  26334. {
  26335. int res = TEST_SKIPPED;
  26336. #ifndef NO_SIG_WRAPPER
  26337. int ret;
  26338. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  26339. enum wc_SignatureType sig_type;
  26340. word32 key_len;
  26341. /* Initialize ECC Key */
  26342. ecc_key ecc;
  26343. const char* qx =
  26344. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26345. const char* qy =
  26346. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26347. const char* d =
  26348. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26349. ret = wc_ecc_init(&ecc);
  26350. if (ret == 0) {
  26351. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  26352. }
  26353. if (ret == 0) {
  26354. /* Input for signature type ECC */
  26355. sig_type = WC_SIGNATURE_TYPE_ECC;
  26356. key_len = sizeof(ecc_key);
  26357. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26358. /* Test bad args */
  26359. if (ret > 0) {
  26360. sig_type = (enum wc_SignatureType) 100;
  26361. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26362. if (ret == BAD_FUNC_ARG) {
  26363. sig_type = WC_SIGNATURE_TYPE_ECC;
  26364. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26365. }
  26366. if (ret >= 0) {
  26367. key_len = (word32) 0;
  26368. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26369. }
  26370. if (ret == BAD_FUNC_ARG) {
  26371. ret = SIG_TYPE_E;
  26372. }
  26373. }
  26374. }
  26375. else {
  26376. ret = WOLFSSL_FATAL_ERROR;
  26377. }
  26378. wc_ecc_free(&ecc);
  26379. #else
  26380. ret = SIG_TYPE_E;
  26381. #endif
  26382. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26383. #endif /* NO_SIG_WRAPPER */
  26384. return res;
  26385. }/* END test_wc_SignatureGetSize_ecc() */
  26386. /* Testing wc_SignatureGetSize() for signature type rsa */
  26387. static int test_wc_SignatureGetSize_rsa(void)
  26388. {
  26389. int res = TEST_SKIPPED;
  26390. #ifndef NO_SIG_WRAPPER
  26391. int ret = 0;
  26392. #ifndef NO_RSA
  26393. enum wc_SignatureType sig_type;
  26394. word32 key_len;
  26395. word32 idx = 0;
  26396. /* Initialize RSA Key */
  26397. RsaKey rsa_key;
  26398. byte* tmp = NULL;
  26399. size_t bytes;
  26400. #ifdef USE_CERT_BUFFERS_1024
  26401. bytes = (size_t)sizeof_client_key_der_1024;
  26402. if (bytes < (size_t)sizeof_client_key_der_1024)
  26403. bytes = (size_t)sizeof_client_cert_der_1024;
  26404. #elif defined(USE_CERT_BUFFERS_2048)
  26405. bytes = (size_t)sizeof_client_key_der_2048;
  26406. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26407. bytes = (size_t)sizeof_client_cert_der_2048;
  26408. #else
  26409. bytes = FOURK_BUF;
  26410. #endif
  26411. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26412. if (tmp != NULL) {
  26413. #ifdef USE_CERT_BUFFERS_1024
  26414. XMEMCPY(tmp, client_key_der_1024,
  26415. (size_t)sizeof_client_key_der_1024);
  26416. #elif defined(USE_CERT_BUFFERS_2048)
  26417. XMEMCPY(tmp, client_key_der_2048,
  26418. (size_t)sizeof_client_key_der_2048);
  26419. #elif !defined(NO_FILESYSTEM)
  26420. file = XFOPEN(clientKey, "rb");
  26421. if (file != XBADFILE) {
  26422. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  26423. XFCLOSE(file);
  26424. }
  26425. else {
  26426. ret = WOLFSSL_FATAL_ERROR;
  26427. }
  26428. #else
  26429. ret = WOLFSSL_FATAL_ERROR;
  26430. #endif
  26431. }
  26432. else {
  26433. ret = WOLFSSL_FATAL_ERROR;
  26434. }
  26435. if (ret == 0) {
  26436. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  26437. }
  26438. if (ret == 0) {
  26439. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  26440. }
  26441. if (ret == 0) {
  26442. /* Input for signature type RSA */
  26443. sig_type = WC_SIGNATURE_TYPE_RSA;
  26444. key_len = sizeof(RsaKey);
  26445. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26446. /* Test bad args */
  26447. if (ret > 0) {
  26448. sig_type = (enum wc_SignatureType) 100;
  26449. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26450. if (ret == BAD_FUNC_ARG) {
  26451. sig_type = WC_SIGNATURE_TYPE_RSA;
  26452. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26453. }
  26454. #ifndef HAVE_USER_RSA
  26455. if (ret == BAD_FUNC_ARG)
  26456. #else
  26457. if (ret == 0)
  26458. #endif
  26459. {
  26460. key_len = (word32)0;
  26461. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26462. }
  26463. if (ret == BAD_FUNC_ARG) {
  26464. ret = SIG_TYPE_E;
  26465. }
  26466. }
  26467. }
  26468. else {
  26469. ret = WOLFSSL_FATAL_ERROR;
  26470. }
  26471. wc_FreeRsaKey(&rsa_key);
  26472. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26473. #else
  26474. ret = SIG_TYPE_E;
  26475. #endif
  26476. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  26477. #endif /* NO_SIG_WRAPPER */
  26478. return res;
  26479. }/* END test_wc_SignatureGetSize_rsa(void) */
  26480. /*----------------------------------------------------------------------------*
  26481. | hash.h Tests
  26482. *----------------------------------------------------------------------------*/
  26483. static int test_wc_HashInit(void)
  26484. {
  26485. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  26486. wc_HashAlg hash;
  26487. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26488. enum wc_HashType enumArray[] = {
  26489. #ifndef NO_MD5
  26490. WC_HASH_TYPE_MD5,
  26491. #endif
  26492. #ifndef NO_SHA
  26493. WC_HASH_TYPE_SHA,
  26494. #endif
  26495. #ifndef WOLFSSL_SHA224
  26496. WC_HASH_TYPE_SHA224,
  26497. #endif
  26498. #ifndef NO_SHA256
  26499. WC_HASH_TYPE_SHA256,
  26500. #endif
  26501. #ifndef WOLFSSL_SHA384
  26502. WC_HASH_TYPE_SHA384,
  26503. #endif
  26504. #ifndef WOLFSSL_SHA512
  26505. WC_HASH_TYPE_SHA512,
  26506. #endif
  26507. };
  26508. /* dynamically finds the length */
  26509. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26510. /* For loop to test various arguments... */
  26511. for (i = 0; i < enumlen; i++) {
  26512. /* check for bad args */
  26513. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  26514. ret = 1;
  26515. break;
  26516. }
  26517. wc_HashFree(&hash, enumArray[i]);
  26518. /* check for null ptr */
  26519. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  26520. ret = 1;
  26521. break;
  26522. }
  26523. } /* end of for loop */
  26524. return TEST_RES_CHECK(ret == 0);
  26525. } /* end of test_wc_HashInit */
  26526. /*
  26527. * Unit test function for wc_HashSetFlags()
  26528. */
  26529. static int test_wc_HashSetFlags(void)
  26530. {
  26531. int res = TEST_SKIPPED;
  26532. #ifdef WOLFSSL_HASH_FLAGS
  26533. wc_HashAlg hash;
  26534. int ret = 0;
  26535. word32 flags = 0;
  26536. int i, j;
  26537. int notSupportedLen;
  26538. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26539. enum wc_HashType enumArray[] = {
  26540. #ifndef NO_MD5
  26541. WC_HASH_TYPE_MD5,
  26542. #endif
  26543. #ifndef NO_SHA
  26544. WC_HASH_TYPE_SHA,
  26545. #endif
  26546. #ifdef WOLFSSL_SHA224
  26547. WC_HASH_TYPE_SHA224,
  26548. #endif
  26549. #ifndef NO_SHA256
  26550. WC_HASH_TYPE_SHA256,
  26551. #endif
  26552. #ifdef WOLFSSL_SHA384
  26553. WC_HASH_TYPE_SHA384,
  26554. #endif
  26555. #ifdef WOLFSSL_SHA512
  26556. WC_HASH_TYPE_SHA512,
  26557. #endif
  26558. #ifdef WOLFSSL_SHA3
  26559. WC_HASH_TYPE_SHA3_224,
  26560. #endif
  26561. };
  26562. enum wc_HashType notSupported[] = {
  26563. WC_HASH_TYPE_MD5_SHA,
  26564. WC_HASH_TYPE_MD2,
  26565. WC_HASH_TYPE_MD4,
  26566. WC_HASH_TYPE_BLAKE2B,
  26567. WC_HASH_TYPE_BLAKE2S,
  26568. WC_HASH_TYPE_NONE,
  26569. };
  26570. /* dynamically finds the length */
  26571. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26572. /* For loop to test various arguments... */
  26573. for (i = 0; i < enumlen; i++) {
  26574. ret = wc_HashInit(&hash, enumArray[i]);
  26575. if (ret == 0) {
  26576. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  26577. }
  26578. if (ret == 0) {
  26579. if (flags & WC_HASH_FLAG_ISCOPY) {
  26580. ret = 0;
  26581. }
  26582. }
  26583. if (ret == 0) {
  26584. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  26585. if (ret == BAD_FUNC_ARG) {
  26586. ret = 0;
  26587. }
  26588. }
  26589. wc_HashFree(&hash, enumArray[i]);
  26590. }
  26591. /* For loop to test not supported cases */
  26592. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26593. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26594. ret = wc_HashInit(&hash, notSupported[j]);
  26595. if (ret == 0) {
  26596. ret = -1;
  26597. }
  26598. else if (ret == BAD_FUNC_ARG) {
  26599. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  26600. if (ret == 0) {
  26601. ret = -1;
  26602. }
  26603. else if (ret == BAD_FUNC_ARG) {
  26604. ret = 0;
  26605. }
  26606. }
  26607. if (ret == 0) {
  26608. ret = wc_HashFree(&hash, notSupported[j]);
  26609. if (ret == 0) {
  26610. ret = -1;
  26611. }
  26612. else if (ret == BAD_FUNC_ARG) {
  26613. ret = 0;
  26614. }
  26615. }
  26616. }
  26617. res = TEST_RES_CHECK(ret == 0);
  26618. #endif
  26619. return res;
  26620. } /* END test_wc_HashSetFlags */
  26621. /*
  26622. * Unit test function for wc_HashGetFlags()
  26623. */
  26624. static int test_wc_HashGetFlags(void)
  26625. {
  26626. int res = TEST_SKIPPED;
  26627. #ifdef WOLFSSL_HASH_FLAGS
  26628. wc_HashAlg hash;
  26629. int ret = 0;
  26630. word32 flags = 0;
  26631. int i, j;
  26632. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26633. enum wc_HashType enumArray[] = {
  26634. #ifndef NO_MD5
  26635. WC_HASH_TYPE_MD5,
  26636. #endif
  26637. #ifndef NO_SHA
  26638. WC_HASH_TYPE_SHA,
  26639. #endif
  26640. #ifdef WOLFSSL_SHA224
  26641. WC_HASH_TYPE_SHA224,
  26642. #endif
  26643. #ifndef NO_SHA256
  26644. WC_HASH_TYPE_SHA256,
  26645. #endif
  26646. #ifdef WOLFSSL_SHA384
  26647. WC_HASH_TYPE_SHA384,
  26648. #endif
  26649. #ifdef WOLFSSL_SHA512
  26650. WC_HASH_TYPE_SHA512,
  26651. #endif
  26652. #ifdef WOLFSSL_SHA3
  26653. WC_HASH_TYPE_SHA3_224,
  26654. #endif
  26655. };
  26656. enum wc_HashType notSupported[] = {
  26657. WC_HASH_TYPE_MD5_SHA,
  26658. WC_HASH_TYPE_MD2,
  26659. WC_HASH_TYPE_MD4,
  26660. WC_HASH_TYPE_BLAKE2B,
  26661. WC_HASH_TYPE_BLAKE2S,
  26662. WC_HASH_TYPE_NONE,
  26663. };
  26664. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26665. int notSupportedLen;
  26666. /* For loop to test various arguments... */
  26667. for (i = 0; i < enumlen; i++) {
  26668. ret = wc_HashInit(&hash, enumArray[i]);
  26669. if (ret == 0) {
  26670. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  26671. }
  26672. if (ret == 0) {
  26673. if (flags & WC_HASH_FLAG_ISCOPY) {
  26674. ret = 0;
  26675. }
  26676. }
  26677. if (ret == 0) {
  26678. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  26679. if (ret == BAD_FUNC_ARG) {
  26680. ret = 0;
  26681. }
  26682. }
  26683. wc_HashFree(&hash, enumArray[i]);
  26684. if (ret != 0) {
  26685. break;
  26686. }
  26687. }
  26688. /* For loop to test not supported cases */
  26689. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26690. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  26691. ret = wc_HashInit(&hash, notSupported[j]);
  26692. if (ret == 0) {
  26693. ret = -1;
  26694. }
  26695. else if (ret == BAD_FUNC_ARG) {
  26696. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  26697. if (ret == 0) {
  26698. ret = -1;
  26699. }
  26700. else if (ret == BAD_FUNC_ARG) {
  26701. ret = 0;
  26702. }
  26703. }
  26704. if (ret == 0) {
  26705. ret = wc_HashFree(&hash, notSupported[j]);
  26706. if (ret == 0) {
  26707. ret = -1;
  26708. }
  26709. if (ret == BAD_FUNC_ARG) {
  26710. ret = 0;
  26711. }
  26712. }
  26713. }
  26714. res = TEST_RES_CHECK(ret == 0);
  26715. #endif
  26716. return res;
  26717. } /* END test_wc_HashGetFlags */
  26718. /*----------------------------------------------------------------------------*
  26719. | Compatibility Tests
  26720. *----------------------------------------------------------------------------*/
  26721. static int test_wolfSSL_lhash(void)
  26722. {
  26723. int res = TEST_SKIPPED;
  26724. #ifdef OPENSSL_ALL
  26725. const char testStr[] = "Like a true nature's child\n"
  26726. "We were born\n"
  26727. "Born to be wild";
  26728. #ifdef NO_SHA
  26729. AssertIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  26730. #else
  26731. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26732. #endif
  26733. res = TEST_RES_CHECK(1);
  26734. #endif
  26735. return res;
  26736. }
  26737. static int test_wolfSSL_X509_NAME(void)
  26738. {
  26739. int res = TEST_SKIPPED;
  26740. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26741. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26742. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26743. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26744. defined(OPENSSL_EXTRA))
  26745. X509* x509;
  26746. const unsigned char* c;
  26747. unsigned char buf[4096];
  26748. int bytes;
  26749. XFILE f;
  26750. const X509_NAME* a;
  26751. const X509_NAME* b;
  26752. X509_NAME* d2i_name = NULL;
  26753. int sz;
  26754. unsigned char* tmp;
  26755. char file[] = "./certs/ca-cert.der";
  26756. #ifndef OPENSSL_EXTRA_X509_SMALL
  26757. byte empty[] = { /* CN=empty emailAddress= */
  26758. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26759. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26760. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26761. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26762. 0x01, 0x16, 0x00
  26763. };
  26764. #endif
  26765. #ifndef OPENSSL_EXTRA_X509_SMALL
  26766. /* test compile of deprecated function, returns 0 */
  26767. AssertIntEQ(CRYPTO_thread_id(), 0);
  26768. #endif
  26769. AssertNotNull(a = X509_NAME_new());
  26770. X509_NAME_free((X509_NAME*)a);
  26771. f = XFOPEN(file, "rb");
  26772. AssertTrue(f != XBADFILE);
  26773. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  26774. XFCLOSE(f);
  26775. c = buf;
  26776. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26777. /* test cmp function */
  26778. AssertNotNull(a = X509_get_issuer_name(x509));
  26779. AssertNotNull(b = X509_get_subject_name(x509));
  26780. #ifndef OPENSSL_EXTRA_X509_SMALL
  26781. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26782. #endif
  26783. tmp = buf;
  26784. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26785. if (sz > 0 && tmp == buf) {
  26786. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  26787. __LINE__);
  26788. fprintf(stderr, " Expected pointer to be incremented\n");
  26789. abort();
  26790. }
  26791. #ifndef OPENSSL_EXTRA_X509_SMALL
  26792. tmp = buf;
  26793. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26794. #endif
  26795. /* if output parameter is NULL, should still return required size. */
  26796. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26797. /* retry but with the function creating a buffer */
  26798. tmp = NULL;
  26799. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26800. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26801. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26802. #ifndef OPENSSL_EXTRA_X509_SMALL
  26803. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  26804. #endif
  26805. X509_NAME_free((X509_NAME*)b);
  26806. X509_NAME_free(d2i_name);
  26807. X509_free(x509);
  26808. #ifndef OPENSSL_EXTRA_X509_SMALL
  26809. /* test with an empty domain component */
  26810. tmp = empty;
  26811. sz = sizeof(empty);
  26812. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26813. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26814. /* size of empty emailAddress will be 0 */
  26815. tmp = buf;
  26816. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26817. (char*)tmp, sizeof(buf)), 0);
  26818. /* should contain no organization name */
  26819. tmp = buf;
  26820. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26821. (char*)tmp, sizeof(buf)), -1);
  26822. X509_NAME_free(d2i_name);
  26823. #endif
  26824. res = TEST_RES_CHECK(1);
  26825. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26826. return res;
  26827. }
  26828. static int test_wolfSSL_X509_NAME_hash(void)
  26829. {
  26830. int res = TEST_SKIPPED;
  26831. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  26832. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26833. BIO* bio;
  26834. X509* x509 = NULL;
  26835. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26836. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26837. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26838. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26839. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26840. X509_free(x509);
  26841. BIO_free(bio);
  26842. res = TEST_RES_CHECK(1);
  26843. #endif
  26844. return res;
  26845. }
  26846. static int test_wolfSSL_X509_NAME_print_ex(void)
  26847. {
  26848. int res = TEST_SKIPPED;
  26849. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26850. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26851. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26852. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26853. !defined(NO_BIO) && !defined(NO_RSA)
  26854. int memSz;
  26855. byte* mem = NULL;
  26856. BIO* bio = NULL;
  26857. BIO* membio = NULL;
  26858. X509* x509 = NULL;
  26859. X509_NAME* name = NULL;
  26860. const char* expNormal = "C=US, CN=wolfssl.com";
  26861. const char* expReverse = "CN=wolfssl.com, C=US";
  26862. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26863. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26864. const char* expRFC5523 =
  26865. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26866. /* Test with real cert (svrCertFile) first */
  26867. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26868. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26869. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26870. AssertNotNull(name = X509_get_subject_name(x509));
  26871. /* Test without flags */
  26872. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26873. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26874. BIO_free(membio);
  26875. /* Test flag: XN_FLAG_RFC2253 */
  26876. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26877. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26878. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26879. BIO_free(membio);
  26880. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26881. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26882. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26883. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26884. BIO_free(membio);
  26885. X509_free(x509);
  26886. BIO_free(bio);
  26887. /* Test normal case without escaped characters */
  26888. {
  26889. /* Create name: "/C=US/CN=wolfssl.com" */
  26890. AssertNotNull(name = X509_NAME_new());
  26891. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26892. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26893. WOLFSSL_SUCCESS);
  26894. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26895. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26896. WOLFSSL_SUCCESS);
  26897. /* Test without flags */
  26898. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26899. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26900. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26901. AssertIntEQ(memSz, XSTRLEN(expNormal));
  26902. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26903. BIO_free(membio);
  26904. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26905. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26906. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26907. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26908. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26909. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26910. BIO_free(membio);
  26911. /* Test flags: XN_FLAG_DN_REV - reversed */
  26912. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26913. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26914. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26915. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26916. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26917. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26918. BIO_free(membio);
  26919. X509_NAME_free(name);
  26920. }
  26921. /* Test RFC2253 characters are escaped with backslashes */
  26922. {
  26923. AssertNotNull(name = X509_NAME_new());
  26924. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26925. /* space at beginning and end, and: ,+"\ */
  26926. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26927. WOLFSSL_SUCCESS);
  26928. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26929. /* # at beginning, and: <>;*/
  26930. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26931. WOLFSSL_SUCCESS);
  26932. /* Test without flags */
  26933. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26934. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26935. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26936. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  26937. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26938. XSTRLEN(expNotEscaped)), 0);
  26939. BIO_free(membio);
  26940. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26941. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26942. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26943. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26944. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26945. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  26946. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26947. BIO_free(membio);
  26948. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26949. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26950. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26951. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26952. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26953. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26954. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26955. XSTRLEN(expNotEscapedRev)), 0);
  26956. BIO_free(membio);
  26957. X509_NAME_free(name);
  26958. }
  26959. res = TEST_RES_CHECK(1);
  26960. #endif
  26961. return res;
  26962. }
  26963. #ifndef NO_BIO
  26964. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26965. {
  26966. int res = TEST_SKIPPED;
  26967. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26968. STACK_OF(X509_INFO) *info_stack;
  26969. X509_INFO *info;
  26970. int len;
  26971. int i;
  26972. const char* files[] = {
  26973. cliCertFile,
  26974. cliKeyFile,
  26975. /* This needs to be the order as svrCertFile contains the
  26976. * intermediate cert as well. */
  26977. svrKeyFile,
  26978. svrCertFile,
  26979. NULL,
  26980. };
  26981. const char** curFile;
  26982. BIO *fileBIO;
  26983. BIO *concatBIO = NULL;
  26984. byte tmp[FOURK_BUF];
  26985. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26986. * to group objects together. */
  26987. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26988. for (curFile = files; *curFile != NULL; curFile++) {
  26989. int fileLen;
  26990. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26991. fileLen = wolfSSL_BIO_get_len(fileBIO);
  26992. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26993. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  26994. fileLen -= len;
  26995. }
  26996. /* Make sure we read the entire file */
  26997. AssertIntEQ(fileLen, 0);
  26998. BIO_free(fileBIO);
  26999. }
  27000. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  27001. NULL));
  27002. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  27003. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27004. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  27005. AssertNotNull(info->x509);
  27006. AssertNull(info->crl);
  27007. if (i != 0) {
  27008. AssertNotNull(info->x_pkey);
  27009. AssertIntEQ(X509_check_private_key(info->x509,
  27010. info->x_pkey->dec_pkey), 1);
  27011. }
  27012. else {
  27013. AssertNull(info->x_pkey);
  27014. }
  27015. }
  27016. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27017. BIO_free(concatBIO);
  27018. res = TEST_RES_CHECK(1);
  27019. #endif
  27020. return res;
  27021. }
  27022. #endif
  27023. #ifndef NO_BIO
  27024. static int test_wolfSSL_X509_INFO(void)
  27025. {
  27026. int res = TEST_SKIPPED;
  27027. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  27028. STACK_OF(X509_INFO) *info_stack;
  27029. X509_INFO *info;
  27030. BIO *cert;
  27031. int i;
  27032. /* PEM in hex format to avoid null terminator */
  27033. byte data[] = {
  27034. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  27035. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  27036. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  27037. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  27038. 0x2d, 0x2d, 0x2d
  27039. };
  27040. /* PEM in hex format to avoid null terminator */
  27041. byte data2[] = {
  27042. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  27043. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  27044. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  27045. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  27046. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  27047. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  27048. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  27049. };
  27050. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27051. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27052. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27053. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  27054. AssertNotNull(info->x509);
  27055. AssertNull(info->crl);
  27056. AssertNull(info->x_pkey);
  27057. }
  27058. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27059. BIO_free(cert);
  27060. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  27061. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27062. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27063. BIO_free(cert);
  27064. /* This case should fail due to invalid input. */
  27065. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27066. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  27067. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27068. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27069. BIO_free(cert);
  27070. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  27071. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  27072. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  27073. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  27074. BIO_free(cert);
  27075. res = TEST_RES_CHECK(1);
  27076. #endif
  27077. return res;
  27078. }
  27079. #endif
  27080. static int test_wolfSSL_X509_subject_name_hash(void)
  27081. {
  27082. int res = TEST_SKIPPED;
  27083. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27084. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27085. X509* x509;
  27086. X509_NAME* subjectName = NULL;
  27087. unsigned long ret1 = 0;
  27088. unsigned long ret2 = 0;
  27089. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27090. SSL_FILETYPE_PEM));
  27091. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  27092. /* These two
  27093. * - X509_subject_name_hash(x509)
  27094. * - X509_NAME_hash(X509_get_subject_name(x509))
  27095. * should give the same hash, if !defined(NO_SHA) is true. */
  27096. ret1 = X509_subject_name_hash(x509);
  27097. AssertIntNE(ret1, 0);
  27098. #if !defined(NO_SHA)
  27099. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  27100. AssertIntNE(ret2, 0);
  27101. AssertIntEQ(ret1, ret2);
  27102. #else
  27103. (void) ret2;
  27104. #endif
  27105. X509_free(x509);
  27106. res = TEST_RES_CHECK(1);
  27107. #endif
  27108. return res;
  27109. }
  27110. static int test_wolfSSL_X509_issuer_name_hash(void)
  27111. {
  27112. int res = TEST_SKIPPED;
  27113. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27114. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27115. X509* x509;
  27116. X509_NAME* issuertName = NULL;
  27117. unsigned long ret1 = 0;
  27118. unsigned long ret2 = 0;
  27119. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27120. SSL_FILETYPE_PEM));
  27121. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  27122. /* These two
  27123. * - X509_issuer_name_hash(x509)
  27124. * - X509_NAME_hash(X509_get_issuer_name(x509))
  27125. * should give the same hash, if !defined(NO_SHA) is true. */
  27126. ret1 = X509_issuer_name_hash(x509);
  27127. AssertIntNE(ret1, 0);
  27128. #if !defined(NO_SHA)
  27129. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  27130. AssertIntNE(ret2, 0);
  27131. AssertIntEQ(ret1, ret2);
  27132. #else
  27133. (void) ret2;
  27134. #endif
  27135. X509_free(x509);
  27136. res = TEST_RES_CHECK(1);
  27137. #endif
  27138. return res;
  27139. }
  27140. static int test_wolfSSL_X509_check_host(void)
  27141. {
  27142. int res = TEST_SKIPPED;
  27143. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27144. && !defined(NO_SHA) && !defined(NO_RSA)
  27145. X509* x509;
  27146. const char altName[] = "example.com";
  27147. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27148. SSL_FILETYPE_PEM));
  27149. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  27150. WOLFSSL_SUCCESS);
  27151. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  27152. WOLFSSL_FAILURE);
  27153. X509_free(x509);
  27154. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  27155. WOLFSSL_FAILURE);
  27156. res = TEST_RES_CHECK(1);
  27157. #endif
  27158. return res;
  27159. }
  27160. static int test_wolfSSL_X509_check_email(void)
  27161. {
  27162. int res = TEST_SKIPPED;
  27163. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  27164. X509* x509;
  27165. const char goodEmail[] = "info@wolfssl.com";
  27166. const char badEmail[] = "disinfo@wolfssl.com";
  27167. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27168. SSL_FILETYPE_PEM));
  27169. /* Should fail on non-matching email address */
  27170. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  27171. WOLFSSL_FAILURE);
  27172. /* Should succeed on matching email address */
  27173. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  27174. WOLFSSL_SUCCESS);
  27175. /* Should compute length internally when not provided */
  27176. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  27177. WOLFSSL_SUCCESS);
  27178. /* Should fail when email address is NULL */
  27179. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  27180. WOLFSSL_FAILURE);
  27181. X509_free(x509);
  27182. /* Should fail when x509 is NULL */
  27183. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  27184. WOLFSSL_FAILURE);
  27185. res = TEST_RES_CHECK(1);
  27186. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  27187. return res;
  27188. }
  27189. static int test_wolfSSL_DES(void)
  27190. {
  27191. int res = TEST_SKIPPED;
  27192. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  27193. const_DES_cblock myDes;
  27194. DES_cblock iv;
  27195. DES_key_schedule key;
  27196. word32 i;
  27197. DES_LONG dl;
  27198. unsigned char msg[] = "hello wolfssl";
  27199. DES_check_key(1);
  27200. DES_set_key(&myDes, &key);
  27201. /* check, check of odd parity */
  27202. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  27203. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27204. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  27205. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27206. /* set odd parity for success case */
  27207. DES_set_odd_parity(&myDes);
  27208. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  27209. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  27210. myDes[3]);
  27211. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  27212. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27213. AssertIntEQ(key[i], myDes[i]);
  27214. }
  27215. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  27216. /* check weak key */
  27217. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  27218. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27219. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  27220. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27221. /* now do unchecked copy of a weak key over */
  27222. DES_set_key_unchecked(&myDes, &key);
  27223. /* compare arrays, should be the same */
  27224. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27225. AssertIntEQ(key[i], myDes[i]);
  27226. }
  27227. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  27228. /* check DES_key_sched API */
  27229. XMEMSET(key, 1, sizeof(DES_key_schedule));
  27230. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  27231. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  27232. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  27233. /* compare arrays, should be the same */
  27234. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27235. AssertIntEQ(key[i], myDes[i]);
  27236. }
  27237. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  27238. * DES_cbc_encrypt on the input */
  27239. XMEMSET(iv, 0, sizeof(DES_cblock));
  27240. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  27241. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  27242. AssertIntEQ(dl, 480052723);
  27243. res = TEST_RES_CHECK(1);
  27244. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  27245. return res;
  27246. }
  27247. static int test_wc_PemToDer(void)
  27248. {
  27249. int res = TEST_SKIPPED;
  27250. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27251. int ret;
  27252. DerBuffer* pDer = NULL;
  27253. const char* ca_cert = "./certs/server-cert.pem";
  27254. byte* cert_buf = NULL;
  27255. size_t cert_sz = 0;
  27256. int eccKey = 0;
  27257. EncryptedInfo info;
  27258. XMEMSET(&info, 0, sizeof(info));
  27259. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27260. if (ret == 0) {
  27261. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  27262. &pDer, NULL, &info, &eccKey);
  27263. AssertIntEQ(ret, 0);
  27264. wc_FreeDer(&pDer);
  27265. }
  27266. if (cert_buf)
  27267. free(cert_buf);
  27268. #ifdef HAVE_ECC
  27269. {
  27270. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  27271. byte key_buf[256] = {0};
  27272. /* Test fail of loading a key with cert type */
  27273. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  27274. key_buf[0] = '\n';
  27275. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  27276. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  27277. &pDer, NULL, &info, &eccKey)), 0);
  27278. #ifdef OPENSSL_EXTRA
  27279. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  27280. &pDer, NULL, &info, &eccKey)), 0);
  27281. #endif
  27282. wc_FreeDer(&pDer);
  27283. if (cert_buf)
  27284. free(cert_buf);
  27285. }
  27286. #endif
  27287. res = TEST_RES_CHECK(1);
  27288. #endif
  27289. return res;
  27290. }
  27291. static int test_wc_AllocDer(void)
  27292. {
  27293. int res = TEST_SKIPPED;
  27294. #if !defined(NO_CERTS)
  27295. int ret;
  27296. DerBuffer* pDer = NULL;
  27297. word32 testSize = 1024;
  27298. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  27299. AssertIntEQ(ret, 0);
  27300. AssertNotNull(pDer);
  27301. wc_FreeDer(&pDer);
  27302. res = TEST_RES_CHECK(1);
  27303. #endif
  27304. return res;
  27305. }
  27306. static int test_wc_CertPemToDer(void)
  27307. {
  27308. int res = TEST_SKIPPED;
  27309. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27310. int ret;
  27311. const char* ca_cert = "./certs/ca-cert.pem";
  27312. byte* cert_buf = NULL;
  27313. size_t cert_sz = 0, cert_dersz = 0;
  27314. byte* cert_der = NULL;
  27315. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27316. if (ret == 0) {
  27317. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27318. cert_der = (byte*)malloc(cert_dersz);
  27319. if (cert_der) {
  27320. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27321. cert_der, (int)cert_dersz, CERT_TYPE);
  27322. AssertIntGE(ret, 0);
  27323. }
  27324. }
  27325. if (cert_der)
  27326. free(cert_der);
  27327. if (cert_buf)
  27328. free(cert_buf);
  27329. res = TEST_RES_CHECK(1);
  27330. #endif
  27331. return res;
  27332. }
  27333. static int test_wc_PubKeyPemToDer(void)
  27334. {
  27335. int res = TEST_SKIPPED;
  27336. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  27337. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27338. int ret;
  27339. const char* key = "./certs/ecc-client-keyPub.pem";
  27340. byte* cert_buf = NULL;
  27341. size_t cert_sz = 0, cert_dersz = 0;
  27342. byte* cert_der = NULL;
  27343. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27344. cert_der, (int)cert_dersz);
  27345. AssertIntGE(ret, BAD_FUNC_ARG);
  27346. ret = load_file(key, &cert_buf, &cert_sz);
  27347. if (ret == 0) {
  27348. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27349. cert_der = (byte*)malloc(cert_dersz);
  27350. if (cert_der) {
  27351. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27352. cert_der, (int)cert_dersz);
  27353. AssertIntGE(ret, 0);
  27354. }
  27355. }
  27356. if (cert_der)
  27357. free(cert_der);
  27358. if (cert_buf)
  27359. free(cert_buf);
  27360. res = TEST_RES_CHECK(1);
  27361. #endif
  27362. return res;
  27363. }
  27364. static int test_wc_PemPubKeyToDer(void)
  27365. {
  27366. int res = TEST_SKIPPED;
  27367. #if !defined(NO_FILESYSTEM) && \
  27368. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27369. int ret;
  27370. const char* key = "./certs/ecc-client-keyPub.pem";
  27371. size_t cert_dersz = 1024;
  27372. byte* cert_der = (byte*)malloc(cert_dersz);
  27373. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  27374. AssertIntGE(ret, BAD_FUNC_ARG);
  27375. if (cert_der) {
  27376. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  27377. AssertIntGE(ret, 0);
  27378. free(cert_der);
  27379. }
  27380. res = TEST_RES_CHECK(1);
  27381. #endif
  27382. return res;
  27383. }
  27384. static int test_wc_GetPubKeyDerFromCert(void)
  27385. {
  27386. int res = TEST_SKIPPED;
  27387. #if !defined(NO_RSA) || defined(HAVE_ECC)
  27388. int ret;
  27389. word32 idx = 0;
  27390. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27391. word32 keyDerSz = (word32)sizeof(keyDer);
  27392. DecodedCert decoded;
  27393. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  27394. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  27395. word32 certBufSz = sizeof(certBuf);
  27396. #endif
  27397. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  27398. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  27399. XFILE fp;
  27400. #endif
  27401. #ifndef NO_RSA
  27402. RsaKey rsaKey;
  27403. #if defined(USE_CERT_BUFFERS_2048)
  27404. byte* rsaCertDer = (byte*)client_cert_der_2048;
  27405. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  27406. #elif defined(USE_CERT_BUFFERS_1024)
  27407. byte* rsaCertDer = (byte*)client_cert_der_1024;
  27408. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  27409. #else
  27410. unsigned char rsaCertDer[TWOK_BUF];
  27411. word32 rsaCertDerSz;
  27412. #endif
  27413. #endif
  27414. #ifdef HAVE_ECC
  27415. ecc_key eccKey;
  27416. #if defined(USE_CERT_BUFFERS_256)
  27417. byte* eccCert = (byte*)cliecc_cert_der_256;
  27418. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  27419. #else
  27420. unsigned char eccCert[ONEK_BUF];
  27421. word32 eccCertSz;
  27422. XFILE fp2;
  27423. #endif
  27424. #endif
  27425. #ifndef NO_RSA
  27426. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  27427. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  27428. AssertTrue((fp != XBADFILE));
  27429. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  27430. XFCLOSE(fp);
  27431. #endif
  27432. /* good test case - RSA DER cert */
  27433. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  27434. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27435. AssertIntEQ(ret, 0);
  27436. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27437. AssertIntEQ(ret, 0);
  27438. AssertIntGT(keyDerSz, 0);
  27439. /* sanity check, verify we can import DER public key */
  27440. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27441. AssertIntEQ(ret, 0);
  27442. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27443. AssertIntEQ(ret, 0);
  27444. wc_FreeRsaKey(&rsaKey);
  27445. /* test LENGTH_ONLY_E case */
  27446. keyDerSz = 0;
  27447. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27448. AssertIntEQ(ret, LENGTH_ONLY_E);
  27449. AssertIntGT(keyDerSz, 0);
  27450. /* bad args: DecodedCert NULL */
  27451. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  27452. AssertIntEQ(ret, BAD_FUNC_ARG);
  27453. /* bad args: output key buff size */
  27454. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  27455. AssertIntEQ(ret, BAD_FUNC_ARG);
  27456. /* bad args: zero size output key buffer */
  27457. keyDerSz = 0;
  27458. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27459. AssertIntEQ(ret, BAD_FUNC_ARG);
  27460. wc_FreeDecodedCert(&decoded);
  27461. /* Certificate Request Tests */
  27462. #ifdef WOLFSSL_CERT_REQ
  27463. {
  27464. XMEMSET(certBuf, 0, sizeof(certBuf));
  27465. fp = XFOPEN("./certs/csr.signed.der", "rb");
  27466. AssertTrue((fp != XBADFILE));
  27467. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  27468. XFCLOSE(fp);
  27469. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  27470. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  27471. AssertIntEQ(ret, 0);
  27472. /* good test case - RSA DER certificate request */
  27473. keyDerSz = sizeof(keyDer);
  27474. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27475. AssertIntEQ(ret, 0);
  27476. AssertIntGT(keyDerSz, 0);
  27477. /* sanity check, verify we can import DER public key */
  27478. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27479. AssertIntEQ(ret, 0);
  27480. idx = 0;
  27481. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27482. AssertIntEQ(ret, 0);
  27483. wc_FreeRsaKey(&rsaKey);
  27484. wc_FreeDecodedCert(&decoded);
  27485. }
  27486. #endif /* WOLFSSL_CERT_REQ */
  27487. #endif /* NO_RSA */
  27488. #ifdef HAVE_ECC
  27489. #ifndef USE_CERT_BUFFERS_256
  27490. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  27491. AssertTrue((fp2 != XBADFILE));
  27492. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  27493. XFCLOSE(fp2);
  27494. #endif
  27495. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  27496. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27497. AssertIntEQ(ret, 0);
  27498. /* good test case - ECC */
  27499. XMEMSET(keyDer, 0, sizeof(keyDer));
  27500. keyDerSz = sizeof(keyDer);
  27501. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27502. AssertIntEQ(ret, 0);
  27503. AssertIntGT(keyDerSz, 0);
  27504. /* sanity check, verify we can import DER public key */
  27505. ret = wc_ecc_init(&eccKey);
  27506. AssertIntEQ(ret, 0);
  27507. idx = 0; /* reset idx to 0, used above in RSA case */
  27508. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  27509. AssertIntEQ(ret, 0);
  27510. wc_ecc_free(&eccKey);
  27511. /* test LENGTH_ONLY_E case */
  27512. keyDerSz = 0;
  27513. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27514. AssertIntEQ(ret, LENGTH_ONLY_E);
  27515. AssertIntGT(keyDerSz, 0);
  27516. wc_FreeDecodedCert(&decoded);
  27517. #endif
  27518. res = TEST_RES_CHECK(1);
  27519. #endif /* !NO_RSA || HAVE_ECC */
  27520. return res;
  27521. }
  27522. static int test_wc_CheckCertSigPubKey(void)
  27523. {
  27524. int res = TEST_SKIPPED;
  27525. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27526. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  27527. int ret;
  27528. const char* ca_cert = "./certs/ca-cert.pem";
  27529. byte* cert_buf = NULL;
  27530. size_t cert_sz = 0;
  27531. byte* cert_der = NULL;
  27532. word32 cert_dersz = 0;
  27533. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27534. word32 keyDerSz = (word32)sizeof(keyDer);
  27535. DecodedCert decoded;
  27536. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27537. if (ret == 0) {
  27538. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  27539. cert_der = (byte*)malloc(cert_dersz);
  27540. if (cert_der) {
  27541. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27542. cert_der, (int)cert_dersz, CERT_TYPE);
  27543. AssertIntGE(ret, 0);
  27544. }
  27545. }
  27546. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  27547. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27548. AssertIntEQ(ret, 0);
  27549. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27550. AssertIntEQ(ret, 0);
  27551. AssertIntGT(keyDerSz, 0);
  27552. /* Good test case. */
  27553. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27554. RSAk);
  27555. AssertIntEQ(ret, 0);
  27556. /* No certificate. */
  27557. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  27558. ECDSAk);
  27559. AssertIntEQ(ret, BAD_FUNC_ARG);
  27560. /* Bad cert size. */
  27561. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  27562. RSAk);
  27563. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  27564. /* No public key. */
  27565. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  27566. RSAk);
  27567. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  27568. /* Bad public key size. */
  27569. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27570. RSAk);
  27571. AssertIntEQ(ret, BAD_FUNC_ARG);
  27572. /* Wrong aglo. */
  27573. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27574. ECDSAk);
  27575. AssertIntEQ(ret, ASN_PARSE_E);
  27576. wc_FreeDecodedCert(&decoded);
  27577. if (cert_der)
  27578. free(cert_der);
  27579. if (cert_buf)
  27580. free(cert_buf);
  27581. res = TEST_RES_CHECK(1);
  27582. #endif
  27583. return res;
  27584. }
  27585. static int test_wolfSSL_certs(void)
  27586. {
  27587. int res = TEST_SKIPPED;
  27588. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27589. !defined(NO_RSA)
  27590. X509* x509ext;
  27591. #ifdef OPENSSL_ALL
  27592. X509* x509;
  27593. WOLFSSL_X509_EXTENSION* ext;
  27594. ASN1_OBJECT* obj;
  27595. #endif
  27596. WOLFSSL* ssl;
  27597. WOLFSSL_CTX* ctx;
  27598. STACK_OF(ASN1_OBJECT)* sk;
  27599. ASN1_STRING* asn1_str;
  27600. AUTHORITY_KEYID* akey;
  27601. BASIC_CONSTRAINTS* bc;
  27602. int crit;
  27603. #ifndef NO_WOLFSSL_SERVER
  27604. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27605. #else
  27606. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27607. #endif
  27608. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27609. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27610. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27611. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27612. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27613. #endif
  27614. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27615. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27616. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27617. #endif
  27618. AssertNotNull(ssl = SSL_new(ctx));
  27619. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27620. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27621. #endif
  27622. #ifdef HAVE_PK_CALLBACKS
  27623. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27624. #endif /* HAVE_PK_CALLBACKS */
  27625. /* create and use x509 */
  27626. #ifdef OPENSSL_ALL
  27627. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  27628. AssertNotNull(x509);
  27629. #endif
  27630. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  27631. AssertNotNull(x509ext);
  27632. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27633. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27634. /* with loading in a new cert the check on private key should now fail */
  27635. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27636. #endif
  27637. #if defined(USE_CERT_BUFFERS_2048)
  27638. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  27639. (unsigned char*)server_cert_der_2048,
  27640. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27641. #endif
  27642. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27643. /************* Get Digest of Certificate ******************/
  27644. {
  27645. byte digest[64]; /* max digest size */
  27646. word32 digestSz;
  27647. XMEMSET(digest, 0, sizeof(digest));
  27648. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27649. WOLFSSL_SUCCESS);
  27650. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27651. WOLFSSL_SUCCESS);
  27652. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27653. WOLFSSL_FAILURE);
  27654. }
  27655. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27656. /* test and checkout X509 extensions */
  27657. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  27658. &crit, NULL);
  27659. AssertNotNull(bc);
  27660. AssertIntEQ(crit, 0);
  27661. #ifdef OPENSSL_ALL
  27662. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  27663. AssertNotNull(ext);
  27664. X509_EXTENSION_free(ext);
  27665. AssertNotNull(ext = X509_EXTENSION_new());
  27666. X509_EXTENSION_set_critical(ext, 1);
  27667. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27668. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27669. ASN1_OBJECT_free(obj);
  27670. X509_EXTENSION_free(ext);
  27671. AssertNotNull(ext = X509_EXTENSION_new());
  27672. X509_EXTENSION_set_critical(ext, 0);
  27673. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27674. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27675. NULL);
  27676. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27677. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27678. * and X509_get_ext_d2i has created new */
  27679. X509_EXTENSION_free(ext);
  27680. #endif
  27681. BASIC_CONSTRAINTS_free(bc);
  27682. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  27683. AssertNotNull(asn1_str);
  27684. AssertIntEQ(crit, 1);
  27685. AssertIntEQ(asn1_str->type, NID_key_usage);
  27686. #ifdef OPENSSL_ALL
  27687. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  27688. AssertNotNull(ext);
  27689. X509_EXTENSION_free(ext);
  27690. #endif
  27691. ASN1_STRING_free(asn1_str);
  27692. #ifdef OPENSSL_ALL
  27693. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  27694. &crit, NULL);
  27695. AssertNotNull(sk);
  27696. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  27697. AssertNotNull(ext);
  27698. X509_EXTENSION_free(ext);
  27699. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27700. #else
  27701. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27702. &crit, NULL);
  27703. AssertNull(sk);
  27704. #endif
  27705. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27706. NID_authority_key_identifier, &crit, NULL);
  27707. AssertNotNull(akey);
  27708. #ifdef OPENSSL_ALL
  27709. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  27710. AssertNotNull(ext);
  27711. X509_EXTENSION_free(ext);
  27712. #endif
  27713. wolfSSL_AUTHORITY_KEYID_free(akey);
  27714. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27715. NID_private_key_usage_period, &crit, NULL);
  27716. /* AssertNotNull(sk); NID not yet supported */
  27717. AssertIntEQ(crit, -1);
  27718. sk_ASN1_OBJECT_free(sk);
  27719. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  27720. &crit, NULL);
  27721. {
  27722. int i;
  27723. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27724. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27725. AssertIntEQ(gen->type, GEN_DNS);
  27726. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27727. }
  27728. }
  27729. /* AssertNotNull(sk); no alt names set */
  27730. sk_GENERAL_NAME_free(sk);
  27731. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  27732. &crit, NULL);
  27733. /* AssertNotNull(sk); NID not yet supported */
  27734. AssertIntEQ(crit, -1);
  27735. sk_ASN1_OBJECT_free(sk);
  27736. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  27737. NULL);
  27738. /* AssertNotNull(sk); no auth info set */
  27739. sk_ASN1_OBJECT_free(sk);
  27740. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  27741. &crit, NULL);
  27742. /* AssertNotNull(sk); NID not yet supported */
  27743. AssertIntEQ(crit, -1);
  27744. sk_ASN1_OBJECT_free(sk);
  27745. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  27746. &crit, NULL);
  27747. /* AssertNotNull(sk); NID not yet supported */
  27748. AssertIntEQ(crit, -1);
  27749. sk_ASN1_OBJECT_free(sk);
  27750. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27751. NID_certificate_policies, &crit, NULL);
  27752. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  27753. AssertNull(sk);
  27754. #else
  27755. /* AssertNotNull(sk); no cert policy set */
  27756. #endif
  27757. sk_ASN1_OBJECT_free(sk);
  27758. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  27759. &crit, NULL);
  27760. /* AssertNotNull(sk); NID not yet supported */
  27761. AssertIntEQ(crit, -1);
  27762. sk_ASN1_OBJECT_free(sk);
  27763. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  27764. &crit, NULL);
  27765. /* AssertNotNull(sk); NID not yet supported */
  27766. AssertIntEQ(crit, -1);
  27767. sk_ASN1_OBJECT_free(sk);
  27768. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  27769. &crit, NULL);
  27770. /* AssertNotNull(sk); NID not yet supported */
  27771. AssertIntEQ(crit, -1);
  27772. sk_ASN1_OBJECT_free(sk);
  27773. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  27774. NULL);
  27775. /* AssertNotNull(sk); NID not yet supported */
  27776. AssertIntEQ(crit, -1);
  27777. sk_ASN1_OBJECT_free(sk);
  27778. /* test invalid cases */
  27779. crit = 0;
  27780. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  27781. AssertNull(sk);
  27782. AssertIntEQ(crit, -1);
  27783. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  27784. NULL, NULL);
  27785. AssertNull(sk);
  27786. AssertIntEQ(SSL_get_hit(ssl), 0);
  27787. #ifdef OPENSSL_ALL
  27788. X509_free(x509);
  27789. #endif
  27790. X509_free(x509ext);
  27791. SSL_free(ssl);
  27792. SSL_CTX_free(ctx);
  27793. res = TEST_RES_CHECK(1);
  27794. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27795. return res;
  27796. }
  27797. static int test_wolfSSL_X509_check_private_key(void)
  27798. {
  27799. int res = TEST_SKIPPED;
  27800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27801. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27802. X509* x509;
  27803. EVP_PKEY* pkey = NULL;
  27804. const byte* key;
  27805. /* Check with correct key */
  27806. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27807. SSL_FILETYPE_PEM)));
  27808. key = client_key_der_2048;
  27809. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27810. &key, (long)sizeof_client_key_der_2048));
  27811. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  27812. EVP_PKEY_free(pkey);
  27813. pkey = NULL;
  27814. /* Check with wrong key */
  27815. key = server_key_der_2048;
  27816. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27817. &key, (long)sizeof_server_key_der_2048));
  27818. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  27819. /* test for incorrect parameter */
  27820. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  27821. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  27822. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  27823. EVP_PKEY_free(pkey);
  27824. X509_free(x509);
  27825. res = TEST_RES_CHECK(1);
  27826. #endif
  27827. return res;
  27828. }
  27829. static int test_wolfSSL_ASN1_TIME_print(void)
  27830. {
  27831. int res = TEST_SKIPPED;
  27832. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  27833. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27834. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  27835. !defined(NO_BIO)
  27836. BIO* bio;
  27837. X509* x509;
  27838. const unsigned char* der = client_cert_der_2048;
  27839. ASN1_TIME* t;
  27840. unsigned char buf[25];
  27841. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27842. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27843. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27844. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  27845. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27846. AssertIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27847. /* create a bad time and test results */
  27848. AssertNotNull(t = X509_get_notAfter(x509));
  27849. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  27850. t->data[8] = 0;
  27851. t->data[3] = 0;
  27852. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  27853. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27854. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27855. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  27856. BIO_free(bio);
  27857. X509_free(x509);
  27858. res = TEST_RES_CHECK(1);
  27859. #endif
  27860. return res;
  27861. }
  27862. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27863. {
  27864. int res = TEST_SKIPPED;
  27865. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27866. BIO* bio;
  27867. ASN1_UTCTIME* utc = NULL;
  27868. unsigned char buf[25];
  27869. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27870. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27871. /* NULL parameter check */
  27872. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27873. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27874. BIO_free(bio);
  27875. /* Valid date */
  27876. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27877. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27878. DYNAMIC_TYPE_ASN1));
  27879. utc->type = ASN_UTC_TIME;
  27880. utc->length = ASN_UTC_TIME_SIZE;
  27881. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27882. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27883. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27884. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27885. XMEMSET(buf, 0, sizeof(buf));
  27886. BIO_free(bio);
  27887. /* Invalid format */
  27888. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27889. utc->type = ASN_UTC_TIME;
  27890. utc->length = ASN_UTC_TIME_SIZE;
  27891. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27892. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27893. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27894. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27895. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27896. BIO_free(bio);
  27897. res = TEST_RES_CHECK(1);
  27898. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27899. return res;
  27900. }
  27901. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27902. {
  27903. int res = TEST_SKIPPED;
  27904. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27905. ASN1_TIME* fromTime;
  27906. ASN1_TIME* toTime;
  27907. int daysDiff;
  27908. int secsDiff;
  27909. AssertNotNull((fromTime = ASN1_TIME_new()));
  27910. /* Feb 22, 2003, 21:15:15 */
  27911. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27912. AssertNotNull((toTime = ASN1_TIME_new()));
  27913. /* Dec 19, 2010, 18:10:11 */
  27914. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27915. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27916. /* Error conditions. */
  27917. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27918. WOLFSSL_FAILURE);
  27919. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27920. WOLFSSL_FAILURE);
  27921. /* If both times are NULL, difference is 0. */
  27922. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27923. WOLFSSL_SUCCESS);
  27924. AssertIntEQ(daysDiff, 0);
  27925. AssertIntEQ(secsDiff, 0);
  27926. /* If one time is NULL, it defaults to the current time. */
  27927. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27928. WOLFSSL_SUCCESS);
  27929. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27930. WOLFSSL_SUCCESS);
  27931. /* Normal operation. Both times non-NULL. */
  27932. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27933. WOLFSSL_SUCCESS);
  27934. AssertIntEQ(daysDiff, 2856);
  27935. AssertIntEQ(secsDiff, 75296);
  27936. /* Swapping the times should return negative values. */
  27937. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27938. WOLFSSL_SUCCESS);
  27939. AssertIntEQ(daysDiff, -2856);
  27940. AssertIntEQ(secsDiff, -75296);
  27941. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27942. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27943. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27944. /* Compare regression test: No seconds difference, just difference in days.
  27945. */
  27946. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27947. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27948. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27949. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27950. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27951. /* Edge case with Unix epoch. */
  27952. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27953. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27954. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27955. WOLFSSL_SUCCESS);
  27956. AssertIntEQ(daysDiff, 3652);
  27957. AssertIntEQ(secsDiff, 0);
  27958. /* Edge case with year > 2038 (year 2038 problem). */
  27959. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27960. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27961. WOLFSSL_SUCCESS);
  27962. AssertIntEQ(daysDiff, 2932896);
  27963. AssertIntEQ(secsDiff, 86399);
  27964. ASN1_TIME_free(fromTime);
  27965. ASN1_TIME_free(toTime);
  27966. res = TEST_RES_CHECK(1);
  27967. #endif
  27968. return res;
  27969. }
  27970. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27971. {
  27972. int res = TEST_SKIPPED;
  27973. #if defined(OPENSSL_EXTRA)
  27974. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27975. unsigned char nullstr[32];
  27976. XMEMSET(nullstr, 0, 32);
  27977. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27978. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27979. DYNAMIC_TYPE_TMP_BUFFER);
  27980. if (asn1_gtime) {
  27981. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27982. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27983. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27984. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27985. }
  27986. res = TEST_RES_CHECK(1);
  27987. #endif /* OPENSSL_EXTRA */
  27988. return res;
  27989. }
  27990. static int test_wolfSSL_private_keys(void)
  27991. {
  27992. int res = TEST_SKIPPED;
  27993. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27994. !defined(NO_FILESYSTEM)
  27995. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27996. WOLFSSL* ssl;
  27997. WOLFSSL_CTX* ctx;
  27998. EVP_PKEY* pkey = NULL;
  27999. OpenSSL_add_all_digests();
  28000. OpenSSL_add_all_algorithms();
  28001. #ifndef NO_RSA
  28002. #ifndef NO_WOLFSSL_SERVER
  28003. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28004. #else
  28005. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28006. #endif
  28007. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28008. /* Have to load a cert before you can check the private key against that
  28009. * certificates public key! */
  28010. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28011. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  28012. #endif
  28013. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28014. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28015. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28016. #endif
  28017. AssertNotNull(ssl = SSL_new(ctx));
  28018. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28019. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28020. #endif
  28021. #ifdef USE_CERT_BUFFERS_2048
  28022. {
  28023. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28024. unsigned char buf[FOURK_BUF];
  28025. word32 bufSz;
  28026. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  28027. (unsigned char*)client_key_der_2048,
  28028. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28029. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28030. /* Should mismatch now that a different private key loaded */
  28031. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28032. #endif
  28033. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28034. (unsigned char*)server_key,
  28035. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28036. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28037. /* After loading back in DER format of original key, should match */
  28038. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28039. #endif
  28040. /* test loading private key to the WOLFSSL_CTX */
  28041. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28042. (unsigned char*)client_key_der_2048,
  28043. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28044. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28045. /* Should mismatch now that a different private key loaded */
  28046. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28047. #endif
  28048. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28049. (unsigned char*)server_key,
  28050. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28051. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28052. /* After loading back in DER format of original key, should match */
  28053. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28054. #endif
  28055. /* pkey not set yet, expecting to fail */
  28056. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28057. /* set PKEY and test again */
  28058. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28059. &server_key, (long)sizeof_server_key_der_2048));
  28060. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28061. /* reuse PKEY structure and test
  28062. * this should be checked with a memory management sanity checker */
  28063. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  28064. server_key = (const unsigned char*)server_key_der_2048;
  28065. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28066. &server_key, (long)sizeof_server_key_der_2048));
  28067. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28068. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28069. bufSz = FOURK_BUF;
  28070. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28071. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28072. RSAk, NULL, 0)), 0);
  28073. server_key = (const unsigned char*)buf;
  28074. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28075. (long)bufSz));
  28076. }
  28077. #endif
  28078. EVP_PKEY_free(pkey);
  28079. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28080. SSL_CTX_free(ctx);
  28081. #endif /* end of RSA private key match tests */
  28082. #ifdef HAVE_ECC
  28083. #ifndef NO_WOLFSSL_SERVER
  28084. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28085. #else
  28086. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28087. #endif
  28088. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28089. WOLFSSL_FILETYPE_PEM));
  28090. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28091. WOLFSSL_FILETYPE_PEM));
  28092. AssertNotNull(ssl = SSL_new(ctx));
  28093. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28094. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28095. #endif
  28096. SSL_free(ssl);
  28097. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28098. WOLFSSL_FILETYPE_PEM));
  28099. AssertNotNull(ssl = SSL_new(ctx));
  28100. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28101. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28102. #endif
  28103. SSL_free(ssl);
  28104. SSL_CTX_free(ctx);
  28105. #endif /* end of ECC private key match tests */
  28106. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28107. #ifndef NO_WOLFSSL_SERVER
  28108. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28109. #else
  28110. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28111. #endif
  28112. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  28113. WOLFSSL_FILETYPE_PEM));
  28114. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  28115. WOLFSSL_FILETYPE_PEM));
  28116. AssertNotNull(ssl = SSL_new(ctx));
  28117. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28118. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28119. #endif
  28120. SSL_free(ssl);
  28121. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  28122. WOLFSSL_FILETYPE_PEM));
  28123. AssertNotNull(ssl = SSL_new(ctx));
  28124. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28125. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28126. #endif
  28127. SSL_free(ssl);
  28128. SSL_CTX_free(ctx);
  28129. #endif /* end of Ed25519 private key match tests */
  28130. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  28131. #ifndef NO_WOLFSSL_SERVER
  28132. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28133. #else
  28134. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28135. #endif
  28136. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  28137. WOLFSSL_FILETYPE_PEM));
  28138. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  28139. WOLFSSL_FILETYPE_PEM));
  28140. AssertNotNull(ssl = SSL_new(ctx));
  28141. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28142. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28143. #endif
  28144. SSL_free(ssl);
  28145. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  28146. WOLFSSL_FILETYPE_PEM));
  28147. AssertNotNull(ssl = SSL_new(ctx));
  28148. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28149. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28150. #endif
  28151. SSL_free(ssl);
  28152. SSL_CTX_free(ctx);
  28153. #endif /* end of Ed448 private key match tests */
  28154. EVP_cleanup();
  28155. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  28156. CONF_modules_free();
  28157. ENGINE_cleanup();
  28158. CONF_modules_unload();
  28159. (void)ssl;
  28160. (void)ctx;
  28161. (void)pkey;
  28162. res = TEST_RES_CHECK(1);
  28163. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28164. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28165. return res;
  28166. }
  28167. static int test_wolfSSL_PEM_read_PrivateKey(void)
  28168. {
  28169. int res = TEST_SKIPPED;
  28170. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28171. && !defined(NO_FILESYSTEM)
  28172. XFILE file;
  28173. const char* fname = "./certs/server-key.pem";
  28174. EVP_PKEY* pkey;
  28175. RSA* rsa;
  28176. WOLFSSL_EVP_PKEY_CTX* ctx;
  28177. unsigned char* sig;
  28178. size_t sigLen;
  28179. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  28180. size_t tbsLen = sizeof(tbs);
  28181. /* Check error case. */
  28182. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  28183. /* Read in an RSA key. */
  28184. file = XFOPEN(fname, "rb");
  28185. AssertTrue(file != XBADFILE);
  28186. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  28187. XFCLOSE(file);
  28188. /* Make sure the key is usable by signing some data with it. */
  28189. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  28190. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  28191. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  28192. DYNAMIC_TYPE_TMP_BUFFER));
  28193. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28194. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28195. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  28196. WOLFSSL_SUCCESS);
  28197. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28198. EVP_PKEY_CTX_free(ctx);
  28199. EVP_PKEY_free(pkey);
  28200. res = TEST_RES_CHECK(1);
  28201. #endif
  28202. return res;
  28203. }
  28204. static int test_wolfSSL_PEM_read_PUBKEY(void)
  28205. {
  28206. int res = TEST_SKIPPED;
  28207. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28208. && !defined(NO_FILESYSTEM)
  28209. XFILE file;
  28210. const char* fname = "./certs/client-keyPub.pem";
  28211. EVP_PKEY* pkey;
  28212. /* Check error case. */
  28213. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  28214. /* Read in an RSA key. */
  28215. file = XFOPEN(fname, "rb");
  28216. AssertTrue(file != XBADFILE);
  28217. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  28218. EVP_PKEY_free(pkey);
  28219. XFCLOSE(file);
  28220. res = TEST_RES_CHECK(1);
  28221. #endif
  28222. return res;
  28223. }
  28224. static int test_wolfSSL_PEM_PrivateKey(void)
  28225. {
  28226. int res = TEST_SKIPPED;
  28227. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28228. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  28229. #ifndef NO_BIO
  28230. BIO* bio = NULL;
  28231. #endif
  28232. EVP_PKEY* pkey = NULL;
  28233. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28234. #ifndef NO_BIO
  28235. /* test creating new EVP_PKEY with bad arg */
  28236. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  28237. /* test loading RSA key using BIO */
  28238. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28239. {
  28240. XFILE file;
  28241. const char* fname = "./certs/server-key.pem";
  28242. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  28243. size_t sz;
  28244. byte* buf;
  28245. EVP_PKEY* pkey2;
  28246. EVP_PKEY* pkey3;
  28247. RSA* rsa_key = NULL;
  28248. file = XFOPEN(fname, "rb");
  28249. AssertTrue((file != XBADFILE));
  28250. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28251. sz = XFTELL(file);
  28252. XREWIND(file);
  28253. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28254. if (buf) {
  28255. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28256. }
  28257. XFCLOSE(file);
  28258. /* Test using BIO new mem and loading PEM private key */
  28259. bio = BIO_new_mem_buf(buf, (int)sz);
  28260. AssertNotNull(bio);
  28261. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28262. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28263. BIO_free(bio);
  28264. bio = NULL;
  28265. AssertNotNull(pkey2 = EVP_PKEY_new());
  28266. pkey2->type = EVP_PKEY_RSA;
  28267. /* Test parameter copy */
  28268. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  28269. EVP_PKEY_free(pkey2);
  28270. EVP_PKEY_free(pkey);
  28271. pkey = NULL;
  28272. /* Qt unit test case : rsa pkcs8 key */
  28273. file = XFOPEN(fname_rsa_p8, "rb");
  28274. AssertTrue((file != XBADFILE));
  28275. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28276. sz = XFTELL(file);
  28277. XREWIND(file);
  28278. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28279. if (buf)
  28280. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28281. XFCLOSE(file);
  28282. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28283. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28284. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28285. BIO_free(bio);
  28286. bio = NULL;
  28287. AssertNotNull(pkey3 = EVP_PKEY_new());
  28288. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  28289. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  28290. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28291. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28292. #else
  28293. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28294. #endif
  28295. RSA_free(rsa_key);
  28296. EVP_PKEY_free(pkey3);
  28297. EVP_PKEY_free(pkey);
  28298. pkey = NULL;
  28299. }
  28300. #endif
  28301. /* test loading ECC key using BIO */
  28302. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28303. {
  28304. XFILE file;
  28305. const char* fname = "./certs/ecc-key.pem";
  28306. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  28307. size_t sz;
  28308. byte* buf;
  28309. EVP_PKEY* pkey2;
  28310. EVP_PKEY* pkey3;
  28311. EC_KEY* ec_key;
  28312. int nid = 0;
  28313. file = XFOPEN(fname, "rb");
  28314. AssertTrue((file != XBADFILE));
  28315. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28316. sz = XFTELL(file);
  28317. XREWIND(file);
  28318. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28319. if (buf)
  28320. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28321. XFCLOSE(file);
  28322. /* Test using BIO new mem and loading PEM private key */
  28323. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28324. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28325. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28326. BIO_free(bio);
  28327. bio = NULL;
  28328. AssertNotNull(pkey2 = EVP_PKEY_new());
  28329. AssertNotNull(pkey3 = EVP_PKEY_new());
  28330. pkey2->type = EVP_PKEY_EC;
  28331. /* Test parameter copy */
  28332. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  28333. /* Qt unit test case 1*/
  28334. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28335. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28336. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28337. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28338. #else
  28339. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28340. #endif
  28341. /* Test default digest */
  28342. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  28343. AssertIntEQ(nid, NID_sha256);
  28344. EC_KEY_free(ec_key);
  28345. EVP_PKEY_free(pkey3);
  28346. EVP_PKEY_free(pkey2);
  28347. EVP_PKEY_free(pkey);
  28348. pkey = NULL;
  28349. /* Qt unit test case ec pkcs8 key */
  28350. file = XFOPEN(fname_ecc_p8, "rb");
  28351. AssertTrue((file != XBADFILE));
  28352. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28353. sz = XFTELL(file);
  28354. XREWIND(file);
  28355. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28356. if (buf)
  28357. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28358. XFCLOSE(file);
  28359. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28360. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28361. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28362. BIO_free(bio);
  28363. bio = NULL;
  28364. AssertNotNull(pkey3 = EVP_PKEY_new());
  28365. /* Qt unit test case */
  28366. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28367. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28368. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28369. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28370. #else
  28371. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28372. #endif
  28373. EC_KEY_free(ec_key);
  28374. EVP_PKEY_free(pkey3);
  28375. EVP_PKEY_free(pkey);
  28376. pkey = NULL;
  28377. }
  28378. #endif
  28379. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  28380. defined(WOLFSSL_CERT_GEN))
  28381. {
  28382. #define BIO_PEM_TEST_CHAR 'a'
  28383. EVP_PKEY* pkey2 = NULL;
  28384. unsigned char extra[10];
  28385. int i;
  28386. BIO* pub_bio = NULL;
  28387. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  28388. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28389. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  28390. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28391. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  28392. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  28393. &server_key, (long)sizeof_server_key_der_2048));
  28394. AssertNull(pkey);
  28395. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28396. &server_key, (long)sizeof_server_key_der_2048));
  28397. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  28398. NULL), WOLFSSL_FAILURE);
  28399. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28400. NULL), WOLFSSL_FAILURE);
  28401. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  28402. NULL), WOLFSSL_SUCCESS);
  28403. AssertIntGT(BIO_pending(bio), 0);
  28404. AssertIntEQ(BIO_pending(bio), 1679);
  28405. /* Check if the pubkey API writes only the public key */
  28406. #ifdef WOLFSSL_KEY_GEN
  28407. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  28408. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  28409. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  28410. AssertIntGT(BIO_pending(pub_bio), 0);
  28411. /* Previously both the private key and the pubkey calls would write
  28412. * out the private key and the PEM header was the only difference.
  28413. * The public PEM should be significantly shorter than the
  28414. * private key versison. */
  28415. AssertIntEQ(BIO_pending(pub_bio), 451);
  28416. #endif
  28417. /* test creating new EVP_PKEY with good args */
  28418. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28419. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  28420. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  28421. /* test of reuse of EVP_PKEY */
  28422. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28423. AssertIntEQ(BIO_pending(bio), 0);
  28424. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  28425. SSL_SUCCESS);
  28426. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  28427. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28428. AssertNotNull(pkey);
  28429. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  28430. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  28431. }
  28432. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  28433. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  28434. for (i = 0; i < 10; i++) {
  28435. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  28436. }
  28437. BIO_free(pub_bio);
  28438. BIO_free(bio);
  28439. bio = NULL;
  28440. EVP_PKEY_free(pkey);
  28441. pkey = NULL;
  28442. EVP_PKEY_free(pkey2);
  28443. }
  28444. #endif
  28445. /* key is DES encrypted */
  28446. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  28447. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  28448. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  28449. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  28450. {
  28451. XFILE f;
  28452. wc_pem_password_cb* passwd_cb;
  28453. void* passwd_cb_userdata;
  28454. SSL_CTX* ctx;
  28455. char passwd[] = "bad password";
  28456. #ifndef WOLFSSL_NO_TLS12
  28457. #ifndef NO_WOLFSSL_SERVER
  28458. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28459. #else
  28460. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28461. #endif
  28462. #else
  28463. #ifndef NO_WOLFSSL_SERVER
  28464. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28465. #else
  28466. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28467. #endif
  28468. #endif
  28469. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28470. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  28471. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  28472. AssertNull(passwd_cb_userdata =
  28473. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28474. /* fail case with password call back */
  28475. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  28476. (void*)passwd));
  28477. BIO_free(bio);
  28478. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28479. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28480. (void*)passwd));
  28481. BIO_free(bio);
  28482. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  28483. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  28484. /* use callback that works */
  28485. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28486. (void*)"yassl123"));
  28487. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28488. EVP_PKEY_free(pkey);
  28489. pkey = NULL;
  28490. BIO_free(bio);
  28491. bio = NULL;
  28492. SSL_CTX_free(ctx);
  28493. }
  28494. #endif /* !defined(NO_DES3) */
  28495. #endif /* !NO_BIO */
  28496. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28497. {
  28498. unsigned char buf[2048];
  28499. size_t bytes;
  28500. XFILE f;
  28501. SSL_CTX* ctx;
  28502. #ifndef WOLFSSL_NO_TLS12
  28503. #ifndef NO_WOLFSSL_SERVER
  28504. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28505. #else
  28506. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28507. #endif
  28508. #else
  28509. #ifndef NO_WOLFSSL_SERVER
  28510. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28511. #else
  28512. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28513. #endif
  28514. #endif
  28515. f = XFOPEN("./certs/ecc-key.der", "rb");
  28516. AssertTrue((f != XBADFILE));
  28517. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  28518. XFCLOSE(f);
  28519. server_key = buf;
  28520. pkey = NULL;
  28521. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  28522. AssertNull(pkey);
  28523. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  28524. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28525. EVP_PKEY_free(pkey);
  28526. pkey = NULL;
  28527. SSL_CTX_free(ctx);
  28528. }
  28529. #endif
  28530. res = TEST_RES_CHECK(1);
  28531. #ifndef NO_BIO
  28532. (void)bio;
  28533. #endif
  28534. (void)pkey;
  28535. (void)server_key;
  28536. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  28537. return res;
  28538. }
  28539. static int test_wolfSSL_PEM_file_RSAKey(void)
  28540. {
  28541. int res = TEST_SKIPPED;
  28542. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28543. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28544. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28545. RSA* rsa = NULL;
  28546. XFILE fp;
  28547. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  28548. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  28549. XFCLOSE(fp);
  28550. AssertIntEQ(RSA_size(rsa), 256);
  28551. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  28552. AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  28553. AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  28554. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  28555. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28556. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  28557. RSA_free(rsa);
  28558. res = TEST_RES_CHECK(1);
  28559. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28560. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28561. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28562. return res;
  28563. }
  28564. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  28565. {
  28566. int res = TEST_SKIPPED;
  28567. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  28568. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  28569. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  28570. RSA* rsa = NULL;
  28571. XFILE f = NULL;
  28572. f = XFOPEN(svrKeyFile, "r");
  28573. AssertTrue((f != XBADFILE));
  28574. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28575. AssertIntEQ(RSA_size(rsa), 256);
  28576. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  28577. NULL), WOLFSSL_FAILURE);
  28578. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  28579. NULL), WOLFSSL_FAILURE);
  28580. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  28581. WOLFSSL_SUCCESS);
  28582. RSA_free(rsa);
  28583. XFCLOSE(f);
  28584. #ifdef HAVE_ECC
  28585. f = XFOPEN(eccKeyFile, "r");
  28586. AssertTrue((f != XBADFILE));
  28587. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28588. XFCLOSE(f);
  28589. #endif /* HAVE_ECC */
  28590. res = TEST_RES_CHECK(1);
  28591. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28592. return res;
  28593. }
  28594. #ifndef NO_BIO
  28595. static int test_wolfSSL_PEM_bio_RSAKey(void)
  28596. {
  28597. int res = TEST_SKIPPED;
  28598. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28599. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28600. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28601. RSA* rsa = NULL;
  28602. BIO* bio = NULL;
  28603. /* PrivateKey */
  28604. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28605. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  28606. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  28607. AssertNotNull(rsa);
  28608. AssertIntEQ(RSA_size(rsa), 256);
  28609. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28610. NULL), WOLFSSL_FAILURE);
  28611. BIO_free(bio);
  28612. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28613. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  28614. NULL), WOLFSSL_SUCCESS);
  28615. BIO_free(bio);
  28616. RSA_free(rsa);
  28617. /* PUBKEY */
  28618. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  28619. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28620. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28621. AssertIntEQ(RSA_size(rsa), 256);
  28622. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28623. BIO_free(bio);
  28624. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28625. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  28626. BIO_free(bio);
  28627. RSA_free(rsa);
  28628. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  28629. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  28630. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28631. BIO_free(bio);
  28632. RSA_free(rsa);
  28633. #ifdef HAVE_ECC
  28634. /* ensure that non-rsa keys do not work */
  28635. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28636. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28637. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28638. BIO_free(bio);
  28639. RSA_free(rsa);
  28640. #endif /* HAVE_ECC */
  28641. res = TEST_RES_CHECK(1);
  28642. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28643. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28644. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28645. return res;
  28646. }
  28647. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  28648. {
  28649. int res = TEST_SKIPPED;
  28650. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28651. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28652. RSA* rsa = NULL;
  28653. RSA* rsa_dup = NULL;
  28654. BIO* bio = NULL;
  28655. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28656. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28657. AssertIntEQ(RSA_size(rsa), 256);
  28658. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  28659. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  28660. /* Test duplicating empty key. */
  28661. rsa_dup = RSA_new();
  28662. AssertNull(RSAPublicKey_dup(rsa_dup));
  28663. RSA_free(rsa_dup);
  28664. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28665. AssertPtrNE(rsa_dup, rsa);
  28666. #endif
  28667. /* test if valgrind complains about unreleased memory */
  28668. RSA_up_ref(rsa);
  28669. RSA_free(rsa);
  28670. BIO_free(bio);
  28671. RSA_free(rsa);
  28672. RSA_free(rsa_dup);
  28673. #ifdef HAVE_ECC
  28674. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28675. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28676. BIO_free(bio);
  28677. #endif /* HAVE_ECC */
  28678. res = TEST_RES_CHECK(1);
  28679. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28680. return res;
  28681. }
  28682. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  28683. {
  28684. int res = TEST_SKIPPED;
  28685. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28686. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28687. XFILE file;
  28688. const char* fname = "./certs/client-keyPub.pem";
  28689. RSA *rsa;
  28690. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  28691. file = XFOPEN(fname, "rb");
  28692. AssertTrue((file != XBADFILE));
  28693. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  28694. AssertIntEQ(RSA_size(rsa), 256);
  28695. RSA_free(rsa);
  28696. XFCLOSE(file);
  28697. res = TEST_RES_CHECK(1);
  28698. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28699. return res;
  28700. }
  28701. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28702. {
  28703. int res = TEST_SKIPPED;
  28704. #ifndef HAVE_SELFTEST
  28705. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28706. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28707. DSA* dsa = NULL;
  28708. BIO* bio = NULL;
  28709. /* PrivateKey */
  28710. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28711. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28712. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28713. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28714. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  28715. WOLFSSL_FAILURE);
  28716. BIO_free(bio);
  28717. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28718. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  28719. WOLFSSL_SUCCESS);
  28720. BIO_free(bio);
  28721. DSA_free(dsa);
  28722. /* PUBKEY */
  28723. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28724. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28725. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28726. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28727. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28728. BIO_free(bio);
  28729. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28730. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28731. BIO_free(bio);
  28732. DSA_free(dsa);
  28733. #ifdef HAVE_ECC
  28734. /* ensure that non-dsa keys do not work */
  28735. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28736. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28737. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28738. BIO_free(bio);
  28739. DSA_free(dsa);
  28740. #endif /* HAVE_ECC */
  28741. res = TEST_RES_CHECK(1);
  28742. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28743. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28744. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28745. #endif /* HAVE_SELFTEST */
  28746. return res;
  28747. }
  28748. static int test_wolfSSL_PEM_bio_ECKey(void)
  28749. {
  28750. int res = TEST_SKIPPED;
  28751. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28752. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28753. EC_KEY* ec = NULL;
  28754. BIO* bio = NULL;
  28755. /* PrivateKey */
  28756. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28757. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28758. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28759. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28760. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28761. NULL),WOLFSSL_FAILURE);
  28762. BIO_free(bio);
  28763. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28764. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28765. NULL), WOLFSSL_SUCCESS);
  28766. BIO_free(bio);
  28767. EC_KEY_free(ec);
  28768. /* PUBKEY */
  28769. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28770. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28771. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28772. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28773. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28774. BIO_free(bio);
  28775. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28776. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28777. BIO_free(bio);
  28778. /* Same test as above, but with a file pointer rather than a BIO. */
  28779. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28780. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28781. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  28782. EC_KEY_free(ec);
  28783. #ifndef NO_RSA
  28784. /* ensure that non-ec keys do not work */
  28785. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28786. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28787. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28788. BIO_free(bio);
  28789. EC_KEY_free(ec);
  28790. #endif /* HAVE_ECC */
  28791. res = TEST_RES_CHECK(1);
  28792. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28793. return res;
  28794. }
  28795. static int test_wolfSSL_PEM_PUBKEY(void)
  28796. {
  28797. int res = TEST_SKIPPED;
  28798. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28799. BIO* bio = NULL;
  28800. EVP_PKEY* pkey = NULL;
  28801. /* test creating new EVP_PKEY with bad arg */
  28802. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28803. /* test loading ECC key using BIO */
  28804. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28805. {
  28806. XFILE file;
  28807. const char* fname = "./certs/ecc-client-keyPub.pem";
  28808. size_t sz;
  28809. byte* buf;
  28810. EVP_PKEY* pkey2;
  28811. EC_KEY* ec_key;
  28812. file = XFOPEN(fname, "rb");
  28813. AssertTrue((file != XBADFILE));
  28814. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  28815. sz = XFTELL(file);
  28816. XREWIND(file);
  28817. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28818. if (buf)
  28819. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28820. XFCLOSE(file);
  28821. /* Test using BIO new mem and loading PEM private key */
  28822. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28823. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28824. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28825. BIO_free(bio);
  28826. bio = NULL;
  28827. /* Qt unit test case*/
  28828. AssertNotNull(pkey2 = EVP_PKEY_new());
  28829. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28830. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28831. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28832. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28833. #else
  28834. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28835. #endif
  28836. EC_KEY_free(ec_key);
  28837. EVP_PKEY_free(pkey2);
  28838. EVP_PKEY_free(pkey);
  28839. pkey = NULL;
  28840. }
  28841. #endif
  28842. (void)bio;
  28843. (void)pkey;
  28844. res = TEST_RES_CHECK(1);
  28845. #endif
  28846. return res;
  28847. }
  28848. #endif /* !NO_BIO */
  28849. static int test_DSA_do_sign_verify(void)
  28850. {
  28851. int res = TEST_SKIPPED;
  28852. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28853. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28854. !defined(NO_DSA)
  28855. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28856. DSA_SIG* sig;
  28857. DSA* dsa;
  28858. word32 bytes;
  28859. byte sigBin[DSA_SIG_SIZE];
  28860. int dsacheck;
  28861. #ifdef USE_CERT_BUFFERS_1024
  28862. byte tmp[ONEK_BUF];
  28863. XMEMSET(tmp, 0, sizeof(tmp));
  28864. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28865. bytes = sizeof_dsa_key_der_1024;
  28866. #elif defined(USE_CERT_BUFFERS_2048)
  28867. byte tmp[TWOK_BUF];
  28868. XMEMSET(tmp, 0, sizeof(tmp));
  28869. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28870. bytes = sizeof_dsa_key_der_2048;
  28871. #else
  28872. byte tmp[TWOK_BUF];
  28873. XMEMSET(tmp, 0, sizeof(tmp));
  28874. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  28875. if (fp == XBADFILE) {
  28876. return WOLFSSL_BAD_FILE;
  28877. }
  28878. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  28879. XFCLOSE(fp);
  28880. #endif /* END USE_CERT_BUFFERS_1024 */
  28881. XMEMSET(digest, 202, sizeof(digest));
  28882. AssertNotNull(dsa = DSA_new());
  28883. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28884. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28885. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28886. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28887. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28888. DSA_SIG_free(sig);
  28889. DSA_free(dsa);
  28890. res = TEST_RES_CHECK(1);
  28891. #endif
  28892. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28893. return res;
  28894. }
  28895. static int test_wolfSSL_tmp_dh(void)
  28896. {
  28897. int res = TEST_SKIPPED;
  28898. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28899. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28900. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28901. byte buff[6000];
  28902. char file[] = "./certs/dsaparams.pem";
  28903. XFILE f;
  28904. int bytes;
  28905. DSA* dsa;
  28906. DH* dh;
  28907. #if defined(WOLFSSL_DH_EXTRA) && \
  28908. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28909. DH* dh2;
  28910. #endif
  28911. BIO* bio;
  28912. SSL* ssl;
  28913. SSL_CTX* ctx;
  28914. #ifndef NO_WOLFSSL_SERVER
  28915. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28916. #else
  28917. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28918. #endif
  28919. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28920. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28921. AssertNotNull(ssl = SSL_new(ctx));
  28922. f = XFOPEN(file, "rb");
  28923. AssertTrue((f != XBADFILE));
  28924. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  28925. XFCLOSE(f);
  28926. bio = BIO_new_mem_buf((void*)buff, bytes);
  28927. AssertNotNull(bio);
  28928. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28929. AssertNotNull(dsa);
  28930. dh = wolfSSL_DSA_dup_DH(dsa);
  28931. AssertNotNull(dh);
  28932. #if defined(WOLFSSL_DH_EXTRA) && \
  28933. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28934. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  28935. #endif
  28936. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28937. #ifndef NO_WOLFSSL_SERVER
  28938. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28939. #else
  28940. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28941. #endif
  28942. BIO_free(bio);
  28943. DSA_free(dsa);
  28944. DH_free(dh);
  28945. #if defined(WOLFSSL_DH_EXTRA) && \
  28946. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28947. DH_free(dh2);
  28948. #endif
  28949. SSL_free(ssl);
  28950. SSL_CTX_free(ctx);
  28951. res = TEST_RES_CHECK(1);
  28952. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28953. #endif
  28954. return res;
  28955. }
  28956. static int test_wolfSSL_ctrl(void)
  28957. {
  28958. int res = TEST_SKIPPED;
  28959. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28960. byte buff[6000];
  28961. BIO* bio;
  28962. int bytes;
  28963. BUF_MEM* ptr = NULL;
  28964. XMEMSET(buff, 0, sizeof(buff));
  28965. bytes = sizeof(buff);
  28966. bio = BIO_new_mem_buf((void*)buff, bytes);
  28967. AssertNotNull(bio);
  28968. AssertNotNull(BIO_s_socket());
  28969. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28970. /* needs tested after stubs filled out @TODO
  28971. SSL_ctrl
  28972. SSL_CTX_ctrl
  28973. */
  28974. BIO_free(bio);
  28975. res = TEST_RES_CHECK(1);
  28976. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28977. return res;
  28978. }
  28979. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28980. {
  28981. int res = TEST_SKIPPED;
  28982. #ifdef OPENSSL_EXTRA
  28983. static const unsigned char pw[] = "password";
  28984. static const int pwSz = sizeof(pw) - 1;
  28985. size_t checkPwSz = 0;
  28986. const unsigned char* checkPw = NULL;
  28987. WOLFSSL_EVP_PKEY* key = NULL;
  28988. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28989. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28990. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  28991. if (key) {
  28992. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  28993. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  28994. AssertIntEQ(key->pkey_sz, pwSz);
  28995. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28996. }
  28997. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28998. AssertIntEQ((int)checkPwSz, pwSz);
  28999. if (checkPw) {
  29000. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  29001. }
  29002. wolfSSL_EVP_PKEY_free(key);
  29003. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  29004. if (key) {
  29005. AssertIntEQ(key->pkey_sz, 0);
  29006. }
  29007. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29008. (void)checkPw;
  29009. AssertIntEQ((int)checkPwSz, 0);
  29010. wolfSSL_EVP_PKEY_free(key);
  29011. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  29012. if (key) {
  29013. AssertIntEQ(key->pkey_sz, 0);
  29014. }
  29015. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  29016. (void)checkPw;
  29017. AssertIntEQ((int)checkPwSz, 0);
  29018. wolfSSL_EVP_PKEY_free(key);
  29019. res = TEST_RES_CHECK(1);
  29020. #endif /* OPENSSL_EXTRA */
  29021. return res;
  29022. }
  29023. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  29024. {
  29025. int res = TEST_SKIPPED;
  29026. #ifdef OPENSSL_EXTRA
  29027. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  29028. const char *priv = "ABCDEFGHIJKLMNOP";
  29029. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  29030. WOLFSSL_EVP_PKEY* key = NULL;
  29031. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29032. NULL, NULL, AES_128_KEY_SIZE, cipher));
  29033. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29034. NULL, (const unsigned char *)priv, 0, cipher));
  29035. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29036. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  29037. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  29038. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  29039. wolfSSL_EVP_PKEY_free(key);
  29040. res = TEST_RES_CHECK(1);
  29041. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  29042. #endif /* OPENSSL_EXTRA */
  29043. return res;
  29044. }
  29045. static int test_wolfSSL_EVP_Digest(void)
  29046. {
  29047. int res = TEST_SKIPPED;
  29048. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29049. const char* in = "abc";
  29050. int inLen = (int)XSTRLEN(in);
  29051. byte out[WC_SHA256_DIGEST_SIZE];
  29052. unsigned int outLen;
  29053. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  29054. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  29055. "\x15\xAD";
  29056. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  29057. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  29058. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  29059. res = TEST_RES_CHECK(1);
  29060. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  29061. return res;
  29062. }
  29063. static int test_wolfSSL_EVP_Digest_all(void)
  29064. {
  29065. int res = TEST_SKIPPED;
  29066. #ifdef OPENSSL_EXTRA
  29067. const char* digests[] = {
  29068. #ifndef NO_MD5
  29069. "MD5",
  29070. #endif
  29071. #ifndef NO_SHA
  29072. "SHA",
  29073. #endif
  29074. #ifdef WOLFSSL_SHA224
  29075. "SHA224",
  29076. #endif
  29077. #ifndef NO_SHA256
  29078. "SHA256",
  29079. #endif
  29080. #ifdef WOLFSSL_SHA384
  29081. "SHA384",
  29082. #endif
  29083. #ifdef WOLFSSL_SHA512
  29084. "SHA512",
  29085. #endif
  29086. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  29087. "SHA512_224",
  29088. #endif
  29089. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  29090. "SHA512_256",
  29091. #endif
  29092. #ifdef WOLFSSL_SHA3
  29093. #ifndef WOLFSSL_NOSHA3_224
  29094. "SHA3_224",
  29095. #endif
  29096. #ifndef WOLFSSL_NOSHA3_256
  29097. "SHA3_256",
  29098. #endif
  29099. "SHA3_384",
  29100. #ifndef WOLFSSL_NOSHA3_512
  29101. "SHA3_512",
  29102. #endif
  29103. #endif /* WOLFSSL_SHA3 */
  29104. NULL
  29105. };
  29106. const char** d;
  29107. const unsigned char in[] = "abc";
  29108. int inLen = XSTR_SIZEOF(in);
  29109. byte out[WC_MAX_DIGEST_SIZE];
  29110. unsigned int outLen;
  29111. for (d = digests; *d != NULL; d++) {
  29112. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  29113. AssertIntGT(outLen, 0);
  29114. AssertIntEQ(EVP_MD_size(*d), outLen);
  29115. }
  29116. res = TEST_RES_CHECK(1);
  29117. #endif
  29118. return res;
  29119. }
  29120. static int test_wolfSSL_EVP_MD_size(void)
  29121. {
  29122. int res = TEST_SKIPPED;
  29123. #ifdef OPENSSL_EXTRA
  29124. WOLFSSL_EVP_MD_CTX mdCtx;
  29125. #ifdef WOLFSSL_SHA3
  29126. #ifndef WOLFSSL_NOSHA3_224
  29127. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29128. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  29129. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  29130. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  29131. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29132. #endif
  29133. #ifndef WOLFSSL_NOSHA3_256
  29134. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29135. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  29136. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  29137. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  29138. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29139. #endif
  29140. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29141. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  29142. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  29143. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  29144. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29145. #ifndef WOLFSSL_NOSHA3_512
  29146. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29147. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  29148. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  29149. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  29150. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29151. #endif
  29152. #endif /* WOLFSSL_SHA3 */
  29153. #ifndef NO_SHA256
  29154. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29155. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  29156. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  29157. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  29158. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  29159. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  29160. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29161. #endif
  29162. #ifndef NO_MD5
  29163. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29164. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  29165. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  29166. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  29167. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  29168. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  29169. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29170. #endif
  29171. #ifdef WOLFSSL_SHA224
  29172. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29173. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  29174. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  29175. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  29176. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  29177. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  29178. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29179. #endif
  29180. #ifdef WOLFSSL_SHA384
  29181. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29182. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  29183. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  29184. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  29185. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  29186. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  29187. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29188. #endif
  29189. #ifdef WOLFSSL_SHA512
  29190. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29191. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  29192. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  29193. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  29194. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  29195. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  29196. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29197. #endif
  29198. #ifndef NO_SHA
  29199. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29200. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  29201. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29202. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29203. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29204. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29205. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29206. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29207. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  29208. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29209. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29210. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29211. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29212. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29213. #endif
  29214. /* error case */
  29215. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29216. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  29217. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  29218. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  29219. /* Cleanup is valid on uninit'ed struct */
  29220. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29221. res = TEST_RES_CHECK(1);
  29222. #endif /* OPENSSL_EXTRA */
  29223. return res;
  29224. }
  29225. static int test_wolfSSL_EVP_MD_pkey_type(void)
  29226. {
  29227. int res = TEST_SKIPPED;
  29228. #ifdef OPENSSL_EXTRA
  29229. const WOLFSSL_EVP_MD* md;
  29230. #ifndef NO_MD5
  29231. AssertNotNull(md = EVP_md5());
  29232. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  29233. #endif
  29234. #ifndef NO_SHA
  29235. AssertNotNull(md = EVP_sha1());
  29236. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  29237. #endif
  29238. #ifdef WOLFSSL_SHA224
  29239. AssertNotNull(md = EVP_sha224());
  29240. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  29241. #endif
  29242. AssertNotNull(md = EVP_sha256());
  29243. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  29244. #ifdef WOLFSSL_SHA384
  29245. AssertNotNull(md = EVP_sha384());
  29246. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  29247. #endif
  29248. #ifdef WOLFSSL_SHA512
  29249. AssertNotNull(md = EVP_sha512());
  29250. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  29251. #endif
  29252. res = TEST_RES_CHECK(1);
  29253. #endif
  29254. return res;
  29255. }
  29256. #ifdef OPENSSL_EXTRA
  29257. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  29258. size_t testKeySz, const char* testData, size_t testDataSz,
  29259. const byte* testResult, size_t testResultSz)
  29260. {
  29261. unsigned char check[WC_MAX_DIGEST_SIZE];
  29262. size_t checkSz = -1;
  29263. WOLFSSL_EVP_PKEY* key;
  29264. WOLFSSL_EVP_MD_CTX mdCtx;
  29265. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  29266. testKey, (int)testKeySz));
  29267. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29268. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29269. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29270. (unsigned int)testDataSz), 1);
  29271. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29272. AssertIntEQ((int)checkSz, (int)testResultSz);
  29273. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29274. AssertIntEQ((int)checkSz,(int)testResultSz);
  29275. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29276. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29277. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29278. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29279. (unsigned int)testDataSz), 1);
  29280. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29281. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29282. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29283. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29284. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29285. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29286. AssertIntEQ((int)checkSz, (int)testResultSz);
  29287. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29288. AssertIntEQ((int)checkSz,(int)testResultSz);
  29289. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29290. (unsigned int)testDataSz - 4), 1);
  29291. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29292. AssertIntEQ((int)checkSz,(int)testResultSz);
  29293. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29294. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29295. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29296. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29297. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29298. (unsigned int)testDataSz - 4), 1);
  29299. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29300. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29301. wolfSSL_EVP_PKEY_free(key);
  29302. }
  29303. #endif
  29304. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  29305. {
  29306. int res = TEST_SKIPPED;
  29307. #ifdef OPENSSL_EXTRA
  29308. static const unsigned char testKey[] =
  29309. {
  29310. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29311. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29312. 0x0b, 0x0b, 0x0b, 0x0b
  29313. };
  29314. static const char testData[] = "Hi There";
  29315. #ifdef WOLFSSL_SHA224
  29316. static const unsigned char testResultSha224[] =
  29317. {
  29318. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  29319. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  29320. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  29321. 0x53, 0x68, 0x4b, 0x22
  29322. };
  29323. #endif
  29324. #ifndef NO_SHA256
  29325. static const unsigned char testResultSha256[] =
  29326. {
  29327. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  29328. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  29329. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  29330. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  29331. };
  29332. #endif
  29333. #ifdef WOLFSSL_SHA384
  29334. static const unsigned char testResultSha384[] =
  29335. {
  29336. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  29337. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  29338. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  29339. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  29340. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  29341. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  29342. };
  29343. #endif
  29344. #ifdef WOLFSSL_SHA512
  29345. static const unsigned char testResultSha512[] =
  29346. {
  29347. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  29348. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  29349. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  29350. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  29351. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  29352. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  29353. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  29354. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  29355. };
  29356. #endif
  29357. #ifdef WOLFSSL_SHA3
  29358. #ifndef WOLFSSL_NOSHA3_224
  29359. static const unsigned char testResultSha3_224[] =
  29360. {
  29361. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  29362. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  29363. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  29364. 0xf3, 0xc8, 0x60, 0xf7
  29365. };
  29366. #endif
  29367. #ifndef WOLFSSL_NOSHA3_256
  29368. static const unsigned char testResultSha3_256[] =
  29369. {
  29370. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  29371. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  29372. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  29373. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  29374. };
  29375. #endif
  29376. #ifndef WOLFSSL_NOSHA3_384
  29377. static const unsigned char testResultSha3_384[] =
  29378. {
  29379. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  29380. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  29381. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  29382. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  29383. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  29384. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  29385. };
  29386. #endif
  29387. #ifndef WOLFSSL_NOSHA3_512
  29388. static const unsigned char testResultSha3_512[] =
  29389. {
  29390. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  29391. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  29392. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  29393. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  29394. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  29395. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  29396. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  29397. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  29398. };
  29399. #endif
  29400. #endif
  29401. #ifndef NO_SHA256
  29402. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  29403. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  29404. #endif
  29405. #ifdef WOLFSSL_SHA224
  29406. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  29407. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  29408. #endif
  29409. #ifdef WOLFSSL_SHA384
  29410. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  29411. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  29412. #endif
  29413. #ifdef WOLFSSL_SHA512
  29414. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  29415. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  29416. #endif
  29417. #ifdef WOLFSSL_SHA3
  29418. #ifndef WOLFSSL_NOSHA3_224
  29419. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  29420. testData, XSTRLEN(testData), testResultSha3_224,
  29421. sizeof(testResultSha3_224));
  29422. #endif
  29423. #ifndef WOLFSSL_NOSHA3_256
  29424. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  29425. testData, XSTRLEN(testData), testResultSha3_256,
  29426. sizeof(testResultSha3_256));
  29427. #endif
  29428. #ifndef WOLFSSL_NOSHA3_384
  29429. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  29430. testData, XSTRLEN(testData), testResultSha3_384,
  29431. sizeof(testResultSha3_384));
  29432. #endif
  29433. #ifndef WOLFSSL_NOSHA3_512
  29434. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  29435. testData, XSTRLEN(testData), testResultSha3_512,
  29436. sizeof(testResultSha3_512));
  29437. #endif
  29438. #endif
  29439. res = TEST_RES_CHECK(1);
  29440. #endif /* OPENSSL_EXTRA */
  29441. return res;
  29442. }
  29443. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  29444. {
  29445. int res = TEST_SKIPPED;
  29446. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  29447. defined(USE_CERT_BUFFERS_2048)
  29448. WOLFSSL_EVP_PKEY* privKey;
  29449. WOLFSSL_EVP_PKEY* pubKey;
  29450. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  29451. const char testData[] = "Hi There";
  29452. WOLFSSL_EVP_MD_CTX mdCtx;
  29453. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  29454. size_t checkSz = -1;
  29455. int sz = 2048 / 8;
  29456. const unsigned char* cp;
  29457. const unsigned char* p;
  29458. unsigned char check[2048/8];
  29459. size_t i;
  29460. int paddings[] = {
  29461. RSA_PKCS1_PADDING,
  29462. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  29463. RSA_PKCS1_PSS_PADDING,
  29464. #endif
  29465. };
  29466. cp = client_key_der_2048;
  29467. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  29468. sizeof_client_key_der_2048)));
  29469. p = client_keypub_der_2048;
  29470. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29471. sizeof_client_keypub_der_2048)));
  29472. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29473. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  29474. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29475. NULL, privKey), 1);
  29476. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29477. (unsigned int)XSTRLEN(testData)), 1);
  29478. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29479. AssertIntEQ((int)checkSz, sz);
  29480. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29481. AssertIntEQ((int)checkSz,sz);
  29482. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29483. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29484. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  29485. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29486. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29487. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29488. NULL, pubKey), 1);
  29489. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29490. (unsigned int)XSTRLEN(testData)),
  29491. 1);
  29492. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29493. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29494. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29495. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29496. NULL, privKey), 1);
  29497. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29498. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29499. AssertIntEQ((int)checkSz, sz);
  29500. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29501. AssertIntEQ((int)checkSz, sz);
  29502. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29503. (unsigned int)XSTRLEN(testData) - 4), 1);
  29504. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29505. AssertIntEQ((int)checkSz, sz);
  29506. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29507. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29508. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29509. NULL, pubKey), 1);
  29510. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29511. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29512. (unsigned int)XSTRLEN(testData) - 4),
  29513. 1);
  29514. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29515. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29516. /* Check all signing padding types */
  29517. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  29518. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29519. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  29520. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  29521. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29522. paddings[i]), 1);
  29523. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29524. (unsigned int)XSTRLEN(testData)), 1);
  29525. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29526. AssertIntEQ((int)checkSz, sz);
  29527. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29528. AssertIntEQ((int)checkSz,sz);
  29529. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29530. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29531. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  29532. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  29533. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29534. paddings[i]), 1);
  29535. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29536. (unsigned int)XSTRLEN(testData)), 1);
  29537. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29538. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29539. }
  29540. wolfSSL_EVP_PKEY_free(pubKey);
  29541. wolfSSL_EVP_PKEY_free(privKey);
  29542. res = TEST_RES_CHECK(1);
  29543. #endif
  29544. return res;
  29545. }
  29546. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  29547. {
  29548. int res = TEST_SKIPPED;
  29549. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29550. WOLFSSL_EVP_PKEY* privKey;
  29551. WOLFSSL_EVP_PKEY* pubKey;
  29552. const char testData[] = "Hi There";
  29553. WOLFSSL_EVP_MD_CTX mdCtx;
  29554. size_t checkSz = -1;
  29555. const unsigned char* cp;
  29556. const unsigned char* p;
  29557. unsigned char check[2048/8];
  29558. cp = ecc_clikey_der_256;
  29559. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  29560. sizeof_ecc_clikey_der_256);
  29561. AssertNotNull(privKey);
  29562. p = ecc_clikeypub_der_256;
  29563. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29564. sizeof_ecc_clikeypub_der_256)));
  29565. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29566. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29567. NULL, privKey), 1);
  29568. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29569. (unsigned int)XSTRLEN(testData)), 1);
  29570. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29571. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29572. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29573. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29574. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29575. NULL, pubKey), 1);
  29576. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29577. (unsigned int)XSTRLEN(testData)),
  29578. 1);
  29579. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29580. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29581. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29582. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29583. NULL, privKey), 1);
  29584. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29585. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29586. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29587. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29588. (unsigned int)XSTRLEN(testData) - 4), 1);
  29589. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29590. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29591. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29592. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29593. NULL, pubKey), 1);
  29594. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29595. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29596. (unsigned int)XSTRLEN(testData) - 4),
  29597. 1);
  29598. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29599. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29600. wolfSSL_EVP_PKEY_free(pubKey);
  29601. wolfSSL_EVP_PKEY_free(privKey);
  29602. res = TEST_RES_CHECK(1);
  29603. #endif
  29604. return res;
  29605. }
  29606. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29607. {
  29608. int res = TEST_SKIPPED;
  29609. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29610. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29611. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29612. char caFile[] = "./certs/client-ca.pem";
  29613. char clientFile[] = "./certs/client-cert.pem";
  29614. SSL_CTX* ctx;
  29615. X509* x509;
  29616. BIO *bio = NULL;
  29617. X509 *cert = NULL;
  29618. X509 *ca;
  29619. STACK_OF(X509) *chain = NULL;
  29620. STACK_OF(X509) *chain2 = NULL;
  29621. #ifndef NO_WOLFSSL_SERVER
  29622. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29623. #else
  29624. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29625. #endif
  29626. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  29627. AssertNotNull(x509);
  29628. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29629. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  29630. AssertNotNull(x509);
  29631. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29632. /* additional test of getting EVP_PKEY key size from X509
  29633. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29634. * allowed with user RSA */
  29635. {
  29636. EVP_PKEY* pkey;
  29637. #if defined(HAVE_ECC)
  29638. X509* ecX509;
  29639. #endif /* HAVE_ECC */
  29640. AssertNotNull(pkey = X509_get_pubkey(x509));
  29641. /* current RSA key is 2048 bit (256 bytes) */
  29642. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  29643. EVP_PKEY_free(pkey);
  29644. #if defined(HAVE_ECC)
  29645. #if defined(USE_CERT_BUFFERS_256)
  29646. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29647. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29648. SSL_FILETYPE_ASN1));
  29649. #else
  29650. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  29651. SSL_FILETYPE_PEM));
  29652. #endif
  29653. pkey = X509_get_pubkey(ecX509);
  29654. AssertNotNull(pkey);
  29655. /* current ECC key is 256 bit (32 bytes) */
  29656. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  29657. X509_free(ecX509);
  29658. EVP_PKEY_free(pkey);
  29659. #endif /* HAVE_ECC */
  29660. }
  29661. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29662. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29663. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29664. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  29665. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29666. #endif
  29667. SSL_CTX_free(ctx);
  29668. #ifndef NO_WOLFSSL_SERVER
  29669. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29670. #else
  29671. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29672. #endif
  29673. /* Test haproxy use case */
  29674. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29675. /* Read Certificate */
  29676. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29677. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29678. AssertNotNull(chain = sk_X509_new_null());
  29679. AssertIntEQ(sk_X509_push(chain, ca), 1);
  29680. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  29681. AssertNotNull(ca = sk_X509_shift(chain2));
  29682. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29683. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29684. BIO_free(bio);
  29685. X509_free(cert);
  29686. sk_X509_pop_free(chain, X509_free);
  29687. sk_X509_pop_free(chain2, X509_free);
  29688. SSL_CTX_free(ctx);
  29689. res = TEST_RES_CHECK(1);
  29690. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29691. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29692. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29693. return res;
  29694. }
  29695. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29696. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29697. {
  29698. int res = TEST_SKIPPED;
  29699. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29700. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29701. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29702. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29703. tcp_ready ready;
  29704. func_args client_args;
  29705. func_args server_args;
  29706. #ifndef SINGLE_THREADED
  29707. THREAD_TYPE serverThread;
  29708. #endif
  29709. callback_functions client_cb;
  29710. callback_functions server_cb;
  29711. int line = 0;
  29712. int flag = ERR_TXT_STRING;
  29713. const char* file = NULL;
  29714. const char* data = NULL;
  29715. /* create a failed connection and inspect the error */
  29716. #ifdef WOLFSSL_TIRTOS
  29717. fdOpenSession(Task_self());
  29718. #endif
  29719. XMEMSET(&client_args, 0, sizeof(func_args));
  29720. XMEMSET(&server_args, 0, sizeof(func_args));
  29721. StartTCP();
  29722. InitTcpReady(&ready);
  29723. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29724. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29725. client_cb.method = wolfTLSv1_1_client_method;
  29726. server_cb.method = wolfTLSv1_2_server_method;
  29727. server_args.signal = &ready;
  29728. server_args.callbacks = &server_cb;
  29729. client_args.signal = &ready;
  29730. client_args.callbacks = &client_cb;
  29731. #ifndef SINGLE_THREADED
  29732. start_thread(test_server_nofail, &server_args, &serverThread);
  29733. wait_tcp_ready(&server_args);
  29734. test_client_nofail(&client_args, NULL);
  29735. join_thread(serverThread);
  29736. #endif
  29737. FreeTcpReady(&ready);
  29738. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29739. AssertNotNull(data);
  29740. /* check clearing error state */
  29741. ERR_remove_state(0);
  29742. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29743. ERR_peek_last_error_line(NULL, &line);
  29744. AssertIntEQ(line, 0);
  29745. ERR_peek_last_error_line(&file, NULL);
  29746. AssertNull(file);
  29747. /* retry connection to fill error queue */
  29748. XMEMSET(&client_args, 0, sizeof(func_args));
  29749. XMEMSET(&server_args, 0, sizeof(func_args));
  29750. StartTCP();
  29751. InitTcpReady(&ready);
  29752. client_cb.method = wolfTLSv1_1_client_method;
  29753. server_cb.method = wolfTLSv1_2_server_method;
  29754. server_args.signal = &ready;
  29755. server_args.callbacks = &server_cb;
  29756. client_args.signal = &ready;
  29757. client_args.callbacks = &client_cb;
  29758. start_thread(test_server_nofail, &server_args, &serverThread);
  29759. wait_tcp_ready(&server_args);
  29760. test_client_nofail(&client_args, NULL);
  29761. join_thread(serverThread);
  29762. FreeTcpReady(&ready);
  29763. /* check that error code was stored */
  29764. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29765. ERR_peek_last_error_line(NULL, &line);
  29766. AssertIntNE(line, 0);
  29767. ERR_peek_last_error_line(&file, NULL);
  29768. AssertNotNull(file);
  29769. #ifdef WOLFSSL_TIRTOS
  29770. fdOpenSession(Task_self());
  29771. #endif
  29772. fprintf(stderr, "\nTesting error print out\n");
  29773. ERR_print_errors_fp(stderr);
  29774. fprintf(stderr, "Done testing print out\n\n");
  29775. res = TEST_RES_CHECK(1);
  29776. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29777. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29778. return res;
  29779. }
  29780. #endif
  29781. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29782. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29783. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29784. {
  29785. (void) ok;
  29786. (void) ctx;
  29787. fprintf(stderr, "ENTER verify_cb\n");
  29788. return SSL_SUCCESS;
  29789. }
  29790. #endif
  29791. static int test_wolfSSL_X509_Name_canon(void)
  29792. {
  29793. int res = TEST_SKIPPED;
  29794. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29795. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29796. defined(WOLFSSL_CERT_GEN) && \
  29797. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29798. const long ex_hash1 = 0x0fdb2da4;
  29799. const long ex_hash2 = 0x9f3e8c9e;
  29800. X509_NAME *name = NULL;
  29801. X509 *x509 = NULL;
  29802. FILE* file = NULL;
  29803. unsigned long hash = 0;
  29804. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29805. byte *pbuf = NULL;
  29806. word32 len = 0;
  29807. (void) ex_hash2;
  29808. file = XFOPEN(caCertFile, "rb");
  29809. AssertNotNull(file);
  29810. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29811. AssertNotNull(name = X509_get_issuer_name(x509));
  29812. /* When output buffer is NULL, should return necessary output buffer
  29813. * length.*/
  29814. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29815. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29816. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29817. hash = (((unsigned long)digest[3] << 24) |
  29818. ((unsigned long)digest[2] << 16) |
  29819. ((unsigned long)digest[1] << 8) |
  29820. ((unsigned long)digest[0]));
  29821. AssertIntEQ(hash, ex_hash1);
  29822. XFCLOSE(file);
  29823. X509_free(x509);
  29824. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29825. pbuf = NULL;
  29826. file = XFOPEN(cliCertFile, "rb");
  29827. AssertNotNull(file);
  29828. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29829. AssertNotNull(name = X509_get_issuer_name(x509));
  29830. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29831. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29832. hash = (((unsigned long)digest[3] << 24) |
  29833. ((unsigned long)digest[2] << 16) |
  29834. ((unsigned long)digest[1] << 8) |
  29835. ((unsigned long)digest[0]));
  29836. AssertIntEQ(hash, ex_hash2);
  29837. XFCLOSE(file);
  29838. X509_free(x509);
  29839. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29840. res = TEST_RES_CHECK(1);
  29841. #endif
  29842. return res;
  29843. }
  29844. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29845. {
  29846. int res = TEST_SKIPPED;
  29847. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29848. const int MAX_DIR = 4;
  29849. const char paths[][32] = {
  29850. "./certs/ed25519",
  29851. "./certs/ecc",
  29852. "./certs/crl",
  29853. "./certs/",
  29854. };
  29855. char CertCrl_path[MAX_FILENAME_SZ];
  29856. char *p;
  29857. X509_STORE* str;
  29858. X509_LOOKUP* lookup;
  29859. WOLFSSL_STACK* sk = NULL;
  29860. int len, total_len, i;
  29861. (void) sk;
  29862. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29863. /* illegal string */
  29864. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29865. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29866. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29867. SSL_FILETYPE_PEM,NULL), 0);
  29868. /* free store */
  29869. X509_STORE_free(str);
  29870. /* short folder string */
  29871. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29872. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29873. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29874. SSL_FILETYPE_PEM,NULL), 1);
  29875. #if defined(WOLFSSL_INT_H)
  29876. /* only available when including internal.h */
  29877. AssertNotNull(sk = lookup->dirs->dir_entry);
  29878. #endif
  29879. /* free store */
  29880. X509_STORE_free(str);
  29881. /* typical function check */
  29882. p = &CertCrl_path[0];
  29883. total_len = 0;
  29884. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29885. len = (int)XSTRLEN((const char*)&paths[i]);
  29886. total_len += len;
  29887. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29888. p += len;
  29889. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29890. }
  29891. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29892. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29893. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29894. SSL_FILETYPE_PEM,NULL), 1);
  29895. #if defined(WOLFSSL_INT_H)
  29896. /* only available when including internal.h */
  29897. AssertNotNull(sk = lookup->dirs->dir_entry);
  29898. #endif
  29899. X509_STORE_free(str);
  29900. res = TEST_RES_CHECK(1);
  29901. #endif
  29902. return res;
  29903. }
  29904. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29905. {
  29906. int res = TEST_SKIPPED;
  29907. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29908. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29909. defined(WOLFSSL_SIGNER_DER_CERT)
  29910. X509_STORE_CTX* ctx;
  29911. X509_STORE* str;
  29912. X509_LOOKUP* lookup;
  29913. X509* cert1;
  29914. X509* x509Ca;
  29915. X509* x509Svr;
  29916. X509* issuer;
  29917. WOLFSSL_STACK* sk = NULL;
  29918. X509_NAME* caName;
  29919. X509_NAME* issuerName;
  29920. FILE* file1 = NULL;
  29921. int i, cert_count, cmp;
  29922. char der[] = "certs/ca-cert.der";
  29923. #ifdef HAVE_CRL
  29924. char pem[][100] = {
  29925. "./certs/crl/crl.pem",
  29926. "./certs/crl/crl2.pem",
  29927. "./certs/crl/caEccCrl.pem",
  29928. "./certs/crl/eccCliCRL.pem",
  29929. "./certs/crl/eccSrvCRL.pem",
  29930. ""
  29931. };
  29932. #endif
  29933. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  29934. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29935. fclose(file1);
  29936. AssertNotNull(ctx = X509_STORE_CTX_new());
  29937. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29938. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29939. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29940. SSL_FILETYPE_PEM,NULL), 1);
  29941. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29942. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29943. /* check if CA cert is loaded into the store */
  29944. for (i = 0; i < cert_count; i++) {
  29945. x509Ca = sk_X509_value(sk, i);
  29946. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29947. }
  29948. AssertNotNull((x509Svr =
  29949. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29950. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29951. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29952. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29953. AssertNotNull(issuer);
  29954. caName = X509_get_subject_name(x509Ca);
  29955. AssertNotNull(caName);
  29956. issuerName = X509_get_subject_name(issuer);
  29957. AssertNotNull(issuerName);
  29958. cmp = X509_NAME_cmp(caName, issuerName);
  29959. AssertIntEQ(cmp, 0);
  29960. /* load der format */
  29961. X509_free(issuer);
  29962. X509_STORE_CTX_free(ctx);
  29963. X509_STORE_free(str);
  29964. sk_X509_pop_free(sk, NULL);
  29965. X509_free(x509Svr);
  29966. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29967. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29968. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29969. SSL_FILETYPE_ASN1,NULL), 1);
  29970. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29971. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29972. /* check if CA cert is loaded into the store */
  29973. for (i = 0; i < cert_count; i++) {
  29974. x509Ca = sk_X509_value(sk, i);
  29975. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29976. }
  29977. X509_STORE_free(str);
  29978. sk_X509_pop_free(sk, NULL);
  29979. X509_free(cert1);
  29980. #ifdef HAVE_CRL
  29981. AssertNotNull(str = wolfSSL_X509_STORE_new());
  29982. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29983. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29984. SSL_FILETYPE_PEM,NULL), 1);
  29985. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29986. "certs/server-revoked-cert.pem",
  29987. SSL_FILETYPE_PEM,NULL), 1);
  29988. if (str) {
  29989. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29990. WOLFSSL_FILETYPE_PEM), 1);
  29991. /* since store hasn't yet known the revoked cert*/
  29992. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29993. "certs/server-revoked-cert.pem",
  29994. WOLFSSL_FILETYPE_PEM), 1);
  29995. }
  29996. for (i = 0; pem[i][0] != '\0'; i++)
  29997. {
  29998. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29999. SSL_FILETYPE_PEM, NULL), 1);
  30000. }
  30001. if (str) {
  30002. /* since store knows crl list */
  30003. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  30004. "certs/server-revoked-cert.pem",
  30005. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  30006. }
  30007. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  30008. X509_STORE_free(str);
  30009. #endif
  30010. res = TEST_RES_CHECK(1);
  30011. #endif
  30012. return res;
  30013. }
  30014. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  30015. {
  30016. int res = TEST_SKIPPED;
  30017. #if defined(OPENSSL_EXTRA)
  30018. X509_STORE_CTX_cleanup(NULL);
  30019. X509_STORE_CTX_trusted_stack(NULL, NULL);
  30020. AssertTrue(1); /* to confirm previous call gives no harm */
  30021. res = TEST_RES_CHECK(1);
  30022. #endif
  30023. return res;
  30024. }
  30025. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  30026. {
  30027. int res = TEST_SKIPPED;
  30028. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30029. #ifdef WOLFSSL_SIGNER_DER_CERT
  30030. int cmp;
  30031. #endif
  30032. X509_STORE_CTX* ctx;
  30033. X509_STORE* str;
  30034. X509* x509Ca;
  30035. X509* x509Svr;
  30036. X509* issuer;
  30037. X509_NAME* caName;
  30038. X509_NAME* issuerName;
  30039. AssertNotNull(ctx = X509_STORE_CTX_new());
  30040. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30041. AssertNotNull((x509Ca =
  30042. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  30043. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  30044. AssertNotNull((x509Svr =
  30045. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30046. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30047. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30048. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30049. AssertNotNull(issuer);
  30050. caName = X509_get_subject_name(x509Ca);
  30051. AssertNotNull(caName);
  30052. issuerName = X509_get_subject_name(issuer);
  30053. AssertNotNull(issuerName);
  30054. #ifdef WOLFSSL_SIGNER_DER_CERT
  30055. cmp = X509_NAME_cmp(caName, issuerName);
  30056. AssertIntEQ(cmp, 0);
  30057. #endif
  30058. X509_free(issuer);
  30059. X509_STORE_CTX_free(ctx);
  30060. X509_free(x509Svr);
  30061. X509_STORE_free(str);
  30062. X509_free(x509Ca);
  30063. res = TEST_RES_CHECK(1);
  30064. #endif
  30065. return res;
  30066. }
  30067. static int test_wolfSSL_PKCS7_certs(void)
  30068. {
  30069. int res = TEST_SKIPPED;
  30070. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  30071. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  30072. STACK_OF(X509)* sk = NULL;
  30073. STACK_OF(X509_INFO)* info_sk = NULL;
  30074. PKCS7 *p7 = NULL;
  30075. BIO* bio;
  30076. const byte* p = NULL;
  30077. int buflen = 0;
  30078. int i;
  30079. /* Test twice. Once with d2i and once without to test
  30080. * that everything is free'd correctly. */
  30081. for (i = 0; i < 2; i++) {
  30082. AssertNotNull(p7 = PKCS7_new());
  30083. p7->version = 1;
  30084. #ifdef NO_SHA
  30085. p7->hashOID = SHA256h;
  30086. #else
  30087. p7->hashOID = SHAh;
  30088. #endif
  30089. AssertNotNull(bio = BIO_new(BIO_s_file()));
  30090. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30091. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  30092. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  30093. AssertNotNull(sk = sk_X509_new_null());
  30094. while (sk_X509_INFO_num(info_sk)) {
  30095. X509_INFO* info;
  30096. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  30097. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  30098. info->x509 = NULL;
  30099. X509_INFO_free(info);
  30100. }
  30101. sk_X509_INFO_free(info_sk);
  30102. BIO_free(bio);
  30103. bio = BIO_new(BIO_s_mem());
  30104. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  30105. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  30106. if (i == 0) {
  30107. PKCS7_free(p7);
  30108. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  30109. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  30110. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  30111. /* PKCS7_free free's the certs */
  30112. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  30113. }
  30114. BIO_free(bio);
  30115. PKCS7_free(p7);
  30116. }
  30117. res = TEST_RES_CHECK(1);
  30118. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30119. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  30120. return res;
  30121. }
  30122. static int test_wolfSSL_X509_STORE_CTX(void)
  30123. {
  30124. int res = TEST_SKIPPED;
  30125. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30126. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30127. X509_STORE_CTX* ctx;
  30128. X509_STORE* str;
  30129. X509* x509;
  30130. #ifdef OPENSSL_ALL
  30131. X509* x5092;
  30132. STACK_OF(X509) *sk, *sk2, *sk3;
  30133. #endif
  30134. AssertNotNull(ctx = X509_STORE_CTX_new());
  30135. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30136. AssertNotNull((x509 =
  30137. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30138. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  30139. #ifdef OPENSSL_ALL
  30140. /* sk_X509_new only in OPENSSL_ALL */
  30141. sk = sk_X509_new_null();
  30142. AssertNotNull(sk);
  30143. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  30144. #else
  30145. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  30146. #endif
  30147. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  30148. X509_STORE_CTX_set_error(ctx, -5);
  30149. X509_STORE_CTX_set_error(NULL, -5);
  30150. X509_STORE_CTX_free(ctx);
  30151. #ifdef OPENSSL_ALL
  30152. sk_X509_pop_free(sk, NULL);
  30153. #endif
  30154. X509_STORE_free(str);
  30155. X509_free(x509);
  30156. AssertNotNull(ctx = X509_STORE_CTX_new());
  30157. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  30158. X509_STORE_CTX_free(ctx);
  30159. #ifdef OPENSSL_ALL
  30160. /* test X509_STORE_CTX_get(1)_chain */
  30161. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  30162. SSL_FILETYPE_PEM)));
  30163. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  30164. SSL_FILETYPE_PEM)));
  30165. AssertNotNull((sk = sk_X509_new_null()));
  30166. AssertIntEQ(sk_X509_push(sk, x509), 1);
  30167. AssertNotNull((str = X509_STORE_new()));
  30168. AssertNotNull((ctx = X509_STORE_CTX_new()));
  30169. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  30170. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  30171. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  30172. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  30173. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  30174. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  30175. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  30176. X509_STORE_CTX_free(ctx);
  30177. X509_STORE_free(str);
  30178. /* CTX certs not freed yet */
  30179. X509_free(x5092);
  30180. sk_X509_pop_free(sk, NULL);
  30181. /* sk3 is dup so free here */
  30182. sk_X509_pop_free(sk3, NULL);
  30183. #endif
  30184. /* test X509_STORE_CTX_get/set_ex_data */
  30185. {
  30186. int i = 0, tmpData = 5;
  30187. void* tmpDataRet;
  30188. AssertNotNull(ctx = X509_STORE_CTX_new());
  30189. #ifdef HAVE_EX_DATA
  30190. for (i = 0; i < MAX_EX_DATA; i++) {
  30191. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30192. WOLFSSL_SUCCESS);
  30193. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30194. AssertNotNull(tmpDataRet);
  30195. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30196. }
  30197. #else
  30198. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30199. WOLFSSL_FAILURE);
  30200. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30201. AssertNull(tmpDataRet);
  30202. #endif
  30203. X509_STORE_CTX_free(ctx);
  30204. }
  30205. /* test X509_STORE_get/set_ex_data */
  30206. {
  30207. int i = 0, tmpData = 99;
  30208. void* tmpDataRet;
  30209. AssertNotNull(str = X509_STORE_new());
  30210. #ifdef HAVE_EX_DATA
  30211. for (i = 0; i < MAX_EX_DATA; i++) {
  30212. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30213. WOLFSSL_SUCCESS);
  30214. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30215. AssertNotNull(tmpDataRet);
  30216. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30217. }
  30218. #else
  30219. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30220. WOLFSSL_FAILURE);
  30221. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30222. AssertNull(tmpDataRet);
  30223. #endif
  30224. X509_STORE_free(str);
  30225. }
  30226. res = TEST_RES_CHECK(1);
  30227. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30228. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30229. return res;
  30230. }
  30231. static int test_wolfSSL_X509_STORE_set_flags(void)
  30232. {
  30233. int res = TEST_SKIPPED;
  30234. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30235. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30236. X509_STORE* store;
  30237. X509* x509;
  30238. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  30239. AssertNotNull((x509 =
  30240. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  30241. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  30242. #ifdef HAVE_CRL
  30243. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  30244. #else
  30245. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  30246. NOT_COMPILED_IN);
  30247. #endif
  30248. wolfSSL_X509_free(x509);
  30249. wolfSSL_X509_STORE_free(store);
  30250. res = TEST_RES_CHECK(1);
  30251. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30252. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30253. return res;
  30254. }
  30255. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  30256. {
  30257. int res = TEST_SKIPPED;
  30258. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30259. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30260. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  30261. WOLFSSL_X509_STORE* store;
  30262. WOLFSSL_X509_LOOKUP* lookup;
  30263. AssertNotNull(store = wolfSSL_X509_STORE_new());
  30264. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  30265. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  30266. X509_FILETYPE_PEM), 1);
  30267. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  30268. X509_FILETYPE_PEM), 1);
  30269. if (store) {
  30270. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  30271. WOLFSSL_FILETYPE_PEM), 1);
  30272. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30273. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  30274. }
  30275. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  30276. X509_FILETYPE_PEM), 1);
  30277. if (store) {
  30278. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30279. WOLFSSL_FILETYPE_PEM), 1);
  30280. }
  30281. wolfSSL_X509_STORE_free(store);
  30282. res = TEST_RES_CHECK(1);
  30283. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30284. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30285. return res;
  30286. }
  30287. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  30288. {
  30289. int res = TEST_SKIPPED;
  30290. #if defined(OPENSSL_EXTRA)
  30291. WOLFSSL_X509_STORE_CTX* ctx;
  30292. time_t c_time;
  30293. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  30294. c_time = 365*24*60*60;
  30295. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  30296. AssertTrue(
  30297. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  30298. AssertTrue(ctx->param->check_time == c_time);
  30299. wolfSSL_X509_STORE_CTX_free(ctx);
  30300. res = TEST_RES_CHECK(1);
  30301. #endif /* OPENSSL_EXTRA */
  30302. return res;
  30303. }
  30304. static int test_wolfSSL_CTX_get0_set1_param(void)
  30305. {
  30306. int res = TEST_SKIPPED;
  30307. #if defined(OPENSSL_EXTRA)
  30308. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30309. int ret;
  30310. SSL_CTX* ctx;
  30311. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30312. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  30313. char testIPv4[] = "127.0.0.1";
  30314. char testhostName[] = "foo.hoge.com";
  30315. #ifndef NO_WOLFSSL_SERVER
  30316. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30317. #else
  30318. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30319. #endif
  30320. AssertNull(SSL_CTX_get0_param(NULL));
  30321. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  30322. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  30323. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  30324. AssertNotNull(pvpm);
  30325. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30326. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  30327. (int)XSTRLEN(testhostName));
  30328. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  30329. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  30330. ret = SSL_CTX_set1_param(ctx, pvpm);
  30331. AssertIntEQ(1, ret);
  30332. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  30333. (int)XSTRLEN(testhostName)));
  30334. AssertIntEQ(0x01, pParam->hostFlags);
  30335. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  30336. /* test for incorrect patameter */
  30337. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  30338. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  30339. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  30340. SSL_CTX_free(ctx);
  30341. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  30342. res = TEST_RES_CHECK(1);
  30343. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30344. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30345. return res;
  30346. }
  30347. static int test_wolfSSL_get0_param(void)
  30348. {
  30349. int res = TEST_SKIPPED;
  30350. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30351. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30352. SSL_CTX* ctx;
  30353. SSL* ssl;
  30354. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30355. #ifndef NO_WOLFSSL_SERVER
  30356. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30357. #else
  30358. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30359. #endif
  30360. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30361. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30362. AssertNotNull(ssl = SSL_new(ctx));
  30363. pParam = SSL_get0_param(ssl);
  30364. (void)pParam;
  30365. SSL_free(ssl);
  30366. SSL_CTX_free(ctx);
  30367. res = TEST_RES_CHECK(1);
  30368. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30369. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30370. return res;
  30371. }
  30372. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  30373. {
  30374. int res = TEST_SKIPPED;
  30375. #if defined(OPENSSL_EXTRA)
  30376. const char host[] = "www.example.com";
  30377. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30378. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  30379. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  30380. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30381. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30382. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  30383. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30384. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30385. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30386. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30387. res = TEST_RES_CHECK(1);
  30388. #endif /* OPENSSL_EXTRA */
  30389. return res;
  30390. }
  30391. static int test_wolfSSL_set1_host(void)
  30392. {
  30393. int res = TEST_SKIPPED;
  30394. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30395. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30396. const char host[] = "www.test_wolfSSL_set1_host.com";
  30397. const char emptyStr[] = "";
  30398. SSL_CTX* ctx;
  30399. SSL* ssl;
  30400. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30401. #ifndef NO_WOLFSSL_SERVER
  30402. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30403. #else
  30404. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30405. #endif
  30406. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30407. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30408. AssertNotNull(ssl = SSL_new(ctx));
  30409. pParam = SSL_get0_param(ssl);
  30410. /* we should get back host string */
  30411. SSL_set1_host(ssl, host);
  30412. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30413. /* we should get back empty string */
  30414. SSL_set1_host(ssl, emptyStr);
  30415. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30416. /* we should get back host string */
  30417. SSL_set1_host(ssl, host);
  30418. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30419. /* we should get back empty string */
  30420. SSL_set1_host(ssl, NULL);
  30421. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30422. SSL_free(ssl);
  30423. SSL_CTX_free(ctx);
  30424. res = TEST_RES_CHECK(1);
  30425. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30426. #endif /* OPENSSL_EXTRA */
  30427. return res;
  30428. }
  30429. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  30430. {
  30431. int res = TEST_SKIPPED;
  30432. #if defined(OPENSSL_EXTRA)
  30433. unsigned char buf[16] = {0};
  30434. WOLFSSL_X509_VERIFY_PARAM* param;
  30435. AssertNotNull(param = X509_VERIFY_PARAM_new());
  30436. /* test 127.0.0.1 */
  30437. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  30438. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  30439. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  30440. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  30441. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30442. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  30443. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  30444. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  30445. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30446. AssertIntEQ(XSTRNCMP(param->ipasc,
  30447. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  30448. /* test 2001:db8:: */
  30449. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30450. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30451. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30452. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  30453. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30454. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  30455. /* test ::1234:5678 */
  30456. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  30457. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30458. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30459. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30460. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30461. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  30462. /* test 2001:db8::1234:5678 */
  30463. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30464. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30465. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30466. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30467. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30468. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  30469. sizeof(param->ipasc)), 0);
  30470. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  30471. /* 2001:db8:1::ab9:c0a8:102 */
  30472. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30473. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  30474. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  30475. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  30476. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30477. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  30478. sizeof(param->ipasc)), 0);
  30479. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30480. res = TEST_RES_CHECK(1);
  30481. #endif /* OPENSSL_EXTRA */
  30482. return res;
  30483. }
  30484. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  30485. {
  30486. int res = TEST_SKIPPED;
  30487. #if defined(OPENSSL_EXTRA)
  30488. X509_STORE* store;
  30489. X509_STORE_CTX* ctx;
  30490. X509_STORE_CTX* ctx_no_init;
  30491. AssertNotNull((store = X509_STORE_new()));
  30492. AssertNotNull(ctx = X509_STORE_CTX_new());
  30493. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  30494. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  30495. AssertNull(X509_STORE_CTX_get0_store(NULL));
  30496. /* should return NULL if ctx has not bee initialized */
  30497. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  30498. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  30499. wolfSSL_X509_STORE_CTX_free(ctx);
  30500. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  30501. X509_STORE_free(store);
  30502. res = TEST_RES_CHECK(1);
  30503. #endif /* OPENSSL_EXTRA */
  30504. return res;
  30505. }
  30506. static int test_wolfSSL_CTX_set_client_CA_list(void)
  30507. {
  30508. int res = TEST_SKIPPED;
  30509. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30510. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  30511. WOLFSSL_CTX* ctx;
  30512. WOLFSSL* ssl;
  30513. X509_NAME* name = NULL;
  30514. STACK_OF(X509_NAME)* names = NULL;
  30515. STACK_OF(X509_NAME)* ca_list = NULL;
  30516. int i, names_len;
  30517. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30518. /* Send two X501 names in cert request */
  30519. names = SSL_load_client_CA_file(cliCertFile);
  30520. AssertNotNull(names);
  30521. ca_list = SSL_load_client_CA_file(caCertFile);
  30522. AssertNotNull(ca_list);
  30523. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  30524. SSL_CTX_set_client_CA_list(ctx, names);
  30525. /* This should only free the stack structure */
  30526. sk_X509_NAME_free(ca_list);
  30527. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30528. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30529. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30530. for (i=0; i<names_len; i++) {
  30531. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30532. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30533. }
  30534. /* Needed to be able to create ssl object */
  30535. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30536. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30537. AssertNotNull(ssl = wolfSSL_new(ctx));
  30538. /* load again as old names are responsibility of ctx to free*/
  30539. names = SSL_load_client_CA_file(cliCertFile);
  30540. AssertNotNull(names);
  30541. SSL_set_client_CA_list(ssl, names);
  30542. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  30543. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30544. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30545. for (i=0; i<names_len; i++) {
  30546. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30547. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30548. }
  30549. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  30550. {
  30551. tcp_ready ready;
  30552. func_args server_args;
  30553. callback_functions server_cb;
  30554. THREAD_TYPE serverThread;
  30555. WOLFSSL* ssl_client;
  30556. WOLFSSL_CTX* ctx_client;
  30557. SOCKET_T sockfd = 0;
  30558. /* wolfSSL_get_client_CA_list() with handshake */
  30559. StartTCP();
  30560. InitTcpReady(&ready);
  30561. XMEMSET(&server_args, 0, sizeof(func_args));
  30562. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30563. server_args.signal = &ready;
  30564. server_args.callbacks = &server_cb;
  30565. /* we are responsible for free'ing WOLFSSL_CTX */
  30566. server_cb.ctx = ctx;
  30567. server_cb.isSharedCtx = 1;
  30568. AssertIntEQ(WOLFSSL_SUCCESS,
  30569. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30570. start_thread(test_server_nofail, &server_args, &serverThread);
  30571. wait_tcp_ready(&server_args);
  30572. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30573. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30574. AssertIntEQ(WOLFSSL_SUCCESS,
  30575. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  30576. AssertIntEQ(WOLFSSL_SUCCESS,
  30577. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30578. AssertIntEQ(WOLFSSL_SUCCESS,
  30579. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30580. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  30581. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30582. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30583. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30584. /* We are expecting two cert names to be sent */
  30585. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  30586. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30587. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30588. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  30589. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  30590. }
  30591. wolfSSL_shutdown(ssl_client);
  30592. wolfSSL_free(ssl_client);
  30593. wolfSSL_CTX_free(ctx_client);
  30594. join_thread(serverThread);
  30595. FreeTcpReady(&ready);
  30596. }
  30597. #endif
  30598. wolfSSL_free(ssl);
  30599. wolfSSL_CTX_free(ctx);
  30600. res = TEST_RES_CHECK(1);
  30601. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  30602. * !NO_BIO */
  30603. return res;
  30604. }
  30605. static int test_wolfSSL_CTX_add_client_CA(void)
  30606. {
  30607. int res = TEST_SKIPPED;
  30608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30609. !defined(NO_WOLFSSL_CLIENT)
  30610. WOLFSSL_CTX* ctx;
  30611. WOLFSSL_X509* x509;
  30612. WOLFSSL_X509* x509_a;
  30613. STACK_OF(X509_NAME)* ca_list;
  30614. int ret = 0;
  30615. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30616. /* Add client cert */
  30617. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  30618. AssertNotNull(x509);
  30619. ret = SSL_CTX_add_client_CA(ctx, x509);
  30620. AssertIntEQ(ret, SSL_SUCCESS);
  30621. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30622. /* Add another client cert */
  30623. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30624. SSL_FILETYPE_PEM));
  30625. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30626. /* test for incorrect parameter */
  30627. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30628. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30629. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30630. X509_free(x509);
  30631. X509_free(x509_a);
  30632. SSL_CTX_free(ctx);
  30633. res = TEST_RES_CHECK(1);
  30634. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30635. return res;
  30636. }
  30637. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30638. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  30639. {
  30640. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30641. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  30642. WOLFSSL* ssl = NULL;
  30643. SOCKET_T sfd = 0;
  30644. SOCKET_T cfd = 0;
  30645. word16 port;
  30646. char msg[] = "I hear you fa shizzle!";
  30647. int len = (int) XSTRLEN(msg);
  30648. char input[1024];
  30649. int idx;
  30650. int ret, err = 0;
  30651. #ifdef WOLFSSL_TIRTOS
  30652. fdOpenSession(Task_self());
  30653. #endif
  30654. ((func_args*)args)->return_code = TEST_FAIL;
  30655. port = ((func_args*)args)->signal->port;
  30656. AssertIntEQ(WOLFSSL_SUCCESS,
  30657. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30658. AssertIntEQ(WOLFSSL_SUCCESS,
  30659. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30660. WOLFSSL_FILETYPE_PEM));
  30661. AssertIntEQ(WOLFSSL_SUCCESS,
  30662. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30663. WOLFSSL_FILETYPE_PEM));
  30664. if (callbacks->ctx_ready)
  30665. callbacks->ctx_ready(ctx);
  30666. ssl = wolfSSL_new(ctx);
  30667. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30668. CloseSocket(sfd);
  30669. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30670. if (callbacks->ssl_ready)
  30671. callbacks->ssl_ready(ssl);
  30672. do {
  30673. err = 0; /* Reset error */
  30674. ret = wolfSSL_accept(ssl);
  30675. if (ret != WOLFSSL_SUCCESS) {
  30676. err = wolfSSL_get_error(ssl, 0);
  30677. }
  30678. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30679. if (ret != WOLFSSL_SUCCESS) {
  30680. char buff[WOLFSSL_MAX_ERROR_SZ];
  30681. fprintf(stderr, "error = %d, %s\n", err,
  30682. wolfSSL_ERR_error_string(err, buff));
  30683. }
  30684. else {
  30685. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30686. input[idx] = 0;
  30687. fprintf(stderr, "Client message: %s\n", input);
  30688. }
  30689. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  30690. #ifdef WOLFSSL_TIRTOS
  30691. Task_yield();
  30692. #endif
  30693. ((func_args*)args)->return_code = TEST_SUCCESS;
  30694. }
  30695. if (callbacks->on_result)
  30696. callbacks->on_result(ssl);
  30697. wolfSSL_shutdown(ssl);
  30698. wolfSSL_free(ssl);
  30699. wolfSSL_CTX_free(ctx);
  30700. CloseSocket(cfd);
  30701. #ifdef WOLFSSL_TIRTOS
  30702. fdCloseSession(Task_self());
  30703. #endif
  30704. #ifndef WOLFSSL_TIRTOS
  30705. return 0;
  30706. #endif
  30707. }
  30708. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30709. {
  30710. AssertNotNull(ssl);
  30711. AssertNotNull(line);
  30712. XFILE fp;
  30713. const byte lf = '\n';
  30714. fp = XFOPEN("./MyKeyLog.txt", "a");
  30715. XFWRITE( line, 1, strlen(line),fp);
  30716. XFWRITE( (void*)&lf,1,1,fp);
  30717. XFCLOSE(fp);
  30718. }
  30719. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30720. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30721. {
  30722. int res = TEST_SKIPPED;
  30723. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30724. !defined(NO_WOLFSSL_CLIENT)
  30725. SSL_CTX* ctx;
  30726. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30727. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30728. SSL_CTX_free(ctx);
  30729. SSL_CTX_set_keylog_callback(NULL, NULL);
  30730. res = TEST_RES_CHECK(1);
  30731. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30732. return res;
  30733. }
  30734. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30735. {
  30736. int res = TEST_SKIPPED;
  30737. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30738. !defined(NO_WOLFSSL_CLIENT)
  30739. SSL_CTX* ctx;
  30740. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30741. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30742. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30743. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30744. SSL_CTX_set_keylog_callback(ctx, NULL );
  30745. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30746. SSL_CTX_free(ctx);
  30747. res = TEST_RES_CHECK(1);
  30748. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30749. return res;
  30750. }
  30751. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30752. {
  30753. int res = TEST_SKIPPED;
  30754. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30755. /* This test is intended for checking whether keylog callback is called
  30756. * in client during TLS handshake between the client and a server.
  30757. */
  30758. tcp_ready ready;
  30759. func_args client_args;
  30760. func_args server_args;
  30761. THREAD_TYPE serverThread;
  30762. callback_functions server_cbf;
  30763. callback_functions client_cbf;
  30764. SOCKET_T sockfd = 0;
  30765. WOLFSSL_CTX* ctx;
  30766. WOLFSSL* ssl;
  30767. XFILE fp;
  30768. char msg[64] = "hello wolfssl!";
  30769. char reply[1024];
  30770. int msgSz = (int)XSTRLEN(msg);
  30771. #ifdef WOLFSSL_TIRTOS
  30772. fdOpenSession(Task_self());
  30773. #endif
  30774. InitTcpReady(&ready);
  30775. ready.port = 22222;
  30776. XMEMSET(&client_args, 0, sizeof(func_args));
  30777. XMEMSET(&server_args, 0, sizeof(func_args));
  30778. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30779. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30780. server_cbf.method = wolfTLSv1_2_server_method;
  30781. server_args.callbacks = &server_cbf;
  30782. server_args.signal = &ready;
  30783. /* clean up keylog file */
  30784. fp = XFOPEN("./MyKeyLog.txt", "w");
  30785. XFCLOSE(fp);
  30786. /* start server task */
  30787. start_thread(server_task, &server_args, &serverThread);
  30788. wait_tcp_ready(&server_args);
  30789. /* run as a TLS1.2 client */
  30790. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30791. AssertIntEQ(WOLFSSL_SUCCESS,
  30792. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30793. AssertIntEQ(WOLFSSL_SUCCESS,
  30794. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30795. AssertIntEQ(WOLFSSL_SUCCESS,
  30796. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30797. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30798. /* set keylog callback */
  30799. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30800. /* get connected the server task */
  30801. AssertNotNull(ssl = wolfSSL_new(ctx));
  30802. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30803. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30804. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30805. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30806. wolfSSL_shutdown(ssl);
  30807. wolfSSL_free(ssl);
  30808. wolfSSL_CTX_free(ctx);
  30809. CloseSocket(sockfd);
  30810. join_thread(serverThread);
  30811. FreeTcpReady(&ready);
  30812. #ifdef WOLFSSL_TIRTOS
  30813. fdOpenSession(Task_self());
  30814. #endif
  30815. /* check if the keylog file exists */
  30816. char buff[300] = {0};
  30817. int found = 0;
  30818. fp = XFOPEN("./MyKeyLog.txt", "r");
  30819. AssertNotNull(fp);
  30820. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30821. if (0 == strncmp(buff,"CLIENT_RANDOM ",
  30822. sizeof("CLIENT_RANDOM ")-1)) {
  30823. found = 1;
  30824. break;
  30825. }
  30826. }
  30827. XFCLOSE(fp);
  30828. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30829. AssertNotNull( found );
  30830. res = TEST_RES_CHECK(1);
  30831. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30832. return res;
  30833. }
  30834. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30835. {
  30836. int res = TEST_SKIPPED;
  30837. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30838. defined(HAVE_SECRET_CALLBACK)
  30839. /* This test is intended for checking whether keylog callback is called
  30840. * in client during TLS handshake between the client and a server.
  30841. */
  30842. tcp_ready ready;
  30843. func_args client_args;
  30844. func_args server_args;
  30845. THREAD_TYPE serverThread;
  30846. callback_functions server_cbf;
  30847. callback_functions client_cbf;
  30848. SOCKET_T sockfd = 0;
  30849. WOLFSSL_CTX* ctx;
  30850. WOLFSSL* ssl;
  30851. XFILE fp;
  30852. char msg[64] = "hello wolfssl!";
  30853. char reply[1024];
  30854. int msgSz = (int)XSTRLEN(msg);
  30855. #ifdef WOLFSSL_TIRTOS
  30856. fdOpenSession(Task_self());
  30857. #endif
  30858. InitTcpReady(&ready);
  30859. ready.port = 22222;
  30860. XMEMSET(&client_args, 0, sizeof(func_args));
  30861. XMEMSET(&server_args, 0, sizeof(func_args));
  30862. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30863. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30864. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30865. server_args.callbacks = &server_cbf;
  30866. server_args.signal = &ready;
  30867. /* clean up keylog file */
  30868. fp = XFOPEN("./MyKeyLog.txt", "w");
  30869. XFCLOSE(fp);
  30870. /* start server task */
  30871. start_thread(server_task, &server_args, &serverThread);
  30872. wait_tcp_ready(&server_args);
  30873. /* run as a TLS1.3 client */
  30874. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  30875. AssertIntEQ(WOLFSSL_SUCCESS,
  30876. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30877. AssertIntEQ(WOLFSSL_SUCCESS,
  30878. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30879. AssertIntEQ(WOLFSSL_SUCCESS,
  30880. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30881. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30882. /* set keylog callback */
  30883. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30884. /* get connected the server task */
  30885. AssertNotNull(ssl = wolfSSL_new(ctx));
  30886. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30887. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30888. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30889. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30890. wolfSSL_free(ssl);
  30891. wolfSSL_CTX_free(ctx);
  30892. join_thread(serverThread);
  30893. FreeTcpReady(&ready);
  30894. #ifdef WOLFSSL_TIRTOS
  30895. fdOpenSession(Task_self());
  30896. #endif
  30897. /* check if the keylog file exists */
  30898. {
  30899. char buff[300] = {0};
  30900. int found[4] = {0};
  30901. int numfnd = 0;
  30902. int i;
  30903. fp = XFOPEN("./MyKeyLog.txt", "r");
  30904. AssertNotNull(fp);
  30905. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30906. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  30907. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30908. found[0] = 1;
  30909. continue;
  30910. }
  30911. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  30912. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30913. found[1] = 1;
  30914. continue;
  30915. }
  30916. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  30917. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  30918. found[2] = 1;
  30919. continue;
  30920. }
  30921. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  30922. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  30923. found[3] = 1;
  30924. continue;
  30925. }
  30926. }
  30927. XFCLOSE(fp);
  30928. for (i = 0; i < 4; i++) {
  30929. if (found[i] != 0)
  30930. numfnd++;
  30931. }
  30932. AssertIntEQ(numfnd, 4);
  30933. }
  30934. res = TEST_RES_CHECK(1);
  30935. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  30936. return res;
  30937. }
  30938. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30939. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30940. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30941. static void post_auth_version_cb(WOLFSSL* ssl)
  30942. {
  30943. /* do handshake and then test version error */
  30944. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30945. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30946. }
  30947. static void post_auth_version_client_cb(WOLFSSL* ssl)
  30948. {
  30949. /* do handshake and then test version error */
  30950. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30951. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30952. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30953. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30954. /* check was added to error queue */
  30955. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30956. /* check the string matches expected string */
  30957. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30958. "WRONG_SSL_VERSION");
  30959. #endif
  30960. }
  30961. static void post_auth_cb(WOLFSSL* ssl)
  30962. {
  30963. WOLFSSL_X509* x509;
  30964. /* do handshake and then test version error */
  30965. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30966. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30967. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30968. wolfSSL_X509_free(x509);
  30969. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30970. }
  30971. static void set_post_auth_cb(WOLFSSL* ssl)
  30972. {
  30973. if (!wolfSSL_is_server(ssl)) {
  30974. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30975. }
  30976. else {
  30977. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30978. }
  30979. }
  30980. #endif
  30981. static int test_wolfSSL_Tls13_postauth(void)
  30982. {
  30983. int res = TEST_SKIPPED;
  30984. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30985. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30986. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30987. tcp_ready ready;
  30988. func_args client_args;
  30989. func_args server_args;
  30990. callback_functions server_cbf;
  30991. callback_functions client_cbf;
  30992. THREAD_TYPE serverThread;
  30993. XMEMSET(&client_args, 0, sizeof(func_args));
  30994. XMEMSET(&server_args, 0, sizeof(func_args));
  30995. StartTCP();
  30996. InitTcpReady(&ready);
  30997. #if defined(USE_WINDOWS_API)
  30998. /* use RNG to get random port if using windows */
  30999. ready.port = GetRandomPort();
  31000. #endif
  31001. server_args.signal = &ready;
  31002. client_args.signal = &ready;
  31003. /* test version failure doing post auth with TLS 1.2 connection */
  31004. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31005. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31006. server_cbf.method = wolfTLSv1_2_server_method;
  31007. server_cbf.ssl_ready = set_post_auth_cb;
  31008. server_cbf.on_result = post_auth_version_cb;
  31009. client_cbf.ssl_ready = set_post_auth_cb;
  31010. client_cbf.on_result = post_auth_version_client_cb;
  31011. server_args.callbacks = &server_cbf;
  31012. client_args.callbacks = &client_cbf;
  31013. start_thread(test_server_nofail, &server_args, &serverThread);
  31014. wait_tcp_ready(&server_args);
  31015. test_client_nofail(&client_args, NULL);
  31016. join_thread(serverThread);
  31017. /* tests on post auth with TLS 1.3 */
  31018. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  31019. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  31020. server_cbf.method = wolfTLSv1_3_server_method;
  31021. server_cbf.ssl_ready = set_post_auth_cb;
  31022. client_cbf.ssl_ready = set_post_auth_cb;
  31023. server_cbf.on_result = post_auth_cb;
  31024. client_cbf.on_result = NULL;
  31025. server_args.callbacks = &server_cbf;
  31026. client_args.callbacks = &client_cbf;
  31027. start_thread(test_server_nofail, &server_args, &serverThread);
  31028. wait_tcp_ready(&server_args);
  31029. test_client_nofail(&client_args, NULL);
  31030. join_thread(serverThread);
  31031. FreeTcpReady(&ready);
  31032. res = TEST_RES_CHECK(1);
  31033. #endif
  31034. return res;
  31035. }
  31036. static int test_wolfSSL_X509_NID(void)
  31037. {
  31038. int res = TEST_SKIPPED;
  31039. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  31040. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  31041. int sigType;
  31042. int nameSz;
  31043. X509* cert;
  31044. EVP_PKEY* pubKeyTmp;
  31045. X509_NAME* name;
  31046. char commonName[80];
  31047. char countryName[80];
  31048. char localityName[80];
  31049. char stateName[80];
  31050. char orgName[80];
  31051. char orgUnit[80];
  31052. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  31053. /* convert cert from DER to internal WOLFSSL_X509 struct */
  31054. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  31055. sizeof_client_cert_der_2048));
  31056. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  31057. /* extract PUBLIC KEY from cert */
  31058. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  31059. /* extract signatureType */
  31060. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  31061. /* extract subjectName info */
  31062. AssertNotNull(name = X509_get_subject_name(cert));
  31063. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  31064. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31065. NULL, 0)), 0);
  31066. AssertIntEQ(nameSz, 15);
  31067. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31068. commonName, sizeof(commonName))), 0);
  31069. AssertIntEQ(nameSz, 15);
  31070. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  31071. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  31072. commonName, 9)), 0);
  31073. AssertIntEQ(nameSz, 8);
  31074. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  31075. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  31076. countryName, sizeof(countryName))), 0);
  31077. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  31078. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  31079. localityName, sizeof(localityName))), 0);
  31080. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  31081. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  31082. stateName, sizeof(stateName))), 0);
  31083. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  31084. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  31085. orgName, sizeof(orgName))), 0);
  31086. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  31087. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  31088. orgUnit, sizeof(orgUnit))), 0);
  31089. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  31090. EVP_PKEY_free(pubKeyTmp);
  31091. X509_free(cert);
  31092. res = TEST_RES_CHECK(1);
  31093. #endif
  31094. return res;
  31095. }
  31096. static int test_wolfSSL_CTX_set_srp_username(void)
  31097. {
  31098. int res = TEST_SKIPPED;
  31099. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31100. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31101. WOLFSSL_CTX* ctx;
  31102. WOLFSSL* ssl;
  31103. const char *username = "TESTUSER";
  31104. const char *password = "TESTPASSWORD";
  31105. int r;
  31106. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31107. AssertNotNull(ctx);
  31108. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31109. AssertIntEQ(r,SSL_SUCCESS);
  31110. wolfSSL_CTX_free(ctx);
  31111. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31112. AssertNotNull(ctx);
  31113. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31114. AssertIntEQ(r,SSL_SUCCESS);
  31115. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31116. AssertIntEQ(r,SSL_SUCCESS);
  31117. AssertNotNull(ssl = SSL_new(ctx));
  31118. AssertNotNull(SSL_get_srp_username(ssl));
  31119. AssertStrEQ(SSL_get_srp_username(ssl), username);
  31120. wolfSSL_free(ssl);
  31121. wolfSSL_CTX_free(ctx);
  31122. res = TEST_RES_CHECK(1);
  31123. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31124. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31125. return res;
  31126. }
  31127. static int test_wolfSSL_CTX_set_srp_password(void)
  31128. {
  31129. int res = TEST_SKIPPED;
  31130. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31131. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31132. WOLFSSL_CTX* ctx;
  31133. const char *username = "TESTUSER";
  31134. const char *password = "TESTPASSWORD";
  31135. int r;
  31136. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31137. AssertNotNull(ctx);
  31138. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31139. AssertIntEQ(r,SSL_SUCCESS);
  31140. wolfSSL_CTX_free(ctx);
  31141. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31142. AssertNotNull(ctx);
  31143. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31144. AssertIntEQ(r,SSL_SUCCESS);
  31145. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31146. AssertIntEQ(r,SSL_SUCCESS);
  31147. wolfSSL_CTX_free(ctx);
  31148. res = TEST_RES_CHECK(1);
  31149. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31150. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31151. return res;
  31152. }
  31153. static int test_wolfSSL_X509_STORE(void)
  31154. {
  31155. int res = TEST_SKIPPED;
  31156. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31157. X509_STORE *store;
  31158. #ifdef HAVE_CRL
  31159. X509_STORE_CTX *storeCtx;
  31160. X509_CRL *crl;
  31161. X509 *ca, *cert;
  31162. const char crlPem[] = "./certs/crl/crl.revoked";
  31163. const char srvCert[] = "./certs/server-revoked-cert.pem";
  31164. const char caCert[] = "./certs/ca-cert.pem";
  31165. XFILE fp;
  31166. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31167. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31168. SSL_FILETYPE_PEM)));
  31169. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31170. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31171. SSL_FILETYPE_PEM)));
  31172. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31173. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31174. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31175. X509_STORE_free(store);
  31176. X509_STORE_CTX_free(storeCtx);
  31177. X509_free(cert);
  31178. X509_free(ca);
  31179. /* should fail to verify now after adding in CRL */
  31180. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31181. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31182. SSL_FILETYPE_PEM)));
  31183. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31184. fp = XFOPEN(crlPem, "rb");
  31185. AssertTrue((fp != XBADFILE));
  31186. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  31187. NULL, NULL));
  31188. XFCLOSE(fp);
  31189. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  31190. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  31191. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31192. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31193. SSL_FILETYPE_PEM)));
  31194. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31195. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31196. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  31197. X509_CRL_free(crl);
  31198. X509_STORE_free(store);
  31199. X509_STORE_CTX_free(storeCtx);
  31200. X509_free(cert);
  31201. X509_free(ca);
  31202. #endif /* HAVE_CRL */
  31203. #ifndef WOLFCRYPT_ONLY
  31204. {
  31205. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31206. SSL_CTX* ctx;
  31207. SSL* ssl;
  31208. int i;
  31209. for (i = 0; i < 2; i++) {
  31210. #ifndef NO_WOLFSSL_SERVER
  31211. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31212. #else
  31213. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31214. #endif
  31215. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31216. SSL_CTX_set_cert_store(ctx, store);
  31217. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31218. SSL_CTX_set_cert_store(ctx, store);
  31219. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31220. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31221. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31222. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31223. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31224. AssertNotNull(ssl = SSL_new(ctx));
  31225. if (i == 0) {
  31226. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  31227. }
  31228. else {
  31229. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  31230. X509_STORE_free(store);
  31231. }
  31232. SSL_free(ssl);
  31233. SSL_CTX_free(ctx);
  31234. }
  31235. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31236. }
  31237. #endif
  31238. res = TEST_RES_CHECK(1);
  31239. #endif
  31240. return res;
  31241. }
  31242. static int test_wolfSSL_X509_STORE_load_locations(void)
  31243. {
  31244. int res = TEST_SKIPPED;
  31245. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31246. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31247. SSL_CTX *ctx;
  31248. X509_STORE *store;
  31249. const char ca_file[] = "./certs/ca-cert.pem";
  31250. const char client_pem_file[] = "./certs/client-cert.pem";
  31251. const char client_der_file[] = "./certs/client-cert.der";
  31252. const char ecc_file[] = "./certs/ecc-key.pem";
  31253. const char certs_path[] = "./certs/";
  31254. const char bad_path[] = "./bad-path/";
  31255. #ifdef HAVE_CRL
  31256. const char crl_path[] = "./certs/crl/";
  31257. const char crl_file[] = "./certs/crl/crl.pem";
  31258. #endif
  31259. #ifndef NO_WOLFSSL_SERVER
  31260. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31261. #else
  31262. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31263. #endif
  31264. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31265. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  31266. /* Test bad arguments */
  31267. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  31268. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  31269. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  31270. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  31271. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  31272. #ifdef HAVE_CRL
  31273. /* Test with CRL */
  31274. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  31275. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  31276. #endif
  31277. /* Test with CA */
  31278. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  31279. /* Test with client_cert and certs path */
  31280. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  31281. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  31282. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  31283. /* Clear nodes */
  31284. ERR_clear_error();
  31285. #endif
  31286. SSL_CTX_free(ctx);
  31287. res = TEST_RES_CHECK(1);
  31288. #endif
  31289. return res;
  31290. }
  31291. static int test_X509_STORE_get0_objects(void)
  31292. {
  31293. int res = TEST_SKIPPED;
  31294. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  31295. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31296. X509_STORE *store;
  31297. X509_STORE *store_cpy;
  31298. SSL_CTX *ctx;
  31299. X509_OBJECT *obj;
  31300. STACK_OF(X509_OBJECT) *objs;
  31301. int i;
  31302. /* Setup store */
  31303. #ifndef NO_WOLFSSL_SERVER
  31304. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31305. #else
  31306. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31307. #endif
  31308. AssertNotNull(store_cpy = X509_STORE_new());
  31309. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31310. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  31311. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  31312. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  31313. #ifdef HAVE_CRL
  31314. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  31315. #endif
  31316. /* Store ready */
  31317. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  31318. AssertNotNull(objs = X509_STORE_get0_objects(store));
  31319. #ifdef HAVE_CRL
  31320. #ifdef WOLFSSL_SIGNER_DER_CERT
  31321. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  31322. #else
  31323. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  31324. #endif
  31325. #else
  31326. #ifdef WOLFSSL_SIGNER_DER_CERT
  31327. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  31328. #else
  31329. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  31330. #endif
  31331. #endif
  31332. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  31333. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  31334. switch (X509_OBJECT_get_type(obj)) {
  31335. case X509_LU_X509:
  31336. AssertNotNull(X509_OBJECT_get0_X509(obj));
  31337. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  31338. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  31339. break;
  31340. case X509_LU_CRL:
  31341. #ifdef HAVE_CRL
  31342. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  31343. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  31344. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  31345. break;
  31346. #endif
  31347. case X509_LU_NONE:
  31348. default:
  31349. Fail(("X509_OBJECT_get_type should return x509 or crl "
  31350. "(when built with crl support)"),
  31351. ("Unrecognized X509_OBJECT type or none"));
  31352. }
  31353. }
  31354. X509_STORE_free(store_cpy);
  31355. SSL_CTX_free(ctx);
  31356. res = TEST_RES_CHECK(1);
  31357. #endif
  31358. return res;
  31359. }
  31360. static int test_wolfSSL_BN(void)
  31361. {
  31362. int res = TEST_SKIPPED;
  31363. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31364. BIGNUM* a;
  31365. BIGNUM* b;
  31366. BIGNUM* c;
  31367. BIGNUM* d;
  31368. ASN1_INTEGER* ai;
  31369. AssertNotNull(b = BN_new());
  31370. AssertNotNull(c = BN_new());
  31371. AssertNotNull(d = BN_new());
  31372. ai = ASN1_INTEGER_new();
  31373. AssertNotNull(ai);
  31374. /* at the moment hard setting since no set function */
  31375. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  31376. ai->data[1] = 0x01; /* length of integer */
  31377. ai->data[2] = 0x03;
  31378. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  31379. ASN1_INTEGER_free(ai);
  31380. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31381. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  31382. /* a + 3 = */
  31383. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  31384. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  31385. /* check result 3 + 3*/
  31386. AssertIntEQ(BN_get_word(a), 6);
  31387. /* set a back to 3 */
  31388. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31389. /* a - 3 = */
  31390. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  31391. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  31392. /* check result 3 - 3*/
  31393. AssertIntEQ(BN_get_word(a), 0);
  31394. /* set a back to 3 */
  31395. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31396. /* a^b mod c = */
  31397. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  31398. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  31399. /* check result 3^2 mod 5 */
  31400. AssertIntEQ(BN_get_word(d), 4);
  31401. /* a*b = */
  31402. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  31403. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  31404. /* check result 3*2 */
  31405. AssertIntEQ(BN_get_word(d), 6);
  31406. /* c/b => db + a */
  31407. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  31408. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  31409. /* check result 5/2 */
  31410. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  31411. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  31412. /* set a back to 3 */
  31413. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31414. /* a*b mod c = */
  31415. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  31416. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  31417. /* check result 3*2 mod 5 */
  31418. AssertIntEQ(BN_get_word(d), 1);
  31419. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  31420. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  31421. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31422. /* gcd of a and b */
  31423. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  31424. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  31425. /* check result gcd(16, 24) */
  31426. AssertIntEQ(BN_get_word(d), 8);
  31427. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  31428. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  31429. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  31430. AssertIntEQ(BN_is_zero(b), 0);
  31431. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  31432. AssertIntEQ(BN_is_zero(b), 1);
  31433. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  31434. AssertIntEQ(BN_is_zero(b), 0);
  31435. /* set b back to 2 */
  31436. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31437. {
  31438. /* BN_mod_inverse test */
  31439. BIGNUM *r = BN_new();
  31440. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  31441. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  31442. BN_free(val);
  31443. }
  31444. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  31445. defined(WOLFSSL_SP_INT_NEGATIVE))
  31446. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  31447. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  31448. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  31449. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  31450. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  31451. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31452. {
  31453. char* ret;
  31454. AssertNotNull(ret = BN_bn2dec(c));
  31455. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31456. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31457. }
  31458. #endif
  31459. AssertIntEQ(BN_get_word(c), 4);
  31460. #endif
  31461. BN_free(a);
  31462. BN_free(b);
  31463. BN_free(c);
  31464. BN_clear_free(d);
  31465. /* check that converting NULL and the null string returns an error */
  31466. a = NULL;
  31467. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  31468. AssertIntLE(BN_hex2bn(&a, ""), 0);
  31469. AssertNull(a);
  31470. /* check that getting a string and a bin of the same number are equal,
  31471. * and that the comparison works EQ, LT and GT */
  31472. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  31473. AssertNotNull(b = BN_new());
  31474. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  31475. AssertNotNull(c = BN_new());
  31476. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  31477. AssertIntEQ(BN_cmp(a, b), 0);
  31478. AssertIntLT(BN_cmp(a, c), 0);
  31479. AssertIntGT(BN_cmp(c, b), 0);
  31480. AssertIntEQ(BN_set_word(a, 0), 1);
  31481. AssertIntEQ(BN_is_zero(a), 1);
  31482. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  31483. AssertIntEQ(BN_is_zero(a), 0);
  31484. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  31485. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  31486. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  31487. AssertIntEQ(BN_is_zero(a), 1);
  31488. BN_free(a);
  31489. BN_free(b);
  31490. BN_free(c);
  31491. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  31492. {
  31493. BIGNUM *ap;
  31494. BIGNUM bv;
  31495. BIGNUM cv;
  31496. BIGNUM dv;
  31497. AssertNotNull(ap = BN_new());
  31498. BN_init(&bv);
  31499. BN_init(&cv);
  31500. BN_init(&dv);
  31501. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31502. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31503. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31504. /* a^b mod c = */
  31505. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31506. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31507. /* check result 3^2 mod 5 */
  31508. AssertIntEQ(BN_get_word(&dv), 4);
  31509. /* a*b mod c = */
  31510. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31511. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31512. /* check result 3*2 mod 5 */
  31513. AssertIntEQ(BN_get_word(&dv), 1);
  31514. BN_free(ap);
  31515. }
  31516. #endif
  31517. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31518. AssertNotNull(a = BN_new());
  31519. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  31520. SSL_SUCCESS);
  31521. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  31522. BN_free(a);
  31523. #endif
  31524. res = TEST_RES_CHECK(1);
  31525. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31526. return res;
  31527. }
  31528. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31529. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31530. #define TEST_ARG 0x1234
  31531. static void msg_cb(int write_p, int version, int content_type,
  31532. const void *buf, size_t len, SSL *ssl, void *arg)
  31533. {
  31534. (void)write_p;
  31535. (void)version;
  31536. (void)content_type;
  31537. (void)buf;
  31538. (void)len;
  31539. (void)ssl;
  31540. AssertTrue(arg == (void*)TEST_ARG);
  31541. }
  31542. #endif
  31543. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31544. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31545. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31546. !defined(NO_WOLFSSL_SERVER)
  31547. #ifndef SINGLE_THREADED
  31548. #if defined(SESSION_CERTS)
  31549. #include "wolfssl/internal.h"
  31550. #endif
  31551. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31552. {
  31553. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31554. STACK_OF(X509)* sk;
  31555. X509* x509;
  31556. int i, num;
  31557. BIO* bio;
  31558. #endif
  31559. (void) ctx;
  31560. fprintf(stderr, "\n===== msgcb called ====\n");
  31561. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31562. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31563. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31564. AssertNotNull(SSL_get0_verified_chain(ssl));
  31565. #else
  31566. (void) ssl;
  31567. #endif
  31568. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31569. bio = BIO_new(BIO_s_file());
  31570. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  31571. sk = SSL_get_peer_cert_chain(ssl);
  31572. AssertNotNull(sk);
  31573. if (!sk) {
  31574. BIO_free(bio);
  31575. return SSL_FAILURE;
  31576. }
  31577. num = sk_X509_num(sk);
  31578. AssertTrue(num > 0);
  31579. for (i = 0; i < num; i++) {
  31580. x509 = sk_X509_value(sk,i);
  31581. AssertNotNull(x509);
  31582. if (!x509)
  31583. break;
  31584. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  31585. X509_print(bio,x509);
  31586. fprintf(stderr, "\n\n");
  31587. }
  31588. BIO_free(bio);
  31589. #endif
  31590. return SSL_SUCCESS;
  31591. }
  31592. #endif
  31593. #endif
  31594. static int test_wolfSSL_msgCb(void)
  31595. {
  31596. int res = TEST_SKIPPED;
  31597. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31598. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31599. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31600. !defined(NO_WOLFSSL_SERVER)
  31601. tcp_ready ready;
  31602. func_args client_args;
  31603. func_args server_args;
  31604. #ifndef SINGLE_THREADED
  31605. THREAD_TYPE serverThread;
  31606. #endif
  31607. callback_functions client_cb;
  31608. callback_functions server_cb;
  31609. /* create a failed connection and inspect the error */
  31610. #ifdef WOLFSSL_TIRTOS
  31611. fdOpenSession(Task_self());
  31612. #endif
  31613. XMEMSET(&client_args, 0, sizeof(func_args));
  31614. XMEMSET(&server_args, 0, sizeof(func_args));
  31615. StartTCP();
  31616. InitTcpReady(&ready);
  31617. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31618. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31619. #ifndef WOLFSSL_NO_TLS12
  31620. client_cb.method = wolfTLSv1_2_client_method;
  31621. server_cb.method = wolfTLSv1_2_server_method;
  31622. #else
  31623. client_cb.method = wolfTLSv1_3_client_method;
  31624. server_cb.method = wolfTLSv1_3_server_method;
  31625. #endif
  31626. server_args.signal = &ready;
  31627. server_args.callbacks = &server_cb;
  31628. client_args.signal = &ready;
  31629. client_args.callbacks = &client_cb;
  31630. client_args.return_code = TEST_FAIL;
  31631. #ifndef SINGLE_THREADED
  31632. start_thread(test_server_nofail, &server_args, &serverThread);
  31633. wait_tcp_ready(&server_args);
  31634. test_client_nofail(&client_args, msgCb);
  31635. join_thread(serverThread);
  31636. #endif
  31637. FreeTcpReady(&ready);
  31638. #ifndef SINGLE_THREADED
  31639. AssertTrue(client_args.return_code);
  31640. AssertTrue(server_args.return_code);
  31641. #endif
  31642. #ifdef WOLFSSL_TIRTOS
  31643. fdOpenSession(Task_self());
  31644. #endif
  31645. res = TEST_RES_CHECK(1);
  31646. #endif
  31647. return res;
  31648. }
  31649. static int test_wolfSSL_either_side(void)
  31650. {
  31651. int res = TEST_SKIPPED;
  31652. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31653. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31654. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31655. tcp_ready ready;
  31656. func_args client_args;
  31657. func_args server_args;
  31658. #ifndef SINGLE_THREADED
  31659. THREAD_TYPE serverThread;
  31660. #endif
  31661. callback_functions client_cb;
  31662. callback_functions server_cb;
  31663. /* create a failed connection and inspect the error */
  31664. #ifdef WOLFSSL_TIRTOS
  31665. fdOpenSession(Task_self());
  31666. #endif
  31667. XMEMSET(&client_args, 0, sizeof(func_args));
  31668. XMEMSET(&server_args, 0, sizeof(func_args));
  31669. StartTCP();
  31670. InitTcpReady(&ready);
  31671. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31672. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31673. /* Use different CTX for client and server */
  31674. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31675. AssertNotNull(client_cb.ctx);
  31676. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31677. AssertNotNull(server_cb.ctx);
  31678. /* we are responsible for free'ing WOLFSSL_CTX */
  31679. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31680. server_args.signal = &ready;
  31681. server_args.callbacks = &server_cb;
  31682. client_args.signal = &ready;
  31683. client_args.callbacks = &client_cb;
  31684. client_args.return_code = TEST_FAIL;
  31685. #ifndef SINGLE_THREADED
  31686. start_thread(test_server_nofail, &server_args, &serverThread);
  31687. wait_tcp_ready(&server_args);
  31688. test_client_nofail(&client_args, NULL);
  31689. join_thread(serverThread);
  31690. #endif
  31691. wolfSSL_CTX_free(client_cb.ctx);
  31692. wolfSSL_CTX_free(server_cb.ctx);
  31693. FreeTcpReady(&ready);
  31694. #ifndef SINGLE_THREADED
  31695. AssertTrue(client_args.return_code);
  31696. AssertTrue(server_args.return_code);
  31697. #endif
  31698. #ifdef WOLFSSL_TIRTOS
  31699. fdOpenSession(Task_self());
  31700. #endif
  31701. res = TEST_RES_CHECK(1);
  31702. #endif
  31703. return res;
  31704. }
  31705. static int test_wolfSSL_DTLS_either_side(void)
  31706. {
  31707. int res = TEST_SKIPPED;
  31708. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31709. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31710. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  31711. defined(WOLFSSL_DTLS)
  31712. tcp_ready ready;
  31713. func_args client_args;
  31714. func_args server_args;
  31715. #ifndef SINGLE_THREADED
  31716. THREAD_TYPE serverThread;
  31717. #endif
  31718. callback_functions client_cb;
  31719. callback_functions server_cb;
  31720. /* create a failed connection and inspect the error */
  31721. #ifdef WOLFSSL_TIRTOS
  31722. fdOpenSession(Task_self());
  31723. #endif
  31724. XMEMSET(&client_args, 0, sizeof(func_args));
  31725. XMEMSET(&server_args, 0, sizeof(func_args));
  31726. StartTCP();
  31727. InitTcpReady(&ready);
  31728. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31729. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31730. /* Use different CTX for client and server */
  31731. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31732. AssertNotNull(client_cb.ctx);
  31733. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31734. AssertNotNull(server_cb.ctx);
  31735. /* we are responsible for free'ing WOLFSSL_CTX */
  31736. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31737. server_args.signal = &ready;
  31738. server_args.callbacks = &server_cb;
  31739. client_args.signal = &ready;
  31740. client_args.callbacks = &client_cb;
  31741. client_args.return_code = TEST_FAIL;
  31742. #ifndef SINGLE_THREADED
  31743. start_thread(test_server_nofail, &server_args, &serverThread);
  31744. wait_tcp_ready(&server_args);
  31745. test_client_nofail(&client_args, NULL);
  31746. join_thread(serverThread);
  31747. #endif
  31748. wolfSSL_CTX_free(client_cb.ctx);
  31749. wolfSSL_CTX_free(server_cb.ctx);
  31750. FreeTcpReady(&ready);
  31751. #ifndef SINGLE_THREADED
  31752. AssertTrue(client_args.return_code);
  31753. AssertTrue(server_args.return_code);
  31754. #endif
  31755. #ifdef WOLFSSL_TIRTOS
  31756. fdOpenSession(Task_self());
  31757. #endif
  31758. res = TEST_RES_CHECK(1);
  31759. #endif
  31760. return res;
  31761. }
  31762. static int test_generate_cookie(void)
  31763. {
  31764. int res = TEST_SKIPPED;
  31765. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  31766. SSL_CTX* ctx;
  31767. SSL* ssl;
  31768. byte buf[FOURK_BUF] = {0};
  31769. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  31770. AssertNotNull(ssl = SSL_new(ctx));
  31771. /* Test unconnected */
  31772. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  31773. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  31774. wolfSSL_SetCookieCtx(ssl, ctx);
  31775. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  31776. AssertNull(wolfSSL_GetCookieCtx(NULL));
  31777. SSL_free(ssl);
  31778. SSL_CTX_free(ctx);
  31779. res = TEST_RES_CHECK(1);
  31780. #endif
  31781. return res;
  31782. }
  31783. static int test_wolfSSL_set_options(void)
  31784. {
  31785. int res = TEST_SKIPPED;
  31786. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31787. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31788. WOLFSSL* ssl;
  31789. WOLFSSL_CTX* ctx;
  31790. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31791. char appData[] = "extra msg";
  31792. #endif
  31793. #ifdef OPENSSL_EXTRA
  31794. unsigned char protos[] = {
  31795. 7, 't', 'l', 's', '/', '1', '.', '2',
  31796. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  31797. };
  31798. unsigned int len = sizeof(protos);
  31799. void *arg = (void *)TEST_ARG;
  31800. #endif
  31801. #ifndef NO_WOLFSSL_SERVER
  31802. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31803. #else
  31804. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31805. #endif
  31806. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31807. WOLFSSL_FILETYPE_PEM));
  31808. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31809. WOLFSSL_FILETYPE_PEM));
  31810. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  31811. == WOLFSSL_OP_NO_TLSv1);
  31812. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  31813. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31814. WOLFSSL_OP_NO_SSLv2)), 0);
  31815. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31816. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31817. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  31818. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31819. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31820. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31821. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31822. WOLFSSL_OP_NO_COMPRESSION));
  31823. wolfSSL_CTX_free(ctx);
  31824. #ifndef NO_WOLFSSL_SERVER
  31825. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  31826. AssertNotNull(ctx);
  31827. #else
  31828. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31829. AssertNotNull(ctx);
  31830. #endif
  31831. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31832. WOLFSSL_FILETYPE_PEM));
  31833. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31834. WOLFSSL_FILETYPE_PEM));
  31835. #ifdef OPENSSL_EXTRA
  31836. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  31837. #endif
  31838. AssertNotNull(ssl = wolfSSL_new(ctx));
  31839. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31840. #ifdef HAVE_EX_DATA
  31841. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  31842. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31843. if (ssl) {
  31844. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  31845. appData, sizeof(appData)), 0);
  31846. }
  31847. #else
  31848. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  31849. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31850. #endif
  31851. #endif
  31852. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  31853. WOLFSSL_OP_NO_TLSv1);
  31854. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  31855. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31856. WOLFSSL_OP_NO_SSLv2)), 0);
  31857. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31858. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31859. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  31860. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31861. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31862. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31863. #ifdef OPENSSL_EXTRA
  31864. AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31865. WOLFSSL_OP_NO_COMPRESSION));
  31866. #endif
  31867. #ifdef OPENSSL_EXTRA
  31868. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  31869. wolfSSL_set_msg_callback_arg(ssl, arg);
  31870. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31871. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  31872. #else
  31873. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  31874. #endif
  31875. #endif
  31876. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  31877. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  31878. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  31879. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  31880. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31881. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  31882. #else
  31883. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  31884. #endif
  31885. #endif /* HAVE_ALPN && !NO_BIO */
  31886. #endif
  31887. wolfSSL_free(ssl);
  31888. wolfSSL_CTX_free(ctx);
  31889. res = TEST_RES_CHECK(1);
  31890. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31891. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31892. return res;
  31893. }
  31894. static int test_wolfSSL_sk_SSL_CIPHER(void)
  31895. {
  31896. int res = TEST_SKIPPED;
  31897. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31898. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31899. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31900. SSL* ssl;
  31901. SSL_CTX* ctx;
  31902. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  31903. #ifndef NO_WOLFSSL_SERVER
  31904. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31905. #else
  31906. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31907. #endif
  31908. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31909. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31910. AssertNotNull(ssl = SSL_new(ctx));
  31911. AssertNotNull(sk = SSL_get_ciphers(ssl));
  31912. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  31913. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  31914. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  31915. /* error case because connection has not been established yet */
  31916. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  31917. sk_SSL_CIPHER_free(dupSk);
  31918. /* sk is pointer to internal struct that should be free'd in SSL_free */
  31919. SSL_free(ssl);
  31920. SSL_CTX_free(ctx);
  31921. res = TEST_RES_CHECK(1);
  31922. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31923. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31924. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31925. return res;
  31926. }
  31927. static int test_wolfSSL_set1_curves_list(void)
  31928. {
  31929. int res = TEST_SKIPPED;
  31930. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31931. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31932. SSL* ssl = NULL;
  31933. SSL_CTX* ctx = NULL;
  31934. #ifndef NO_WOLFSSL_SERVER
  31935. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31936. #else
  31937. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31938. #endif
  31939. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  31940. SSL_FILETYPE_PEM));
  31941. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  31942. AssertNotNull(ssl = SSL_new(ctx));
  31943. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  31944. #ifdef HAVE_ECC
  31945. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  31946. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  31947. #endif
  31948. #ifdef HAVE_CURVE25519
  31949. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  31950. #else
  31951. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  31952. #endif
  31953. #ifdef HAVE_CURVE448
  31954. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  31955. #else
  31956. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  31957. #endif
  31958. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  31959. #ifdef HAVE_ECC
  31960. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  31961. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  31962. #endif
  31963. #ifdef HAVE_CURVE25519
  31964. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  31965. #else
  31966. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  31967. #endif
  31968. #ifdef HAVE_CURVE448
  31969. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  31970. #else
  31971. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  31972. #endif
  31973. SSL_free(ssl);
  31974. SSL_CTX_free(ctx);
  31975. res = TEST_RES_CHECK(1);
  31976. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31977. #endif
  31978. return res;
  31979. }
  31980. static int test_wolfSSL_set1_sigalgs_list(void)
  31981. {
  31982. int res = TEST_SKIPPED;
  31983. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  31984. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31985. SSL* ssl;
  31986. SSL_CTX* ctx;
  31987. #ifndef NO_WOLFSSL_SERVER
  31988. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31989. #else
  31990. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31991. #endif
  31992. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31993. SSL_FILETYPE_PEM));
  31994. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31995. AssertNotNull(ssl = SSL_new(ctx));
  31996. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31997. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  31998. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31999. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  32000. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  32001. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  32002. #ifndef NO_RSA
  32003. #ifndef NO_SHA256
  32004. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32005. WOLFSSL_FAILURE);
  32006. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32007. WOLFSSL_FAILURE);
  32008. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  32009. WOLFSSL_SUCCESS);
  32010. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  32011. WOLFSSL_SUCCESS);
  32012. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  32013. WOLFSSL_FAILURE);
  32014. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  32015. WOLFSSL_FAILURE);
  32016. #ifdef WC_RSA_PSS
  32017. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  32018. WOLFSSL_SUCCESS);
  32019. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  32020. WOLFSSL_SUCCESS);
  32021. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  32022. WOLFSSL_SUCCESS);
  32023. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  32024. WOLFSSL_SUCCESS);
  32025. #endif
  32026. #ifdef WOLFSSL_SHA512
  32027. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32028. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32029. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32030. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32031. #elif defined(WOLFSSL_SHA384)
  32032. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32033. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32034. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32035. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32036. #endif
  32037. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  32038. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  32039. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  32040. WOLFSSL_FAILURE);
  32041. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  32042. WOLFSSL_FAILURE);
  32043. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  32044. WOLFSSL_FAILURE);
  32045. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  32046. WOLFSSL_FAILURE);
  32047. #endif
  32048. #endif
  32049. #ifdef HAVE_ECC
  32050. #ifndef NO_SHA256
  32051. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  32052. WOLFSSL_SUCCESS);
  32053. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  32054. #ifdef WOLFSSL_SHA512
  32055. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32056. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32057. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32058. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32059. #elif defined(WOLFSSL_SHA384)
  32060. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32061. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32062. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32063. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32064. #endif
  32065. #endif
  32066. #endif
  32067. #ifdef HAVE_ED25519
  32068. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  32069. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  32070. #endif
  32071. #ifdef HAVE_ED448
  32072. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  32073. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  32074. #endif
  32075. #ifndef NO_DSA
  32076. #ifndef NO_SHA256
  32077. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  32078. WOLFSSL_SUCCESS);
  32079. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  32080. WOLFSSL_SUCCESS);
  32081. #endif
  32082. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  32083. defined(WOLFSSL_ALLOW_TLS_SHA1))
  32084. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  32085. WOLFSSL_SUCCESS);
  32086. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  32087. WOLFSSL_SUCCESS);
  32088. #endif
  32089. #endif
  32090. SSL_free(ssl);
  32091. SSL_CTX_free(ctx);
  32092. res = TEST_RES_CHECK(1);
  32093. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32094. #endif
  32095. return res;
  32096. }
  32097. /* Testing wolfSSL_set_tlsext_status_type function.
  32098. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  32099. */
  32100. static int test_wolfSSL_set_tlsext_status_type(void)
  32101. {
  32102. int res = TEST_SKIPPED;
  32103. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  32104. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  32105. SSL* ssl;
  32106. SSL_CTX* ctx;
  32107. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32108. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32109. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32110. AssertNotNull(ssl = SSL_new(ctx));
  32111. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  32112. SSL_SUCCESS);
  32113. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  32114. SSL_free(ssl);
  32115. SSL_CTX_free(ctx);
  32116. res = TEST_RES_CHECK(1);
  32117. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32118. return res;
  32119. }
  32120. #ifndef NO_BIO
  32121. static int test_wolfSSL_PEM_read_bio(void)
  32122. {
  32123. int res = TEST_SKIPPED;
  32124. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32125. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32126. byte buff[6000];
  32127. XFILE f;
  32128. int bytes;
  32129. X509* x509;
  32130. BIO* bio = NULL;
  32131. BUF_MEM* buf;
  32132. f = XFOPEN(cliCertFile, "rb");
  32133. AssertTrue((f != XBADFILE));
  32134. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  32135. XFCLOSE(f);
  32136. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32137. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32138. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32139. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32140. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32141. /* BIO should return the set EOF value */
  32142. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32143. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32144. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32145. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32146. BIO_free(bio);
  32147. BUF_MEM_free(buf);
  32148. X509_free(x509);
  32149. res = TEST_RES_CHECK(1);
  32150. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32151. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32152. return res;
  32153. }
  32154. #if defined(OPENSSL_EXTRA)
  32155. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32156. long argl, long ret)
  32157. {
  32158. (void)bio;
  32159. (void)cmd;
  32160. (void)argp;
  32161. (void)argi;
  32162. (void)argl;
  32163. return ret;
  32164. }
  32165. #endif
  32166. static int test_wolfSSL_BIO(void)
  32167. {
  32168. int res = TEST_SKIPPED;
  32169. #if defined(OPENSSL_EXTRA)
  32170. const unsigned char* p;
  32171. byte buff[20];
  32172. BIO* bio1;
  32173. BIO* bio2;
  32174. BIO* bio3;
  32175. char* bufPt;
  32176. int i;
  32177. for (i = 0; i < 20; i++) {
  32178. buff[i] = i;
  32179. }
  32180. /* test BIO_free with NULL */
  32181. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32182. /* Creating and testing type BIO_s_bio */
  32183. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32184. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  32185. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  32186. /* read/write before set up */
  32187. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32188. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32189. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  32190. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32191. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32192. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32193. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32194. XMEMCPY(bufPt, buff, 10);
  32195. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32196. /* write buffer full */
  32197. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32198. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32199. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32200. /* write the other direction with pair */
  32201. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32202. XMEMCPY(bufPt, buff, 8);
  32203. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32204. /* try read */
  32205. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32206. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32207. /* try read using ctrl function */
  32208. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32209. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32210. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32211. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32212. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32213. for (i = 0; i < 20; i++) {
  32214. AssertIntEQ((int)bufPt[i], i);
  32215. }
  32216. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32217. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32218. for (i = 0; i < 8; i++) {
  32219. AssertIntEQ((int)bufPt[i], i);
  32220. }
  32221. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32222. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32223. /* new pair */
  32224. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32225. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32226. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32227. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32228. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32229. /* test wrap around... */
  32230. AssertIntEQ(BIO_reset(bio1), 0);
  32231. AssertIntEQ(BIO_reset(bio3), 0);
  32232. /* fill write buffer, read only small amount then write again */
  32233. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32234. XMEMCPY(bufPt, buff, 20);
  32235. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32236. for (i = 0; i < 4; i++) {
  32237. AssertIntEQ(bufPt[i], i);
  32238. }
  32239. /* try writing over read index */
  32240. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32241. XMEMSET(bufPt, 0, 4);
  32242. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32243. /* read and write 0 bytes */
  32244. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32245. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32246. /* should read only to end of write buffer then need to read again */
  32247. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32248. for (i = 0; i < 16; i++) {
  32249. AssertIntEQ(bufPt[i], buff[4 + i]);
  32250. }
  32251. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32252. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32253. for (i = 0; i < 4; i++) {
  32254. AssertIntEQ(bufPt[i], 0);
  32255. }
  32256. /* read index should not have advanced with nread0 */
  32257. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32258. for (i = 0; i < 4; i++) {
  32259. AssertIntEQ(bufPt[i], 0);
  32260. }
  32261. /* write and fill up buffer checking reset of index state */
  32262. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32263. XMEMCPY(bufPt, buff, 20);
  32264. /* test reset on data in bio1 write buffer */
  32265. AssertIntEQ(BIO_reset(bio1), 0);
  32266. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32267. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32268. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32269. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32270. AssertNotNull(p);
  32271. XMEMCPY(bufPt, buff, 20);
  32272. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32273. for (i = 0; i < 6; i++) {
  32274. AssertIntEQ(bufPt[i], i);
  32275. }
  32276. /* test case of writing twice with offset read index */
  32277. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32278. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32279. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32280. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32281. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32282. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32283. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32284. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32285. BIO_free(bio1);
  32286. BIO_free(bio3);
  32287. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32288. {
  32289. BIO* bioA = NULL;
  32290. BIO* bioB = NULL;
  32291. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32292. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32293. BIO_free(bioA);
  32294. bioA = NULL;
  32295. BIO_free(bioB);
  32296. bioB = NULL;
  32297. }
  32298. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32299. /* BIOs with file pointers */
  32300. #if !defined(NO_FILESYSTEM)
  32301. {
  32302. XFILE f1;
  32303. XFILE f2;
  32304. BIO* f_bio1;
  32305. BIO* f_bio2;
  32306. unsigned char cert[300];
  32307. char testFile[] = "tests/bio_write_test.txt";
  32308. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32309. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32310. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32311. /* Failure due to wrong BIO type */
  32312. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32313. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32314. f1 = XFOPEN(svrCertFile, "rwb");
  32315. AssertTrue((f1 != XBADFILE));
  32316. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32317. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  32318. WOLFSSL_SUCCESS);
  32319. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32320. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32321. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32322. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32323. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32324. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32325. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32326. AssertIntEQ(BIO_reset(f_bio2), 0);
  32327. AssertIntEQ(BIO_tell(NULL),-1);
  32328. AssertIntEQ(BIO_tell(f_bio2),0);
  32329. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  32330. AssertIntEQ(BIO_tell(f_bio2),4);
  32331. BIO_free(f_bio1);
  32332. BIO_free(f_bio2);
  32333. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32334. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32335. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32336. BIO_free(f_bio1);
  32337. }
  32338. #endif /* !defined(NO_FILESYSTEM) */
  32339. /* BIO info callback */
  32340. {
  32341. const char* testArg = "test";
  32342. BIO* cb_bio;
  32343. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32344. BIO_set_callback(cb_bio, bioCallback);
  32345. AssertNotNull(BIO_get_callback(cb_bio));
  32346. BIO_set_callback(cb_bio, NULL);
  32347. AssertNull(BIO_get_callback(cb_bio));
  32348. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32349. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32350. AssertNull(BIO_get_callback_arg(NULL));
  32351. BIO_free(cb_bio);
  32352. }
  32353. /* BIO_vfree */
  32354. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32355. BIO_vfree(NULL);
  32356. BIO_vfree(bio1);
  32357. res = TEST_RES_CHECK(1);
  32358. #endif
  32359. return res;
  32360. }
  32361. #endif /* !NO_BIO */
  32362. static int test_wolfSSL_ASN1_STRING(void)
  32363. {
  32364. int res = TEST_SKIPPED;
  32365. #if defined(OPENSSL_EXTRA)
  32366. ASN1_STRING* str = NULL;
  32367. const char data[] = "hello wolfSSL";
  32368. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  32369. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  32370. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  32371. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  32372. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  32373. ASN1_STRING_free(str);
  32374. res = TEST_RES_CHECK(1);
  32375. #endif
  32376. return res;
  32377. }
  32378. static int test_wolfSSL_ASN1_BIT_STRING(void)
  32379. {
  32380. int res = TEST_SKIPPED;
  32381. #ifdef OPENSSL_ALL
  32382. ASN1_BIT_STRING* str;
  32383. AssertNotNull(str = ASN1_BIT_STRING_new());
  32384. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  32385. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  32386. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  32387. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  32388. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  32389. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  32390. ASN1_BIT_STRING_free(str);
  32391. res = TEST_RES_CHECK(1);
  32392. #endif
  32393. return res;
  32394. }
  32395. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  32396. {
  32397. int res = TEST_SKIPPED;
  32398. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  32399. BIO *bio, *out;
  32400. ASN1_INTEGER* ai;
  32401. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  32402. char tmp[1024];
  32403. int tmpSz;
  32404. const char expected1[] = "123456";
  32405. const char expected2[] = "112345678912345678901234567890";
  32406. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  32407. AssertNotNull(out = BIO_new(BIO_s_mem()));
  32408. AssertNotNull(ai = ASN1_INTEGER_new());
  32409. /* read first line */
  32410. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32411. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  32412. XMEMSET(tmp, 0, 1024);
  32413. tmpSz = BIO_read(out, tmp, 1024);
  32414. AssertIntEQ(tmpSz, 6);
  32415. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  32416. /* fail on second line (not % 2) */
  32417. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32418. /* read 3rd long line */
  32419. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32420. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  32421. XMEMSET(tmp, 0, 1024);
  32422. tmpSz = BIO_read(out, tmp, 1024);
  32423. AssertIntEQ(tmpSz, 30);
  32424. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  32425. BIO_free(out);
  32426. BIO_free(bio);
  32427. ASN1_INTEGER_free(ai);
  32428. res = TEST_RES_CHECK(1);
  32429. #endif
  32430. return res;
  32431. }
  32432. static int test_wolfSSL_a2i_IPADDRESS(void)
  32433. {
  32434. int res = TEST_SKIPPED;
  32435. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32436. const unsigned char* data;
  32437. int dataSz = 0;
  32438. ASN1_OCTET_STRING *st;
  32439. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32440. const unsigned char ipv6_exp[] = {
  32441. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32442. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32443. };
  32444. const unsigned char ipv6_home[] = {
  32445. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32446. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32447. };
  32448. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32449. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32450. data = ASN1_STRING_get0_data(st);
  32451. dataSz = ASN1_STRING_length(st);
  32452. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  32453. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32454. ASN1_STRING_free(st);
  32455. AssertNotNull(st = a2i_IPADDRESS("::1"));
  32456. data = ASN1_STRING_get0_data(st);
  32457. dataSz = ASN1_STRING_length(st);
  32458. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32459. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32460. ASN1_STRING_free(st);
  32461. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32462. data = ASN1_STRING_get0_data(st);
  32463. dataSz = ASN1_STRING_length(st);
  32464. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32465. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32466. ASN1_STRING_free(st);
  32467. res = TEST_RES_CHECK(1);
  32468. #endif
  32469. return res;
  32470. }
  32471. static int test_wolfSSL_DES_ecb_encrypt(void)
  32472. {
  32473. int res = TEST_SKIPPED;
  32474. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32475. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  32476. WOLFSSL_DES_key_schedule key;
  32477. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  32478. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  32479. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  32480. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32481. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32482. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32483. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32484. /* Encrypt messages */
  32485. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  32486. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  32487. {
  32488. /* Decrypt messages */
  32489. int ret1 = 0;
  32490. int ret2 = 0;
  32491. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  32492. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  32493. AssertIntEQ(ret1,0);
  32494. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  32495. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  32496. AssertIntEQ(ret2,0);
  32497. }
  32498. res = TEST_RES_CHECK(1);
  32499. #endif
  32500. return res;
  32501. }
  32502. static int test_wolfSSL_ASN1_TIME_adj(void)
  32503. {
  32504. int res = TEST_SKIPPED;
  32505. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32506. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32507. const int year = 365*24*60*60;
  32508. const int day = 24*60*60;
  32509. const int hour = 60*60;
  32510. const int mini = 60;
  32511. const byte asn_utc_time = ASN_UTC_TIME;
  32512. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32513. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  32514. #endif
  32515. WOLFSSL_ASN1_TIME *asn_time, *s;
  32516. int offset_day;
  32517. long offset_sec;
  32518. char date_str[CTC_DATE_SIZE + 1];
  32519. time_t t;
  32520. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  32521. /* UTC notation test */
  32522. /* 2000/2/15 20:30:00 */
  32523. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32524. offset_day = 7;
  32525. offset_sec = 45 * mini;
  32526. /* offset_sec = -45 * min;*/
  32527. AssertNotNull(asn_time =
  32528. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  32529. AssertTrue(asn_time->type == asn_utc_time);
  32530. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32531. date_str[CTC_DATE_SIZE] = '\0';
  32532. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  32533. /* negative offset */
  32534. offset_sec = -45 * mini;
  32535. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32536. AssertNotNull(asn_time);
  32537. AssertTrue(asn_time->type == asn_utc_time);
  32538. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32539. date_str[CTC_DATE_SIZE] = '\0';
  32540. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  32541. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32542. XMEMSET(date_str, 0, sizeof(date_str));
  32543. /* Generalized time will overflow time_t if not long */
  32544. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32545. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  32546. DYNAMIC_TYPE_OPENSSL);
  32547. /* GeneralizedTime notation test */
  32548. /* 2055/03/01 09:00:00 */
  32549. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  32550. offset_day = 12;
  32551. offset_sec = 10 * mini;
  32552. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32553. AssertTrue(asn_time->type == asn_gen_time);
  32554. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32555. date_str[CTC_DATE_SIZE] = '\0';
  32556. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  32557. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32558. XMEMSET(date_str, 0, sizeof(date_str));
  32559. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  32560. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  32561. s = NULL;
  32562. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  32563. offset_day = 7;
  32564. offset_sec = 45 * mini;
  32565. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32566. AssertTrue(asn_time->type == asn_utc_time);
  32567. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32568. date_str[CTC_DATE_SIZE] = '\0';
  32569. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32570. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32571. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  32572. AssertTrue(asn_time->type == asn_utc_time);
  32573. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32574. date_str[CTC_DATE_SIZE] = '\0';
  32575. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32576. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32577. res = TEST_RES_CHECK(1);
  32578. #endif
  32579. return res;
  32580. }
  32581. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  32582. {
  32583. int res = TEST_SKIPPED;
  32584. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  32585. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  32586. && !defined(NO_ASN_TIME)
  32587. ASN1_TIME asnTime;
  32588. struct tm tm;
  32589. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  32590. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  32591. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  32592. AssertIntEQ(tm.tm_sec, 15);
  32593. AssertIntEQ(tm.tm_min, 15);
  32594. AssertIntEQ(tm.tm_hour, 21);
  32595. AssertIntEQ(tm.tm_mday, 22);
  32596. AssertIntEQ(tm.tm_mon, 1);
  32597. AssertIntEQ(tm.tm_year, 100);
  32598. AssertIntEQ(tm.tm_isdst, 0);
  32599. #ifdef XMKTIME
  32600. AssertIntEQ(tm.tm_wday, 2);
  32601. AssertIntEQ(tm.tm_yday, 52);
  32602. #endif
  32603. res = TEST_RES_CHECK(1);
  32604. #endif
  32605. return res;
  32606. }
  32607. static int test_wolfSSL_X509_cmp_time(void)
  32608. {
  32609. int res = TEST_SKIPPED;
  32610. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32611. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32612. WOLFSSL_ASN1_TIME asn_time;
  32613. time_t t;
  32614. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32615. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32616. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32617. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32618. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32619. res = TEST_RES_CHECK(1);
  32620. #endif
  32621. return res;
  32622. }
  32623. static int test_wolfSSL_X509_time_adj(void)
  32624. {
  32625. int res = TEST_SKIPPED;
  32626. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32627. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32628. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32629. !defined(NO_ASN_TIME)
  32630. X509* x509;
  32631. time_t t, not_before, not_after;
  32632. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32633. client_cert_der_2048, sizeof_client_cert_der_2048,
  32634. WOLFSSL_FILETYPE_ASN1));
  32635. t = 0;
  32636. not_before = wc_Time(0);
  32637. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32638. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32639. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32640. /* Check X509_gmtime_adj, too. */
  32641. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32642. X509_free(x509);
  32643. res = TEST_RES_CHECK(1);
  32644. #endif
  32645. return res;
  32646. }
  32647. static int test_wolfSSL_X509(void)
  32648. {
  32649. int res = TEST_SKIPPED;
  32650. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  32651. && !defined(NO_RSA)
  32652. X509* x509;
  32653. #ifndef NO_BIO
  32654. BIO* bio;
  32655. X509_STORE_CTX* ctx;
  32656. X509_STORE* store;
  32657. #endif
  32658. char der[] = "certs/ca-cert.der";
  32659. XFILE fp;
  32660. AssertNotNull(x509 = X509_new());
  32661. X509_free(x509);
  32662. #ifndef NO_BIO
  32663. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  32664. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32665. #ifdef WOLFSSL_CERT_GEN
  32666. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32667. #endif
  32668. AssertNotNull(ctx = X509_STORE_CTX_new());
  32669. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32670. AssertNotNull(store = X509_STORE_new());
  32671. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32672. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32673. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32674. X509_STORE_CTX_free(ctx);
  32675. X509_STORE_free(store);
  32676. X509_free(x509);
  32677. BIO_free(bio);
  32678. #endif
  32679. /** d2i_X509_fp test **/
  32680. fp = XFOPEN(der, "rb");
  32681. AssertTrue((fp != XBADFILE));
  32682. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32683. AssertNotNull(x509);
  32684. X509_free(x509);
  32685. XFCLOSE(fp);
  32686. fp = XFOPEN(der, "rb");
  32687. AssertTrue((fp != XBADFILE));
  32688. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32689. AssertNotNull(x509);
  32690. X509_free(x509);
  32691. XFCLOSE(fp);
  32692. /* X509_up_ref test */
  32693. AssertIntEQ(X509_up_ref(NULL), 0);
  32694. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  32695. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32696. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32697. X509_free(x509); /* refCount = 2 */
  32698. X509_free(x509); /* refCount = 1 */
  32699. X509_free(x509); /* refCount = 0, free */
  32700. res = TEST_RES_CHECK(1);
  32701. #endif
  32702. return res;
  32703. }
  32704. static int test_wolfSSL_X509_get_ext_count(void)
  32705. {
  32706. int res = TEST_SKIPPED;
  32707. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32708. !defined(NO_RSA)
  32709. int ret = 0;
  32710. WOLFSSL_X509* x509;
  32711. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  32712. FILE* f;
  32713. /* NULL parameter check */
  32714. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  32715. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32716. SSL_FILETYPE_PEM));
  32717. AssertIntEQ(X509_get_ext_count(x509), 5);
  32718. wolfSSL_X509_free(x509);
  32719. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  32720. SSL_FILETYPE_PEM));
  32721. AssertIntEQ(X509_get_ext_count(x509), 5);
  32722. wolfSSL_X509_free(x509);
  32723. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  32724. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  32725. fclose(f);
  32726. /* wolfSSL_X509_get_ext_count() valid input */
  32727. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  32728. /* wolfSSL_X509_get_ext_count() NULL argument */
  32729. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  32730. wolfSSL_X509_free(x509);
  32731. res = TEST_RES_CHECK(1);
  32732. #endif
  32733. return res;
  32734. }
  32735. static int test_wolfSSL_X509_sign2(void)
  32736. {
  32737. int res = TEST_SKIPPED;
  32738. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32739. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  32740. defined(WOLFSSL_CERT_EXT) && \
  32741. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32742. WOLFSSL_X509 *x509, *ca;
  32743. const unsigned char *der;
  32744. const unsigned char *pt;
  32745. WOLFSSL_EVP_PKEY *priv;
  32746. WOLFSSL_X509_NAME *name;
  32747. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  32748. int derSz;
  32749. const int year = 365*24*60*60;
  32750. const int day = 24*60*60;
  32751. const int hour = 60*60;
  32752. const int mini = 60;
  32753. time_t t;
  32754. const unsigned char expected[] = {
  32755. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  32756. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  32757. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  32758. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  32759. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  32760. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  32761. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  32762. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  32763. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  32764. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  32765. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  32766. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  32767. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  32768. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  32769. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32770. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  32771. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  32772. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  32773. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  32774. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  32775. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  32776. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  32777. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  32778. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  32779. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  32780. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  32781. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  32782. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  32783. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  32784. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  32785. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  32786. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  32787. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  32788. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32789. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  32790. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  32791. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  32792. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  32793. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  32794. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  32795. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  32796. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  32797. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  32798. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  32799. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  32800. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  32801. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  32802. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  32803. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  32804. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  32805. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  32806. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  32807. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  32808. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  32809. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  32810. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  32811. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  32812. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  32813. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  32814. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  32815. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  32816. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  32817. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32818. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32819. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  32820. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32821. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32822. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  32823. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  32824. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  32825. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  32826. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  32827. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  32828. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  32829. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  32830. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  32831. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  32832. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  32833. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  32834. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  32835. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  32836. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  32837. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  32838. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  32839. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  32840. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  32841. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  32842. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  32843. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  32844. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  32845. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  32846. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  32847. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  32848. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  32849. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  32850. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  32851. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  32852. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  32853. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  32854. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  32855. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  32856. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  32857. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  32858. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  32859. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  32860. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  32861. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  32862. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  32863. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  32864. };
  32865. pt = ca_key_der_2048;
  32866. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  32867. sizeof_ca_key_der_2048));
  32868. pt = client_cert_der_2048;
  32869. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  32870. sizeof_client_cert_der_2048));
  32871. pt = ca_cert_der_2048;
  32872. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  32873. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  32874. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32875. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32876. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  32877. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  32878. AssertIntEQ(notAfter->length, 13);
  32879. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  32880. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  32881. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  32882. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  32883. AssertIntEQ(derSz, sizeof(expected));
  32884. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  32885. wolfSSL_X509_free(ca);
  32886. wolfSSL_X509_free(x509);
  32887. wolfSSL_EVP_PKEY_free(priv);
  32888. wolfSSL_ASN1_TIME_free(notBefore);
  32889. wolfSSL_ASN1_TIME_free(notAfter);
  32890. res = TEST_RES_CHECK(1);
  32891. #endif
  32892. return res;
  32893. }
  32894. static int test_wolfSSL_X509_sign(void)
  32895. {
  32896. int res = TEST_SKIPPED;
  32897. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32898. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  32899. int ret;
  32900. char *cn;
  32901. word32 cnSz;
  32902. X509_NAME *name;
  32903. X509 *x509, *ca;
  32904. DecodedCert dCert;
  32905. EVP_PKEY *pub;
  32906. EVP_PKEY *priv;
  32907. EVP_MD_CTX *mctx;
  32908. #if defined(USE_CERT_BUFFERS_1024)
  32909. const unsigned char* rsaPriv = client_key_der_1024;
  32910. const unsigned char* rsaPub = client_keypub_der_1024;
  32911. const unsigned char* certIssuer = client_cert_der_1024;
  32912. long clientKeySz = (long)sizeof_client_key_der_1024;
  32913. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  32914. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  32915. #elif defined(USE_CERT_BUFFERS_2048)
  32916. const unsigned char* rsaPriv = client_key_der_2048;
  32917. const unsigned char* rsaPub = client_keypub_der_2048;
  32918. const unsigned char* certIssuer = client_cert_der_2048;
  32919. long clientKeySz = (long)sizeof_client_key_der_2048;
  32920. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  32921. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  32922. #endif
  32923. byte sn[16];
  32924. int snSz = sizeof(sn);
  32925. /* Set X509_NAME fields */
  32926. AssertNotNull(name = X509_NAME_new());
  32927. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  32928. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  32929. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32930. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  32931. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32932. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  32933. /* Get private and public keys */
  32934. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  32935. clientKeySz));
  32936. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  32937. AssertNotNull(x509 = X509_new());
  32938. /* Set version 3 */
  32939. AssertIntNE(X509_set_version(x509, 2L), 0);
  32940. /* Set subject name, add pubkey, and sign certificate */
  32941. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  32942. X509_NAME_free(name);
  32943. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  32944. #ifdef WOLFSSL_ALT_NAMES
  32945. /* Add some subject alt names */
  32946. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  32947. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  32948. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32949. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  32950. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32951. "sphygmomanometer",
  32952. ASN_DNS_TYPE), SSL_SUCCESS);
  32953. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32954. "supercalifragilisticexpialidocious",
  32955. ASN_DNS_TYPE), SSL_SUCCESS);
  32956. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32957. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32958. ASN_DNS_TYPE), SSL_SUCCESS);
  32959. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32960. {
  32961. unsigned char ip4_type[] = {127,128,0,255};
  32962. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32963. 0xff, 0xee, 0x99, 0x88,
  32964. 0x77, 0x66, 0x55, 0x44,
  32965. 0x00, 0x33, 0x22, 0x11};
  32966. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32967. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32968. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32969. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32970. }
  32971. #endif
  32972. #endif /* WOLFSSL_ALT_NAMES */
  32973. /* test valid sign case */
  32974. ret = X509_sign(x509, priv, EVP_sha256());
  32975. /* test valid X509_sign_ctx case */
  32976. AssertNotNull(mctx = EVP_MD_CTX_new());
  32977. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32978. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  32979. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32980. AssertIntEQ(X509_get_ext_count(x509), 1);
  32981. #endif
  32982. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32983. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32984. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32985. #endif
  32986. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32987. WOLFSSL_SUCCESS);
  32988. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32989. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32990. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  32991. * with the MSB set. See GenerateInteger in asn.c */
  32992. #ifndef USE_CERT_BUFFERS_1024
  32993. #ifndef WOLFSSL_ALT_NAMES
  32994. /* Valid case - size should be 798-797 with 16 byte serial number */
  32995. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  32996. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32997. /* Valid case - size should be 955-956 with 16 byte serial number */
  32998. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  32999. #else
  33000. /* Valid case - size should be 926-927 with 16 byte serial number */
  33001. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  33002. #endif
  33003. #else
  33004. #ifndef WOLFSSL_ALT_NAMES
  33005. /* Valid case - size should be 537-538 with 16 byte serial number */
  33006. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  33007. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33008. /* Valid case - size should be 695-696 with 16 byte serial number */
  33009. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  33010. #else
  33011. /* Valid case - size should be 666-667 with 16 byte serial number */
  33012. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  33013. #endif
  33014. #endif
  33015. /* check that issuer name is as expected after signature */
  33016. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  33017. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  33018. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  33019. AssertNotNull(name = X509_get_subject_name(ca));
  33020. cnSz = X509_NAME_get_sz(name);
  33021. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33022. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33023. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  33024. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33025. #ifdef WOLFSSL_MULTI_ATTRIB
  33026. /* test adding multiple OU's to the signer */
  33027. AssertNotNull(name = X509_get_subject_name(ca));
  33028. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33029. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  33030. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33031. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  33032. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  33033. #endif
  33034. AssertNotNull(name = X509_get_subject_name(ca));
  33035. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  33036. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33037. AssertNotNull(name = X509_get_issuer_name(x509));
  33038. cnSz = X509_NAME_get_sz(name);
  33039. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33040. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33041. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  33042. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  33043. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33044. FreeDecodedCert(&dCert);
  33045. /* Test invalid parameters */
  33046. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  33047. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  33048. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  33049. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  33050. EVP_MD_CTX_free(mctx);
  33051. AssertNotNull(mctx = EVP_MD_CTX_new());
  33052. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  33053. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  33054. /* test invalid version number */
  33055. #if defined(OPENSSL_ALL)
  33056. AssertIntNE(X509_set_version(x509, 6L), 0);
  33057. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33058. /* uses ParseCert which fails on bad version number */
  33059. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  33060. #endif
  33061. EVP_MD_CTX_free(mctx);
  33062. EVP_PKEY_free(priv);
  33063. EVP_PKEY_free(pub);
  33064. X509_free(x509);
  33065. X509_free(ca);
  33066. res = TEST_RES_CHECK(1);
  33067. #endif
  33068. return res;
  33069. }
  33070. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  33071. {
  33072. int res = TEST_SKIPPED;
  33073. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33074. X509* x509 = NULL;
  33075. const X509_ALGOR* alg;
  33076. AssertNotNull(x509 = X509_new());
  33077. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  33078. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33079. X509_free(x509);
  33080. res = TEST_RES_CHECK(1);
  33081. #endif
  33082. return res;
  33083. }
  33084. static int test_wolfSSL_X509_ALGOR_get0(void)
  33085. {
  33086. int res = TEST_SKIPPED;
  33087. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33088. !defined(NO_SHA256) && !defined(NO_RSA)
  33089. X509* x509 = NULL;
  33090. const ASN1_OBJECT* obj = NULL;
  33091. const X509_ALGOR* alg;
  33092. int pptype = 0;
  33093. const void *ppval = NULL;
  33094. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  33095. SSL_FILETYPE_PEM));
  33096. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  33097. /* Invalid case */
  33098. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  33099. AssertNull(obj);
  33100. /* Valid case */
  33101. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  33102. AssertNotNull(obj);
  33103. AssertNull(ppval);
  33104. AssertIntNE(pptype, 0);
  33105. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  33106. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  33107. X509_free(x509);
  33108. res = TEST_RES_CHECK(1);
  33109. #endif
  33110. return res;
  33111. }
  33112. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  33113. {
  33114. int res = TEST_SKIPPED;
  33115. #if defined(OPENSSL_EXTRA)
  33116. X509_VERIFY_PARAM *paramTo;
  33117. X509_VERIFY_PARAM *paramFrom;
  33118. int ret;
  33119. char testIPv4[] = "127.0.0.1";
  33120. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33121. char testhostName1[] = "foo.hoge.com";
  33122. char testhostName2[] = "foobar.hoge.com";
  33123. paramTo = X509_VERIFY_PARAM_new();
  33124. AssertNotNull(paramTo);
  33125. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  33126. paramFrom = X509_VERIFY_PARAM_new();
  33127. AssertNotNull(paramFrom);
  33128. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  33129. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33130. (int)XSTRLEN(testhostName1));
  33131. AssertIntEQ(1, ret);
  33132. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33133. (int)XSTRLEN(testhostName1)));
  33134. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33135. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33136. AssertIntEQ(0x01, paramFrom->hostFlags);
  33137. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  33138. AssertIntEQ(0, ret);
  33139. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  33140. AssertIntEQ(1, ret);
  33141. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33142. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  33143. AssertIntEQ(1, ret);
  33144. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  33145. AssertIntEQ(1, ret);
  33146. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33147. /* null pointer */
  33148. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  33149. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33150. /* in the case of "from" null, returns success */
  33151. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  33152. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  33153. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  33154. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33155. /* inherit flags test : VPARAM_DEFAULT */
  33156. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33157. AssertIntEQ(1, ret);
  33158. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33159. (int)XSTRLEN(testhostName1)));
  33160. AssertIntEQ(0x01, paramTo->hostFlags);
  33161. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33162. /* inherit flags test : VPARAM OVERWRITE */
  33163. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33164. (int)XSTRLEN(testhostName2));
  33165. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33166. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33167. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33168. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33169. AssertIntEQ(1, ret);
  33170. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33171. (int)XSTRLEN(testhostName1)));
  33172. AssertIntEQ(0x01, paramTo->hostFlags);
  33173. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33174. /* inherit flags test : VPARAM_RESET_FLAGS */
  33175. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33176. (int)XSTRLEN(testhostName2));
  33177. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33178. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33179. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33180. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33181. AssertIntEQ(1, ret);
  33182. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33183. (int)XSTRLEN(testhostName1)));
  33184. AssertIntEQ(0x01, paramTo->hostFlags);
  33185. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33186. /* inherit flags test : VPARAM_LOCKED */
  33187. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33188. (int)XSTRLEN(testhostName2));
  33189. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33190. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33191. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33192. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33193. AssertIntEQ(1, ret);
  33194. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33195. (int)XSTRLEN(testhostName2)));
  33196. AssertIntEQ(0x00, paramTo->hostFlags);
  33197. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33198. /* test for incorrect parameters */
  33199. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  33200. AssertIntEQ(0, ret);
  33201. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  33202. AssertIntEQ(0, ret);
  33203. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33204. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33205. AssertIntEQ(1, ret);
  33206. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33207. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  33208. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33209. AssertIntEQ(1, ret);
  33210. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33211. AssertIntEQ(0, ret);
  33212. X509_VERIFY_PARAM_free(paramTo);
  33213. X509_VERIFY_PARAM_free(paramFrom);
  33214. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33215. res = TEST_RES_CHECK(1);
  33216. #endif
  33217. return res;
  33218. }
  33219. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33220. static int test_wolfSSL_check_domain_verify_count = 0;
  33221. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33222. WOLFSSL_X509_STORE_CTX* store)
  33223. {
  33224. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  33225. AssertIntEQ(preverify, 1);
  33226. test_wolfSSL_check_domain_verify_count++;
  33227. return 1;
  33228. }
  33229. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33230. {
  33231. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  33232. /* Domain check should only be done on the leaf cert */
  33233. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33234. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33235. "wolfSSL Server Chain", 0), 1);
  33236. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33237. test_wolfSSL_check_domain_verify_cb);
  33238. }
  33239. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33240. {
  33241. /* Use a cert with different domains in chain */
  33242. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33243. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33244. }
  33245. static int test_wolfSSL_check_domain(void)
  33246. {
  33247. tcp_ready ready;
  33248. func_args client_args;
  33249. func_args server_args;
  33250. THREAD_TYPE serverThread;
  33251. callback_functions func_cb_client;
  33252. callback_functions func_cb_server;
  33253. XMEMSET(&client_args, 0, sizeof(func_args));
  33254. XMEMSET(&server_args, 0, sizeof(func_args));
  33255. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33256. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33257. #ifdef WOLFSSL_TIRTOS
  33258. fdOpenSession(Task_self());
  33259. #endif
  33260. StartTCP();
  33261. InitTcpReady(&ready);
  33262. #if defined(USE_WINDOWS_API)
  33263. /* use RNG to get random port if using windows */
  33264. ready.port = GetRandomPort();
  33265. #endif
  33266. server_args.signal = &ready;
  33267. client_args.signal = &ready;
  33268. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33269. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33270. client_args.callbacks = &func_cb_client;
  33271. server_args.callbacks = &func_cb_server;
  33272. start_thread(test_server_nofail, &server_args, &serverThread);
  33273. wait_tcp_ready(&server_args);
  33274. test_client_nofail(&client_args, NULL);
  33275. join_thread(serverThread);
  33276. AssertTrue(client_args.return_code);
  33277. AssertTrue(server_args.return_code);
  33278. FreeTcpReady(&ready);
  33279. /* Should have been called once for each cert in sent chain */
  33280. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33281. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33282. #else
  33283. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33284. #endif
  33285. return TEST_RES_CHECK(1);
  33286. }
  33287. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  33288. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33289. {
  33290. int res = TEST_SKIPPED;
  33291. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33292. X509* x509 = NULL;
  33293. X509_PUBKEY* pubKey;
  33294. AssertNotNull(x509 = X509_new());
  33295. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33296. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33297. X509_free(x509);
  33298. res = TEST_RES_CHECK(1);
  33299. #endif
  33300. return res;
  33301. }
  33302. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33303. {
  33304. int res = TEST_SKIPPED;
  33305. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33306. !defined(NO_SHA256) && !defined(NO_RSA)
  33307. X509* x509 = NULL;
  33308. ASN1_OBJECT* obj = NULL;
  33309. const ASN1_OBJECT* pa_oid = NULL;
  33310. X509_PUBKEY* pubKey;
  33311. X509_PUBKEY* pubKey2;
  33312. EVP_PKEY* evpKey;
  33313. const unsigned char *pk;
  33314. int ppklen, pptype;
  33315. X509_ALGOR *pa;
  33316. const void *pval;
  33317. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33318. SSL_FILETYPE_PEM));
  33319. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33320. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33321. AssertNotNull(pk);
  33322. AssertNotNull(pa);
  33323. AssertNotNull(pubKey);
  33324. AssertIntGT(ppklen, 0);
  33325. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33326. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33327. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33328. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33329. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33330. AssertNotNull(pk);
  33331. AssertNotNull(pa);
  33332. AssertIntGT(ppklen, 0);
  33333. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33334. AssertNotNull(pa_oid);
  33335. AssertNull(pval);
  33336. AssertIntEQ(pptype, V_ASN1_NULL);
  33337. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33338. X509_PUBKEY_free(pubKey2);
  33339. X509_free(x509);
  33340. EVP_PKEY_free(evpKey);
  33341. res = TEST_RES_CHECK(1);
  33342. #endif
  33343. return res;
  33344. }
  33345. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33346. {
  33347. int res = TEST_SKIPPED;
  33348. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33349. X509* x509 = NULL;
  33350. ASN1_OBJECT* obj = NULL;
  33351. ASN1_OBJECT* poid;
  33352. const ASN1_OBJECT* pa_oid = NULL;
  33353. X509_PUBKEY* pubKey;
  33354. X509_PUBKEY* pubKey2;
  33355. EVP_PKEY* evpKey;
  33356. const unsigned char *pk;
  33357. int ppklen, pptype;
  33358. X509_ALGOR *pa;
  33359. const void *pval;
  33360. char buf[50];
  33361. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33362. SSL_FILETYPE_PEM));
  33363. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33364. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33365. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33366. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33367. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33368. AssertNotNull(pk);
  33369. AssertNotNull(pa);
  33370. AssertIntGT(ppklen, 0);
  33371. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33372. AssertNotNull(pa_oid);
  33373. AssertNotNull(pval);
  33374. AssertIntEQ(pptype, V_ASN1_OBJECT);
  33375. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33376. poid = (ASN1_OBJECT *)pval;
  33377. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33378. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33379. X509_PUBKEY_free(pubKey2);
  33380. X509_free(x509);
  33381. EVP_PKEY_free(evpKey);
  33382. res = TEST_RES_CHECK(1);
  33383. #endif
  33384. return res;
  33385. }
  33386. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33387. {
  33388. int res = TEST_SKIPPED;
  33389. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33390. word32 bytes;
  33391. #ifdef USE_CERT_BUFFERS_1024
  33392. byte tmp[ONEK_BUF];
  33393. #elif defined(USE_CERT_BUFFERS_2048)
  33394. byte tmp[TWOK_BUF];
  33395. #else
  33396. byte tmp[TWOK_BUF];
  33397. #endif /* END USE_CERT_BUFFERS_1024 */
  33398. const unsigned char* dsaKeyDer = tmp;
  33399. ASN1_OBJECT* obj = NULL;
  33400. ASN1_STRING* str;
  33401. const ASN1_OBJECT* pa_oid = NULL;
  33402. X509_PUBKEY* pubKey = NULL;
  33403. EVP_PKEY* evpKey = NULL;
  33404. const unsigned char *pk;
  33405. int ppklen, pptype;
  33406. X509_ALGOR *pa;
  33407. const void *pval;
  33408. #ifdef USE_CERT_BUFFERS_1024
  33409. XMEMSET(tmp, 0, sizeof(tmp));
  33410. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33411. bytes = sizeof_dsa_key_der_1024;
  33412. #elif defined(USE_CERT_BUFFERS_2048)
  33413. XMEMSET(tmp, 0, sizeof(tmp));
  33414. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33415. bytes = sizeof_dsa_key_der_2048;
  33416. #else
  33417. {
  33418. XFILE fp;
  33419. XMEMSET(tmp, 0, sizeof(tmp));
  33420. fp = XFOPEN("./certs/dsa2048.der", "rb");
  33421. if (fp == XBADFILE) {
  33422. return WOLFSSL_BAD_FILE;
  33423. }
  33424. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  33425. XFCLOSE(fp);
  33426. }
  33427. #endif
  33428. /* Initialize pkey with der format dsa key */
  33429. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33430. AssertNotNull(pubKey = X509_PUBKEY_new());
  33431. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33432. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33433. AssertNotNull(pk);
  33434. AssertNotNull(pa);
  33435. AssertIntGT(ppklen, 0);
  33436. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33437. AssertNotNull(pa_oid);
  33438. AssertNotNull(pval);
  33439. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  33440. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33441. str = (ASN1_STRING *)pval;
  33442. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33443. #ifdef USE_CERT_BUFFERS_1024
  33444. AssertIntEQ(ASN1_STRING_length(str), 291);
  33445. #else
  33446. AssertIntEQ(ASN1_STRING_length(str), 549);
  33447. #endif /* END USE_CERT_BUFFERS_1024 */
  33448. X509_PUBKEY_free(pubKey);
  33449. EVP_PKEY_free(evpKey);
  33450. res = TEST_RES_CHECK(1);
  33451. #endif
  33452. return res;
  33453. }
  33454. static int test_wolfSSL_RAND(void)
  33455. {
  33456. int res = TEST_SKIPPED;
  33457. #if defined(OPENSSL_EXTRA)
  33458. byte seed[16];
  33459. XMEMSET(seed, 0, sizeof(seed));
  33460. RAND_seed(seed, sizeof(seed));
  33461. AssertIntEQ(RAND_poll(), 1);
  33462. RAND_cleanup();
  33463. AssertIntEQ(RAND_egd(NULL), -1);
  33464. #ifndef NO_FILESYSTEM
  33465. {
  33466. char fname[100];
  33467. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33468. AssertIntEQ(RAND_write_file(NULL), 0);
  33469. }
  33470. #endif
  33471. res = TEST_RES_CHECK(1);
  33472. #endif
  33473. return res;
  33474. }
  33475. static int test_wolfSSL_BUF(void)
  33476. {
  33477. int res = TEST_SKIPPED;
  33478. #if defined(OPENSSL_EXTRA)
  33479. BUF_MEM* buf;
  33480. AssertNotNull(buf = BUF_MEM_new());
  33481. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  33482. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  33483. BUF_MEM_free(buf);
  33484. res = TEST_RES_CHECK(1);
  33485. #endif
  33486. return res;
  33487. }
  33488. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33489. static int stub_rand_seed(const void *buf, int num)
  33490. {
  33491. (void)buf;
  33492. (void)num;
  33493. return 123;
  33494. }
  33495. static int stub_rand_bytes(unsigned char *buf, int num)
  33496. {
  33497. (void)buf;
  33498. (void)num;
  33499. return 456;
  33500. }
  33501. static byte* was_stub_rand_cleanup_called(void)
  33502. {
  33503. static byte was_called = 0;
  33504. return &was_called;
  33505. }
  33506. static void stub_rand_cleanup(void)
  33507. {
  33508. byte* was_called = was_stub_rand_cleanup_called();
  33509. *was_called = 1;
  33510. return;
  33511. }
  33512. static byte* was_stub_rand_add_called(void)
  33513. {
  33514. static byte was_called = 0;
  33515. return &was_called;
  33516. }
  33517. static int stub_rand_add(const void *buf, int num, double entropy)
  33518. {
  33519. byte* was_called = was_stub_rand_add_called();
  33520. (void)buf;
  33521. (void)num;
  33522. (void)entropy;
  33523. *was_called = 1;
  33524. return 0;
  33525. }
  33526. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33527. {
  33528. (void)buf;
  33529. (void)num;
  33530. return 9876;
  33531. }
  33532. static int stub_rand_status(void)
  33533. {
  33534. return 5432;
  33535. }
  33536. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33537. static int test_wolfSSL_RAND_set_rand_method(void)
  33538. {
  33539. int res = TEST_SKIPPED;
  33540. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33541. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33542. unsigned char* buf = NULL;
  33543. int num = 0;
  33544. double entropy = 0;
  33545. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33546. byte* was_add_called = was_stub_rand_add_called();
  33547. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33548. DYNAMIC_TYPE_TMP_BUFFER);
  33549. AssertIntNE(wolfSSL_RAND_status(), 5432);
  33550. AssertIntEQ(*was_cleanup_called, 0);
  33551. RAND_cleanup();
  33552. AssertIntEQ(*was_cleanup_called, 0);
  33553. rand_methods.seed = &stub_rand_seed;
  33554. rand_methods.bytes = &stub_rand_bytes;
  33555. rand_methods.cleanup = &stub_rand_cleanup;
  33556. rand_methods.add = &stub_rand_add;
  33557. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33558. rand_methods.status = &stub_rand_status;
  33559. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33560. AssertIntEQ(RAND_seed(buf, num), 123);
  33561. AssertIntEQ(RAND_bytes(buf, num), 456);
  33562. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33563. AssertIntEQ(RAND_status(), 5432);
  33564. AssertIntEQ(*was_add_called, 0);
  33565. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  33566. RAND_add(buf, num, entropy);
  33567. AssertIntEQ(*was_add_called, 1);
  33568. was_add_called = 0;
  33569. AssertIntEQ(*was_cleanup_called, 0);
  33570. RAND_cleanup();
  33571. AssertIntEQ(*was_cleanup_called, 1);
  33572. *was_cleanup_called = 0;
  33573. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  33574. AssertIntNE(RAND_status(), 5432);
  33575. AssertIntEQ(*was_cleanup_called, 0);
  33576. RAND_cleanup();
  33577. AssertIntEQ(*was_cleanup_called, 0);
  33578. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33579. res = TEST_RES_CHECK(1);
  33580. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33581. return res;
  33582. }
  33583. static int test_wolfSSL_RAND_bytes(void)
  33584. {
  33585. int res = TEST_SKIPPED;
  33586. #if defined(OPENSSL_EXTRA)
  33587. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33588. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33589. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33590. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33591. int max_bufsize;
  33592. byte *my_buf;
  33593. /* sanity check */
  33594. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  33595. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  33596. max_bufsize = size4;
  33597. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33598. DYNAMIC_TYPE_TMP_BUFFER);
  33599. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  33600. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  33601. AssertNotNull(my_buf);
  33602. XMEMSET(my_buf, 0, max_bufsize);
  33603. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  33604. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  33605. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  33606. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  33607. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33608. res = TEST_RES_CHECK(1);
  33609. #endif
  33610. return res;
  33611. }
  33612. static int test_wolfSSL_BN_rand(void)
  33613. {
  33614. int res = TEST_SKIPPED;
  33615. #if defined(OPENSSL_EXTRA)
  33616. BIGNUM* bn;
  33617. BIGNUM* range;
  33618. /* Error conditions. */
  33619. /* NULL BN. */
  33620. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  33621. AssertNotNull(bn = BN_new());
  33622. /* Negative bits. */
  33623. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  33624. /* 0 bits and top is not -1. */
  33625. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  33626. /* 0 bits and bottom is not 0. */
  33627. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  33628. /* 1 bit and top is 1. */
  33629. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  33630. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  33631. AssertIntEQ(BN_num_bits(bn), 0);
  33632. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33633. AssertIntEQ(BN_num_bits(bn), 8);
  33634. /* When top is 0, top bit should be 1. */
  33635. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33636. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  33637. /* When top is 1, top 2 bits should be 1. */
  33638. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33639. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  33640. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  33641. /* When bottom is 1, bottom bit should be 1. */
  33642. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  33643. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33644. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33645. * case, requesting a 13-bit random number would actually return a 16-bit
  33646. * random number. */
  33647. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  33648. AssertIntEQ(BN_num_bits(bn), 13);
  33649. AssertNotNull(range = BN_new());
  33650. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  33651. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  33652. BN_free(bn);
  33653. BN_free(range);
  33654. res = TEST_RES_CHECK(1);
  33655. #endif
  33656. return res;
  33657. }
  33658. static int test_wolfSSL_pseudo_rand(void)
  33659. {
  33660. int res = TEST_SKIPPED;
  33661. #if defined(OPENSSL_EXTRA)
  33662. BIGNUM* bn;
  33663. unsigned char bin[8];
  33664. int i;
  33665. /* BN_pseudo_rand returns 1 on success 0 on failure
  33666. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  33667. for (i = 0; i < 10; i++) {
  33668. AssertNotNull(bn = BN_new());
  33669. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33670. AssertIntGT(BN_bn2bin(bn, bin),0);
  33671. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  33672. BN_free(bn);
  33673. }
  33674. for (i = 0; i < 10; i++) {
  33675. AssertNotNull(bn = BN_new());
  33676. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  33677. AssertIntGT(BN_bn2bin(bn, bin),0);
  33678. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  33679. BN_free(bn);
  33680. }
  33681. res = TEST_RES_CHECK(1);
  33682. #endif
  33683. return res;
  33684. }
  33685. static int test_wolfSSL_PKCS8_Compat(void)
  33686. {
  33687. int res = TEST_SKIPPED;
  33688. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  33689. #ifndef NO_BIO
  33690. PKCS8_PRIV_KEY_INFO* pt;
  33691. BIO* bio;
  33692. XFILE f;
  33693. int bytes;
  33694. char pkcs8_buffer[512];
  33695. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33696. EVP_PKEY *pkey = NULL;
  33697. #endif
  33698. /* file from wolfssl/certs/ directory */
  33699. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  33700. AssertTrue(f != XBADFILE);
  33701. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  33702. XFCLOSE(f);
  33703. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33704. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33705. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33706. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  33707. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  33708. /* gets PKCS8 pointer to pkey */
  33709. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  33710. EVP_PKEY_free(pkey);
  33711. #endif
  33712. BIO_free(bio);
  33713. PKCS8_PRIV_KEY_INFO_free(pt);
  33714. res = TEST_RES_CHECK(1);
  33715. #endif
  33716. #endif
  33717. return res;
  33718. }
  33719. static int test_wolfSSL_PKCS8_d2i(void)
  33720. {
  33721. int res = TEST_SKIPPED;
  33722. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  33723. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  33724. * requires a 12 byte password in FIPS mode. This test ends up
  33725. * trying to use an 8 byte password. */
  33726. #ifndef NO_FILESYSTEM
  33727. unsigned char pkcs8_buffer[2048];
  33728. const unsigned char* p;
  33729. int bytes;
  33730. XFILE file;
  33731. WOLFSSL_EVP_PKEY* pkey = NULL;
  33732. #ifndef NO_BIO
  33733. BIO* bio;
  33734. #if defined(OPENSSL_ALL) && \
  33735. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  33736. defined(HAVE_ECC)) && \
  33737. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33738. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  33739. #endif
  33740. #endif
  33741. #ifndef NO_RSA
  33742. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  33743. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  33744. #ifndef NO_DES3
  33745. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  33746. #endif
  33747. #endif /* NO_RSA */
  33748. #ifdef HAVE_ECC
  33749. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  33750. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  33751. #ifndef NO_DES3
  33752. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  33753. #endif
  33754. #endif /* HAVE_ECC */
  33755. #endif /* !NO_FILESYSTEM */
  33756. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  33757. #ifndef NO_RSA
  33758. #ifdef USE_CERT_BUFFERS_1024
  33759. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  33760. int rsaSz = sizeof_server_key_der_1024;
  33761. #else
  33762. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  33763. int rsaSz = sizeof_server_key_der_2048;
  33764. #endif
  33765. #endif
  33766. #ifdef HAVE_ECC
  33767. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  33768. int ecSz = sizeof_ecc_key_der_256;
  33769. #endif
  33770. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  33771. #ifndef NO_FILESYSTEM
  33772. (void)pkcs8_buffer;
  33773. (void)p;
  33774. (void)bytes;
  33775. (void)file;
  33776. #ifndef NO_BIO
  33777. (void)bio;
  33778. #endif
  33779. #endif
  33780. #ifdef OPENSSL_ALL
  33781. #ifndef NO_RSA
  33782. /* Try to auto-detect normal RSA private key */
  33783. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  33784. EVP_PKEY_free(pkey);
  33785. #endif
  33786. #ifdef HAVE_ECC
  33787. /* Try to auto-detect normal EC private key */
  33788. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  33789. EVP_PKEY_free(pkey);
  33790. #endif
  33791. #endif /* OPENSSL_ALL */
  33792. #ifndef NO_FILESYSTEM
  33793. #ifndef NO_RSA
  33794. /* Get DER encoded RSA PKCS#8 data. */
  33795. file = XFOPEN(rsaDerPkcs8File, "rb");
  33796. AssertTrue(file != XBADFILE);
  33797. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33798. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33799. file)), 0);
  33800. XFCLOSE(file);
  33801. p = pkcs8_buffer;
  33802. #ifdef OPENSSL_ALL
  33803. /* Try to decode - auto-detect key type. */
  33804. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33805. #else
  33806. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  33807. #endif
  33808. /* Get PEM encoded RSA PKCS#8 data. */
  33809. file = XFOPEN(rsaPemPkcs8File, "rb");
  33810. AssertTrue(file != XBADFILE);
  33811. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33812. file)), 0);
  33813. XFCLOSE(file);
  33814. #if defined(OPENSSL_ALL) && \
  33815. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33816. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33817. /* Write PKCS#8 PEM to BIO. */
  33818. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33819. NULL), bytes);
  33820. /* Compare file and written data */
  33821. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33822. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33823. BIO_free(bio);
  33824. #if !defined(NO_DES3) && !defined(NO_SHA)
  33825. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33826. /* Write Encrypted PKCS#8 PEM to BIO. */
  33827. bytes = 1834;
  33828. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  33829. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33830. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33831. (void*)"yassl123"));
  33832. EVP_PKEY_free(evpPkey);
  33833. BIO_free(bio);
  33834. #endif /* !NO_DES3 && !NO_SHA */
  33835. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33836. EVP_PKEY_free(pkey);
  33837. /* PKCS#8 encrypted RSA key */
  33838. #ifndef NO_DES3
  33839. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  33840. AssertTrue(file != XBADFILE);
  33841. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33842. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33843. file)), 0);
  33844. XFCLOSE(file);
  33845. #if defined(OPENSSL_ALL) && \
  33846. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33847. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33848. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33849. (void*)"yassl123"));
  33850. EVP_PKEY_free(pkey);
  33851. BIO_free(bio);
  33852. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33853. #endif /* !NO_DES3 */
  33854. #endif /* NO_RSA */
  33855. #ifdef HAVE_ECC
  33856. /* PKCS#8 encode EC key */
  33857. file = XFOPEN(ecDerPkcs8File, "rb");
  33858. AssertTrue(file != XBADFILE);
  33859. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33860. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33861. file)), 0);
  33862. XFCLOSE(file);
  33863. p = pkcs8_buffer;
  33864. #ifdef OPENSSL_ALL
  33865. /* Try to decode - auto-detect key type. */
  33866. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33867. #else
  33868. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  33869. #endif
  33870. /* Get PEM encoded RSA PKCS#8 data. */
  33871. file = XFOPEN(ecPemPkcs8File, "rb");
  33872. AssertTrue(file != XBADFILE);
  33873. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33874. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33875. file)), 0);
  33876. XFCLOSE(file);
  33877. #if defined(OPENSSL_ALL) && \
  33878. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  33879. defined(HAVE_AES_CBC)
  33880. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33881. /* Write PKCS#8 PEM to BIO. */
  33882. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33883. NULL), bytes);
  33884. /* Compare file and written data */
  33885. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33886. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33887. BIO_free(bio);
  33888. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33889. /* Write Encrypted PKCS#8 PEM to BIO. */
  33890. bytes = 379;
  33891. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  33892. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33893. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33894. (void*)"yassl123"));
  33895. EVP_PKEY_free(evpPkey);
  33896. BIO_free(bio);
  33897. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  33898. EVP_PKEY_free(pkey);
  33899. /* PKCS#8 encrypted EC key */
  33900. #ifndef NO_DES3
  33901. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  33902. AssertTrue(file != XBADFILE);
  33903. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33904. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33905. file)), 0);
  33906. XFCLOSE(file);
  33907. #if defined(OPENSSL_ALL) && \
  33908. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33909. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33910. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33911. (void*)"yassl123"));
  33912. EVP_PKEY_free(pkey);
  33913. BIO_free(bio);
  33914. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33915. #endif /* !NO_DES3 */
  33916. #endif /* HAVE_ECC */
  33917. #endif /* !NO_FILESYSTEM */
  33918. res = TEST_RES_CHECK(1);
  33919. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  33920. return res;
  33921. }
  33922. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33923. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33924. #define LOGGING_THREADS 5
  33925. #define ERROR_COUNT 10
  33926. /* copied from logging.c since this is not exposed otherwise */
  33927. #ifndef ERROR_QUEUE_MAX
  33928. #ifdef ERROR_QUEUE_PER_THREAD
  33929. #define ERROR_QUEUE_MAX 16
  33930. #else
  33931. /* this breaks from compat of unlimited error queue size */
  33932. #define ERROR_QUEUE_MAX 100
  33933. #endif
  33934. #endif
  33935. static volatile int loggingThreadsReady;
  33936. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  33937. {
  33938. const char* file;
  33939. int line;
  33940. unsigned long err;
  33941. int errorCount = 0;
  33942. int i;
  33943. (void)args;
  33944. while (!loggingThreadsReady);
  33945. for (i = 0; i < ERROR_COUNT; i++)
  33946. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33947. while ((err = ERR_get_error_line(&file, &line))) {
  33948. AssertIntEQ(err, 990 + errorCount);
  33949. errorCount++;
  33950. }
  33951. AssertIntEQ(errorCount, ERROR_COUNT);
  33952. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  33953. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  33954. errorCount = 0;
  33955. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  33956. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33957. while ((err = ERR_get_error_line(&file, &line))) {
  33958. AssertIntEQ(err, 990 + errorCount);
  33959. errorCount++;
  33960. }
  33961. /* test that the 3 errors over the max were dropped */
  33962. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  33963. return 0;
  33964. }
  33965. #endif
  33966. static int test_error_queue_per_thread(void)
  33967. {
  33968. int res = TEST_SKIPPED;
  33969. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33970. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33971. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33972. int i;
  33973. ERR_clear_error(); /* clear out any error nodes */
  33974. loggingThreadsReady = 0;
  33975. for (i = 0; i < LOGGING_THREADS; i++)
  33976. start_thread(test_logging, NULL, &loggingThreads[i]);
  33977. loggingThreadsReady = 1;
  33978. for (i = 0; i < LOGGING_THREADS; i++)
  33979. join_thread(loggingThreads[i]);
  33980. res = TEST_RES_CHECK(1);
  33981. #endif
  33982. return res;
  33983. }
  33984. static int test_wolfSSL_ERR_put_error(void)
  33985. {
  33986. int res = TEST_SKIPPED;
  33987. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33988. defined(DEBUG_WOLFSSL)
  33989. const char* file;
  33990. int line;
  33991. ERR_clear_error(); /* clear out any error nodes */
  33992. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33993. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33994. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33995. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  33996. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33997. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  33998. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33999. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  34000. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  34001. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  34002. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  34003. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  34004. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  34005. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  34006. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  34007. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  34008. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  34009. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  34010. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  34011. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  34012. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  34013. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  34014. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  34015. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  34016. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  34017. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  34018. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  34019. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  34020. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  34021. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  34022. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  34023. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  34024. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  34025. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  34026. "this file", 100);
  34027. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  34028. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  34029. AssertIntEQ(line, 100);
  34030. AssertIntEQ(wolfSSL_ERR_peek_error(),
  34031. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  34032. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  34033. #endif
  34034. /* try reading past end of error queue */
  34035. file = NULL;
  34036. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34037. AssertNull(file);
  34038. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  34039. PEMerr(4,4);
  34040. AssertIntEQ(ERR_get_error(), 4);
  34041. /* Empty and free up all error nodes */
  34042. ERR_clear_error();
  34043. /* Verify all nodes are cleared */
  34044. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  34045. ERR_clear_error();
  34046. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  34047. res = TEST_RES_CHECK(1);
  34048. #endif
  34049. return res;
  34050. }
  34051. /*
  34052. * This is a regression test for a bug where the peek/get error functions were
  34053. * drawing from the end of the queue rather than the front.
  34054. */
  34055. static int test_wolfSSL_ERR_get_error_order(void)
  34056. {
  34057. int res = TEST_SKIPPED;
  34058. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  34059. /* Empty the queue. */
  34060. wolfSSL_ERR_clear_error();
  34061. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  34062. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  34063. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  34064. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  34065. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  34066. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  34067. res = TEST_RES_CHECK(1);
  34068. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  34069. return res;
  34070. }
  34071. #ifndef NO_BIO
  34072. static int test_wolfSSL_ERR_print_errors(void)
  34073. {
  34074. int res = TEST_SKIPPED;
  34075. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34076. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  34077. BIO* bio;
  34078. char buf[1024];
  34079. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34080. ERR_clear_error(); /* clear out any error nodes */
  34081. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34082. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  34083. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  34084. ERR_print_errors(bio);
  34085. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  34086. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  34087. buf, 55), 0);
  34088. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  34089. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  34090. buf, 56), 0);
  34091. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  34092. AssertIntEQ(buf[0], '\0');
  34093. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  34094. BIO_free(bio);
  34095. res = TEST_RES_CHECK(1);
  34096. #endif
  34097. return res;
  34098. }
  34099. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34100. defined(DEBUG_WOLFSSL)
  34101. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  34102. {
  34103. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  34104. return 0;
  34105. }
  34106. #endif
  34107. static int test_wolfSSL_ERR_print_errors_cb(void)
  34108. {
  34109. int res = TEST_SKIPPED;
  34110. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34111. defined(DEBUG_WOLFSSL)
  34112. BIO* bio;
  34113. char buf[1024];
  34114. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34115. ERR_clear_error(); /* clear out any error nodes */
  34116. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34117. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  34118. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  34119. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  34120. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  34121. buf, 53), 0);
  34122. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  34123. buf + 53, 55), 0);
  34124. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  34125. BIO_free(bio);
  34126. res = TEST_RES_CHECK(1);
  34127. #endif
  34128. return res;
  34129. }
  34130. /*
  34131. * Testing WOLFSSL_ERROR_MSG
  34132. */
  34133. static int test_WOLFSSL_ERROR_MSG(void)
  34134. {
  34135. int res = TEST_SKIPPED;
  34136. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34137. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34138. const char* msg = TEST_STRING;
  34139. WOLFSSL_ERROR_MSG(msg);
  34140. res = TEST_RES_CHECK(1);
  34141. #endif
  34142. return res;
  34143. }/*End test_WOLFSSL_ERROR_MSG*/
  34144. /*
  34145. * Testing wc_ERR_remove_state
  34146. */
  34147. static int test_wc_ERR_remove_state(void)
  34148. {
  34149. int res = TEST_SKIPPED;
  34150. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34151. wc_ERR_remove_state();
  34152. res = TEST_RES_CHECK(1);
  34153. #endif
  34154. return res;
  34155. }/*End test_wc_ERR_remove_state*/
  34156. /*
  34157. * Testing wc_ERR_print_errors_fp
  34158. */
  34159. static int test_wc_ERR_print_errors_fp(void)
  34160. {
  34161. int res = TEST_SKIPPED;
  34162. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34163. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34164. long sz;
  34165. XFILE fp;
  34166. int ret = 0;
  34167. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34168. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  34169. wc_ERR_print_errors_fp(fp);
  34170. #if defined(DEBUG_WOLFSSL)
  34171. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34172. sz = XFTELL(fp);
  34173. #ifdef NO_ERROR_QUEUE
  34174. /* File should be empty when NO_ERROR_QUEUE is defined */
  34175. if (sz != 0) {
  34176. ret = BAD_FUNC_ARG;
  34177. }
  34178. #else
  34179. if (sz == 0) {
  34180. ret = BAD_FUNC_ARG;
  34181. }
  34182. #endif
  34183. #endif
  34184. XFCLOSE(fp);
  34185. (void)sz;
  34186. res = TEST_RES_CHECK(ret == 0);
  34187. #endif
  34188. return res;
  34189. }/*End test_wc_ERR_print_errors_fp*/
  34190. #ifdef DEBUG_WOLFSSL
  34191. static void Logging_cb(const int logLevel, const char *const logMessage)
  34192. {
  34193. (void)logLevel;
  34194. (void)logMessage;
  34195. }
  34196. #endif
  34197. /*
  34198. * Testing wolfSSL_GetLoggingCb
  34199. */
  34200. static int test_wolfSSL_GetLoggingCb(void)
  34201. {
  34202. int ret = 0;
  34203. #ifdef DEBUG_WOLFSSL
  34204. /* Testing without wolfSSL_SetLoggingCb() */
  34205. if (ret == 0) {
  34206. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  34207. ret = 0;
  34208. }
  34209. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  34210. ret = -1;
  34211. }
  34212. }
  34213. /* Testing with wolfSSL_SetLoggingCb() */
  34214. if (ret == 0) {
  34215. ret = wolfSSL_SetLoggingCb(Logging_cb);
  34216. if (ret == 0) {
  34217. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  34218. ret = -1;
  34219. }
  34220. if (ret == 0) {
  34221. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  34222. ret = 0;
  34223. }
  34224. }
  34225. /* reset logging callback */
  34226. wolfSSL_SetLoggingCb(NULL);
  34227. }
  34228. }
  34229. #endif
  34230. if (ret == 0) {
  34231. if (wolfSSL_GetLoggingCb() != NULL) {
  34232. ret = -1;
  34233. }
  34234. }
  34235. return TEST_RES_CHECK(ret == 0);
  34236. }/*End test_wolfSSL_GetLoggingCb*/
  34237. #endif /* !NO_BIO */
  34238. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34239. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34240. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34241. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34242. {
  34243. static const unsigned char key[] = "simple test key";
  34244. HMAC_CTX* hmac;
  34245. ENGINE* e = NULL;
  34246. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34247. unsigned int len;
  34248. AssertNotNull(hmac = HMAC_CTX_new());
  34249. HMAC_CTX_init(hmac);
  34250. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34251. SSL_SUCCESS);
  34252. /* re-using test key as data to hash */
  34253. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34254. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34255. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34256. AssertIntEQ(len, md_len);
  34257. AssertIntEQ(HMAC_size(hmac), md_len);
  34258. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  34259. HMAC_cleanup(hmac);
  34260. HMAC_CTX_free(hmac);
  34261. len = 0;
  34262. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34263. AssertIntEQ(len, md_len);
  34264. return 0;
  34265. }
  34266. #endif
  34267. static int test_wolfSSL_HMAC(void)
  34268. {
  34269. int res = TEST_SKIPPED;
  34270. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34271. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34272. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34273. #ifndef NO_SHA256
  34274. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  34275. #endif
  34276. #ifdef WOLFSSL_SHA224
  34277. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  34278. #endif
  34279. #ifdef WOLFSSL_SHA384
  34280. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  34281. #endif
  34282. #ifdef WOLFSSL_SHA512
  34283. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  34284. #endif
  34285. #ifdef WOLFSSL_SHA3
  34286. #ifndef WOLFSSL_NOSHA3_224
  34287. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  34288. #endif
  34289. #ifndef WOLFSSL_NOSHA3_256
  34290. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  34291. #endif
  34292. #ifndef WOLFSSL_NOSHA3_384
  34293. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  34294. #endif
  34295. #ifndef WOLFSSL_NOSHA3_512
  34296. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  34297. #endif
  34298. #endif
  34299. #ifndef NO_SHA
  34300. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  34301. #endif
  34302. res = TEST_RES_CHECK(1);
  34303. #endif
  34304. return res;
  34305. }
  34306. static int test_wolfSSL_CMAC(void)
  34307. {
  34308. int res = TEST_SKIPPED;
  34309. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34310. defined(WOLFSSL_AES_DIRECT)
  34311. int i;
  34312. byte key[AES_128_KEY_SIZE];
  34313. CMAC_CTX* cmacCtx = NULL;
  34314. byte out[AES_BLOCK_SIZE];
  34315. size_t outLen = AES_BLOCK_SIZE;
  34316. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34317. key[i] = i;
  34318. }
  34319. AssertNotNull(cmacCtx = CMAC_CTX_new());
  34320. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34321. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34322. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34323. NULL), SSL_SUCCESS);
  34324. /* re-using test key as data to hash */
  34325. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34326. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34327. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34328. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  34329. CMAC_CTX_free(cmacCtx);
  34330. res = TEST_RES_CHECK(1);
  34331. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34332. return res;
  34333. }
  34334. static int test_wolfSSL_OBJ(void)
  34335. {
  34336. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34337. * mode
  34338. */
  34339. int res = TEST_SKIPPED;
  34340. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34341. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34342. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34343. ASN1_OBJECT *obj = NULL;
  34344. ASN1_OBJECT *obj2 = NULL;
  34345. char buf[50];
  34346. XFILE fp;
  34347. X509 *x509 = NULL;
  34348. X509_NAME *x509Name;
  34349. X509_NAME_ENTRY *x509NameEntry;
  34350. ASN1_OBJECT *asn1Name = NULL;
  34351. int numNames;
  34352. BIO *bio = NULL;
  34353. int nid;
  34354. int i, j;
  34355. const char *f[] = {
  34356. #ifndef NO_RSA
  34357. "./certs/ca-cert.der",
  34358. #endif
  34359. #ifdef HAVE_ECC
  34360. "./certs/ca-ecc-cert.der",
  34361. "./certs/ca-ecc384-cert.der",
  34362. #endif
  34363. NULL};
  34364. ASN1_OBJECT *field_name_obj = NULL;
  34365. int lastpos = -1;
  34366. int tmp = -1;
  34367. ASN1_STRING *asn1 = NULL;
  34368. unsigned char *buf_dyn = NULL;
  34369. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34370. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34371. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34372. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34373. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34374. ASN1_OBJECT_free(obj);
  34375. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34376. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34377. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34378. #ifdef WOLFSSL_CERT_EXT
  34379. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34380. #endif
  34381. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34382. AssertNotNull(obj2 = OBJ_dup(obj));
  34383. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  34384. ASN1_OBJECT_free(obj);
  34385. ASN1_OBJECT_free(obj2);
  34386. for (i = 0; f[i] != NULL; i++)
  34387. {
  34388. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34389. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  34390. XFCLOSE(fp);
  34391. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  34392. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34393. /* Get the Common Name by using OBJ_txt2obj */
  34394. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34395. do
  34396. {
  34397. lastpos = tmp;
  34398. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34399. } while (tmp > -1);
  34400. AssertIntNE(lastpos, -1);
  34401. ASN1_OBJECT_free(field_name_obj);
  34402. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34403. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34404. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34405. /*
  34406. * All Common Names should be www.wolfssl.com
  34407. * This makes testing easier as we can test for the expected value.
  34408. */
  34409. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34410. OPENSSL_free(buf_dyn);
  34411. bio = BIO_new(BIO_s_mem());
  34412. AssertTrue(bio != NULL);
  34413. for (j = 0; j < numNames; j++)
  34414. {
  34415. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34416. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34417. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34418. }
  34419. BIO_free(bio);
  34420. X509_free(x509);
  34421. }
  34422. #ifdef HAVE_PKCS12
  34423. {
  34424. PKCS12 *p12;
  34425. int boolRet;
  34426. EVP_PKEY *pkey = NULL;
  34427. const char *p12_f[] = {
  34428. #if !defined(NO_DES3) && !defined(NO_RSA)
  34429. "./certs/test-servercert.p12",
  34430. #endif
  34431. NULL};
  34432. for (i = 0; p12_f[i] != NULL; i++)
  34433. {
  34434. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34435. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34436. XFCLOSE(fp);
  34437. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34438. &pkey, &x509, NULL)) > 0);
  34439. wc_PKCS12_free(p12);
  34440. EVP_PKEY_free(pkey);
  34441. x509Name = X509_get_issuer_name(x509);
  34442. AssertNotNull(x509Name);
  34443. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34444. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34445. for (j = 0; j < numNames; j++)
  34446. {
  34447. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34448. AssertNotNull(asn1Name =
  34449. X509_NAME_ENTRY_get_object(x509NameEntry));
  34450. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34451. }
  34452. BIO_free(bio);
  34453. X509_free(x509);
  34454. }
  34455. }
  34456. #endif /* HAVE_PKCS12 */
  34457. res = TEST_RES_CHECK(1);
  34458. #endif
  34459. return res;
  34460. }
  34461. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  34462. {
  34463. int res = TEST_SKIPPED;
  34464. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  34465. ASN1_OBJECT *obj = NULL;
  34466. BIO *bio = NULL;
  34467. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34468. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34469. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  34470. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  34471. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  34472. BIO_free(bio);
  34473. ASN1_OBJECT_free(obj);
  34474. res = TEST_RES_CHECK(1);
  34475. #endif
  34476. return res;
  34477. }
  34478. static int test_wolfSSL_OBJ_cmp(void)
  34479. {
  34480. int res = TEST_SKIPPED;
  34481. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34482. ASN1_OBJECT *obj = NULL;
  34483. ASN1_OBJECT *obj2 = NULL;
  34484. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34485. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34486. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34487. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34488. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34489. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34490. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  34491. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  34492. ASN1_OBJECT_free(obj);
  34493. ASN1_OBJECT_free(obj2);
  34494. res = TEST_RES_CHECK(1);
  34495. #endif
  34496. return res;
  34497. }
  34498. static int test_wolfSSL_OBJ_txt2nid(void)
  34499. {
  34500. int res = TEST_SKIPPED;
  34501. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  34502. int i;
  34503. static const struct {
  34504. const char* sn;
  34505. const char* ln;
  34506. const char* oid;
  34507. int nid;
  34508. } testVals[] = {
  34509. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34510. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34511. NID_id_on_dnsSRV },
  34512. { "msUPN", "Microsoft User Principal Name",
  34513. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34514. { NULL, NULL, NULL, NID_undef }
  34515. };
  34516. /* Invalid cases */
  34517. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34518. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34519. /* Valid cases */
  34520. for (i = 0; testVals[i].sn != NULL; i++) {
  34521. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34522. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34523. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34524. }
  34525. res = TEST_RES_CHECK(1);
  34526. #endif
  34527. return res;
  34528. }
  34529. static int test_wolfSSL_OBJ_txt2obj(void)
  34530. {
  34531. int res = TEST_SKIPPED;
  34532. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34533. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34534. int i;
  34535. char buf[50];
  34536. ASN1_OBJECT* obj;
  34537. static const struct {
  34538. const char* oidStr;
  34539. const char* sn;
  34540. const char* ln;
  34541. } objs_list[] = {
  34542. #if defined(WOLFSSL_APACHE_HTTPD)
  34543. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34544. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34545. #endif
  34546. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34547. { NULL, NULL, NULL }
  34548. };
  34549. static const struct {
  34550. const char* numeric;
  34551. const char* name;
  34552. } objs_named[] = {
  34553. /* In dictionary but not in normal list. */
  34554. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34555. /* Made up OID. */
  34556. { "1.3.5.7", "1.3.5.7" },
  34557. { NULL, NULL }
  34558. };
  34559. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  34560. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  34561. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34562. /* Test numerical value of oid (oidStr) */
  34563. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34564. /* Convert object back to text to confirm oid is correct */
  34565. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34566. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34567. ASN1_OBJECT_free(obj);
  34568. XMEMSET(buf, 0, sizeof(buf));
  34569. /* Test short name (sn) */
  34570. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34571. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34572. /* Convert object back to text to confirm oid is correct */
  34573. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34574. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34575. ASN1_OBJECT_free(obj);
  34576. XMEMSET(buf, 0, sizeof(buf));
  34577. /* Test long name (ln) - should fail when no_name = 1 */
  34578. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34579. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34580. /* Convert object back to text to confirm oid is correct */
  34581. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34582. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34583. ASN1_OBJECT_free(obj);
  34584. XMEMSET(buf, 0, sizeof(buf));
  34585. }
  34586. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34587. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34588. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34589. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34590. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34591. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34592. ASN1_OBJECT_free(obj);
  34593. }
  34594. res = TEST_RES_CHECK(1);
  34595. #endif
  34596. return res;
  34597. }
  34598. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  34599. {
  34600. int res = TEST_SKIPPED;
  34601. #if defined(OPENSSL_EXTRA) && \
  34602. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34603. char buf[50] = {0};
  34604. ASN1_OBJECT* obj;
  34605. const char* oid = "2.5.29.19";
  34606. const char* ln = "X509v3 Basic Constraints";
  34607. obj = NULL;
  34608. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  34609. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  34610. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  34611. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  34612. XMEMSET(buf, 0, sizeof(buf));
  34613. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  34614. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  34615. ASN1_OBJECT_free(obj);
  34616. res = TEST_RES_CHECK(1);
  34617. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  34618. return res;
  34619. }
  34620. static int test_wolfSSL_PEM_write_bio_X509(void)
  34621. {
  34622. int res = TEST_SKIPPED;
  34623. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  34624. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  34625. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  34626. !defined(NO_FILESYSTEM)
  34627. /* This test contains the hard coded expected
  34628. * lengths. Update if necessary */
  34629. FILE* fp = NULL;
  34630. WOLFSSL_EVP_PKEY *priv = NULL;
  34631. BIO* input = NULL;
  34632. BIO* output = NULL;
  34633. X509* x509a = NULL;
  34634. X509* x509b = NULL;
  34635. ASN1_TIME* notBeforeA = NULL;
  34636. ASN1_TIME* notAfterA = NULL;
  34637. ASN1_TIME* notBeforeB = NULL;
  34638. ASN1_TIME* notAfterB = NULL;
  34639. int expectedLen;
  34640. fp = XFOPEN("certs/server-key.pem", "rb");
  34641. AssertNotNull(fp);
  34642. priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  34643. XFCLOSE(fp);
  34644. fp = NULL;
  34645. AssertNotNull(priv);
  34646. AssertNotNull(input = BIO_new_file(
  34647. "certs/test/cert-ext-multiple.pem", "rb"));
  34648. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34649. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34650. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34651. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  34652. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  34653. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  34654. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34655. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34656. /* compare length against expected */
  34657. expectedLen = 2000;
  34658. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34659. /* read exported X509 PEM back into struct, sanity check on export,
  34660. * make sure notBefore/notAfter are the same and certs are identical. */
  34661. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34662. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  34663. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  34664. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34665. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34666. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34667. X509_free(x509b);
  34668. /* Reset output buffer */
  34669. BIO_free(output);
  34670. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34671. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34672. if (x509a->authKeyIdSrc != NULL) {
  34673. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34674. x509a->authKeyId = x509a->authKeyIdSrc;
  34675. x509a->authKeyIdSrc = NULL;
  34676. x509a->authKeyIdSrcSz = 0;
  34677. }
  34678. /* Resign to re-generate the der */
  34679. AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  34680. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34681. /* Check that we generate a smaller output since the AKID will
  34682. * only contain the KeyIdentifier without any additional
  34683. * information */
  34684. /* Here we copy the validity struct from the original */
  34685. expectedLen = 1688;
  34686. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34687. /* Reset buffers and x509 */
  34688. BIO_free(input);
  34689. BIO_free(output);
  34690. X509_free(x509a);
  34691. /* test CA and basicConstSet values are encoded when
  34692. * the cert is a CA */
  34693. AssertNotNull(input = BIO_new_file(
  34694. "certs/server-cert.pem", "rb"));
  34695. /* read PEM into X509 struct */
  34696. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34697. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34698. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34699. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34700. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  34701. * values are maintained and certs are identical.*/
  34702. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34703. AssertIntEQ(x509b->isCa, 1);
  34704. AssertIntEQ(x509b->basicConstSet, 1);
  34705. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34706. X509_free(x509a);
  34707. X509_free(x509b);
  34708. BIO_free(input);
  34709. BIO_free(output);
  34710. /* test CA and basicConstSet values are encoded when
  34711. * the cert is not CA */
  34712. AssertNotNull(input = BIO_new_file(
  34713. "certs/client-uri-cert.pem", "rb"));
  34714. /* read PEM into X509 struct */
  34715. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34716. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34717. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34718. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34719. /* read exported X509 PEM back into struct, ensure isCa and
  34720. * basicConstSet values are maintained and certs are identical */
  34721. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34722. AssertIntEQ(x509b->isCa, 0);
  34723. AssertIntEQ(x509b->basicConstSet, 1);
  34724. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34725. wolfSSL_EVP_PKEY_free(priv);
  34726. X509_free(x509a);
  34727. X509_free(x509b);
  34728. BIO_free(input);
  34729. BIO_free(output);
  34730. res = TEST_RES_CHECK(1);
  34731. #endif
  34732. return res;
  34733. }
  34734. static int test_wolfSSL_X509_NAME_ENTRY(void)
  34735. {
  34736. int res = TEST_SKIPPED;
  34737. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34738. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  34739. X509* x509;
  34740. #ifndef NO_BIO
  34741. BIO* bio;
  34742. #endif
  34743. X509_NAME* nm;
  34744. X509_NAME_ENTRY* entry;
  34745. unsigned char cn[] = "another name to add";
  34746. #ifdef OPENSSL_ALL
  34747. int i, names_len;
  34748. #endif
  34749. AssertNotNull(x509 =
  34750. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34751. #ifndef NO_BIO
  34752. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34753. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  34754. #endif
  34755. #ifdef WOLFSSL_CERT_REQ
  34756. {
  34757. X509_REQ* req;
  34758. #ifndef NO_BIO
  34759. BIO* bReq;
  34760. #endif
  34761. AssertNotNull(req =
  34762. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34763. #ifndef NO_BIO
  34764. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  34765. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  34766. BIO_free(bReq);
  34767. #endif
  34768. X509_free(req);
  34769. }
  34770. #endif
  34771. AssertNotNull(nm = X509_get_subject_name(x509));
  34772. /* Test add entry */
  34773. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  34774. 0x0c, cn, (int)sizeof(cn)));
  34775. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34776. #ifdef WOLFSSL_CERT_EXT
  34777. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  34778. (byte*)"support@wolfssl.com", 19, -1,
  34779. 1), WOLFSSL_SUCCESS);
  34780. #endif
  34781. X509_NAME_ENTRY_free(entry);
  34782. #ifdef WOLFSSL_CERT_REQ
  34783. {
  34784. unsigned char srv_pkcs9p[] = "Server";
  34785. char* subject;
  34786. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  34787. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  34788. subject = X509_NAME_oneline(nm, 0, 0);
  34789. #ifdef DEBUG_WOLFSSL
  34790. fprintf(stderr, "\n\t%s\n", subject);
  34791. #endif
  34792. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  34793. }
  34794. #endif
  34795. /* Test add entry by text */
  34796. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  34797. 0x0c, cn, (int)sizeof(cn)));
  34798. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  34799. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  34800. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  34801. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  34802. #endif
  34803. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34804. X509_NAME_ENTRY_free(entry);
  34805. /* Test add entry by NID */
  34806. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  34807. cn, -1, -1, 0), SSL_SUCCESS);
  34808. #ifdef OPENSSL_ALL
  34809. /* stack of name entry */
  34810. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  34811. for (i=0; i<names_len; i++) {
  34812. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  34813. }
  34814. #endif
  34815. #ifndef NO_BIO
  34816. BIO_free(bio);
  34817. #endif
  34818. X509_free(x509); /* free's nm */
  34819. res = TEST_RES_CHECK(1);
  34820. #endif
  34821. return res;
  34822. }
  34823. static int test_wolfSSL_X509_set_name(void)
  34824. {
  34825. int res = TEST_SKIPPED;
  34826. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34827. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34828. X509* x509;
  34829. X509_NAME* name;
  34830. AssertNotNull(name = X509_NAME_new());
  34831. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34832. (byte*)"wolfssl.com", 11, 0, 1),
  34833. WOLFSSL_SUCCESS);
  34834. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34835. (byte*)"support@wolfssl.com", 19, -1,
  34836. 1), WOLFSSL_SUCCESS);
  34837. AssertNotNull(x509 = X509_new());
  34838. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  34839. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  34840. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  34841. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  34842. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  34843. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  34844. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  34845. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34846. X509_free(x509);
  34847. X509_NAME_free(name);
  34848. res = TEST_RES_CHECK(1);
  34849. #endif /* OPENSSL_ALL && !NO_CERTS */
  34850. return res;
  34851. }
  34852. static int test_wolfSSL_X509_set_notAfter(void)
  34853. {
  34854. int res = TEST_SKIPPED;
  34855. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34856. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34857. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34858. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  34859. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  34860. /* Generalized time will overflow time_t if not long */
  34861. X509* x;
  34862. BIO* bio;
  34863. ASN1_TIME *asn_time, *time_check;
  34864. const int year = 365*24*60*60;
  34865. const int day = 24*60*60;
  34866. const int hour = 60*60;
  34867. const int mini = 60;
  34868. int offset_day;
  34869. unsigned char buf[25];
  34870. time_t t;
  34871. /*
  34872. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34873. */
  34874. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  34875. offset_day = 7;
  34876. /*
  34877. * Free these.
  34878. */
  34879. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34880. AssertNotNull(asn_time);
  34881. AssertNotNull(x = X509_new());
  34882. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34883. /*
  34884. * Tests
  34885. */
  34886. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  34887. /* time_check is simply (ANS1_TIME*)x->notAfter */
  34888. AssertNotNull(time_check = X509_get_notAfter(x));
  34889. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34890. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34891. /* Convert to human readable format and compare to intended date */
  34892. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34893. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34894. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  34895. /*
  34896. * Cleanup
  34897. */
  34898. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34899. X509_free(x);
  34900. BIO_free(bio);
  34901. res = TEST_RES_CHECK(1);
  34902. #endif
  34903. return res;
  34904. }
  34905. static int test_wolfSSL_X509_set_notBefore(void)
  34906. {
  34907. int res = TEST_SKIPPED;
  34908. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34909. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34910. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34911. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  34912. X509* x;
  34913. BIO* bio;
  34914. ASN1_TIME *asn_time, *time_check;
  34915. const int year = 365*24*60*60;
  34916. const int day = 24*60*60;
  34917. const int hour = 60*60;
  34918. const int mini = 60;
  34919. int offset_day;
  34920. unsigned char buf[25];
  34921. time_t t;
  34922. /*
  34923. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34924. */
  34925. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  34926. offset_day = 7;
  34927. /*
  34928. * Free these.
  34929. */
  34930. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34931. AssertNotNull(asn_time);
  34932. AssertNotNull(x = X509_new());
  34933. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34934. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  34935. /*
  34936. * Main Tests
  34937. */
  34938. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  34939. /* time_check == (ANS1_TIME*)x->notBefore */
  34940. AssertNotNull(time_check = X509_get_notBefore(x));
  34941. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34942. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34943. /* Convert to human readable format and compare to intended date */
  34944. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34945. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34946. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  34947. /*
  34948. * Cleanup
  34949. */
  34950. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34951. X509_free(x);
  34952. BIO_free(bio);
  34953. res = TEST_RES_CHECK(1);
  34954. #endif
  34955. return res;
  34956. }
  34957. static int test_wolfSSL_X509_set_version(void)
  34958. {
  34959. int res = TEST_SKIPPED;
  34960. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34961. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34962. X509* x509;
  34963. long v = 2L;
  34964. long maxInt = INT_MAX;
  34965. AssertNotNull(x509 = X509_new());
  34966. /* These should pass. */
  34967. AssertTrue(wolfSSL_X509_set_version(x509, v));
  34968. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  34969. /* Fail Case: When v(long) is greater than x509->version(int). */
  34970. v = maxInt+1;
  34971. AssertFalse(wolfSSL_X509_set_version(x509, v));
  34972. /* Cleanup */
  34973. X509_free(x509);
  34974. res = TEST_RES_CHECK(1);
  34975. #endif
  34976. return res;
  34977. }
  34978. #ifndef NO_BIO
  34979. static int test_wolfSSL_BIO_gets(void)
  34980. {
  34981. int res = TEST_SKIPPED;
  34982. #if defined(OPENSSL_EXTRA)
  34983. BIO* bio;
  34984. BIO* bio2;
  34985. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  34986. char emp[] = "";
  34987. char bio_buffer[20];
  34988. int bufferSz = 20;
  34989. /* try with bad args */
  34990. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  34991. /* try with real msg */
  34992. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  34993. XMEMSET(bio_buffer, 0, bufferSz);
  34994. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  34995. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  34996. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  34997. AssertFalse(bio2 != BIO_next(bio));
  34998. /* make buffer filled with no terminating characters */
  34999. XMEMSET(bio_buffer, 1, bufferSz);
  35000. /* BIO_gets reads a line of data */
  35001. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35002. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35003. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35004. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  35005. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35006. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35007. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35008. /* check not null terminated string */
  35009. BIO_free(bio);
  35010. msg[0] = 0x33;
  35011. msg[1] = 0x33;
  35012. msg[2] = 0x33;
  35013. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35014. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  35015. AssertIntEQ(bio_buffer[0], msg[0]);
  35016. AssertIntEQ(bio_buffer[1], msg[1]);
  35017. AssertIntNE(bio_buffer[2], msg[2]);
  35018. BIO_free(bio);
  35019. msg[3] = 0x33;
  35020. bio_buffer[3] = 0x33;
  35021. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35022. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  35023. AssertIntEQ(bio_buffer[0], msg[0]);
  35024. AssertIntEQ(bio_buffer[1], msg[1]);
  35025. AssertIntEQ(bio_buffer[2], msg[2]);
  35026. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  35027. /* check reading an empty string */
  35028. BIO_free(bio);
  35029. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  35030. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35031. AssertStrEQ(emp, bio_buffer);
  35032. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35033. /* check error cases */
  35034. BIO_free(bio);
  35035. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  35036. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35037. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35038. #if !defined(NO_FILESYSTEM)
  35039. {
  35040. BIO* f_bio;
  35041. XFILE f;
  35042. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  35043. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35044. f = XFOPEN(svrCertFile, "rb");
  35045. AssertTrue((f != XBADFILE));
  35046. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  35047. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35048. BIO_free(f_bio);
  35049. }
  35050. #endif /* NO_FILESYSTEM */
  35051. BIO_free(bio);
  35052. BIO_free(bio2);
  35053. /* try with type BIO */
  35054. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  35055. sizeof(msg));
  35056. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35057. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35058. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  35059. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  35060. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  35061. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  35062. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  35063. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35064. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35065. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35066. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  35067. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35068. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35069. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35070. BIO_free(bio);
  35071. BIO_free(bio2);
  35072. /* check reading an empty string */
  35073. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  35074. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  35075. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35076. AssertStrEQ(emp, bio_buffer);
  35077. BIO_free(bio);
  35078. res = TEST_RES_CHECK(1);
  35079. #endif
  35080. return res;
  35081. }
  35082. static int test_wolfSSL_BIO_puts(void)
  35083. {
  35084. int res = TEST_SKIPPED;
  35085. #if defined(OPENSSL_EXTRA)
  35086. BIO* bio;
  35087. char input[] = "hello\0world\n.....ok\n\0";
  35088. char output[128];
  35089. XMEMSET(output, 0, sizeof(output));
  35090. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35091. AssertIntEQ(BIO_puts(bio, input), 5);
  35092. AssertIntEQ(BIO_pending(bio), 5);
  35093. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  35094. AssertIntEQ(BIO_pending(bio), 19);
  35095. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  35096. AssertStrEQ(output, "helloworld\n");
  35097. AssertIntEQ(BIO_pending(bio), 8);
  35098. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  35099. AssertStrEQ(output, ".....ok\n");
  35100. AssertIntEQ(BIO_pending(bio), 0);
  35101. AssertIntEQ(BIO_puts(bio, ""), -1);
  35102. BIO_free(bio);
  35103. res = TEST_RES_CHECK(1);
  35104. #endif
  35105. return res;
  35106. }
  35107. static int test_wolfSSL_BIO_dump(void)
  35108. {
  35109. int res = TEST_SKIPPED;
  35110. #if defined(OPENSSL_EXTRA)
  35111. BIO* bio;
  35112. static const unsigned char data[] = {
  35113. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  35114. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  35115. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  35116. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  35117. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  35118. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  35119. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  35120. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  35121. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  35122. 0xB4
  35123. };
  35124. /* Generated with OpenSSL. */
  35125. static const char expected[] =
  35126. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  35127. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  35128. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  35129. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  35130. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  35131. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  35132. static const char expectedAll[] =
  35133. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  35134. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  35135. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  35136. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35137. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35138. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35139. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35140. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35141. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35142. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35143. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35144. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35145. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35146. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35147. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35148. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35149. char output[16 * 80];
  35150. int i;
  35151. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35152. /* Example key dumped. */
  35153. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35154. sizeof(expected) - 1);
  35155. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35156. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35157. /* Try every possible value for a character. */
  35158. for (i = 0; i < 256; i++)
  35159. output[i] = i;
  35160. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35161. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35162. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35163. BIO_free(bio);
  35164. res = TEST_RES_CHECK(1);
  35165. #endif
  35166. return res;
  35167. }
  35168. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35169. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35170. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35171. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35172. {
  35173. (void)ssl;
  35174. (void)buf;
  35175. (void)sz;
  35176. (void)ctx;
  35177. return WOLFSSL_CBIO_ERR_WANT_READ;
  35178. }
  35179. #endif
  35180. static int test_wolfSSL_BIO_should_retry(void)
  35181. {
  35182. int res = TEST_SKIPPED;
  35183. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35184. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35185. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35186. tcp_ready ready;
  35187. func_args server_args;
  35188. THREAD_TYPE serverThread;
  35189. SOCKET_T sockfd = 0;
  35190. WOLFSSL_CTX* ctx;
  35191. WOLFSSL* ssl;
  35192. char msg[64] = "hello wolfssl!";
  35193. char reply[1024];
  35194. int msgSz = (int)XSTRLEN(msg);
  35195. int ret;
  35196. BIO* bio;
  35197. XMEMSET(&server_args, 0, sizeof(func_args));
  35198. #ifdef WOLFSSL_TIRTOS
  35199. fdOpenSession(Task_self());
  35200. #endif
  35201. StartTCP();
  35202. InitTcpReady(&ready);
  35203. #if defined(USE_WINDOWS_API)
  35204. /* use RNG to get random port if using windows */
  35205. ready.port = GetRandomPort();
  35206. #endif
  35207. server_args.signal = &ready;
  35208. start_thread(test_server_nofail, &server_args, &serverThread);
  35209. wait_tcp_ready(&server_args);
  35210. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35211. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35212. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35213. #endif
  35214. AssertIntEQ(WOLFSSL_SUCCESS,
  35215. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35216. AssertIntEQ(WOLFSSL_SUCCESS,
  35217. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35218. AssertIntEQ(WOLFSSL_SUCCESS,
  35219. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35220. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35221. /* force retry */
  35222. ssl = wolfSSL_new(ctx);
  35223. AssertNotNull(ssl);
  35224. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35225. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35226. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  35227. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  35228. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  35229. AssertIntNE(BIO_should_retry(bio), 0);
  35230. /* now perform successful connection */
  35231. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35232. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35233. BIO_read(bio, reply, sizeof(reply));
  35234. ret = wolfSSL_get_error(ssl, -1);
  35235. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35236. AssertIntNE(BIO_should_retry(bio), 0);
  35237. }
  35238. else {
  35239. AssertIntEQ(BIO_should_retry(bio), 0);
  35240. }
  35241. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35242. XSTRLEN("I hear you fa shizzle!")), 0);
  35243. BIO_free(bio);
  35244. wolfSSL_CTX_free(ctx);
  35245. join_thread(serverThread);
  35246. FreeTcpReady(&ready);
  35247. #ifdef WOLFSSL_TIRTOS
  35248. fdOpenSession(Task_self());
  35249. #endif
  35250. res = TEST_RES_CHECK(1);
  35251. #endif
  35252. return res;
  35253. }
  35254. static int test_wolfSSL_BIO_connect(void)
  35255. {
  35256. int res = TEST_SKIPPED;
  35257. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35258. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35259. tcp_ready ready;
  35260. func_args server_args;
  35261. THREAD_TYPE serverThread;
  35262. BIO *tcpBio;
  35263. BIO *sslBio;
  35264. SSL_CTX* ctx;
  35265. SSL *ssl;
  35266. SSL *sslPtr;
  35267. char msg[] = "hello wolfssl!";
  35268. char reply[30];
  35269. char buff[10] = {0};
  35270. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35271. AssertIntEQ(WOLFSSL_SUCCESS,
  35272. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35273. AssertIntEQ(WOLFSSL_SUCCESS,
  35274. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35275. AssertIntEQ(WOLFSSL_SUCCESS,
  35276. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35277. /* Setup server */
  35278. XMEMSET(&server_args, 0, sizeof(func_args));
  35279. StartTCP();
  35280. InitTcpReady(&ready);
  35281. #if defined(USE_WINDOWS_API)
  35282. /* use RNG to get random port if using windows */
  35283. ready.port = GetRandomPort();
  35284. #endif
  35285. server_args.signal = &ready;
  35286. start_thread(test_server_nofail, &server_args, &serverThread);
  35287. wait_tcp_ready(&server_args);
  35288. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35289. /* Start the test proper */
  35290. /* Setup the TCP BIO */
  35291. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35292. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35293. /* Setup the SSL object */
  35294. AssertNotNull(ssl = SSL_new(ctx));
  35295. SSL_set_connect_state(ssl);
  35296. /* Setup the SSL BIO */
  35297. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35298. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35299. /* Verify that BIO_get_ssl works. */
  35300. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35301. AssertPtrEq(ssl, sslPtr);
  35302. /* Link BIO's so that sslBio uses tcpBio for IO */
  35303. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35304. /* Do TCP connect */
  35305. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35306. /* Do TLS handshake */
  35307. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35308. /* Test writing */
  35309. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35310. /* Expect length of default wolfSSL reply */
  35311. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35312. /* Clean it all up */
  35313. BIO_free_all(sslBio);
  35314. /* Server clean up */
  35315. join_thread(serverThread);
  35316. FreeTcpReady(&ready);
  35317. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35318. * after. */
  35319. XMEMSET(&server_args, 0, sizeof(func_args));
  35320. StartTCP();
  35321. InitTcpReady(&ready);
  35322. #if defined(USE_WINDOWS_API)
  35323. /* use RNG to get random port if using windows */
  35324. ready.port = GetRandomPort();
  35325. #endif
  35326. server_args.signal = &ready;
  35327. start_thread(test_server_nofail, &server_args, &serverThread);
  35328. wait_tcp_ready(&server_args);
  35329. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35330. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35331. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35332. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35333. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35334. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35335. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35336. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35337. /* Attempt to close the TLS connection gracefully. */
  35338. BIO_ssl_shutdown(sslBio);
  35339. BIO_free_all(sslBio);
  35340. join_thread(serverThread);
  35341. FreeTcpReady(&ready);
  35342. SSL_CTX_free(ctx);
  35343. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35344. wc_ecc_fp_free(); /* free per thread cache */
  35345. #endif
  35346. res = TEST_RES_CHECK(1);
  35347. #endif
  35348. return res;
  35349. }
  35350. static int test_wolfSSL_BIO_tls(void)
  35351. {
  35352. int res = TEST_SKIPPED;
  35353. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35354. SSL_CTX* ctx;
  35355. SSL *ssl;
  35356. BIO *readBio;
  35357. BIO *writeBio;
  35358. int ret, err = 0;
  35359. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35360. AssertNotNull(ssl = SSL_new(ctx));
  35361. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  35362. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  35363. /* Qt reads data from write-bio,
  35364. * then writes the read data into plain packet.
  35365. * Qt reads data from plain packet,
  35366. * then writes the read data into read-bio.
  35367. */
  35368. SSL_set_bio(ssl, readBio, writeBio);
  35369. do {
  35370. #ifdef WOLFSSL_ASYNC_CRYPT
  35371. if (err == WC_PENDING_E) {
  35372. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35373. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35374. }
  35375. #endif
  35376. ret = SSL_connect(ssl);
  35377. err = SSL_get_error(ssl, 0);
  35378. } while (err == WC_PENDING_E);
  35379. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35380. /* in this use case, should return WANT READ
  35381. * so that Qt will read the data from plain packet for next state.
  35382. */
  35383. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  35384. SSL_free(ssl);
  35385. SSL_CTX_free(ctx);
  35386. res = TEST_RES_CHECK(1);
  35387. #endif
  35388. return res;
  35389. }
  35390. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35391. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35392. {
  35393. BIO* clientBio;
  35394. SSL* sslClient;
  35395. SSL_CTX* ctx;
  35396. char connectAddr[20]; /* IP + port */;
  35397. (void)args;
  35398. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35399. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35400. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35401. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35402. AssertNotNull(sslClient = SSL_new(ctx));
  35403. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35404. SSL_set_bio(sslClient, clientBio, clientBio);
  35405. AssertIntEQ(SSL_connect(sslClient), 1);
  35406. SSL_free(sslClient);
  35407. SSL_CTX_free(ctx);
  35408. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35409. wc_ecc_fp_free(); /* free per thread cache */
  35410. #endif
  35411. return 0;
  35412. }
  35413. #endif
  35414. static int test_wolfSSL_BIO_accept(void)
  35415. {
  35416. int res = TEST_SKIPPED;
  35417. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35418. BIO* serverBindBio;
  35419. BIO* serverAcceptBio;
  35420. SSL* sslServer;
  35421. SSL_CTX* ctx;
  35422. func_args args;
  35423. THREAD_TYPE thread;
  35424. char port[10]; /* 10 bytes should be enough to store the string
  35425. * representation of the port */
  35426. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35427. AssertNotNull(serverBindBio = BIO_new_accept(port));
  35428. /* First BIO_do_accept binds the port */
  35429. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35430. XMEMSET(&args, 0, sizeof(func_args));
  35431. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35432. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35433. /* Let's plug it into SSL to test */
  35434. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35435. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35436. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35437. AssertNotNull(sslServer = SSL_new(ctx));
  35438. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35439. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35440. AssertIntEQ(SSL_accept(sslServer), 1);
  35441. join_thread(thread);
  35442. BIO_free(serverBindBio);
  35443. SSL_free(sslServer);
  35444. SSL_CTX_free(ctx);
  35445. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35446. wc_ecc_fp_free(); /* free per thread cache */
  35447. #endif
  35448. res = TEST_RES_CHECK(1);
  35449. #endif
  35450. return res;
  35451. }
  35452. static int test_wolfSSL_BIO_write(void)
  35453. {
  35454. int res = TEST_SKIPPED;
  35455. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35456. BIO* bio;
  35457. BIO* bio64;
  35458. BIO* ptr;
  35459. int sz;
  35460. char msg[] = "conversion test";
  35461. char out[40];
  35462. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35463. void* bufPtr = NULL;
  35464. BUF_MEM* buf = NULL;
  35465. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35466. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35467. /* now should convert to base64 then write to memory */
  35468. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35469. BIO_flush(bio);
  35470. /* test BIO chain */
  35471. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35472. AssertNotNull(buf);
  35473. AssertIntEQ(buf->length, 25);
  35474. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35475. AssertPtrEq(buf->data, bufPtr);
  35476. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35477. sz = sizeof(out);
  35478. XMEMSET(out, 0, sz);
  35479. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35480. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35481. /* write then read should return the same message */
  35482. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35483. sz = sizeof(out);
  35484. XMEMSET(out, 0, sz);
  35485. AssertIntEQ(BIO_read(bio, out, sz), 16);
  35486. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35487. /* now try encoding with no line ending */
  35488. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35489. #ifdef HAVE_EX_DATA
  35490. BIO_set_ex_data(bio64, 0, (void*) "data");
  35491. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35492. #endif
  35493. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35494. BIO_flush(bio);
  35495. sz = sizeof(out);
  35496. XMEMSET(out, 0, sz);
  35497. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35498. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35499. BIO_free_all(bio); /* frees bio64 also */
  35500. /* test with more than one bio64 in list */
  35501. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35502. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35503. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  35504. /* now should convert to base64 when stored and then decode with read */
  35505. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35506. BIO_flush(bio);
  35507. sz = sizeof(out);
  35508. XMEMSET(out, 0, sz);
  35509. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35510. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  35511. BIO_clear_flags(bio64, ~0);
  35512. BIO_set_retry_read(bio);
  35513. BIO_free_all(bio); /* frees bio64s also */
  35514. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  35515. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35516. BIO_free(bio);
  35517. res = TEST_RES_CHECK(1);
  35518. #endif
  35519. return res;
  35520. }
  35521. static int test_wolfSSL_BIO_printf(void)
  35522. {
  35523. int res = TEST_SKIPPED;
  35524. #if defined(OPENSSL_ALL)
  35525. BIO* bio;
  35526. int sz = 7;
  35527. char msg[] = "TLS 1.3 for the world";
  35528. char out[60];
  35529. char expected[] = "TLS 1.3 for the world : sz = 7";
  35530. XMEMSET(out, 0, sizeof(out));
  35531. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35532. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35533. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35534. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35535. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35536. BIO_free(bio);
  35537. res = TEST_RES_CHECK(1);
  35538. #endif
  35539. return res;
  35540. }
  35541. static int test_wolfSSL_BIO_f_md(void)
  35542. {
  35543. int res = TEST_SKIPPED;
  35544. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35545. BIO *bio, *mem;
  35546. char msg[] = "message to hash";
  35547. char out[60];
  35548. EVP_MD_CTX* ctx;
  35549. const unsigned char testKey[] =
  35550. {
  35551. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35552. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35553. 0x0b, 0x0b, 0x0b, 0x0b
  35554. };
  35555. const char testData[] = "Hi There";
  35556. const unsigned char testResult[] =
  35557. {
  35558. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35559. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35560. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35561. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35562. };
  35563. const unsigned char expectedHash[] =
  35564. {
  35565. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35566. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35567. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35568. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35569. };
  35570. const unsigned char emptyHash[] =
  35571. {
  35572. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35573. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35574. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35575. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35576. };
  35577. unsigned char check[sizeof(testResult) + 1];
  35578. size_t checkSz = -1;
  35579. EVP_PKEY* key;
  35580. XMEMSET(out, 0, sizeof(out));
  35581. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35582. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35583. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35584. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35585. /* should not be able to write/read yet since just digest wrapper and no
  35586. * data is passing through the bio */
  35587. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  35588. AssertIntEQ(BIO_pending(bio), 0);
  35589. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35590. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  35591. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35592. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35593. BIO_reset(bio);
  35594. /* append BIO mem to bio in order to read/write */
  35595. AssertNotNull(bio = BIO_push(bio, mem));
  35596. XMEMSET(out, 0, sizeof(out));
  35597. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35598. AssertIntEQ(BIO_pending(bio), 16);
  35599. /* this just reads the message and does not hash it (gets calls final) */
  35600. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35601. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35602. /* create a message digest using BIO */
  35603. XMEMSET(out, 0, sizeof(out));
  35604. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35605. AssertIntEQ(BIO_pending(mem), 16);
  35606. AssertIntEQ(BIO_pending(bio), 16);
  35607. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35608. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35609. BIO_free(bio);
  35610. BIO_free(mem);
  35611. /* test with HMAC */
  35612. XMEMSET(out, 0, sizeof(out));
  35613. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35614. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35615. BIO_get_md_ctx(bio, &ctx);
  35616. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  35617. testKey, (int)sizeof(testKey)));
  35618. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35619. AssertNotNull(bio = BIO_push(bio, mem));
  35620. BIO_write(bio, testData, (int)strlen(testData));
  35621. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  35622. EVP_DigestSignFinal(ctx, check, &checkSz);
  35623. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35624. EVP_PKEY_free(key);
  35625. BIO_free(bio);
  35626. BIO_free(mem);
  35627. res = TEST_RES_CHECK(1);
  35628. #endif
  35629. return res;
  35630. }
  35631. static int test_wolfSSL_BIO_up_ref(void)
  35632. {
  35633. int res = TEST_SKIPPED;
  35634. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35635. BIO* bio;
  35636. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35637. AssertIntEQ(BIO_up_ref(NULL), 0);
  35638. AssertIntEQ(BIO_up_ref(bio), 1);
  35639. BIO_free(bio);
  35640. AssertIntEQ(BIO_up_ref(bio), 1);
  35641. BIO_free(bio);
  35642. BIO_free(bio);
  35643. res = TEST_RES_CHECK(1);
  35644. #endif
  35645. return res;
  35646. }
  35647. #endif /* !NO_BIO */
  35648. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35649. /* test that the callback arg is correct */
  35650. static int certCbArg = 0;
  35651. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35652. {
  35653. if (ssl == NULL || arg != &certCbArg)
  35654. return 0;
  35655. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35656. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35657. return 0;
  35658. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35659. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35660. return 0;
  35661. return 1;
  35662. }
  35663. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  35664. {
  35665. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35666. }
  35667. /**
  35668. * This is only done because test_client_nofail has no way to stop
  35669. * certificate and key loading
  35670. */
  35671. static void clientCertClearCb(WOLFSSL* ssl)
  35672. {
  35673. /* Clear the loaded certs to force the callbacks to set them up */
  35674. SSL_certs_clear(ssl);
  35675. }
  35676. static int serverCertCb(WOLFSSL* ssl, void* arg)
  35677. {
  35678. if (ssl == NULL || arg != &certCbArg)
  35679. return 0;
  35680. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  35681. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35682. return 0;
  35683. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  35684. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35685. return 0;
  35686. return 1;
  35687. }
  35688. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  35689. {
  35690. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  35691. }
  35692. /**
  35693. * This is only done because test_server_nofail has no way to stop
  35694. * certificate and key loading
  35695. */
  35696. static void serverCertClearCb(WOLFSSL* ssl)
  35697. {
  35698. /* Clear the loaded certs to force the callbacks to set them up */
  35699. SSL_certs_clear(ssl);
  35700. }
  35701. #endif
  35702. static int test_wolfSSL_cert_cb(void)
  35703. {
  35704. int res = TEST_SKIPPED;
  35705. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35706. callback_functions func_cb_client;
  35707. callback_functions func_cb_server;
  35708. tcp_ready ready;
  35709. func_args client_args;
  35710. func_args server_args;
  35711. THREAD_TYPE serverThread;
  35712. XMEMSET(&client_args, 0, sizeof(func_args));
  35713. XMEMSET(&server_args, 0, sizeof(func_args));
  35714. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  35715. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  35716. #ifdef WOLFSSL_TIRTOS
  35717. fdOpenSession(Task_self());
  35718. #endif
  35719. StartTCP();
  35720. InitTcpReady(&ready);
  35721. #if defined(USE_WINDOWS_API)
  35722. /* use RNG to get random port if using windows */
  35723. ready.port = GetRandomPort();
  35724. #endif
  35725. server_args.signal = &ready;
  35726. client_args.signal = &ready;
  35727. client_args.callbacks = &func_cb_client;
  35728. server_args.callbacks = &func_cb_server;
  35729. func_cb_client.ctx_ready = clientCertSetupCb;
  35730. func_cb_client.ssl_ready = clientCertClearCb;
  35731. func_cb_server.ctx_ready = serverCertSetupCb;
  35732. func_cb_server.ssl_ready = serverCertClearCb;
  35733. start_thread(test_server_nofail, &server_args, &serverThread);
  35734. wait_tcp_ready(&server_args);
  35735. test_client_nofail(&client_args, NULL);
  35736. join_thread(serverThread);
  35737. AssertTrue(client_args.return_code);
  35738. AssertTrue(server_args.return_code);
  35739. FreeTcpReady(&ready);
  35740. #ifdef WOLFSSL_TIRTOS
  35741. fdOpenSession(Task_self());
  35742. #endif
  35743. res = TEST_RES_CHECK(1);
  35744. #endif
  35745. return res;
  35746. }
  35747. static int test_wolfSSL_SESSION(void)
  35748. {
  35749. int res = TEST_SKIPPED;
  35750. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35751. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35752. !defined(NO_SESSION_CACHE)
  35753. WOLFSSL* ssl;
  35754. WOLFSSL_CTX* ctx;
  35755. WOLFSSL_SESSION* sess;
  35756. WOLFSSL_SESSION* sess_copy;
  35757. #ifdef OPENSSL_EXTRA
  35758. unsigned char* sessDer = NULL;
  35759. unsigned char* ptr = NULL;
  35760. const unsigned char context[] = "user app context";
  35761. unsigned int contextSz = (unsigned int)sizeof(context);
  35762. int sz;
  35763. #endif
  35764. int ret, err;
  35765. SOCKET_T sockfd;
  35766. tcp_ready ready;
  35767. func_args server_args;
  35768. THREAD_TYPE serverThread;
  35769. char msg[80];
  35770. const char* sendGET = "GET";
  35771. /* TLS v1.3 requires session tickets */
  35772. /* CHACHA and POLY1305 required for myTicketEncCb */
  35773. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  35774. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  35775. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  35776. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35777. #else
  35778. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35779. #endif
  35780. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35781. WOLFSSL_FILETYPE_PEM));
  35782. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35783. WOLFSSL_FILETYPE_PEM));
  35784. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35785. WOLFSSL_SUCCESS);
  35786. #ifdef WOLFSSL_ENCRYPTED_KEYS
  35787. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  35788. #endif
  35789. #ifdef HAVE_SESSION_TICKET
  35790. /* Use session tickets, for ticket tests below */
  35791. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  35792. #endif
  35793. XMEMSET(&server_args, 0, sizeof(func_args));
  35794. #ifdef WOLFSSL_TIRTOS
  35795. fdOpenSession(Task_self());
  35796. #endif
  35797. StartTCP();
  35798. InitTcpReady(&ready);
  35799. #if defined(USE_WINDOWS_API)
  35800. /* use RNG to get random port if using windows */
  35801. ready.port = GetRandomPort();
  35802. #endif
  35803. server_args.signal = &ready;
  35804. start_thread(test_server_nofail, &server_args, &serverThread);
  35805. wait_tcp_ready(&server_args);
  35806. /* client connection */
  35807. ssl = wolfSSL_new(ctx);
  35808. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  35809. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35810. #ifdef WOLFSSL_ASYNC_CRYPT
  35811. err = 0; /* Reset error */
  35812. #endif
  35813. do {
  35814. #ifdef WOLFSSL_ASYNC_CRYPT
  35815. if (err == WC_PENDING_E) {
  35816. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35817. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35818. }
  35819. #endif
  35820. ret = wolfSSL_connect(ssl);
  35821. err = wolfSSL_get_error(ssl, 0);
  35822. } while (err == WC_PENDING_E);
  35823. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  35824. #ifdef WOLFSSL_ASYNC_CRYPT
  35825. err = 0; /* Reset error */
  35826. #endif
  35827. do {
  35828. #ifdef WOLFSSL_ASYNC_CRYPT
  35829. if (err == WC_PENDING_E) {
  35830. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35831. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35832. }
  35833. #endif
  35834. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  35835. err = wolfSSL_get_error(ssl, 0);
  35836. } while (err == WC_PENDING_E);
  35837. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  35838. #ifdef WOLFSSL_ASYNC_CRYPT
  35839. err = 0; /* Reset error */
  35840. #endif
  35841. do {
  35842. #ifdef WOLFSSL_ASYNC_CRYPT
  35843. if (err == WC_PENDING_E) {
  35844. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35845. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35846. }
  35847. #endif
  35848. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  35849. err = wolfSSL_get_error(ssl, 0);
  35850. } while (err == WC_PENDING_E);
  35851. AssertIntEQ(ret, 23);
  35852. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  35853. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  35854. #ifdef HAVE_EXT_CACHE
  35855. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  35856. * HAVE_EXT_CACHE we get new objects each time */
  35857. #endif
  35858. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  35859. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  35860. sess = wolfSSL_get_session(ssl);
  35861. #ifdef OPENSSL_EXTRA
  35862. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  35863. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  35864. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  35865. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  35866. #ifdef HAVE_SESSION_TICKET
  35867. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  35868. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  35869. SESSION_TICKET_HINT_DEFAULT);
  35870. #else
  35871. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  35872. #endif
  35873. #else
  35874. (void)sess;
  35875. #endif /* OPENSSL_EXTRA */
  35876. /* Retain copy of the session for later testing */
  35877. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  35878. wolfSSL_shutdown(ssl);
  35879. wolfSSL_free(ssl);
  35880. join_thread(serverThread);
  35881. FreeTcpReady(&ready);
  35882. #ifdef WOLFSSL_TIRTOS
  35883. fdOpenSession(Task_self());
  35884. #endif
  35885. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  35886. {
  35887. X509 *x509;
  35888. char buf[30];
  35889. int bufSz;
  35890. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  35891. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  35892. X509_get_subject_name(x509), NID_organizationalUnitName,
  35893. buf, sizeof(buf))), 0);
  35894. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  35895. if (bufSz == 7) {
  35896. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  35897. }
  35898. if (bufSz == 16) {
  35899. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  35900. }
  35901. }
  35902. #endif
  35903. #ifdef HAVE_EXT_CACHE
  35904. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  35905. wolfSSL_SESSION_free(sess_copy);
  35906. sess_copy = NULL;
  35907. #endif
  35908. #ifdef OPENSSL_EXTRA
  35909. /* get session from DER and update the timeout */
  35910. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  35911. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  35912. wolfSSL_SESSION_free(sess);
  35913. sess = NULL;
  35914. ptr = sessDer;
  35915. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  35916. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  35917. (const unsigned char**)&ptr, sz));
  35918. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  35919. sessDer = NULL;
  35920. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  35921. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35922. #endif
  35923. /* successful set session test */
  35924. AssertNotNull(ssl = wolfSSL_new(ctx));
  35925. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  35926. #ifdef HAVE_SESSION_TICKET
  35927. /* Test set/get session ticket */
  35928. {
  35929. const char* ticket = "This is a session ticket";
  35930. char buf[64] = {0};
  35931. word32 bufSz = (word32)sizeof(buf);
  35932. AssertIntEQ(SSL_SUCCESS,
  35933. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  35934. (word32)XSTRLEN(ticket)));
  35935. AssertIntEQ(SSL_SUCCESS,
  35936. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  35937. AssertStrEQ(ticket, buf);
  35938. }
  35939. #endif
  35940. #ifdef OPENSSL_EXTRA
  35941. /* session timeout case */
  35942. /* make the session to be expired */
  35943. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  35944. XSLEEP_MS(1200);
  35945. /* SSL_set_session should reject specified session but return success
  35946. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  35947. */
  35948. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  35949. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  35950. #else
  35951. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  35952. #endif
  35953. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35954. /* fail case with miss match session context IDs (use compatibility API) */
  35955. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  35956. SSL_SUCCESS);
  35957. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35958. wolfSSL_free(ssl);
  35959. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  35960. SSL_FAILURE);
  35961. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  35962. SSL_SUCCESS);
  35963. AssertNotNull(ssl = wolfSSL_new(ctx));
  35964. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35965. #endif /* OPENSSL_EXTRA */
  35966. wolfSSL_free(ssl);
  35967. wolfSSL_SESSION_free(sess);
  35968. wolfSSL_CTX_free(ctx);
  35969. res = TEST_RES_CHECK(1);
  35970. #endif
  35971. return res;
  35972. }
  35973. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35974. defined(HAVE_EX_DATA)
  35975. static int clientSessRemCountMalloc = 0;
  35976. static int serverSessRemCountMalloc = 0;
  35977. static int clientSessRemCountFree = 0;
  35978. static int serverSessRemCountFree = 0;
  35979. static WOLFSSL_CTX* serverSessCtx = NULL;
  35980. static WOLFSSL_SESSION* serverSess = NULL;
  35981. #ifndef NO_SESSION_CACHE_REF
  35982. static WOLFSSL_CTX* clientSessCtx = NULL;
  35983. static WOLFSSL_SESSION* clientSess = NULL;
  35984. #endif
  35985. static int serverSessRemIdx = 3;
  35986. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  35987. {
  35988. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  35989. (void)ctx;
  35990. AssertNotNull(mallocedData);
  35991. if (!*mallocedData)
  35992. clientSessRemCountFree++;
  35993. else
  35994. serverSessRemCountFree++;
  35995. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  35996. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  35997. }
  35998. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  35999. {
  36000. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  36001. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  36002. !defined(NO_SESSION_CACHE_REF)
  36003. /* Allow downgrade, set min version, and disable TLS 1.3.
  36004. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  36005. * reference to the session cache. But with WOLFSSL_TLS13 and without
  36006. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  36007. * session in the cache. In this case we need to downgrade to previous
  36008. * versions to just use the legacy session ID field. */
  36009. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  36010. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  36011. #endif
  36012. }
  36013. static void SessRemSslSetupCb(WOLFSSL* ssl)
  36014. {
  36015. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  36016. AssertNotNull(mallocedData);
  36017. *mallocedData = SSL_is_server(ssl);
  36018. if (!*mallocedData) {
  36019. clientSessRemCountMalloc++;
  36020. #ifndef NO_SESSION_CACHE_REF
  36021. AssertNotNull(clientSess = SSL_get1_session(ssl));
  36022. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  36023. SSL_SUCCESS);
  36024. #endif
  36025. }
  36026. else {
  36027. serverSessRemCountMalloc++;
  36028. AssertNotNull(serverSess = SSL_get1_session(ssl));
  36029. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  36030. SSL_SUCCESS);
  36031. }
  36032. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  36033. mallocedData), SSL_SUCCESS);
  36034. }
  36035. #endif
  36036. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  36037. {
  36038. int res = TEST_SKIPPED;
  36039. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36040. defined(HAVE_EX_DATA)
  36041. /* Check that the remove callback gets called for external data in a
  36042. * session object */
  36043. callback_functions func_cb;
  36044. tcp_ready ready;
  36045. func_args client_args;
  36046. func_args server_args;
  36047. THREAD_TYPE serverThread;
  36048. XMEMSET(&client_args, 0, sizeof(func_args));
  36049. XMEMSET(&server_args, 0, sizeof(func_args));
  36050. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  36051. #ifdef WOLFSSL_TIRTOS
  36052. fdOpenSession(Task_self());
  36053. #endif
  36054. StartTCP();
  36055. InitTcpReady(&ready);
  36056. #if defined(USE_WINDOWS_API)
  36057. /* use RNG to get random port if using windows */
  36058. ready.port = GetRandomPort();
  36059. #endif
  36060. server_args.signal = &ready;
  36061. client_args.signal = &ready;
  36062. client_args.callbacks = &func_cb;
  36063. server_args.callbacks = &func_cb;
  36064. func_cb.ctx_ready = SessRemCtxSetupCb;
  36065. func_cb.on_result = SessRemSslSetupCb;
  36066. start_thread(test_server_nofail, &server_args, &serverThread);
  36067. wait_tcp_ready(&server_args);
  36068. test_client_nofail(&client_args, NULL);
  36069. join_thread(serverThread);
  36070. AssertTrue(client_args.return_code);
  36071. AssertTrue(server_args.return_code);
  36072. FreeTcpReady(&ready);
  36073. #ifdef WOLFSSL_TIRTOS
  36074. fdOpenSession(Task_self());
  36075. #endif
  36076. /* Both should have been allocated */
  36077. AssertIntEQ(clientSessRemCountMalloc, 1);
  36078. AssertIntEQ(serverSessRemCountMalloc, 1);
  36079. #ifdef NO_SESSION_CACHE_REF
  36080. /* Client session should not be added to cache so this should be free'd when
  36081. * the SSL object was being free'd */
  36082. AssertIntEQ(clientSessRemCountFree, 1);
  36083. #else
  36084. /* Client session is in cache due to requiring a persistent reference */
  36085. AssertIntEQ(clientSessRemCountFree, 0);
  36086. /* Force a cache lookup */
  36087. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36088. /* Force a cache update */
  36089. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  36090. /* This should set the timeout to 0 and call the remove callback from within
  36091. * the session cache. */
  36092. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  36093. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36094. AssertIntEQ(clientSessRemCountFree, 1);
  36095. #endif
  36096. /* Server session is in the cache so ex_data isn't free'd with the SSL
  36097. * object */
  36098. AssertIntEQ(serverSessRemCountFree, 0);
  36099. /* Force a cache lookup */
  36100. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36101. /* Force a cache update */
  36102. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  36103. /* This should set the timeout to 0 and call the remove callback from within
  36104. * the session cache. */
  36105. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  36106. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36107. AssertIntEQ(serverSessRemCountFree, 1);
  36108. /* Need to free the references that we kept */
  36109. SSL_CTX_free(serverSessCtx);
  36110. SSL_SESSION_free(serverSess);
  36111. #ifndef NO_SESSION_CACHE_REF
  36112. SSL_CTX_free(clientSessCtx);
  36113. SSL_SESSION_free(clientSess);
  36114. #endif
  36115. res = TEST_RES_CHECK(1);
  36116. #endif
  36117. return res;
  36118. }
  36119. static int test_wolfSSL_ticket_keys(void)
  36120. {
  36121. int res = TEST_SKIPPED;
  36122. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  36123. !defined(NO_WOLFSSL_SERVER)
  36124. WOLFSSL_CTX* ctx;
  36125. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  36126. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36127. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  36128. WOLFSSL_FAILURE);
  36129. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  36130. WOLFSSL_FAILURE);
  36131. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  36132. WOLFSSL_FAILURE);
  36133. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  36134. WOLFSSL_FAILURE);
  36135. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36136. WOLFSSL_FAILURE);
  36137. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36138. WOLFSSL_FAILURE);
  36139. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36140. WOLFSSL_FAILURE);
  36141. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36142. WOLFSSL_FAILURE);
  36143. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36144. WOLFSSL_FAILURE);
  36145. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36146. WOLFSSL_FAILURE);
  36147. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36148. WOLFSSL_FAILURE);
  36149. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36150. WOLFSSL_FAILURE);
  36151. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36152. WOLFSSL_FAILURE);
  36153. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36154. WOLFSSL_FAILURE);
  36155. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36156. WOLFSSL_SUCCESS);
  36157. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36158. WOLFSSL_SUCCESS);
  36159. wolfSSL_CTX_free(ctx);
  36160. res = TEST_RES_CHECK(1);
  36161. #endif
  36162. return res;
  36163. }
  36164. #ifndef NO_BIO
  36165. static int test_wolfSSL_d2i_PUBKEY(void)
  36166. {
  36167. int res = TEST_SKIPPED;
  36168. #if defined(OPENSSL_EXTRA)
  36169. BIO* bio;
  36170. EVP_PKEY* pkey;
  36171. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36172. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  36173. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36174. /* RSA PUBKEY test */
  36175. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  36176. sizeof_client_keypub_der_2048), 0);
  36177. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36178. EVP_PKEY_free(pkey);
  36179. #endif
  36180. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36181. /* ECC PUBKEY test */
  36182. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36183. sizeof_ecc_clikeypub_der_256), 0);
  36184. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36185. EVP_PKEY_free(pkey);
  36186. #endif
  36187. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36188. /* DSA PUBKEY test */
  36189. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36190. sizeof_dsa_pub_key_der_2048), 0);
  36191. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36192. EVP_PKEY_free(pkey);
  36193. #endif
  36194. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36195. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36196. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36197. (HAVE_FIPS_VERSION > 2))
  36198. /* DH PUBKEY test */
  36199. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36200. sizeof_dh_pub_key_der_2048), 0);
  36201. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36202. EVP_PKEY_free(pkey);
  36203. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36204. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36205. BIO_free(bio);
  36206. (void)pkey;
  36207. res = TEST_RES_CHECK(1);
  36208. #endif
  36209. return res;
  36210. }
  36211. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36212. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36213. {
  36214. BIO* bio = NULL;
  36215. EVP_PKEY* pkey = NULL;
  36216. #ifndef NO_RSA
  36217. #endif
  36218. WOLFSSL_CTX* ctx;
  36219. #if defined(WOLFSSL_KEY_GEN)
  36220. unsigned char buff[4096];
  36221. unsigned char* bufPtr = buff;
  36222. #endif
  36223. /* test creating new EVP_PKEY with bad arg */
  36224. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36225. /* test loading RSA key using BIO */
  36226. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36227. {
  36228. XFILE file;
  36229. const char* fname = "./certs/server-key.der";
  36230. size_t sz;
  36231. byte* buf;
  36232. file = XFOPEN(fname, "rb");
  36233. AssertTrue((file != XBADFILE));
  36234. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36235. sz = XFTELL(file);
  36236. XREWIND(file);
  36237. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36238. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36239. XFCLOSE(file);
  36240. /* Test using BIO new mem and loading DER private key */
  36241. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36242. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36243. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36244. BIO_free(bio);
  36245. bio = NULL;
  36246. EVP_PKEY_free(pkey);
  36247. pkey = NULL;
  36248. }
  36249. #endif
  36250. /* test loading ECC key using BIO */
  36251. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36252. {
  36253. XFILE file;
  36254. const char* fname = "./certs/ecc-key.der";
  36255. size_t sz;
  36256. byte* buf;
  36257. file = XFOPEN(fname, "rb");
  36258. AssertTrue((file != XBADFILE));
  36259. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36260. sz = XFTELL(file);
  36261. XREWIND(file);
  36262. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36263. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36264. XFCLOSE(file);
  36265. /* Test using BIO new mem and loading DER private key */
  36266. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36267. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36268. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36269. BIO_free(bio);
  36270. bio = NULL;
  36271. EVP_PKEY_free(pkey);
  36272. pkey = NULL;
  36273. }
  36274. #endif
  36275. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36276. #ifndef NO_WOLFSSL_SERVER
  36277. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36278. #else
  36279. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36280. #endif
  36281. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36282. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36283. {
  36284. RSA* rsa = NULL;
  36285. /* Tests bad parameters */
  36286. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36287. /* RSA not set yet, expecting to fail*/
  36288. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36289. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36290. /* set RSA using bio*/
  36291. AssertIntGT(BIO_write(bio, client_key_der_2048,
  36292. sizeof_client_key_der_2048), 0);
  36293. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36294. AssertNotNull(rsa);
  36295. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36296. /*i2d RSAprivate key tests */
  36297. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36298. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36299. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36300. sizeof_client_key_der_2048);
  36301. bufPtr -= sizeof_client_key_der_2048;
  36302. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36303. sizeof_client_key_der_2048), 0);
  36304. bufPtr = NULL;
  36305. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36306. sizeof_client_key_der_2048);
  36307. AssertNotNull(bufPtr);
  36308. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36309. sizeof_client_key_der_2048), 0);
  36310. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36311. RSA_free(rsa);
  36312. rsa = RSA_new();
  36313. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36314. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36315. RSA_free(rsa);
  36316. }
  36317. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36318. SSL_CTX_free(ctx);
  36319. ctx = NULL;
  36320. BIO_free(bio);
  36321. bio = NULL;
  36322. return TEST_RES_CHECK(1);
  36323. }
  36324. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36325. #endif /* !NO_BIO */
  36326. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36327. {
  36328. int res = TEST_SKIPPED;
  36329. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36330. !defined(NO_RSA)
  36331. X509* x509;
  36332. GENERAL_NAME* gn;
  36333. unsigned char buf[4096];
  36334. const unsigned char* bufPt;
  36335. int bytes, i;
  36336. int j;
  36337. XFILE f;
  36338. STACK_OF(GENERAL_NAME)* sk;
  36339. f = XFOPEN(cliCertDerFileExt, "rb");
  36340. AssertTrue((f != XBADFILE));
  36341. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36342. XFCLOSE(f);
  36343. for (j = 0; j < 2; ++j) {
  36344. bufPt = buf;
  36345. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36346. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36347. NID_subject_alt_name, NULL, NULL));
  36348. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36349. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36350. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36351. switch (gn->type) {
  36352. case GEN_DNS:
  36353. fprintf(stderr, "found type GEN_DNS\n");
  36354. break;
  36355. case GEN_EMAIL:
  36356. fprintf(stderr, "found type GEN_EMAIL\n");
  36357. break;
  36358. case GEN_URI:
  36359. fprintf(stderr, "found type GEN_URI\n");
  36360. break;
  36361. }
  36362. }
  36363. X509_free(x509);
  36364. if (j == 0) {
  36365. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36366. }
  36367. else {
  36368. /*
  36369. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36370. * it was supposed to. This is a regression test for that bug.
  36371. */
  36372. GENERAL_NAMES_free(sk);
  36373. }
  36374. }
  36375. res = TEST_RES_CHECK(1);
  36376. #endif
  36377. return res;
  36378. }
  36379. static int test_wolfSSL_GENERAL_NAME_print(void)
  36380. {
  36381. int res = TEST_SKIPPED;
  36382. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36383. X509* x509;
  36384. GENERAL_NAME* gn;
  36385. unsigned char buf[4096];
  36386. const unsigned char* bufPt;
  36387. int bytes;
  36388. XFILE f;
  36389. STACK_OF(GENERAL_NAME)* sk;
  36390. BIO* out;
  36391. unsigned char outbuf[128];
  36392. X509_EXTENSION* ext;
  36393. AUTHORITY_INFO_ACCESS* aia;
  36394. ACCESS_DESCRIPTION* ad;
  36395. const unsigned char v4Addr[] = {192,168,53,1};
  36396. const unsigned char v6Addr[] =
  36397. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36398. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36399. const unsigned char email[] =
  36400. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36401. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36402. const char* dnsStr = "DNS:example.com";
  36403. const char* uriStr = "URI:http://127.0.0.1:22220";
  36404. const char* v4addStr = "IP Address:192.168.53.1";
  36405. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36406. const char* emailStr = "email:info@wolfssl.com";
  36407. const char* othrStr = "othername:<unsupported>";
  36408. const char* x400Str = "X400Name:<unsupported>";
  36409. const char* ediStr = "EdiPartyName:<unsupported>";
  36410. /* BIO to output */
  36411. AssertNotNull(out = BIO_new(BIO_s_mem()));
  36412. /* test for NULL param */
  36413. gn = NULL;
  36414. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36415. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36416. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36417. /* test for GEN_DNS */
  36418. f = XFOPEN(cliCertDerFileExt, "rb");
  36419. AssertTrue((f != XBADFILE));
  36420. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36421. XFCLOSE(f);
  36422. bufPt = buf;
  36423. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36424. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36425. NID_subject_alt_name, NULL, NULL));
  36426. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36427. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36428. XMEMSET(outbuf,0,sizeof(outbuf));
  36429. BIO_read(out, outbuf, sizeof(outbuf));
  36430. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36431. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36432. X509_free(x509);
  36433. /* test for GEN_URI */
  36434. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  36435. AssertTrue((f != XBADFILE));
  36436. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36437. XFCLOSE(f);
  36438. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36439. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36440. AssertNotNull(aia);
  36441. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  36442. gn = ad->location;
  36443. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36444. XMEMSET(outbuf,0,sizeof(outbuf));
  36445. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36446. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36447. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36448. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36449. AssertNotNull(aia);
  36450. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36451. X509_free(x509);
  36452. /* test for GEN_IPADD */
  36453. /* ip v4 address */
  36454. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36455. gn->type = GEN_IPADD;
  36456. gn->d.iPAddress->length = sizeof(v4Addr);
  36457. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36458. sizeof(v4Addr)), 1);
  36459. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36460. XMEMSET(outbuf,0,sizeof(outbuf));
  36461. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36462. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36463. GENERAL_NAME_free(gn);
  36464. /* ip v6 address */
  36465. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36466. gn->type = GEN_IPADD;
  36467. gn->d.iPAddress->length = sizeof(v6Addr);
  36468. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36469. sizeof(v6Addr)), 1);
  36470. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36471. XMEMSET(outbuf,0,sizeof(outbuf));
  36472. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36473. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36474. GENERAL_NAME_free(gn);
  36475. /* test for GEN_EMAIL */
  36476. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36477. gn->type = GEN_EMAIL;
  36478. gn->d.rfc822Name->length = sizeof(email);
  36479. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  36480. sizeof(email)), 1);
  36481. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36482. XMEMSET(outbuf,0,sizeof(outbuf));
  36483. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36484. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36485. GENERAL_NAME_free(gn);
  36486. /* test for GEN_OTHERNAME */
  36487. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36488. gn->type = GEN_OTHERNAME;
  36489. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36490. XMEMSET(outbuf,0,sizeof(outbuf));
  36491. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36492. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36493. GENERAL_NAME_free(gn);
  36494. /* test for GEN_X400 */
  36495. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36496. gn->type = GEN_X400;
  36497. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36498. XMEMSET(outbuf,0,sizeof(outbuf));
  36499. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36500. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36501. GENERAL_NAME_free(gn);
  36502. /* test for GEN_EDIPARTY */
  36503. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36504. gn->type = GEN_EDIPARTY;
  36505. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36506. XMEMSET(outbuf,0,sizeof(outbuf));
  36507. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36508. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36509. GENERAL_NAME_free(gn);
  36510. BIO_free(out);
  36511. res = TEST_RES_CHECK(1);
  36512. #endif /* OPENSSL_ALL */
  36513. return res;
  36514. }
  36515. static int test_wolfSSL_sk_DIST_POINT(void)
  36516. {
  36517. int res = TEST_SKIPPED;
  36518. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36519. !defined(NO_RSA)
  36520. X509* x509;
  36521. unsigned char buf[4096];
  36522. const unsigned char* bufPt;
  36523. int bytes, i, j;
  36524. XFILE f;
  36525. DIST_POINT* dp;
  36526. DIST_POINT_NAME* dpn;
  36527. GENERAL_NAME* gn;
  36528. ASN1_IA5STRING* uri;
  36529. STACK_OF(DIST_POINT)* dps;
  36530. STACK_OF(GENERAL_NAME)* gns;
  36531. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36532. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  36533. AssertTrue((f != XBADFILE));
  36534. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36535. XFCLOSE(f);
  36536. bufPt = buf;
  36537. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36538. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36539. NID_crl_distribution_points, NULL, NULL));
  36540. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  36541. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36542. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  36543. AssertNotNull(dpn = dp->distpoint);
  36544. /* this should be type 0, fullname */
  36545. AssertIntEQ(dpn->type, 0);
  36546. gns = dp->distpoint->name.fullname;
  36547. AssertNotNull(gns);
  36548. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36549. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36550. gn = sk_GENERAL_NAME_value(gns, j);
  36551. AssertIntEQ(gn->type, GEN_URI);
  36552. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  36553. AssertNotNull(uri->data);
  36554. AssertIntGT(uri->length, 0);
  36555. }
  36556. }
  36557. X509_free(x509);
  36558. CRL_DIST_POINTS_free(dps);
  36559. res = TEST_RES_CHECK(1);
  36560. #endif
  36561. return res;
  36562. }
  36563. static int test_wolfSSL_MD4(void)
  36564. {
  36565. int res = TEST_SKIPPED;
  36566. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36567. MD4_CTX md4;
  36568. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36569. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36570. "789012345678901234567890";
  36571. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36572. "\xcc\x05\x36";
  36573. int msgSz = (int)XSTRLEN(msg);
  36574. XMEMSET(out, 0, sizeof(out));
  36575. MD4_Init(&md4);
  36576. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36577. MD4_Final(out, &md4);
  36578. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36579. res = TEST_RES_CHECK(1);
  36580. #endif
  36581. return res;
  36582. }
  36583. static int test_wolfSSL_verify_mode(void)
  36584. {
  36585. int res = TEST_SKIPPED;
  36586. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36587. WOLFSSL* ssl;
  36588. WOLFSSL_CTX* ctx;
  36589. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36590. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36591. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36592. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36593. AssertNotNull(ssl = SSL_new(ctx));
  36594. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36595. SSL_free(ssl);
  36596. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36597. AssertNotNull(ssl = SSL_new(ctx));
  36598. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36599. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36600. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36601. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36602. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36603. SSL_free(ssl);
  36604. wolfSSL_CTX_set_verify(ctx,
  36605. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36606. AssertNotNull(ssl = SSL_new(ctx));
  36607. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36608. AssertIntEQ(SSL_get_verify_mode(ssl),
  36609. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36610. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36611. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36612. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36613. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36614. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36615. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36616. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36617. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36618. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36619. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  36620. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  36621. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  36622. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  36623. #endif
  36624. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36625. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36626. SSL_free(ssl);
  36627. SSL_CTX_free(ctx);
  36628. res = TEST_RES_CHECK(1);
  36629. #endif
  36630. return res;
  36631. }
  36632. static int test_wolfSSL_verify_depth(void)
  36633. {
  36634. int res = TEST_SKIPPED;
  36635. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36636. WOLFSSL* ssl;
  36637. WOLFSSL_CTX* ctx;
  36638. long depth;
  36639. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36640. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36641. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36642. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36643. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  36644. AssertNotNull(ssl = SSL_new(ctx));
  36645. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  36646. SSL_free(ssl);
  36647. SSL_CTX_set_verify_depth(ctx, -1);
  36648. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  36649. SSL_CTX_set_verify_depth(ctx, 2);
  36650. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  36651. AssertNotNull(ssl = SSL_new(ctx));
  36652. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  36653. SSL_free(ssl);
  36654. SSL_CTX_free(ctx);
  36655. res = TEST_RES_CHECK(1);
  36656. #endif
  36657. return res;
  36658. }
  36659. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36660. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36661. * buffer of 64 bytes.
  36662. *
  36663. * returns the size of the digest buffer on success and a negative value on
  36664. * failure.
  36665. */
  36666. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  36667. {
  36668. HMAC_CTX ctx1;
  36669. HMAC_CTX ctx2;
  36670. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36671. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36672. unsigned char long_key[] =
  36673. "0123456789012345678901234567890123456789"
  36674. "0123456789012345678901234567890123456789"
  36675. "0123456789012345678901234567890123456789"
  36676. "0123456789012345678901234567890123456789";
  36677. unsigned char msg[] = "message to hash";
  36678. unsigned int digestSz = 64;
  36679. int keySz = sizeof(key);
  36680. int long_keySz = sizeof(long_key);
  36681. int msgSz = sizeof(msg);
  36682. unsigned char digest2[64];
  36683. unsigned int digestSz2 = 64;
  36684. HMAC_CTX_init(&ctx1);
  36685. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36686. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36687. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36688. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36689. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36690. HMAC_CTX_cleanup(&ctx1);
  36691. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36692. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36693. HMAC_CTX_cleanup(&ctx2);
  36694. AssertIntEQ(digestSz, digestSz2);
  36695. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36696. /* test HMAC_Init with NULL key */
  36697. /* init after copy */
  36698. HMAC_CTX_init(&ctx1);
  36699. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36700. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36701. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36702. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36703. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36704. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36705. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36706. HMAC_CTX_cleanup(&ctx1);
  36707. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36708. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36709. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36710. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36711. HMAC_CTX_cleanup(&ctx2);
  36712. AssertIntEQ(digestSz, digestSz2);
  36713. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36714. /* long key */
  36715. HMAC_CTX_init(&ctx1);
  36716. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  36717. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36718. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36719. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36720. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36721. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36722. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36723. HMAC_CTX_cleanup(&ctx1);
  36724. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36725. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36726. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36727. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36728. HMAC_CTX_cleanup(&ctx2);
  36729. AssertIntEQ(digestSz, digestSz2);
  36730. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36731. /* init before copy */
  36732. HMAC_CTX_init(&ctx1);
  36733. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36734. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36735. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36736. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36737. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36738. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36739. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36740. HMAC_CTX_cleanup(&ctx1);
  36741. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36742. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36743. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36744. HMAC_CTX_cleanup(&ctx2);
  36745. AssertIntEQ(digestSz, digestSz2);
  36746. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36747. return digestSz;
  36748. }
  36749. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36750. static int test_wolfSSL_HMAC_CTX(void)
  36751. {
  36752. int res = TEST_SKIPPED;
  36753. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36754. unsigned char digest[64];
  36755. int digestSz;
  36756. #ifndef NO_SHA
  36757. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  36758. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36759. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36760. #endif /* !NO_SHA */
  36761. #ifdef WOLFSSL_SHA224
  36762. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  36763. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36764. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36765. "\x02\x0E", digest, digestSz), 0);
  36766. #endif /* WOLFSSL_SHA224 */
  36767. #ifndef NO_SHA256
  36768. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  36769. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36770. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36771. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36772. #endif /* !NO_SHA256 */
  36773. #ifdef WOLFSSL_SHA384
  36774. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  36775. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36776. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36777. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36778. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36779. digest, digestSz), 0);
  36780. #endif /* WOLFSSL_SHA384 */
  36781. #ifdef WOLFSSL_SHA512
  36782. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  36783. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36784. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36785. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36786. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36787. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36788. digest, digestSz), 0);
  36789. #endif /* WOLFSSL_SHA512 */
  36790. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  36791. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  36792. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36793. "\xE4\x98\xDD", digest, digestSz), 0);
  36794. #endif /* !NO_MD5 */
  36795. res = TEST_RES_CHECK(1);
  36796. #endif
  36797. return res;
  36798. }
  36799. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36800. static void sslMsgCb(int w, int version, int type, const void* buf,
  36801. size_t sz, SSL* ssl, void* arg)
  36802. {
  36803. int i;
  36804. unsigned char* pt = (unsigned char*)buf;
  36805. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  36806. (w)?"Writing":"Reading", (int)sz, version, type);
  36807. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  36808. fprintf(stderr, "\n");
  36809. (void)ssl;
  36810. (void)arg;
  36811. }
  36812. #endif /* OPENSSL_EXTRA */
  36813. static int test_wolfSSL_msg_callback(void)
  36814. {
  36815. int res = TEST_SKIPPED;
  36816. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36817. WOLFSSL* ssl;
  36818. WOLFSSL_CTX* ctx;
  36819. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36820. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  36821. SSL_FILETYPE_PEM));
  36822. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  36823. SSL_FILETYPE_PEM));
  36824. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  36825. SSL_SUCCESS);
  36826. AssertNotNull(ssl = SSL_new(ctx));
  36827. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  36828. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  36829. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  36830. SSL_free(ssl);
  36831. SSL_CTX_free(ctx);
  36832. res = TEST_RES_CHECK(1);
  36833. #endif
  36834. return res;
  36835. }
  36836. static int test_wolfSSL_SHA(void)
  36837. {
  36838. int res = TEST_SKIPPED;
  36839. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36840. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36841. (!defined(HAVE_FIPS) || \
  36842. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36843. {
  36844. const unsigned char in[] = "abc";
  36845. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36846. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36847. unsigned char out[WC_SHA_DIGEST_SIZE];
  36848. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36849. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36850. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36851. /* SHA interface test */
  36852. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36853. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  36854. AssertNotNull(SHA(in, 0, out));
  36855. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36856. AssertNotNull(SHA(NULL, 0, out));
  36857. AssertNotNull(SHA(NULL, 0, NULL));
  36858. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  36859. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36860. }
  36861. #endif
  36862. #if !defined(NO_SHA256)
  36863. {
  36864. const unsigned char in[] = "abc";
  36865. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36866. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36867. "\x15\xAD";
  36868. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36869. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36870. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36871. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36872. #else
  36873. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36874. #endif
  36875. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36876. }
  36877. #endif
  36878. #if defined(WOLFSSL_SHA384)
  36879. {
  36880. const unsigned char in[] = "abc";
  36881. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  36882. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  36883. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  36884. "\xc8\x25\xa7";
  36885. unsigned char out[WC_SHA384_DIGEST_SIZE];
  36886. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  36887. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36888. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  36889. #else
  36890. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  36891. #endif
  36892. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  36893. }
  36894. #endif
  36895. #if defined(WOLFSSL_SHA512)
  36896. {
  36897. const unsigned char in[] = "abc";
  36898. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  36899. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  36900. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  36901. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  36902. "\xa5\x4c\xa4\x9f";
  36903. unsigned char out[WC_SHA512_DIGEST_SIZE];
  36904. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  36905. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36906. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  36907. #else
  36908. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  36909. #endif
  36910. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  36911. }
  36912. #endif
  36913. res = TEST_RES_CHECK(1);
  36914. #endif
  36915. return res;
  36916. }
  36917. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  36918. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36919. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36920. static void binary_dump(void *ptr, int size)
  36921. {
  36922. #ifdef WOLFSSL_EVP_PRINT
  36923. int i = 0;
  36924. unsigned char *p = (unsigned char *) ptr;
  36925. fprintf(stderr, "{");
  36926. while ((p != NULL) && (i < size)) {
  36927. if ((i % 8) == 0) {
  36928. fprintf(stderr, "\n");
  36929. fprintf(stderr, " ");
  36930. }
  36931. fprintf(stderr, "0x%02x, ", p[i]);
  36932. i++;
  36933. }
  36934. fprintf(stderr, "\n};\n");
  36935. #else
  36936. (void) ptr;
  36937. (void) size;
  36938. #endif
  36939. }
  36940. static int last_val = 0x0f;
  36941. static int check_result(unsigned char *data, int len)
  36942. {
  36943. int i;
  36944. for ( ; len; ) {
  36945. last_val = (last_val + 1) % 16;
  36946. for (i = 0; i < 16; len--, i++, data++)
  36947. if (*data != last_val) {
  36948. return -1;
  36949. }
  36950. }
  36951. return 0;
  36952. }
  36953. static int r_offset;
  36954. static int w_offset;
  36955. static void init_offset(void)
  36956. {
  36957. r_offset = 0;
  36958. w_offset = 0;
  36959. }
  36960. static void get_record(unsigned char *data, unsigned char *buf, int len)
  36961. {
  36962. XMEMCPY(buf, data+r_offset, len);
  36963. r_offset += len;
  36964. }
  36965. static void set_record(unsigned char *data, unsigned char *buf, int len)
  36966. {
  36967. XMEMCPY(data+w_offset, buf, len);
  36968. w_offset += len;
  36969. }
  36970. static void set_plain(unsigned char *plain, int rec)
  36971. {
  36972. int i, j;
  36973. unsigned char *p = plain;
  36974. #define BLOCKSZ 16
  36975. for (i=0; i<(rec/BLOCKSZ); i++) {
  36976. for (j=0; j<BLOCKSZ; j++)
  36977. *p++ = (i % 16);
  36978. }
  36979. }
  36980. #endif
  36981. static int test_wolfSSL_EVP_Cipher_extra(void)
  36982. {
  36983. int res = TEST_SKIPPED;
  36984. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36985. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36986. /* aes128-cbc, keylen=16, ivlen=16 */
  36987. byte aes128_cbc_key[] = {
  36988. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36989. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36990. };
  36991. byte aes128_cbc_iv[] = {
  36992. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  36993. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  36994. };
  36995. /* teset data size table */
  36996. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  36997. int test_drive2[] = {8, 3, 8, 512, 0};
  36998. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  36999. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  37000. int test_drive_len[100];
  37001. int ret = 0;
  37002. EVP_CIPHER_CTX *evp = NULL;
  37003. int ilen = 0;
  37004. int klen = 0;
  37005. int i, j;
  37006. const EVP_CIPHER *type;
  37007. byte *iv;
  37008. byte *key;
  37009. int ivlen;
  37010. int keylen;
  37011. #define RECORDS 16
  37012. #define BUFFSZ 512
  37013. byte plain [BUFFSZ * RECORDS];
  37014. byte cipher[BUFFSZ * RECORDS];
  37015. byte inb[BUFFSZ];
  37016. byte outb[BUFFSZ+16];
  37017. int outl, inl;
  37018. iv = aes128_cbc_iv;
  37019. ivlen = sizeof(aes128_cbc_iv);
  37020. key = aes128_cbc_key;
  37021. keylen = sizeof(aes128_cbc_key);
  37022. type = EVP_aes_128_cbc();
  37023. set_plain(plain, BUFFSZ * RECORDS);
  37024. SSL_library_init();
  37025. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  37026. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37027. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37028. klen = EVP_CIPHER_CTX_key_length(evp);
  37029. if (klen > 0 && keylen != klen) {
  37030. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37031. }
  37032. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37033. if (ilen > 0 && ivlen != ilen) {
  37034. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37035. }
  37036. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37037. for (j = 0; j<RECORDS; j++)
  37038. {
  37039. inl = BUFFSZ;
  37040. get_record(plain, inb, inl);
  37041. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  37042. set_record(cipher, outb, outl);
  37043. }
  37044. for (i = 0; test_drive[i]; i++) {
  37045. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37046. init_offset();
  37047. test_drive_len[i] = 0;
  37048. for (j = 0; test_drive[i][j]; j++)
  37049. {
  37050. inl = test_drive[i][j];
  37051. test_drive_len[i] += inl;
  37052. get_record(plain, inb, inl);
  37053. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37054. /* output to cipher buffer, so that following Dec test can detect
  37055. if any error */
  37056. set_record(cipher, outb, outl);
  37057. }
  37058. EVP_CipherFinal(evp, outb, &outl);
  37059. if (outl > 0)
  37060. set_record(cipher, outb, outl);
  37061. }
  37062. for (i = 0; test_drive[i]; i++) {
  37063. last_val = 0x0f;
  37064. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  37065. init_offset();
  37066. for (j = 0; test_drive[i][j]; j++) {
  37067. inl = test_drive[i][j];
  37068. get_record(cipher, inb, inl);
  37069. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  37070. binary_dump(outb, outl);
  37071. AssertIntEQ((ret = check_result(outb, outl)), 0);
  37072. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  37073. }
  37074. ret = EVP_CipherFinal(evp, outb, &outl);
  37075. binary_dump(outb, outl);
  37076. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  37077. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  37078. AssertTrue(ret);
  37079. }
  37080. EVP_CIPHER_CTX_free(evp);
  37081. res = TEST_RES_CHECK(1);
  37082. #endif /* test_EVP_Cipher */
  37083. return res;
  37084. }
  37085. static int test_wolfSSL_PEM_read_DHparams(void)
  37086. {
  37087. int res = TEST_SKIPPED;
  37088. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  37089. !defined(NO_FILESYSTEM)
  37090. DH* dh;
  37091. XFILE fp;
  37092. unsigned char derOut[300];
  37093. unsigned char* derOutBuf = derOut;
  37094. int derOutSz = 0;
  37095. unsigned char derExpected[300];
  37096. int derExpectedSz = 0;
  37097. XMEMSET(derOut, 0, sizeof(derOut));
  37098. XMEMSET(derExpected, 0, sizeof(derExpected));
  37099. /* open DH param file, read into DH struct */
  37100. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  37101. /* bad args */
  37102. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  37103. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  37104. /* good args */
  37105. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37106. XFCLOSE(fp);
  37107. /* read in certs/dh2048.der for comparison against exported params */
  37108. fp = XFOPEN("./certs/dh2048.der", "rb");
  37109. AssertTrue(fp != XBADFILE);
  37110. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  37111. XFCLOSE(fp);
  37112. /* export DH back to DER and compare */
  37113. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  37114. AssertIntEQ(derOutSz, derExpectedSz);
  37115. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  37116. DH_free(dh);
  37117. dh = NULL;
  37118. /* Test parsing with X9.42 header */
  37119. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  37120. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37121. XFCLOSE(fp);
  37122. DH_free(dh);
  37123. res = TEST_RES_CHECK(1);
  37124. #endif
  37125. return res;
  37126. }
  37127. static int test_wolfSSL_AES_ecb_encrypt(void)
  37128. {
  37129. int res = TEST_SKIPPED;
  37130. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37131. AES_KEY aes;
  37132. const byte msg[] =
  37133. {
  37134. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37135. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37136. };
  37137. const byte verify[] =
  37138. {
  37139. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37140. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37141. };
  37142. const byte key[] =
  37143. {
  37144. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37145. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37146. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37147. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37148. };
  37149. byte out[AES_BLOCK_SIZE];
  37150. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37151. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37152. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37153. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37154. #ifdef HAVE_AES_DECRYPT
  37155. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37156. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37157. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37158. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37159. #endif
  37160. /* test bad arguments */
  37161. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37162. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37163. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37164. res = TEST_RES_CHECK(1);
  37165. #endif
  37166. return res;
  37167. }
  37168. static int test_wolfSSL_MD5(void)
  37169. {
  37170. int res = TEST_SKIPPED;
  37171. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37172. byte input1[] = "";
  37173. byte input2[] = "message digest";
  37174. byte hash[WC_MD5_DIGEST_SIZE];
  37175. unsigned char output1[] =
  37176. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37177. unsigned char output2[] =
  37178. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37179. WOLFSSL_MD5_CTX md5;
  37180. XMEMSET(&md5, 0, sizeof(md5));
  37181. /* Test cases for illegal parameters */
  37182. AssertIntEQ(MD5_Init(NULL), 0);
  37183. AssertIntEQ(MD5_Init(&md5), 1);
  37184. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  37185. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37186. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37187. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  37188. AssertIntEQ(MD5_Final(hash, NULL), 0);
  37189. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  37190. /* Init MD5 CTX */
  37191. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37192. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  37193. XSTRLEN((const char*)&input1)), 1);
  37194. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37195. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37196. /* Init MD5 CTX */
  37197. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37198. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37199. (int)XSTRLEN((const char*)input2)), 1);
  37200. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37201. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37202. #if !defined(NO_OLD_NAMES) && \
  37203. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37204. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37205. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37206. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  37207. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  37208. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  37209. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37210. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  37211. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37212. {
  37213. byte data[] = "Data to be hashed.";
  37214. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37215. AssertNotNull(MD5(data, sizeof(data), NULL));
  37216. AssertNotNull(MD5(data, sizeof(data), hash));
  37217. AssertNotNull(MD5(NULL, 0, hash));
  37218. AssertNull(MD5(NULL, sizeof(data), hash));
  37219. }
  37220. #endif
  37221. res = TEST_RES_CHECK(1);
  37222. #endif
  37223. return res;
  37224. }
  37225. static int test_wolfSSL_MD5_Transform(void)
  37226. {
  37227. int res = TEST_SKIPPED;
  37228. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37229. byte input1[] = "";
  37230. byte input2[] = "abc";
  37231. byte local[WC_MD5_BLOCK_SIZE];
  37232. word32 sLen = 0;
  37233. #ifdef BIG_ENDIAN_ORDER
  37234. unsigned char output1[] =
  37235. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37236. unsigned char output2[] =
  37237. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37238. #else
  37239. unsigned char output1[] =
  37240. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37241. unsigned char output2[] =
  37242. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37243. #endif
  37244. union {
  37245. wc_Md5 native;
  37246. MD5_CTX compat;
  37247. } md5;
  37248. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37249. XMEMSET(&local, 0, sizeof(local));
  37250. /* sanity check */
  37251. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  37252. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37253. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37254. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37255. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37256. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37257. /* Init MD5 CTX */
  37258. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37259. /* Do Transform*/
  37260. sLen = (word32)XSTRLEN((char*)input1);
  37261. XMEMCPY(local, input1, sLen);
  37262. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37263. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  37264. WC_MD5_DIGEST_SIZE), 0);
  37265. /* Init MD5 CTX */
  37266. AssertIntEQ(MD5_Init(&md5.compat), 1);
  37267. sLen = (word32)XSTRLEN((char*)input2);
  37268. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37269. XMEMCPY(local, input2, sLen);
  37270. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37271. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  37272. WC_MD5_DIGEST_SIZE), 0);
  37273. res = TEST_RES_CHECK(1);
  37274. #endif
  37275. return res;
  37276. }
  37277. static int test_wolfSSL_SHA224(void)
  37278. {
  37279. int res = TEST_SKIPPED;
  37280. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37281. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37282. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37283. unsigned char input[] =
  37284. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37285. unsigned char output[] =
  37286. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37287. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37288. size_t inLen;
  37289. byte hash[WC_SHA224_DIGEST_SIZE];
  37290. inLen = XSTRLEN((char*)input);
  37291. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37292. AssertNull(SHA224(NULL, inLen, hash));
  37293. AssertNotNull(SHA224(input, 0, hash));
  37294. AssertNotNull(SHA224(input, inLen, NULL));
  37295. AssertNotNull(SHA224(NULL, 0, hash));
  37296. AssertNotNull(SHA224(NULL, 0, NULL));
  37297. AssertNotNull(SHA224(input, inLen, hash));
  37298. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37299. res = TEST_RES_CHECK(1);
  37300. #endif
  37301. return res;
  37302. }
  37303. static int test_wolfSSL_SHA_Transform(void)
  37304. {
  37305. int res = TEST_SKIPPED;
  37306. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37307. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37308. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37309. byte input1[] = "";
  37310. byte input2[] = "abc";
  37311. byte local[WC_SHA_BLOCK_SIZE];
  37312. word32 sLen = 0;
  37313. #ifdef BIG_ENDIAN_ORDER
  37314. unsigned char output1[] =
  37315. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37316. "\x09\xf7\x3a\x93";
  37317. unsigned char output2[] =
  37318. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37319. "\x7c\x6e\x08\xb8";
  37320. #else
  37321. unsigned char output1[] =
  37322. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37323. "\x93\x3a\xf7\x09";
  37324. unsigned char output2[] =
  37325. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37326. "\xb8\x08\x6e\x7c";
  37327. #endif
  37328. union {
  37329. wc_Sha native;
  37330. SHA_CTX compat;
  37331. } sha;
  37332. union {
  37333. wc_Sha native;
  37334. SHA_CTX compat;
  37335. } sha1;
  37336. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37337. XMEMSET(&local, 0, sizeof(local));
  37338. /* sanity check */
  37339. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  37340. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37341. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37342. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  37343. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37344. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37345. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37346. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37347. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37348. /* Init SHA CTX */
  37349. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37350. /* Do Transform*/
  37351. sLen = (word32)XSTRLEN((char*)input1);
  37352. XMEMCPY(local, input1, sLen);
  37353. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37354. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  37355. WC_SHA_DIGEST_SIZE), 0);
  37356. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37357. /* Init SHA CTX */
  37358. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37359. sLen = (word32)XSTRLEN((char*)input2);
  37360. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37361. XMEMCPY(local, input2, sLen);
  37362. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37363. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  37364. WC_SHA_DIGEST_SIZE), 0);
  37365. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37366. /* SHA1 */
  37367. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37368. /* Init SHA CTX */
  37369. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37370. /* Do Transform*/
  37371. sLen = (word32)XSTRLEN((char*)input1);
  37372. XMEMCPY(local, input1, sLen);
  37373. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37374. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  37375. WC_SHA_DIGEST_SIZE), 0);
  37376. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37377. /* Init SHA CTX */
  37378. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37379. sLen = (word32)XSTRLEN((char*)input2);
  37380. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37381. XMEMCPY(local, input2, sLen);
  37382. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37383. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  37384. WC_SHA_DIGEST_SIZE), 0);
  37385. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37386. res = TEST_RES_CHECK(1);
  37387. #endif
  37388. #endif
  37389. return res;
  37390. }
  37391. static int test_wolfSSL_SHA256_Transform(void)
  37392. {
  37393. int res = TEST_SKIPPED;
  37394. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37395. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37396. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37397. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  37398. byte input1[] = "";
  37399. byte input2[] = "abc";
  37400. byte local[WC_SHA256_BLOCK_SIZE];
  37401. word32 sLen = 0;
  37402. #ifdef BIG_ENDIAN_ORDER
  37403. unsigned char output1[] =
  37404. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37405. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37406. unsigned char output2[] =
  37407. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37408. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37409. #else
  37410. unsigned char output1[] =
  37411. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37412. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37413. unsigned char output2[] =
  37414. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37415. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37416. #endif
  37417. union {
  37418. wc_Sha256 native;
  37419. SHA256_CTX compat;
  37420. } sha256;
  37421. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37422. XMEMSET(&local, 0, sizeof(local));
  37423. /* sanity check */
  37424. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  37425. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37426. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37427. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37428. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37429. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37430. /* Init SHA256 CTX */
  37431. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37432. /* Do Transform*/
  37433. sLen = (word32)XSTRLEN((char*)input1);
  37434. XMEMCPY(local, input1, sLen);
  37435. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37436. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  37437. WC_SHA256_DIGEST_SIZE), 0);
  37438. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37439. /* Init SHA256 CTX */
  37440. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37441. sLen = (word32)XSTRLEN((char*)input2);
  37442. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37443. XMEMCPY(local, input2, sLen);
  37444. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37445. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  37446. WC_SHA256_DIGEST_SIZE), 0);
  37447. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37448. res = TEST_RES_CHECK(1);
  37449. #endif
  37450. #endif
  37451. return res;
  37452. }
  37453. static int test_wolfSSL_SHA256(void)
  37454. {
  37455. int res = TEST_SKIPPED;
  37456. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37457. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37458. unsigned char input[] =
  37459. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37460. unsigned char output[] =
  37461. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37462. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37463. "\x06\xC1";
  37464. size_t inLen;
  37465. byte hash[WC_SHA256_DIGEST_SIZE];
  37466. inLen = XSTRLEN((char*)input);
  37467. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37468. AssertNotNull(SHA256(input, inLen, hash));
  37469. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37470. res = TEST_RES_CHECK(1);
  37471. #endif
  37472. return res;
  37473. }
  37474. static int test_wolfSSL_SHA512_Transform(void)
  37475. {
  37476. int res = TEST_SKIPPED;
  37477. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37478. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37479. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37480. byte input1[] = "";
  37481. byte input2[] = "abc";
  37482. byte local[WC_SHA512_BLOCK_SIZE];
  37483. word32 sLen = 0;
  37484. #ifdef BIG_ENDIAN_ORDER
  37485. unsigned char output1[] =
  37486. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37487. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37488. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37489. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37490. unsigned char output2[] =
  37491. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37492. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37493. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37494. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37495. #else
  37496. unsigned char output1[] =
  37497. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37498. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37499. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37500. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37501. unsigned char output2[] =
  37502. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37503. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37504. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37505. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37506. #endif
  37507. union {
  37508. wc_Sha512 native;
  37509. SHA512_CTX compat;
  37510. } sha512;
  37511. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37512. XMEMSET(&local, 0, sizeof(local));
  37513. /* sanity check */
  37514. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  37515. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37516. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37517. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37518. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37519. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37520. /* Init SHA512 CTX */
  37521. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37522. /* Do Transform*/
  37523. sLen = (word32)XSTRLEN((char*)input1);
  37524. XMEMCPY(local, input1, sLen);
  37525. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37526. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  37527. WC_SHA512_DIGEST_SIZE), 0);
  37528. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37529. /* Init SHA512 CTX */
  37530. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  37531. sLen = (word32)XSTRLEN((char*)input2);
  37532. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37533. XMEMCPY(local, input2, sLen);
  37534. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37535. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  37536. WC_SHA512_DIGEST_SIZE), 0);
  37537. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37538. (void)input1;
  37539. res = TEST_RES_CHECK(1);
  37540. #endif
  37541. #endif
  37542. return res;
  37543. }
  37544. static int test_wolfSSL_X509_get_serialNumber(void)
  37545. {
  37546. int res = TEST_SKIPPED;
  37547. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37548. ASN1_INTEGER* a;
  37549. BIGNUM* bn;
  37550. X509* x509;
  37551. char *serialHex;
  37552. byte serial[3];
  37553. int serialSz;
  37554. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37555. SSL_FILETYPE_PEM));
  37556. AssertNotNull(a = X509_get_serialNumber(x509));
  37557. /* check on value of ASN1 Integer */
  37558. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37559. /* test setting serial number and then retrieving it */
  37560. AssertNotNull(a = ASN1_INTEGER_new());
  37561. ASN1_INTEGER_set(a, 3);
  37562. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37563. serialSz = sizeof(serial);
  37564. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37565. WOLFSSL_SUCCESS);
  37566. AssertIntEQ(serialSz, 1);
  37567. AssertIntEQ(serial[0], 3);
  37568. ASN1_INTEGER_free(a);
  37569. /* test setting serial number with 0's in it */
  37570. serial[0] = 0x01;
  37571. serial[1] = 0x00;
  37572. serial[2] = 0x02;
  37573. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37574. a->data[0] = ASN_INTEGER;
  37575. a->data[1] = sizeof(serial);
  37576. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37577. a->length = sizeof(serial) + 2;
  37578. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37579. XMEMSET(serial, 0, sizeof(serial));
  37580. serialSz = sizeof(serial);
  37581. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37582. WOLFSSL_SUCCESS);
  37583. AssertIntEQ(serialSz, 3);
  37584. AssertIntEQ(serial[0], 0x01);
  37585. AssertIntEQ(serial[1], 0x00);
  37586. AssertIntEQ(serial[2], 0x02);
  37587. ASN1_INTEGER_free(a);
  37588. X509_free(x509); /* free's a */
  37589. AssertNotNull(serialHex = BN_bn2hex(bn));
  37590. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37591. AssertStrEQ(serialHex, "01");
  37592. #else
  37593. AssertStrEQ(serialHex, "1");
  37594. #endif
  37595. OPENSSL_free(serialHex);
  37596. AssertIntEQ(BN_get_word(bn), 1);
  37597. BN_free(bn);
  37598. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  37599. a = ASN1_INTEGER_new();
  37600. if (a) {
  37601. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  37602. DYNAMIC_TYPE_OPENSSL));
  37603. a->isDynamic = 1;
  37604. ASN1_INTEGER_free(a);
  37605. }
  37606. res = TEST_RES_CHECK(1);
  37607. #endif
  37608. return res;
  37609. }
  37610. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  37611. {
  37612. int res = TEST_SKIPPED;
  37613. #if defined(OPENSSL_EXTRA)
  37614. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  37615. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  37616. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  37617. res = TEST_RES_CHECK(1);
  37618. #endif
  37619. return res;
  37620. }
  37621. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  37622. {
  37623. int res = TEST_SKIPPED;
  37624. #if defined(OPENSSL_EXTRA)
  37625. #define MAX_HEXSTR_BUFSZ 9
  37626. #define NUM_CASES 5
  37627. struct Output {
  37628. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  37629. long ret;
  37630. };
  37631. int i;
  37632. int j;
  37633. const char* inputs[NUM_CASES] = {
  37634. "aabcd1357e",
  37635. "01:12:23:34:a5:b6:c7:d8:e9",
  37636. ":01:02",
  37637. "012",
  37638. ":ab:ac:d"
  37639. };
  37640. struct Output expectedOutputs[NUM_CASES] = {
  37641. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  37642. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  37643. {{0x01, 0x02}, 2},
  37644. {{0x00}, 0},
  37645. {{0x00}, 0}
  37646. };
  37647. long len = 0;
  37648. unsigned char* returnedBuf = NULL;
  37649. for (i = 0; i < NUM_CASES; ++i) {
  37650. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  37651. if (returnedBuf == NULL) {
  37652. AssertIntEQ(expectedOutputs[i].ret, 0);
  37653. continue;
  37654. }
  37655. AssertIntEQ(expectedOutputs[i].ret, len);
  37656. for (j = 0; j < len; ++j) {
  37657. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  37658. }
  37659. OPENSSL_free(returnedBuf);
  37660. }
  37661. res = TEST_RES_CHECK(1);
  37662. #endif
  37663. return res;
  37664. }
  37665. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  37666. {
  37667. int res = TEST_SKIPPED;
  37668. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  37669. #ifndef NO_BIO
  37670. ASN1_STRING* asn_str;
  37671. const char data[] = "Hello wolfSSL!";
  37672. ASN1_STRING* esc_str;
  37673. const char esc_data[] = "a+;<>";
  37674. BIO *bio;
  37675. unsigned long flags;
  37676. int p_len;
  37677. unsigned char rbuf[255];
  37678. /* setup */
  37679. XMEMSET(rbuf, 0, 255);
  37680. bio = BIO_new(BIO_s_mem());
  37681. BIO_set_write_buf_size(bio,255);
  37682. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37683. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  37684. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37685. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  37686. /* no flags */
  37687. XMEMSET(rbuf, 0, 255);
  37688. flags = 0;
  37689. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37690. AssertIntEQ(p_len, 15);
  37691. BIO_read(bio, (void*)rbuf, 15);
  37692. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  37693. /* RFC2253 Escape */
  37694. XMEMSET(rbuf, 0, 255);
  37695. flags = ASN1_STRFLGS_ESC_2253;
  37696. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  37697. AssertIntEQ(p_len, 9);
  37698. BIO_read(bio, (void*)rbuf, 9);
  37699. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  37700. /* Show type */
  37701. XMEMSET(rbuf, 0, 255);
  37702. flags = ASN1_STRFLGS_SHOW_TYPE;
  37703. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37704. AssertIntEQ(p_len, 28);
  37705. BIO_read(bio, (void*)rbuf, 28);
  37706. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  37707. /* Dump All */
  37708. XMEMSET(rbuf, 0, 255);
  37709. flags = ASN1_STRFLGS_DUMP_ALL;
  37710. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37711. AssertIntEQ(p_len, 31);
  37712. BIO_read(bio, (void*)rbuf, 31);
  37713. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  37714. /* Dump Der */
  37715. XMEMSET(rbuf, 0, 255);
  37716. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  37717. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37718. AssertIntEQ(p_len, 35);
  37719. BIO_read(bio, (void*)rbuf, 35);
  37720. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  37721. /* Dump All + Show type */
  37722. XMEMSET(rbuf, 0, 255);
  37723. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  37724. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37725. AssertIntEQ(p_len, 44);
  37726. BIO_read(bio, (void*)rbuf, 44);
  37727. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  37728. BIO_free(bio);
  37729. ASN1_STRING_free(asn_str);
  37730. ASN1_STRING_free(esc_str);
  37731. res = TEST_RES_CHECK(1);
  37732. #endif /* !NO_BIO */
  37733. #endif
  37734. return res;
  37735. }
  37736. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  37737. {
  37738. int res = TEST_SKIPPED;
  37739. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  37740. WOLFSSL_ASN1_TIME *t;
  37741. WOLFSSL_ASN1_TIME *out;
  37742. WOLFSSL_ASN1_TIME *gtime;
  37743. int tlen = 0;
  37744. unsigned char *data;
  37745. /* UTC Time test */
  37746. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  37747. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  37748. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  37749. t->type = ASN_UTC_TIME;
  37750. t->length = ASN_UTC_TIME_SIZE;
  37751. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37752. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37753. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  37754. data = wolfSSL_ASN1_TIME_get_data(t);
  37755. AssertStrEQ((char*)data, "050727123456Z");
  37756. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37757. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37758. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37759. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37760. /* Generalized Time test */
  37761. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37762. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  37763. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  37764. t->type = ASN_GENERALIZED_TIME;
  37765. t->length = ASN_GENERALIZED_TIME_SIZE;
  37766. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  37767. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37768. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  37769. data = wolfSSL_ASN1_TIME_get_data(t);
  37770. AssertStrEQ((char*)data, "20050727123456Z");
  37771. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37772. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37773. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37774. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37775. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37776. /* Null parameter test */
  37777. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37778. gtime = NULL;
  37779. out = NULL;
  37780. t->type = ASN_UTC_TIME;
  37781. t->length = ASN_UTC_TIME_SIZE;
  37782. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37783. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  37784. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37785. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37786. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37787. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37788. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37789. res = TEST_RES_CHECK(1);
  37790. #endif
  37791. return res;
  37792. }
  37793. static int test_wolfSSL_X509_CA_num(void)
  37794. {
  37795. int res = TEST_SKIPPED;
  37796. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  37797. defined(HAVE_ECC) && !defined(NO_RSA)
  37798. WOLFSSL_X509_STORE *store;
  37799. WOLFSSL_X509 *x509_1, *x509_2;
  37800. int ca_num = 0;
  37801. store = wolfSSL_X509_STORE_new();
  37802. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37803. wolfSSL_X509_STORE_add_cert(store, x509_1);
  37804. ca_num = wolfSSL_X509_CA_num(store);
  37805. AssertIntEQ(ca_num, 1);
  37806. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  37807. wolfSSL_X509_STORE_add_cert(store, x509_2);
  37808. ca_num = wolfSSL_X509_CA_num(store);
  37809. AssertIntEQ(ca_num, 2);
  37810. wolfSSL_X509_free(x509_1);
  37811. wolfSSL_X509_free(x509_2);
  37812. wolfSSL_X509_STORE_free(store);
  37813. res = TEST_RES_CHECK(1);
  37814. #endif
  37815. return res;
  37816. }
  37817. static int test_wolfSSL_X509_check_ca(void)
  37818. {
  37819. int res = TEST_SKIPPED;
  37820. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37821. WOLFSSL_X509 *x509;
  37822. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37823. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  37824. wolfSSL_X509_free(x509);
  37825. res = TEST_RES_CHECK(1);
  37826. #endif
  37827. return res;
  37828. }
  37829. static int test_wolfSSL_X509_check_ip_asc(void)
  37830. {
  37831. int res = TEST_SKIPPED;
  37832. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37833. WOLFSSL_X509 *x509;
  37834. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  37835. #if 0
  37836. /* TODO: add cert gen for testing positive case */
  37837. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  37838. #endif
  37839. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  37840. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  37841. wolfSSL_X509_free(x509);
  37842. res = TEST_RES_CHECK(1);
  37843. #endif
  37844. return res;
  37845. }
  37846. static int test_wolfSSL_make_cert(void)
  37847. {
  37848. int res = TEST_SKIPPED;
  37849. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  37850. int ret;
  37851. Cert cert;
  37852. CertName name;
  37853. RsaKey key;
  37854. WC_RNG rng;
  37855. byte der[FOURK_BUF];
  37856. word32 idx;
  37857. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  37858. #ifdef OPENSSL_EXTRA
  37859. const unsigned char* pt;
  37860. int certSz;
  37861. X509* x509;
  37862. X509_NAME* x509name;
  37863. X509_NAME_ENTRY* entry;
  37864. ASN1_STRING* entryValue;
  37865. #endif
  37866. XMEMSET(&name, 0, sizeof(CertName));
  37867. /* set up cert name */
  37868. XMEMCPY(name.country, "US", sizeof("US"));
  37869. name.countryEnc = CTC_PRINTABLE;
  37870. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  37871. name.stateEnc = CTC_UTF8;
  37872. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  37873. name.localityEnc = CTC_UTF8;
  37874. XMEMCPY(name.sur, "Test", sizeof("Test"));
  37875. name.surEnc = CTC_UTF8;
  37876. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  37877. name.orgEnc = CTC_UTF8;
  37878. XMEMCPY(name.unit, "Development", sizeof("Development"));
  37879. name.unitEnc = CTC_UTF8;
  37880. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  37881. name.commonNameEnc = CTC_UTF8;
  37882. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  37883. name.serialDevEnc = CTC_PRINTABLE;
  37884. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  37885. name.userIdEnc = CTC_PRINTABLE;
  37886. #ifdef WOLFSSL_MULTI_ATTRIB
  37887. #if CTC_MAX_ATTRIB > 2
  37888. {
  37889. NameAttrib* n;
  37890. n = &name.name[0];
  37891. n->id = ASN_DOMAIN_COMPONENT;
  37892. n->type = CTC_UTF8;
  37893. n->sz = sizeof("com");
  37894. XMEMCPY(n->value, "com", sizeof("com"));
  37895. n = &name.name[1];
  37896. n->id = ASN_DOMAIN_COMPONENT;
  37897. n->type = CTC_UTF8;
  37898. n->sz = sizeof("wolfssl");
  37899. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  37900. }
  37901. #endif
  37902. #endif /* WOLFSSL_MULTI_ATTRIB */
  37903. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  37904. #ifndef HAVE_FIPS
  37905. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  37906. #else
  37907. AssertIntEQ(wc_InitRng(&rng), 0);
  37908. #endif
  37909. /* load test RSA key */
  37910. idx = 0;
  37911. #if defined(USE_CERT_BUFFERS_1024)
  37912. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  37913. sizeof_server_key_der_1024), 0);
  37914. #elif defined(USE_CERT_BUFFERS_2048)
  37915. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  37916. sizeof_server_key_der_2048), 0);
  37917. #else
  37918. /* error case, no RSA key loaded, happens later */
  37919. (void)idx;
  37920. #endif
  37921. XMEMSET(&cert, 0 , sizeof(Cert));
  37922. AssertIntEQ(wc_InitCert(&cert), 0);
  37923. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  37924. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  37925. cert.serialSz = (int)sizeof(mySerial);
  37926. cert.isCA = 1;
  37927. #ifndef NO_SHA256
  37928. cert.sigType = CTC_SHA256wRSA;
  37929. #else
  37930. cert.sigType = CTC_SHAwRSA;
  37931. #endif
  37932. /* add SKID from the Public Key */
  37933. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37934. /* add AKID from the Public Key */
  37935. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37936. ret = 0;
  37937. do {
  37938. #if defined(WOLFSSL_ASYNC_CRYPT)
  37939. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  37940. #endif
  37941. if (ret >= 0) {
  37942. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  37943. }
  37944. } while (ret == WC_PENDING_E);
  37945. AssertIntGT(ret, 0);
  37946. #ifdef OPENSSL_EXTRA
  37947. /* der holds a certificate with DC's now check X509 parsing of it */
  37948. certSz = ret;
  37949. pt = der;
  37950. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  37951. AssertNotNull(x509name = X509_get_subject_name(x509));
  37952. #ifdef WOLFSSL_MULTI_ATTRIB
  37953. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37954. -1)), 5);
  37955. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37956. idx)), 6);
  37957. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37958. idx)), -1);
  37959. #endif /* WOLFSSL_MULTI_ATTRIB */
  37960. /* compare DN at index 0 */
  37961. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  37962. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37963. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  37964. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  37965. #ifdef WOLFSSL_MULTI_ATTRIB
  37966. /* get first and second DC and compare result */
  37967. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37968. -1)), 5);
  37969. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37970. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37971. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  37972. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37973. idx)), 6);
  37974. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37975. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37976. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  37977. #endif /* WOLFSSL_MULTI_ATTRIB */
  37978. /* try invalid index locations for regression test and sanity check */
  37979. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  37980. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  37981. X509_free(x509);
  37982. #endif /* OPENSSL_EXTRA */
  37983. wc_FreeRsaKey(&key);
  37984. wc_FreeRng(&rng);
  37985. res = TEST_RES_CHECK(1);
  37986. #endif
  37987. return res;
  37988. }
  37989. static int test_wolfSSL_X509_get_version(void)
  37990. {
  37991. int res = TEST_SKIPPED;
  37992. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37993. WOLFSSL_X509 *x509;
  37994. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37995. AssertNotNull(x509);
  37996. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  37997. wolfSSL_X509_free(x509);
  37998. res = TEST_RES_CHECK(1);
  37999. #endif
  38000. return res;
  38001. }
  38002. static int test_wolfSSL_DES_ncbc(void)
  38003. {
  38004. int res = TEST_SKIPPED;
  38005. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38006. const_DES_cblock myDes;
  38007. DES_cblock iv = {1};
  38008. DES_key_schedule key = {0};
  38009. unsigned char msg[] = "hello wolfssl";
  38010. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38011. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38012. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38013. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38014. /* partial block test */
  38015. DES_set_key(&key, &myDes);
  38016. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38017. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38018. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38019. DES_set_key(&key, &myDes);
  38020. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38021. *((byte*)&iv) = 1;
  38022. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38023. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  38024. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38025. /* full block test */
  38026. DES_set_key(&key, &myDes);
  38027. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38028. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38029. *((byte*)&iv) = 1;
  38030. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38031. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38032. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38033. DES_set_key(&key, &myDes);
  38034. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38035. *((byte*)&iv) = 1;
  38036. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38037. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  38038. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38039. res = TEST_RES_CHECK(1);
  38040. #endif
  38041. return res;
  38042. }
  38043. static int test_wolfSSL_AES_cbc_encrypt(void)
  38044. {
  38045. int res = TEST_SKIPPED;
  38046. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  38047. AES_KEY aes;
  38048. AES_KEY* aesN = NULL;
  38049. size_t len = 0;
  38050. size_t lenB = 0;
  38051. int keySz0 = 0;
  38052. int keySzN = -1;
  38053. byte out[AES_BLOCK_SIZE] = {0};
  38054. byte* outN = NULL;
  38055. /* Test vectors retrieved from:
  38056. * <begin URL>
  38057. * https://csrc.nist.gov/
  38058. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38059. * documents/aes/KAT_AES.zip
  38060. * </end URL>
  38061. */
  38062. const byte* pt128N = NULL;
  38063. byte* key128N = NULL;
  38064. byte* iv128N = NULL;
  38065. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38066. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38067. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38068. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38069. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38070. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38071. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38072. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38073. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38074. len = sizeof(pt128);
  38075. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38076. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38077. AssertIntNE(XMEMCMP(b, g, h), i)
  38078. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38079. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38080. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38081. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38082. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38083. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38084. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38085. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38086. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38087. /* Stressing wolfSSL_AES_set_encrypt_key */
  38088. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38089. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38090. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38091. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38092. /* Stressing wolfSSL_AES_set_decrypt_key */
  38093. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38094. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38095. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38096. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38097. #ifdef WOLFSSL_AES_128
  38098. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38099. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38100. RESET_IV(iv128tmp, iv128);
  38101. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38102. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38103. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38104. #ifdef HAVE_AES_DECRYPT
  38105. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38106. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38107. RESET_IV(iv128tmp, iv128);
  38108. len = sizeof(ct128);
  38109. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38110. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38111. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38112. #endif
  38113. #endif /* WOLFSSL_AES_128 */
  38114. #ifdef WOLFSSL_AES_192
  38115. {
  38116. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38117. * Appendix F.2.3 */
  38118. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38119. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38120. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38121. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38122. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38123. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38124. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38125. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38126. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38127. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38128. len = sizeof(pt192);
  38129. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38130. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38131. RESET_IV(iv192tmp, iv192);
  38132. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38133. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38134. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38135. #ifdef HAVE_AES_DECRYPT
  38136. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38137. len = sizeof(ct192);
  38138. RESET_IV(iv192tmp, iv192);
  38139. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38140. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38141. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38142. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38143. #endif
  38144. }
  38145. #endif /* WOLFSSL_AES_192 */
  38146. #ifdef WOLFSSL_AES_256
  38147. {
  38148. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38149. * Appendix F.2.5 */
  38150. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38151. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38152. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38153. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38154. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38155. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38156. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38157. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38158. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38159. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38160. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38161. len = sizeof(pt256);
  38162. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38163. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38164. RESET_IV(iv256tmp, iv256);
  38165. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38166. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38167. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38168. #ifdef HAVE_AES_DECRYPT
  38169. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38170. len = sizeof(ct256);
  38171. RESET_IV(iv256tmp, iv256);
  38172. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38173. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38174. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38175. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38176. #endif
  38177. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38178. {
  38179. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38180. byte wrapPlain[sizeof(key256)] = { 0 };
  38181. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38182. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38183. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38184. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38185. 15), WOLFSSL_FAILURE);
  38186. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38187. sizeof(key256)), sizeof(wrapCipher));
  38188. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38189. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38190. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38191. 23), WOLFSSL_FAILURE);
  38192. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38193. sizeof(wrapCipher)), sizeof(wrapPlain));
  38194. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38195. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38196. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38197. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38198. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38199. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38200. sizeof(key256)), sizeof(wrapCipher));
  38201. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38202. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38203. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38204. sizeof(wrapCipher)), sizeof(wrapPlain));
  38205. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38206. }
  38207. #endif /* HAVE_AES_KEYWRAP */
  38208. }
  38209. #endif /* WOLFSSL_AES_256 */
  38210. res = TEST_RES_CHECK(1);
  38211. #endif
  38212. return res;
  38213. }
  38214. static int test_wolfSSL_CRYPTO_cts128(void)
  38215. {
  38216. int res = TEST_SKIPPED;
  38217. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  38218. && defined(HAVE_CTS)
  38219. byte tmp[64]; /* Largest vector size */
  38220. /* Test vectors taken form RFC3962 Appendix B */
  38221. const testVector vects[] = {
  38222. {
  38223. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38224. "\x20",
  38225. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38226. "\x97",
  38227. 17, 17
  38228. },
  38229. {
  38230. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38231. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38232. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38233. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38234. 31, 31
  38235. },
  38236. {
  38237. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38238. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38239. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38240. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38241. 32, 32
  38242. },
  38243. {
  38244. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38245. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38246. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38247. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38248. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38249. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38250. 47, 47
  38251. },
  38252. {
  38253. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38254. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38255. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38256. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38257. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38258. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38259. 48, 48
  38260. },
  38261. {
  38262. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38263. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38264. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38265. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38266. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38267. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38268. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38269. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38270. 64, 64
  38271. }
  38272. };
  38273. byte keyBytes[AES_128_KEY_SIZE] = {
  38274. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38275. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38276. };
  38277. size_t i;
  38278. XMEMSET(tmp, 0, sizeof(tmp));
  38279. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38280. AES_KEY encKey;
  38281. AES_KEY decKey;
  38282. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38283. XMEMSET(iv, 0, sizeof(iv));
  38284. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  38285. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  38286. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38287. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38288. vects[i].outLen);
  38289. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38290. XMEMSET(iv, 0, sizeof(iv));
  38291. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38292. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38293. vects[i].inLen);
  38294. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38295. }
  38296. res = TEST_RES_CHECK(1);
  38297. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38298. return res;
  38299. }
  38300. #if defined(OPENSSL_ALL)
  38301. #if !defined(NO_ASN)
  38302. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  38303. {
  38304. int res = TEST_SKIPPED;
  38305. #if !defined(NO_RSA)
  38306. WOLFSSL_X509* x509;
  38307. WOLFSSL_X509_NAME* subject;
  38308. WOLFSSL_X509_NAME_ENTRY* e;
  38309. WOLFSSL_ASN1_STRING* a;
  38310. FILE* file;
  38311. int idx = 0;
  38312. char targetOutput[16] = "www.wolfssl.com";
  38313. unsigned char* actual_output;
  38314. int len = 0;
  38315. int result = 0;
  38316. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  38317. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  38318. fclose(file);
  38319. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  38320. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  38321. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  38322. NID_commonName, -1)), 5);
  38323. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  38324. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  38325. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  38326. result = strncmp((const char*)actual_output, targetOutput, len);
  38327. AssertIntEQ(result, 0);
  38328. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  38329. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  38330. WOLFSSL_FATAL_ERROR);
  38331. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  38332. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  38333. WOLFSSL_FATAL_ERROR);
  38334. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  38335. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  38336. WOLFSSL_FATAL_ERROR);
  38337. wolfSSL_X509_free(x509);
  38338. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38339. res = TEST_RES_CHECK(1);
  38340. #endif
  38341. return res;
  38342. }
  38343. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  38344. {
  38345. ASN1_STRING* asn1str_test;
  38346. ASN1_STRING* asn1str_answer;
  38347. /* Each character is encoded using 4 bytes */
  38348. char input[] = {
  38349. 0, 0, 0, 'T',
  38350. 0, 0, 0, 'e',
  38351. 0, 0, 0, 's',
  38352. 0, 0, 0, 't',
  38353. };
  38354. char output[] = "Test";
  38355. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  38356. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  38357. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  38358. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  38359. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  38360. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  38361. ASN1_STRING_free(asn1str_test);
  38362. ASN1_STRING_free(asn1str_answer);
  38363. return TEST_RES_CHECK(1);
  38364. }
  38365. #endif /* !defined(NO_ASN) */
  38366. static int test_wolfSSL_sk_CIPHER_description(void)
  38367. {
  38368. int res = TEST_SKIPPED;
  38369. #if !defined(NO_RSA)
  38370. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38371. int i,j,k;
  38372. int numCiphers = 0;
  38373. const SSL_METHOD *method = NULL;
  38374. const SSL_CIPHER *cipher = NULL;
  38375. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38376. SSL_CTX *ctx = NULL;
  38377. SSL *ssl = NULL;
  38378. char buf[256];
  38379. char test_str[9] = "0000000";
  38380. const char badStr[] = "unknown";
  38381. const char certPath[] = "./certs/client-cert.pem";
  38382. XMEMSET(buf, 0, sizeof(buf));
  38383. AssertNotNull(method = TLSv1_2_client_method());
  38384. AssertNotNull(ctx = SSL_CTX_new(method));
  38385. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38386. SSL_CTX_set_verify_depth(ctx, 4);
  38387. SSL_CTX_set_options(ctx, flags);
  38388. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38389. WOLFSSL_SUCCESS);
  38390. AssertNotNull(ssl = SSL_new(ctx));
  38391. /* SSL_get_ciphers returns a stack of all configured ciphers
  38392. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38393. */
  38394. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38395. /* loop through the amount of supportedCiphers */
  38396. numCiphers = sk_num(supportedCiphers);
  38397. for (i = 0; i < numCiphers; ++i) {
  38398. /* sk_value increments "sk->data.cipher->cipherOffset".
  38399. * wolfSSL_sk_CIPHER_description sets the description for
  38400. * the cipher based on the provided offset.
  38401. */
  38402. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38403. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38404. }
  38405. /* Search cipher description string for "unknown" descriptor */
  38406. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38407. k = 0;
  38408. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38409. test_str[k] = badStr[k];
  38410. j++;
  38411. k++;
  38412. }
  38413. }
  38414. /* Fail if test_str == badStr == "unknown" */
  38415. AssertStrNE(test_str,badStr);
  38416. }
  38417. SSL_free(ssl);
  38418. SSL_CTX_free(ctx);
  38419. res = TEST_RES_CHECK(1);
  38420. #endif
  38421. return res;
  38422. }
  38423. static int test_wolfSSL_get_ciphers_compat(void)
  38424. {
  38425. int res = TEST_SKIPPED;
  38426. #if !defined(NO_RSA)
  38427. const SSL_METHOD *method = NULL;
  38428. const char certPath[] = "./certs/client-cert.pem";
  38429. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38430. SSL_CTX *ctx = NULL;
  38431. WOLFSSL *ssl = NULL;
  38432. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38433. method = SSLv23_client_method();
  38434. AssertNotNull(method);
  38435. ctx = SSL_CTX_new(method);
  38436. AssertNotNull(ctx);
  38437. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38438. SSL_CTX_set_verify_depth(ctx, 4);
  38439. SSL_CTX_set_options(ctx, flags);
  38440. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38441. WOLFSSL_SUCCESS);
  38442. AssertNotNull(ssl = SSL_new(ctx));
  38443. /* Test Bad NULL input */
  38444. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  38445. /* Test for Good input */
  38446. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38447. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38448. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38449. SSL_free(ssl);
  38450. SSL_CTX_free(ctx);
  38451. res = TEST_RES_CHECK(1);
  38452. #endif
  38453. return res;
  38454. }
  38455. static int test_wolfSSL_X509_PUBKEY_get(void)
  38456. {
  38457. WOLFSSL_X509_PUBKEY pubkey;
  38458. WOLFSSL_X509_PUBKEY* key;
  38459. WOLFSSL_EVP_PKEY evpkey ;
  38460. WOLFSSL_EVP_PKEY* evpPkey;
  38461. WOLFSSL_EVP_PKEY* retEvpPkey;
  38462. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38463. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38464. key = &pubkey;
  38465. evpPkey = &evpkey;
  38466. evpPkey->type = WOLFSSL_SUCCESS;
  38467. key->pkey = evpPkey;
  38468. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38469. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38470. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38471. key->pkey = NULL;
  38472. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38473. return TEST_RES_CHECK(retEvpPkey == NULL);
  38474. }
  38475. static int test_wolfSSL_EC_KEY_dup(void)
  38476. {
  38477. int res = TEST_SKIPPED;
  38478. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  38479. defined(OPENSSL_EXTRA_X509_SMALL))
  38480. WOLFSSL_EC_KEY* ecKey;
  38481. WOLFSSL_EC_KEY* dupKey;
  38482. ecc_key* srcKey;
  38483. ecc_key* destKey;
  38484. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38485. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38486. /* Valid cases */
  38487. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38488. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  38489. /* Compare pubkey */
  38490. srcKey = (ecc_key*)ecKey->internal;
  38491. destKey = (ecc_key*)dupKey->internal;
  38492. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  38493. /* compare EC_GROUP */
  38494. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  38495. /* compare EC_POINT */
  38496. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  38497. dupKey->pub_key, NULL), MP_EQ);
  38498. /* compare BIGNUM */
  38499. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  38500. wolfSSL_EC_KEY_free(dupKey);
  38501. /* Invalid cases */
  38502. /* NULL key */
  38503. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  38504. /* NULL ecc_key */
  38505. wc_ecc_free((ecc_key*)ecKey->internal);
  38506. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  38507. ecKey->internal = NULL; /* Set ecc_key to NULL */
  38508. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38509. wolfSSL_EC_KEY_free(ecKey);
  38510. wolfSSL_EC_KEY_free(dupKey);
  38511. /* NULL Group */
  38512. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38513. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38514. wolfSSL_EC_GROUP_free(ecKey->group);
  38515. ecKey->group = NULL; /* Set group to NULL */
  38516. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38517. wolfSSL_EC_KEY_free(ecKey);
  38518. wolfSSL_EC_KEY_free(dupKey);
  38519. /* NULL public key */
  38520. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38521. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38522. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  38523. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  38524. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38525. wolfSSL_EC_POINT_free(ecKey->pub_key);
  38526. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  38527. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38528. wolfSSL_EC_KEY_free(ecKey);
  38529. wolfSSL_EC_KEY_free(dupKey);
  38530. /* NULL private key */
  38531. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38532. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38533. wolfSSL_BN_free(ecKey->priv_key);
  38534. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  38535. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38536. wolfSSL_EC_KEY_free(ecKey);
  38537. wolfSSL_EC_KEY_free(dupKey);
  38538. /* Test EC_KEY_up_ref */
  38539. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38540. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  38541. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  38542. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  38543. /* reference count doesn't follow duplicate */
  38544. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38545. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  38546. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  38547. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  38548. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  38549. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  38550. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  38551. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  38552. res = TEST_RES_CHECK(1);
  38553. #endif
  38554. return res;
  38555. }
  38556. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38557. {
  38558. int res = TEST_SKIPPED;
  38559. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38560. DSA *dsa = NULL;
  38561. DSA *setDsa = NULL;
  38562. EVP_PKEY *pkey = NULL;
  38563. EVP_PKEY *set1Pkey = NULL;
  38564. SHA_CTX sha;
  38565. byte signature[DSA_SIG_SIZE];
  38566. byte hash[WC_SHA_DIGEST_SIZE];
  38567. word32 bytes;
  38568. int answer;
  38569. #ifdef USE_CERT_BUFFERS_1024
  38570. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38571. int dsaKeySz = sizeof_dsa_key_der_1024;
  38572. byte tmp[ONEK_BUF];
  38573. XMEMSET(tmp, 0, sizeof(tmp));
  38574. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38575. bytes = dsaKeySz;
  38576. #elif defined(USE_CERT_BUFFERS_2048)
  38577. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38578. int dsaKeySz = sizeof_dsa_key_der_2048;
  38579. byte tmp[TWOK_BUF];
  38580. XMEMSET(tmp, 0, sizeof(tmp));
  38581. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38582. bytes = dsaKeySz;
  38583. #else
  38584. byte tmp[TWOK_BUF];
  38585. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38586. int dsaKeySz;
  38587. XMEMSET(tmp, 0, sizeof(tmp));
  38588. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  38589. if (fp == XBADFILE) {
  38590. return WOLFSSL_BAD_FILE;
  38591. }
  38592. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  38593. XFCLOSE(fp);
  38594. #endif /* END USE_CERT_BUFFERS_1024 */
  38595. /* Create hash to later Sign and Verify */
  38596. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38597. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38598. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38599. /* Initialize pkey with der format dsa key */
  38600. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  38601. &dsaKeyDer ,(long)dsaKeySz));
  38602. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38603. /* Should Fail: NULL argument */
  38604. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38605. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38606. /* Should Pass: Initialized pkey argument */
  38607. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38608. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38609. #ifdef USE_CERT_BUFFERS_1024
  38610. AssertIntEQ(DSA_bits(dsa), 1024);
  38611. #else
  38612. AssertIntEQ(DSA_bits(dsa), 2048);
  38613. #endif
  38614. /* Sign */
  38615. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38616. /* Verify. */
  38617. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38618. WOLFSSL_SUCCESS);
  38619. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38620. /* Should Fail: set1Pkey not initialized */
  38621. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38622. /* Initialize set1Pkey */
  38623. set1Pkey = EVP_PKEY_new();
  38624. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38625. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38626. WOLFSSL_SUCCESS);
  38627. /* Should Pass: set dsa into set1Pkey */
  38628. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38629. DSA_free(dsa);
  38630. DSA_free(setDsa);
  38631. EVP_PKEY_free(pkey);
  38632. EVP_PKEY_free(set1Pkey);
  38633. res = TEST_RES_CHECK(1);
  38634. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38635. return res;
  38636. } /* END test_EVP_PKEY_set1_get1_DSA */
  38637. static int test_wolfSSL_DSA_SIG(void)
  38638. {
  38639. int res = TEST_SKIPPED;
  38640. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38641. !defined(HAVE_FIPS)
  38642. DSA *dsa = NULL;
  38643. DSA *dsa2 = NULL;
  38644. DSA_SIG *sig = NULL;
  38645. const BIGNUM *p = NULL;
  38646. const BIGNUM *q = NULL;
  38647. const BIGNUM *g = NULL;
  38648. const BIGNUM *pub = NULL;
  38649. const BIGNUM *priv = NULL;
  38650. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38651. AssertNotNull(dsa = DSA_generate_parameters(2048,
  38652. NULL, 0, NULL, NULL, NULL, NULL));
  38653. DSA_free(dsa);
  38654. AssertNotNull(dsa = DSA_new());
  38655. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  38656. NULL, 0, NULL, NULL, NULL), 1);
  38657. AssertIntEQ(DSA_generate_key(dsa), 1);
  38658. DSA_get0_pqg(dsa, &p, &q, &g);
  38659. DSA_get0_key(dsa, &pub, &priv);
  38660. AssertNotNull(p = BN_dup(p));
  38661. AssertNotNull(q = BN_dup(q));
  38662. AssertNotNull(g = BN_dup(g));
  38663. AssertNotNull(pub = BN_dup(pub));
  38664. AssertNotNull(priv = BN_dup(priv));
  38665. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38666. AssertNotNull(dsa2 = DSA_new());
  38667. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  38668. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  38669. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38670. DSA_free(dsa);
  38671. DSA_free(dsa2);
  38672. DSA_SIG_free(sig);
  38673. res = TEST_RES_CHECK(1);
  38674. #endif
  38675. return res;
  38676. }
  38677. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38678. {
  38679. int res = TEST_SKIPPED;
  38680. #ifdef HAVE_ECC
  38681. WOLFSSL_EC_KEY *ecKey = NULL;
  38682. WOLFSSL_EC_KEY *ecGet1 = NULL;
  38683. EVP_PKEY *pkey = NULL;
  38684. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38685. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38686. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38687. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38688. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38689. /* Should fail since ecKey is empty */
  38690. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38691. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38692. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38693. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38694. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38695. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38696. wolfSSL_EC_KEY_free(ecKey);
  38697. wolfSSL_EC_KEY_free(ecGet1);
  38698. EVP_PKEY_free(pkey);
  38699. res = TEST_RES_CHECK(1);
  38700. #endif /* HAVE_ECC */
  38701. return res;
  38702. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38703. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38704. {
  38705. int res = TEST_SKIPPED;
  38706. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38707. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38708. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38709. DH *dh = NULL;
  38710. DH *setDh = NULL;
  38711. EVP_PKEY *pkey = NULL;
  38712. FILE* f = NULL;
  38713. unsigned char buf[4096];
  38714. const unsigned char* pt = buf;
  38715. const char* dh2048 = "./certs/dh2048.der";
  38716. long len = 0;
  38717. int code = -1;
  38718. XMEMSET(buf, 0, sizeof(buf));
  38719. f = XFOPEN(dh2048, "rb");
  38720. AssertTrue(f != XBADFILE);
  38721. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38722. XFCLOSE(f);
  38723. /* Load dh2048.der into DH with internal format */
  38724. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38725. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38726. AssertIntEQ(code, 0);
  38727. code = -1;
  38728. pkey = wolfSSL_EVP_PKEY_new();
  38729. /* Set DH into PKEY */
  38730. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38731. /* Get DH from PKEY */
  38732. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38733. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38734. AssertIntEQ(code, 0);
  38735. EVP_PKEY_free(pkey);
  38736. DH_free(setDh);
  38737. DH_free(dh);
  38738. res = TEST_RES_CHECK(1);
  38739. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38740. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38741. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38742. return res;
  38743. } /* END test_EVP_PKEY_set1_get1_DH */
  38744. static int test_wolfSSL_CTX_ctrl(void)
  38745. {
  38746. int res = TEST_SKIPPED;
  38747. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38748. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38749. char caFile[] = "./certs/client-ca.pem";
  38750. char clientFile[] = "./certs/client-cert.pem";
  38751. SSL_CTX* ctx;
  38752. X509* x509 = NULL;
  38753. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38754. byte buf[6000];
  38755. char file[] = "./certs/dsaparams.pem";
  38756. XFILE f;
  38757. int bytes;
  38758. BIO* bio;
  38759. DSA* dsa;
  38760. DH* dh;
  38761. #endif
  38762. #ifdef HAVE_ECC
  38763. WOLFSSL_EC_KEY* ecKey;
  38764. #endif
  38765. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38766. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  38767. AssertNotNull(x509);
  38768. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38769. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  38770. AssertNotNull(x509);
  38771. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38772. /* Initialize DH */
  38773. f = XFOPEN(file, "rb");
  38774. AssertTrue((f != XBADFILE));
  38775. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38776. XFCLOSE(f);
  38777. bio = BIO_new_mem_buf((void*)buf, bytes);
  38778. AssertNotNull(bio);
  38779. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38780. AssertNotNull(dsa);
  38781. dh = wolfSSL_DSA_dup_DH(dsa);
  38782. AssertNotNull(dh);
  38783. #endif
  38784. #ifdef HAVE_ECC
  38785. /* Initialize WOLFSSL_EC_KEY */
  38786. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38787. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  38788. #endif
  38789. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38790. /* additional test of getting EVP_PKEY key size from X509
  38791. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38792. * allowed with user RSA */
  38793. {
  38794. EVP_PKEY* pkey;
  38795. #if defined(HAVE_ECC)
  38796. X509* ecX509;
  38797. #endif /* HAVE_ECC */
  38798. AssertNotNull(pkey = X509_get_pubkey(x509));
  38799. /* current RSA key is 2048 bit (256 bytes) */
  38800. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  38801. EVP_PKEY_free(pkey);
  38802. #if defined(HAVE_ECC)
  38803. #if defined(USE_CERT_BUFFERS_256)
  38804. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38805. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38806. SSL_FILETYPE_ASN1));
  38807. #else
  38808. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38809. cliEccCertFile, SSL_FILETYPE_PEM));
  38810. #endif
  38811. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  38812. /* current ECC key is 256 bit (32 bytes) */
  38813. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  38814. X509_free(ecX509);
  38815. EVP_PKEY_free(pkey);
  38816. #endif /* HAVE_ECC */
  38817. }
  38818. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38819. /* Tests should fail with passed in NULL pointer */
  38820. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  38821. SSL_FAILURE);
  38822. #if !defined(NO_DH) && !defined(NO_DSA)
  38823. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  38824. SSL_FAILURE);
  38825. #endif
  38826. #ifdef HAVE_ECC
  38827. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  38828. SSL_FAILURE);
  38829. #endif
  38830. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38831. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38832. */
  38833. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  38834. SSL_SUCCESS);
  38835. /* Test with SSL_CTRL_OPTIONS
  38836. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38837. */
  38838. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  38839. == SSL_OP_NO_TLSv1);
  38840. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38841. /* Test with SSL_CTRL_SET_TMP_DH
  38842. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38843. */
  38844. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38845. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  38846. SSL_SUCCESS);
  38847. #endif
  38848. /* Test with SSL_CTRL_SET_TMP_ECDH
  38849. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38850. */
  38851. #ifdef HAVE_ECC
  38852. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  38853. SSL_SUCCESS);
  38854. #endif
  38855. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38856. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  38857. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38858. #endif
  38859. /* Test for min/max proto */
  38860. #ifndef WOLFSSL_NO_TLS12
  38861. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38862. 0, NULL), SSL_SUCCESS);
  38863. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38864. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38865. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38866. #endif
  38867. #ifdef WOLFSSL_TLS13
  38868. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38869. 0, NULL), SSL_SUCCESS);
  38870. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38871. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38872. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38873. #ifndef WOLFSSL_NO_TLS12
  38874. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38875. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38876. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38877. #endif
  38878. #endif
  38879. /* Cleanup and Pass */
  38880. #if !defined(NO_DH) && !defined(NO_DSA)
  38881. #ifndef NO_BIO
  38882. BIO_free(bio);
  38883. DSA_free(dsa);
  38884. DH_free(dh);
  38885. #endif
  38886. #endif
  38887. #ifdef HAVE_ECC
  38888. wolfSSL_EC_KEY_free(ecKey);
  38889. #endif
  38890. SSL_CTX_free(ctx);
  38891. res = TEST_RES_CHECK(1);
  38892. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38893. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38894. return res;
  38895. }
  38896. static int test_wolfSSL_EVP_PKEY_assign(void)
  38897. {
  38898. int res = TEST_SKIPPED;
  38899. int type;
  38900. WOLFSSL_EVP_PKEY* pkey;
  38901. #ifndef NO_RSA
  38902. WOLFSSL_RSA* rsa;
  38903. #endif
  38904. #ifndef NO_DSA
  38905. WOLFSSL_DSA* dsa;
  38906. #endif
  38907. #ifdef HAVE_ECC
  38908. WOLFSSL_EC_KEY* ecKey;
  38909. #endif
  38910. (void)pkey;
  38911. #ifndef NO_RSA
  38912. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38913. type = EVP_PKEY_RSA;
  38914. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38915. AssertNotNull(rsa = wolfSSL_RSA_new());
  38916. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  38917. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38918. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  38919. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  38920. wolfSSL_EVP_PKEY_free(pkey);
  38921. res = TEST_RES_CHECK(1);
  38922. }
  38923. #endif /* NO_RSA */
  38924. #ifndef NO_DSA
  38925. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38926. type = EVP_PKEY_DSA;
  38927. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38928. AssertNotNull(dsa = wolfSSL_DSA_new());
  38929. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  38930. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38931. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  38932. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  38933. wolfSSL_EVP_PKEY_free(pkey);
  38934. res = TEST_RES_CHECK(1);
  38935. }
  38936. #endif /* NO_DSA */
  38937. #ifdef HAVE_ECC
  38938. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  38939. type = EVP_PKEY_EC;
  38940. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38941. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38942. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  38943. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38944. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  38945. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  38946. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38947. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  38948. wolfSSL_EVP_PKEY_free(pkey);
  38949. res = TEST_RES_CHECK(1);
  38950. }
  38951. #endif /* HAVE_ECC */
  38952. (void)type;
  38953. return res;
  38954. }
  38955. static int test_wolfSSL_EVP_PKEY_base_id(void)
  38956. {
  38957. WOLFSSL_EVP_PKEY* pkey;
  38958. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38959. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  38960. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  38961. EVP_PKEY_free(pkey);
  38962. return TEST_RES_CHECK(1);
  38963. }
  38964. static int test_wolfSSL_EVP_PKEY_id(void)
  38965. {
  38966. WOLFSSL_EVP_PKEY* pkey;
  38967. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38968. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  38969. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  38970. EVP_PKEY_free(pkey);
  38971. return TEST_RES_CHECK(1);
  38972. }
  38973. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  38974. {
  38975. int res = TEST_SKIPPED;
  38976. #if defined(OPENSSL_ALL) && \
  38977. !defined(NO_ECC_SECP) && \
  38978. /* This last bit is taken from ecc.c. It is the condition that
  38979. * defines ECC256 */ \
  38980. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  38981. ECC_MIN_KEY_SZ <= 256)
  38982. EVP_PKEY_CTX* ctx;
  38983. EVP_PKEY* pkey = NULL;
  38984. /* Test error conditions. */
  38985. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  38986. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38987. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  38988. #ifndef NO_RSA
  38989. EVP_PKEY_CTX_free(ctx);
  38990. /* Parameter generation for RSA not supported yet. */
  38991. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  38992. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  38993. #endif
  38994. #ifdef HAVE_ECC
  38995. EVP_PKEY_CTX_free(ctx);
  38996. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38997. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  38998. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  38999. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  39000. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  39001. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  39002. WOLFSSL_SUCCESS);
  39003. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39004. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39005. #endif
  39006. EVP_PKEY_CTX_free(ctx);
  39007. EVP_PKEY_free(pkey);
  39008. res = TEST_RES_CHECK(1);
  39009. #endif
  39010. return res;
  39011. }
  39012. static int test_wolfSSL_EVP_PKEY_keygen(void)
  39013. {
  39014. WOLFSSL_EVP_PKEY* pkey = NULL;
  39015. EVP_PKEY_CTX* ctx = NULL;
  39016. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39017. WOLFSSL_EVP_PKEY* params = NULL;
  39018. DH* dh = NULL;
  39019. const BIGNUM* pubkey = NULL;
  39020. const BIGNUM* privkey = NULL;
  39021. ASN1_INTEGER* asn1int = NULL;
  39022. unsigned int length = 0;
  39023. byte* derBuffer = NULL;
  39024. #endif
  39025. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39026. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39027. /* Bad cases */
  39028. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  39029. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  39030. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  39031. /* Good case */
  39032. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  39033. EVP_PKEY_CTX_free(ctx);
  39034. EVP_PKEY_free(pkey);
  39035. pkey = NULL;
  39036. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39037. /* Test DH keygen */
  39038. {
  39039. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39040. AssertNotNull(dh = DH_get_2048_256());
  39041. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39042. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  39043. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39044. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39045. DH_free(dh);
  39046. EVP_PKEY_CTX_free(ctx);
  39047. EVP_PKEY_free(params);
  39048. /* try exporting generated key to DER, to verify */
  39049. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  39050. DH_get0_key(dh, &pubkey, &privkey);
  39051. AssertNotNull(pubkey);
  39052. AssertNotNull(privkey);
  39053. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  39054. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  39055. ASN1_INTEGER_free(asn1int);
  39056. DH_free(dh);
  39057. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39058. EVP_PKEY_free(pkey);
  39059. }
  39060. #endif
  39061. return TEST_RES_CHECK(1);
  39062. }
  39063. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  39064. {
  39065. WOLFSSL_EVP_PKEY* pkey;
  39066. EVP_PKEY_CTX *ctx;
  39067. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39068. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39069. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39070. EVP_PKEY_CTX_free(ctx);
  39071. EVP_PKEY_free(pkey);
  39072. return TEST_RES_CHECK(1);
  39073. }
  39074. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  39075. {
  39076. int res = TEST_SKIPPED;
  39077. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  39078. WOLFSSL_EVP_PKEY* pkey;
  39079. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39080. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  39081. EVP_PKEY_free(pkey);
  39082. res = TEST_RES_CHECK(1);
  39083. #endif
  39084. return res;
  39085. }
  39086. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  39087. {
  39088. int res = TEST_SKIPPED;
  39089. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  39090. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  39091. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  39092. !defined(NO_FILESYSTEM)
  39093. WOLFSSL_EVP_PKEY* params = NULL;
  39094. WOLFSSL_EVP_PKEY* copy = NULL;
  39095. DH* dh = NULL;
  39096. BIGNUM* p1;
  39097. BIGNUM* g1;
  39098. BIGNUM* q1;
  39099. BIGNUM* p2;
  39100. BIGNUM* g2;
  39101. BIGNUM* q2;
  39102. /* create DH with DH_get_2048_256 params */
  39103. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  39104. AssertNotNull(dh = DH_get_2048_256());
  39105. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39106. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  39107. (const BIGNUM**)&q1,
  39108. (const BIGNUM**)&g1);
  39109. DH_free(dh);
  39110. /* create DH with random generated DH params */
  39111. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  39112. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  39113. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  39114. DH_free(dh);
  39115. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  39116. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  39117. AssertNotNull(dh->p);
  39118. AssertNotNull(dh->g);
  39119. AssertNotNull(dh->q);
  39120. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  39121. (const BIGNUM**)&q2,
  39122. (const BIGNUM**)&g2);
  39123. AssertIntEQ(BN_cmp(p1, p2), 0);
  39124. AssertIntEQ(BN_cmp(q1, q2), 0);
  39125. AssertIntEQ(BN_cmp(g1, g2), 0);
  39126. DH_free(dh);
  39127. EVP_PKEY_free(copy);
  39128. EVP_PKEY_free(params);
  39129. res = TEST_RES_CHECK(1);
  39130. #endif
  39131. return res;
  39132. }
  39133. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39134. {
  39135. WOLFSSL_EVP_PKEY* pkey;
  39136. EVP_PKEY_CTX *ctx;
  39137. int bits = 2048;
  39138. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39139. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39140. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39141. WOLFSSL_SUCCESS);
  39142. EVP_PKEY_CTX_free(ctx);
  39143. EVP_PKEY_free(pkey);
  39144. return TEST_RES_CHECK(1);
  39145. }
  39146. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39147. {
  39148. /* This is large enough to be used for all key sizes */
  39149. byte key[AES_256_KEY_SIZE] = {0};
  39150. byte iv[AES_BLOCK_SIZE] = {0};
  39151. int i, enumlen;
  39152. EVP_CIPHER_CTX *ctx;
  39153. const EVP_CIPHER *init;
  39154. int enumArray[] = {
  39155. #ifdef HAVE_AES_CBC
  39156. NID_aes_128_cbc,
  39157. #endif
  39158. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39159. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39160. #ifdef HAVE_AESGCM
  39161. NID_aes_128_gcm,
  39162. #endif
  39163. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39164. #ifdef WOLFSSL_AES_COUNTER
  39165. NID_aes_128_ctr,
  39166. #endif
  39167. #ifndef NO_DES3
  39168. NID_des_cbc,
  39169. NID_des_ede3_cbc,
  39170. #endif
  39171. };
  39172. int iv_lengths[] = {
  39173. #ifdef HAVE_AES_CBC
  39174. AES_BLOCK_SIZE,
  39175. #endif
  39176. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39177. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39178. #ifdef HAVE_AESGCM
  39179. GCM_NONCE_MID_SZ,
  39180. #endif
  39181. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39182. #ifdef WOLFSSL_AES_COUNTER
  39183. AES_BLOCK_SIZE,
  39184. #endif
  39185. #ifndef NO_DES3
  39186. DES_BLOCK_SIZE,
  39187. DES_BLOCK_SIZE,
  39188. #endif
  39189. };
  39190. enumlen = (sizeof(enumArray)/sizeof(int));
  39191. for (i = 0; i < enumlen; i++) {
  39192. ctx = EVP_CIPHER_CTX_new();
  39193. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  39194. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39195. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39196. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39197. EVP_CIPHER_CTX_free(ctx);
  39198. }
  39199. return TEST_RES_CHECK(1);
  39200. }
  39201. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39202. {
  39203. int res = TEST_SKIPPED;
  39204. #if !defined(NO_DES3)
  39205. byte key[AES_256_KEY_SIZE] = {0};
  39206. byte iv[AES_BLOCK_SIZE] = {0};
  39207. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39208. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39209. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39210. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39211. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  39212. EVP_CIPHER_CTX_free(ctx);
  39213. res = TEST_RES_CHECK(1);
  39214. #endif
  39215. return res;
  39216. }
  39217. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  39218. {
  39219. int res = TEST_SKIPPED;
  39220. #if !defined(NO_DES3)
  39221. byte key[AES_256_KEY_SIZE] = {0};
  39222. byte iv[AES_BLOCK_SIZE] = {0};
  39223. int keylen;
  39224. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39225. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39226. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39227. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39228. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39229. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  39230. WOLFSSL_SUCCESS);
  39231. EVP_CIPHER_CTX_free(ctx);
  39232. res = TEST_RES_CHECK(1);
  39233. #endif
  39234. return res;
  39235. }
  39236. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39237. {
  39238. int res = TEST_SKIPPED;
  39239. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39240. byte key[DES3_KEY_SIZE] = {0};
  39241. byte iv[DES_BLOCK_SIZE] = {0};
  39242. int ivLen, keyLen;
  39243. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39244. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39245. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39246. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39247. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39248. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39249. /* Bad cases */
  39250. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  39251. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  39252. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39253. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39254. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  39255. /* Good case */
  39256. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39257. EVP_CIPHER_CTX_free(ctx);
  39258. res = TEST_RES_CHECK(1);
  39259. #endif
  39260. return res;
  39261. }
  39262. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39263. {
  39264. WOLFSSL_ENGINE* e = NULL;
  39265. int id = 0;
  39266. EVP_PKEY_CTX *ctx;
  39267. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39268. EVP_PKEY_CTX_free(ctx);
  39269. return TEST_RES_CHECK(1);
  39270. }
  39271. static int test_wolfSSL_EVP_rc4(void)
  39272. {
  39273. int res = TEST_SKIPPED;
  39274. #if !defined(NO_RC4)
  39275. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  39276. #endif
  39277. return res;
  39278. }
  39279. static int test_wolfSSL_EVP_enc_null(void)
  39280. {
  39281. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  39282. }
  39283. static int test_wolfSSL_EVP_rc2_cbc(void)
  39284. {
  39285. int res = TEST_SKIPPED;
  39286. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39287. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  39288. #endif
  39289. return res;
  39290. }
  39291. static int test_wolfSSL_EVP_mdc2(void)
  39292. {
  39293. int res = TEST_SKIPPED;
  39294. #if !defined(NO_WOLFSSL_STUB)
  39295. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  39296. #endif
  39297. return res;
  39298. }
  39299. static int test_wolfSSL_EVP_md4(void)
  39300. {
  39301. int res = TEST_SKIPPED;
  39302. #if !defined(NO_MD4)
  39303. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  39304. #endif
  39305. return res;
  39306. }
  39307. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39308. {
  39309. return TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  39310. }
  39311. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39312. {
  39313. return TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  39314. }
  39315. static int test_wolfSSL_EVP_ripemd160(void)
  39316. {
  39317. int res = TEST_SKIPPED;
  39318. #if !defined(NO_WOLFSSL_STUB)
  39319. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  39320. #endif
  39321. return res;
  39322. }
  39323. static int test_wolfSSL_EVP_get_digestbynid(void)
  39324. {
  39325. #ifndef NO_MD5
  39326. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39327. #endif
  39328. #ifndef NO_SHA
  39329. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39330. #endif
  39331. #ifndef NO_SHA256
  39332. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  39333. #endif
  39334. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  39335. return TEST_RES_CHECK(1);
  39336. }
  39337. static int test_wolfSSL_EVP_MD_nid(void)
  39338. {
  39339. #ifndef NO_MD5
  39340. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39341. #endif
  39342. #ifndef NO_SHA
  39343. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39344. #endif
  39345. #ifndef NO_SHA256
  39346. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39347. #endif
  39348. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  39349. return TEST_RES_CHECK(1);
  39350. }
  39351. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39352. {
  39353. int res = TEST_SKIPPED;
  39354. #if defined(HAVE_ECC)
  39355. WOLFSSL_EVP_PKEY* pkey;
  39356. AssertNotNull(pkey = EVP_PKEY_new());
  39357. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  39358. EVP_PKEY_free(pkey);
  39359. res = TEST_RES_CHECK(1);
  39360. #endif
  39361. return res;
  39362. }
  39363. static int test_wolfSSL_EVP_X_STATE(void)
  39364. {
  39365. int res = TEST_SKIPPED;
  39366. #if !defined(NO_DES3) && !defined(NO_RC4)
  39367. byte key[DES3_KEY_SIZE] = {0};
  39368. byte iv[DES_IV_SIZE] = {0};
  39369. EVP_CIPHER_CTX *ctx;
  39370. const EVP_CIPHER *init;
  39371. /* Bad test cases */
  39372. ctx = EVP_CIPHER_CTX_new();
  39373. init = EVP_des_ede3_cbc();
  39374. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39375. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39376. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  39377. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  39378. EVP_CIPHER_CTX_free(ctx);
  39379. /* Good test case */
  39380. ctx = EVP_CIPHER_CTX_new();
  39381. init = wolfSSL_EVP_rc4();
  39382. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39383. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39384. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  39385. EVP_CIPHER_CTX_free(ctx);
  39386. res = TEST_RES_CHECK(1);
  39387. #endif
  39388. return res;
  39389. }
  39390. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39391. {
  39392. int res = TEST_SKIPPED;
  39393. #if !defined(NO_DES3) && !defined(NO_RC4)
  39394. byte key[DES3_KEY_SIZE] = {0};
  39395. byte iv[DES_IV_SIZE] = {0};
  39396. EVP_CIPHER_CTX *ctx;
  39397. const EVP_CIPHER *init;
  39398. /* Bad test cases */
  39399. ctx = EVP_CIPHER_CTX_new();
  39400. init = EVP_des_ede3_cbc();
  39401. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39402. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39403. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39404. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39405. EVP_CIPHER_CTX_free(ctx);
  39406. /* Good test case */
  39407. ctx = EVP_CIPHER_CTX_new();
  39408. init = wolfSSL_EVP_rc4();
  39409. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39410. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39411. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39412. EVP_CIPHER_CTX_free(ctx);
  39413. res = TEST_RES_CHECK(1);
  39414. #endif
  39415. return res;
  39416. }
  39417. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39418. {
  39419. int res = TEST_SKIPPED;
  39420. #ifdef HAVE_AES_CBC
  39421. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39422. #ifdef WOLFSSL_AES_128
  39423. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39424. #endif
  39425. #ifdef WOLFSSL_AES_192
  39426. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39427. #endif
  39428. #ifdef WOLFSSL_AES_256
  39429. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39430. #endif
  39431. res = TEST_RES_CHECK(1);
  39432. }
  39433. #endif
  39434. #ifdef HAVE_AESGCM
  39435. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39436. #ifdef WOLFSSL_AES_128
  39437. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39438. #endif
  39439. #ifdef WOLFSSL_AES_192
  39440. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39441. #endif
  39442. #ifdef WOLFSSL_AES_256
  39443. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39444. #endif
  39445. res = TEST_RES_CHECK(1);
  39446. }
  39447. #endif
  39448. #ifdef WOLFSSL_AES_COUNTER
  39449. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39450. #ifdef WOLFSSL_AES_128
  39451. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39452. #endif
  39453. #ifdef WOLFSSL_AES_192
  39454. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39455. #endif
  39456. #ifdef WOLFSSL_AES_256
  39457. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39458. #endif
  39459. res = TEST_RES_CHECK(1);
  39460. }
  39461. #endif
  39462. #ifdef HAVE_AES_ECB
  39463. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39464. #ifdef WOLFSSL_AES_128
  39465. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39466. #endif
  39467. #ifdef WOLFSSL_AES_192
  39468. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39469. #endif
  39470. #ifdef WOLFSSL_AES_256
  39471. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39472. #endif
  39473. res = TEST_RES_CHECK(1);
  39474. }
  39475. #endif
  39476. #ifdef WOLFSSL_AES_OFB
  39477. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39478. #ifdef WOLFSSL_AES_128
  39479. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39480. #endif
  39481. #ifdef WOLFSSL_AES_192
  39482. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39483. #endif
  39484. #ifdef WOLFSSL_AES_256
  39485. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39486. #endif
  39487. res = TEST_RES_CHECK(1);
  39488. }
  39489. #endif
  39490. #ifndef NO_RC4
  39491. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39492. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39493. res = TEST_RES_CHECK(1);
  39494. }
  39495. #endif
  39496. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39497. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  39498. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39499. res = TEST_RES_CHECK(1);
  39500. }
  39501. #endif
  39502. return res;
  39503. }
  39504. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39505. {
  39506. int i, enumlen;
  39507. int enumArray[] = {
  39508. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39509. #ifdef WOLFSSL_AES_128
  39510. NID_aes_128_cbc,
  39511. #endif
  39512. #ifdef WOLFSSL_AES_192
  39513. NID_aes_192_cbc,
  39514. #endif
  39515. #ifdef WOLFSSL_AES_256
  39516. NID_aes_256_cbc,
  39517. #endif
  39518. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39519. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39520. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39521. #ifdef HAVE_AESGCM
  39522. #ifdef WOLFSSL_AES_128
  39523. NID_aes_128_gcm,
  39524. #endif
  39525. #ifdef WOLFSSL_AES_192
  39526. NID_aes_192_gcm,
  39527. #endif
  39528. #ifdef WOLFSSL_AES_256
  39529. NID_aes_256_gcm,
  39530. #endif
  39531. #endif /* HAVE_AESGCM */
  39532. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39533. #ifdef WOLFSSL_AES_COUNTER
  39534. #ifdef WOLFSSL_AES_128
  39535. NID_aes_128_ctr,
  39536. #endif
  39537. #ifdef WOLFSSL_AES_192
  39538. NID_aes_192_ctr,
  39539. #endif
  39540. #ifdef WOLFSSL_AES_256
  39541. NID_aes_256_ctr,
  39542. #endif
  39543. #endif
  39544. #ifndef NO_DES3
  39545. NID_des_cbc,
  39546. NID_des_ede3_cbc,
  39547. #endif
  39548. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39549. NID_chacha20_poly1305,
  39550. #endif
  39551. };
  39552. int iv_lengths[] = {
  39553. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39554. #ifdef WOLFSSL_AES_128
  39555. AES_BLOCK_SIZE,
  39556. #endif
  39557. #ifdef WOLFSSL_AES_192
  39558. AES_BLOCK_SIZE,
  39559. #endif
  39560. #ifdef WOLFSSL_AES_256
  39561. AES_BLOCK_SIZE,
  39562. #endif
  39563. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39564. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39565. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39566. #ifdef HAVE_AESGCM
  39567. #ifdef WOLFSSL_AES_128
  39568. GCM_NONCE_MID_SZ,
  39569. #endif
  39570. #ifdef WOLFSSL_AES_192
  39571. GCM_NONCE_MID_SZ,
  39572. #endif
  39573. #ifdef WOLFSSL_AES_256
  39574. GCM_NONCE_MID_SZ,
  39575. #endif
  39576. #endif /* HAVE_AESGCM */
  39577. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39578. #ifdef WOLFSSL_AES_COUNTER
  39579. #ifdef WOLFSSL_AES_128
  39580. AES_BLOCK_SIZE,
  39581. #endif
  39582. #ifdef WOLFSSL_AES_192
  39583. AES_BLOCK_SIZE,
  39584. #endif
  39585. #ifdef WOLFSSL_AES_256
  39586. AES_BLOCK_SIZE,
  39587. #endif
  39588. #endif
  39589. #ifndef NO_DES3
  39590. DES_BLOCK_SIZE,
  39591. DES_BLOCK_SIZE,
  39592. #endif
  39593. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39594. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39595. #endif
  39596. };
  39597. enumlen = (sizeof(enumArray)/sizeof(int));
  39598. for (i = 0; i < enumlen; i++) {
  39599. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  39600. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39601. }
  39602. return TEST_RES_CHECK(1);
  39603. }
  39604. static int test_wolfSSL_EVP_SignInit_ex(void)
  39605. {
  39606. WOLFSSL_EVP_MD_CTX mdCtx;
  39607. WOLFSSL_ENGINE* e = 0;
  39608. const EVP_MD* md;
  39609. md = "SHA256";
  39610. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39611. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39612. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39613. return TEST_RES_CHECK(1);
  39614. }
  39615. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39616. {
  39617. int res = TEST_SKIPPED;
  39618. #if !defined(NO_SHA256)
  39619. WOLFSSL_EVP_MD_CTX mdCtx;
  39620. unsigned int s = 0;
  39621. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39622. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39623. /* Bad Case */
  39624. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39625. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39626. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39627. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39628. #else
  39629. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39630. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39631. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39632. #endif
  39633. /* Good Case */
  39634. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39635. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  39636. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39637. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39638. res = TEST_RES_CHECK(1);
  39639. #endif
  39640. return res;
  39641. }
  39642. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39643. {
  39644. int res = TEST_SKIPPED;
  39645. #if !defined(NO_DH) && \
  39646. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39647. FILE* f = NULL;
  39648. unsigned char buf[4096];
  39649. const unsigned char* pt = buf;
  39650. const char* params1 = "./certs/dh2048.der";
  39651. long len = 0;
  39652. WOLFSSL_DH* dh = NULL;
  39653. WOLFSSL_EVP_PKEY* pkey;
  39654. XMEMSET(buf, 0, sizeof(buf));
  39655. f = XFOPEN(params1, "rb");
  39656. AssertTrue(f != XBADFILE);
  39657. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39658. XFCLOSE(f);
  39659. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39660. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39661. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39662. /* Bad cases */
  39663. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39664. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39665. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39666. /* Good case */
  39667. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39668. EVP_PKEY_free(pkey);
  39669. res = TEST_RES_CHECK(1);
  39670. #endif
  39671. return res;
  39672. }
  39673. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39674. {
  39675. int res = TEST_SKIPPED;
  39676. #if defined(OPENSSL_EXTRA)
  39677. EVP_PKEY* pkey;
  39678. EVP_PKEY_CTX* ctx;
  39679. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39680. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39681. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39682. /* void */
  39683. EVP_PKEY_CTX_free(ctx);
  39684. AssertTrue(1);
  39685. #else
  39686. /* int */
  39687. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39688. #endif
  39689. EVP_PKEY_free(pkey);
  39690. res = TEST_RES_CHECK(1);
  39691. #endif
  39692. return res;
  39693. }
  39694. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39695. {
  39696. int res = TEST_SKIPPED;
  39697. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39698. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39699. DH *dh = NULL;
  39700. DH *setDh = NULL;
  39701. EVP_PKEY *pkey = NULL;
  39702. EVP_PKEY_CTX* ctx = NULL;
  39703. FILE* f = NULL;
  39704. unsigned char buf[512];
  39705. const unsigned char* pt = buf;
  39706. const char* dh2048 = "./certs/dh2048.der";
  39707. long len = 0;
  39708. int code = -1;
  39709. XMEMSET(buf, 0, sizeof(buf));
  39710. f = XFOPEN(dh2048, "rb");
  39711. AssertTrue(f != XBADFILE);
  39712. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39713. XFCLOSE(f);
  39714. /* Load dh2048.der into DH with internal format */
  39715. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39716. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39717. AssertIntEQ(code, 0);
  39718. code = -1;
  39719. pkey = wolfSSL_EVP_PKEY_new();
  39720. /* Set DH into PKEY */
  39721. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39722. /* create ctx from pkey */
  39723. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39724. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39725. /* */
  39726. /* TO DO invlaid case */
  39727. /* */
  39728. EVP_PKEY_CTX_free(ctx);
  39729. EVP_PKEY_free(pkey);
  39730. DH_free(setDh);
  39731. DH_free(dh);
  39732. res = TEST_RES_CHECK(1);
  39733. #endif
  39734. #endif
  39735. return res;
  39736. }
  39737. static int test_wolfSSL_EVP_BytesToKey(void)
  39738. {
  39739. int res = TEST_SKIPPED;
  39740. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39741. byte key[AES_BLOCK_SIZE] = {0};
  39742. byte iv[AES_BLOCK_SIZE] = {0};
  39743. int sz = 5;
  39744. int count = 0;
  39745. const EVP_MD* md = "SHA256";
  39746. const EVP_CIPHER *type;
  39747. const unsigned char *salt = (unsigned char *)"salt1234";
  39748. const byte data[] = {
  39749. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39750. 0x72,0x6c,0x64
  39751. };
  39752. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39753. /* Bad cases */
  39754. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39755. 0);
  39756. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39757. 16);
  39758. md = "2";
  39759. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39760. WOLFSSL_FAILURE);
  39761. /* Good case */
  39762. md = "SHA256";
  39763. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39764. 16);
  39765. res = TEST_RES_CHECK(1);
  39766. #endif
  39767. return res;
  39768. }
  39769. static int test_evp_cipher_aes_gcm(void)
  39770. {
  39771. int res = TEST_SKIPPED;
  39772. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39773. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39774. (HAVE_FIPS_VERSION >= 2)))
  39775. /*
  39776. * This test checks data at various points in the encrypt/decrypt process
  39777. * against known values produced using the same test with OpenSSL. This
  39778. * interop testing is critical for verifying the correctness of our
  39779. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39780. * a flow supported by OpenSSL that uses the control command
  39781. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39782. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39783. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39784. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39785. * happened to "cancel out" if both sides of the connection had the bug.
  39786. */
  39787. enum {
  39788. NUM_ENCRYPTIONS = 3,
  39789. AAD_SIZE = 4
  39790. };
  39791. byte plainText1[] = {
  39792. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39793. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39794. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39795. };
  39796. byte plainText2[] = {
  39797. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39798. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39799. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39800. };
  39801. byte plainText3[] = {
  39802. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39803. 0x5b, 0x57, 0x10
  39804. };
  39805. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39806. plainText1,
  39807. plainText2,
  39808. plainText3
  39809. };
  39810. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39811. sizeof(plainText1),
  39812. sizeof(plainText2),
  39813. sizeof(plainText3)
  39814. };
  39815. byte aad1[AAD_SIZE] = {
  39816. 0x00, 0x00, 0x00, 0x01
  39817. };
  39818. byte aad2[AAD_SIZE] = {
  39819. 0x00, 0x00, 0x00, 0x10
  39820. };
  39821. byte aad3[AAD_SIZE] = {
  39822. 0x00, 0x00, 0x01, 0x00
  39823. };
  39824. byte* aads[NUM_ENCRYPTIONS] = {
  39825. aad1,
  39826. aad2,
  39827. aad3
  39828. };
  39829. const byte iv[GCM_NONCE_MID_SZ] = {
  39830. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39831. };
  39832. byte currentIv[GCM_NONCE_MID_SZ];
  39833. const byte key[] = {
  39834. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39835. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39836. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39837. };
  39838. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39839. {
  39840. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39841. 0xEF
  39842. },
  39843. {
  39844. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39845. 0xF0
  39846. },
  39847. {
  39848. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39849. 0xF1
  39850. }
  39851. };
  39852. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39853. {
  39854. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39855. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39856. },
  39857. {
  39858. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39859. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39860. },
  39861. {
  39862. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39863. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39864. }
  39865. };
  39866. const byte expCipherText1[] = {
  39867. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39868. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39869. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39870. };
  39871. const byte expCipherText2[] = {
  39872. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39873. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39874. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39875. };
  39876. const byte expCipherText3[] = {
  39877. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39878. 0x80, 0x5E, 0x6B,
  39879. };
  39880. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39881. expCipherText1,
  39882. expCipherText2,
  39883. expCipherText3
  39884. };
  39885. byte* cipherText;
  39886. byte* calcPlainText;
  39887. byte tag[AES_BLOCK_SIZE];
  39888. EVP_CIPHER_CTX* encCtx = NULL;
  39889. EVP_CIPHER_CTX* decCtx = NULL;
  39890. int i, j, outl;
  39891. /****************************************************/
  39892. for (i = 0; i < 3; ++i) {
  39893. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  39894. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  39895. /* First iteration, set key before IV. */
  39896. if (i == 0) {
  39897. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  39898. SSL_SUCCESS);
  39899. /*
  39900. * The call to EVP_CipherInit below (with NULL key) should clear the
  39901. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  39902. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  39903. * behavior.
  39904. */
  39905. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39906. (void*)iv), SSL_SUCCESS);
  39907. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  39908. SSL_SUCCESS);
  39909. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39910. currentIv), SSL_FAILURE);
  39911. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  39912. SSL_SUCCESS);
  39913. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  39914. SSL_SUCCESS);
  39915. }
  39916. /* Second iteration, IV before key. */
  39917. else {
  39918. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  39919. SSL_SUCCESS);
  39920. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  39921. SSL_SUCCESS);
  39922. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  39923. SSL_SUCCESS);
  39924. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  39925. SSL_SUCCESS);
  39926. }
  39927. /*
  39928. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  39929. * been issued first.
  39930. */
  39931. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39932. currentIv), SSL_FAILURE);
  39933. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39934. (void*)iv), SSL_SUCCESS);
  39935. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39936. (void*)iv), SSL_SUCCESS);
  39937. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  39938. /*************** Encrypt ***************/
  39939. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39940. currentIv), SSL_SUCCESS);
  39941. /* Check current IV against expected. */
  39942. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39943. /* Add AAD. */
  39944. if (i == 2) {
  39945. /* Test streaming API. */
  39946. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  39947. AAD_SIZE), SSL_SUCCESS);
  39948. }
  39949. else {
  39950. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  39951. AAD_SIZE);
  39952. }
  39953. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39954. DYNAMIC_TYPE_TMP_BUFFER));
  39955. /* Encrypt plaintext. */
  39956. if (i == 2) {
  39957. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  39958. plainTexts[j], plainTextSzs[j]),
  39959. SSL_SUCCESS);
  39960. }
  39961. else {
  39962. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  39963. plainTextSzs[j]), plainTextSzs[j]);
  39964. }
  39965. if (i == 2) {
  39966. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  39967. SSL_SUCCESS);
  39968. }
  39969. else {
  39970. /*
  39971. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  39972. * akin to calling EVP_CipherFinal.
  39973. */
  39974. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  39975. }
  39976. /* Check ciphertext against expected. */
  39977. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  39978. 0);
  39979. /* Get and check tag against expected. */
  39980. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  39981. sizeof(tag), tag), SSL_SUCCESS);
  39982. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  39983. /*************** Decrypt ***************/
  39984. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39985. currentIv), SSL_SUCCESS);
  39986. /* Check current IV against expected. */
  39987. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39988. /* Add AAD. */
  39989. if (i == 2) {
  39990. /* Test streaming API. */
  39991. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  39992. AAD_SIZE), SSL_SUCCESS);
  39993. }
  39994. else {
  39995. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  39996. AAD_SIZE);
  39997. }
  39998. /* Set expected tag. */
  39999. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  40000. sizeof(tag), tag), SSL_SUCCESS);
  40001. /* Decrypt ciphertext. */
  40002. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40003. DYNAMIC_TYPE_TMP_BUFFER));
  40004. if (i == 2) {
  40005. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  40006. cipherText, plainTextSzs[j]),
  40007. SSL_SUCCESS);
  40008. }
  40009. else {
  40010. /* This first EVP_Cipher call will check the tag, too. */
  40011. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  40012. plainTextSzs[j]), plainTextSzs[j]);
  40013. }
  40014. if (i == 2) {
  40015. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  40016. SSL_SUCCESS);
  40017. }
  40018. else {
  40019. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  40020. }
  40021. /* Check plaintext against expected. */
  40022. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  40023. 0);
  40024. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40025. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40026. }
  40027. EVP_CIPHER_CTX_free(encCtx);
  40028. EVP_CIPHER_CTX_free(decCtx);
  40029. }
  40030. res = TEST_RES_CHECK(1);
  40031. #endif
  40032. return res;
  40033. }
  40034. static int test_wolfSSL_OBJ_ln(void)
  40035. {
  40036. const int nid_set[] = {
  40037. NID_commonName,
  40038. NID_serialNumber,
  40039. NID_countryName,
  40040. NID_localityName,
  40041. NID_stateOrProvinceName,
  40042. NID_organizationName,
  40043. NID_organizationalUnitName,
  40044. NID_domainComponent,
  40045. NID_businessCategory,
  40046. NID_jurisdictionCountryName,
  40047. NID_jurisdictionStateOrProvinceName,
  40048. NID_emailAddress
  40049. };
  40050. const char* ln_set[] = {
  40051. "commonName",
  40052. "serialNumber",
  40053. "countryName",
  40054. "localityName",
  40055. "stateOrProvinceName",
  40056. "organizationName",
  40057. "organizationalUnitName",
  40058. "domainComponent",
  40059. "businessCategory",
  40060. "jurisdictionCountryName",
  40061. "jurisdictionStateOrProvinceName",
  40062. "emailAddress",
  40063. };
  40064. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  40065. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  40066. #ifdef HAVE_ECC
  40067. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40068. {
  40069. EC_builtin_curve r[27];
  40070. size_t nCurves = sizeof(r) / sizeof(r[0]);
  40071. nCurves = EC_get_builtin_curves(r,nCurves);
  40072. for (i = 0; i < nCurves; i++) {
  40073. /* skip ECC_CURVE_INVALID */
  40074. if (r[i].nid != ECC_CURVE_INVALID) {
  40075. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  40076. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  40077. }
  40078. }
  40079. }
  40080. #endif
  40081. #endif
  40082. for (i = 0; i < maxIdx; i++) {
  40083. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  40084. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  40085. }
  40086. return TEST_RES_CHECK(1);
  40087. }
  40088. static int test_wolfSSL_OBJ_sn(void)
  40089. {
  40090. int i = 0, maxIdx = 7;
  40091. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  40092. NID_stateOrProvinceName,NID_organizationName,
  40093. NID_organizationalUnitName,NID_emailAddress};
  40094. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  40095. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  40096. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  40097. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  40098. WOLFSSL_EMAIL_ADDR};
  40099. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  40100. for (i = 0; i < maxIdx; i++) {
  40101. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  40102. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  40103. }
  40104. return TEST_RES_CHECK(1);
  40105. }
  40106. #if !defined(NO_BIO)
  40107. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  40108. {
  40109. return lh_strhash(s[3]);
  40110. }
  40111. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  40112. {
  40113. return XSTRCMP(a[3], b[3]);
  40114. }
  40115. #endif
  40116. static int test_wolfSSL_TXT_DB(void)
  40117. {
  40118. int res = TEST_SKIPPED;
  40119. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40120. BIO *bio;
  40121. TXT_DB *db = NULL;
  40122. const int columns = 6;
  40123. const char *fields[6] = {
  40124. "V",
  40125. "320926161116Z",
  40126. "",
  40127. "12BD",
  40128. "unknown",
  40129. "/CN=rsa doe",
  40130. };
  40131. char** fields_copy;
  40132. /* Test read */
  40133. AssertNotNull(bio = BIO_new(BIO_s_file()));
  40134. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40135. AssertNotNull(db = TXT_DB_read(bio, columns));
  40136. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40137. DYNAMIC_TYPE_OPENSSL));
  40138. XMEMCPY(fields_copy, fields, sizeof(fields));
  40139. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40140. BIO_free(bio);
  40141. /* Test write */
  40142. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40143. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  40144. BIO_free(bio);
  40145. /* Test index */
  40146. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40147. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40148. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40149. fields[3] = "12DA";
  40150. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40151. fields[3] = "FFFF";
  40152. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40153. fields[3] = "";
  40154. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40155. TXT_DB_free(db);
  40156. res = TEST_RES_CHECK(1);
  40157. #endif
  40158. return res;
  40159. }
  40160. static int test_wolfSSL_NCONF(void)
  40161. {
  40162. int res = TEST_SKIPPED;
  40163. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40164. const char* confFile = "./tests/NCONF_test.cnf";
  40165. CONF* conf = NULL;
  40166. long eline = 0;
  40167. long num = 0;
  40168. AssertNotNull(conf = NCONF_new(NULL));
  40169. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40170. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40171. AssertIntEQ(num, 1234);
  40172. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40173. AssertIntEQ(num, 4321);
  40174. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40175. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40176. "./test-dir/file1");
  40177. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40178. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40179. NCONF_free(conf);
  40180. res = TEST_RES_CHECK(1);
  40181. #endif
  40182. return res;
  40183. }
  40184. #endif /* OPENSSL_ALL */
  40185. static int test_wolfSSL_EC_KEY_set_group(void)
  40186. {
  40187. int res = TEST_SKIPPED;
  40188. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  40189. defined(OPENSSL_EXTRA)
  40190. EC_KEY *key = NULL;
  40191. EC_GROUP *group = NULL;
  40192. const EC_GROUP *group2 = NULL;
  40193. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  40194. AssertNotNull(key = EC_KEY_new());
  40195. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  40196. AssertNotNull(group2 = EC_KEY_get0_group(key));
  40197. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  40198. EC_GROUP_free(group);
  40199. EC_KEY_free(key);
  40200. res = TEST_RES_CHECK(1);
  40201. #endif
  40202. return res;
  40203. }
  40204. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  40205. {
  40206. int res = TEST_SKIPPED;
  40207. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  40208. BIO* bio;
  40209. EC_KEY* key;
  40210. /* Error condition: NULL key. */
  40211. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  40212. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  40213. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  40214. /* Conversion form defaults to uncompressed. */
  40215. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  40216. #ifdef HAVE_COMP_KEY
  40217. /* Explicitly set to compressed. */
  40218. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  40219. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  40220. #endif
  40221. BIO_free(bio);
  40222. EC_KEY_free(key);
  40223. res = TEST_RES_CHECK(1);
  40224. #endif
  40225. return res;
  40226. }
  40227. static int test_wolfSSL_EC_KEY_print_fp(void)
  40228. {
  40229. int res = TEST_SKIPPED;
  40230. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  40231. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  40232. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  40233. !defined(NO_STDIO_FILESYSTEM)
  40234. EC_KEY* key = NULL;
  40235. /* Bad file pointer. */
  40236. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  40237. /* NULL key. */
  40238. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  40239. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  40240. /* Negative indent. */
  40241. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  40242. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40243. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40244. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40245. wolfSSL_EC_KEY_free(key);
  40246. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  40247. NID_X9_62_prime256v1)));
  40248. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40249. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  40250. wolfSSL_EC_KEY_free(key);
  40251. res = TEST_RES_CHECK(1);
  40252. #endif
  40253. return res;
  40254. }
  40255. static int test_wolfSSL_X509V3_EXT_get(void) {
  40256. int res = TEST_SKIPPED;
  40257. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40258. FILE* f;
  40259. int numOfExt =0;
  40260. int extNid = 0;
  40261. int i = 0;
  40262. WOLFSSL_X509* x509;
  40263. WOLFSSL_X509_EXTENSION* ext;
  40264. const WOLFSSL_v3_ext_method* method;
  40265. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40266. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40267. fclose(f);
  40268. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  40269. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40270. for (i = 0; i < numOfExt; i++) {
  40271. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40272. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  40273. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40274. AssertIntEQ(method->ext_nid, extNid);
  40275. }
  40276. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  40277. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40278. wolfSSL_X509_free(x509);
  40279. res = TEST_RES_CHECK(1);
  40280. #endif
  40281. return res;
  40282. }
  40283. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40284. {
  40285. int res = TEST_SKIPPED;
  40286. #ifdef OPENSSL_ALL
  40287. const char *ext_names[] = {
  40288. "subjectKeyIdentifier",
  40289. "authorityKeyIdentifier",
  40290. "subjectAltName",
  40291. "keyUsage",
  40292. };
  40293. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40294. int ext_nids[] = {
  40295. NID_subject_key_identifier,
  40296. NID_authority_key_identifier,
  40297. NID_subject_alt_name,
  40298. NID_key_usage,
  40299. };
  40300. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40301. const char *ext_values[] = {
  40302. "hash",
  40303. "hash",
  40304. "DNS:example.com, IP:127.0.0.1",
  40305. "digitalSignature,keyEncipherment,dataEncipherment",
  40306. };
  40307. size_t i;
  40308. X509_EXTENSION* ext;
  40309. X509* x509 = X509_new();
  40310. for (i = 0; i < ext_names_count; i++) {
  40311. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40312. AssertNotNull(ext);
  40313. X509_EXTENSION_free(ext);
  40314. }
  40315. for (i = 0; i < ext_nids_count; i++) {
  40316. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  40317. AssertNotNull(ext);
  40318. X509_EXTENSION_free(ext);
  40319. }
  40320. /* Test adding extension to X509 */
  40321. for (i = 0; i < ext_nids_count; i++) {
  40322. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40323. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40324. X509_EXTENSION_free(ext);
  40325. }
  40326. X509_free(x509);
  40327. res = TEST_RES_CHECK(1);
  40328. #endif
  40329. return res;
  40330. }
  40331. static int test_wolfSSL_X509V3_EXT(void) {
  40332. int res = TEST_SKIPPED;
  40333. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40334. FILE* f;
  40335. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  40336. char* str;
  40337. unsigned char* data;
  40338. const WOLFSSL_v3_ext_method* method;
  40339. WOLFSSL_X509* x509;
  40340. WOLFSSL_X509_EXTENSION* ext;
  40341. WOLFSSL_X509_EXTENSION* ext2;
  40342. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  40343. WOLFSSL_ASN1_STRING* asn1str;
  40344. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  40345. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  40346. WOLFSSL_BASIC_CONSTRAINTS* bc;
  40347. WOLFSSL_ACCESS_DESCRIPTION* ad;
  40348. WOLFSSL_GENERAL_NAME* gn;
  40349. /* Check NULL argument */
  40350. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40351. /* Using OCSP cert with X509V3 extensions */
  40352. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  40353. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40354. fclose(f);
  40355. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40356. /* Basic Constraints */
  40357. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40358. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40359. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40360. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40361. AssertIntEQ(bc->ca, 1);
  40362. AssertNull(bc->pathlen);
  40363. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40364. i++;
  40365. /* Subject Key Identifier */
  40366. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40367. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40368. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40369. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40370. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40371. asn1str));
  40372. X509_EXTENSION_free(ext2);
  40373. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40374. AssertNotNull(method->i2s);
  40375. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40376. wolfSSL_ASN1_STRING_free(asn1str);
  40377. actual = strcmp(str,
  40378. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40379. AssertIntEQ(actual, 0);
  40380. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40381. i++;
  40382. /* Authority Key Identifier */
  40383. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40384. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40385. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40386. AssertNotNull(aKeyId =
  40387. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  40388. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40389. AssertNotNull(asn1str = aKeyId->keyid);
  40390. AssertNotNull(str =
  40391. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  40392. actual = strcmp(str,
  40393. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40394. AssertIntEQ(actual, 0);
  40395. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40396. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40397. i++;
  40398. /* Key Usage */
  40399. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40400. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40401. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40402. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40403. #if defined(WOLFSSL_QT)
  40404. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40405. #else
  40406. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40407. #endif
  40408. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40409. #ifdef BIG_ENDIAN_ORDER
  40410. actual = data[1];
  40411. #else
  40412. actual = data[0];
  40413. #endif
  40414. AssertIntEQ(actual, expected);
  40415. wolfSSL_ASN1_STRING_free(asn1str);
  40416. #if 1
  40417. i++;
  40418. /* Authority Info Access */
  40419. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40420. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40421. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40422. AssertNotNull(aia =
  40423. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  40424. #if defined(WOLFSSL_QT)
  40425. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40426. #else
  40427. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40428. #endif
  40429. /* URI entry is an ACCESS_DESCRIPTION type */
  40430. #if defined(WOLFSSL_QT)
  40431. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40432. #else
  40433. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40434. #endif
  40435. AssertNotNull(adObj = ad->method);
  40436. /* Make sure nid is OCSP */
  40437. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40438. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40439. AssertNotNull(gn = ad->location);
  40440. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40441. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40442. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40443. #if defined(WOLFSSL_QT)
  40444. str = (char*)ASN1_STRING_get0_data(asn1str);
  40445. #else
  40446. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  40447. #endif
  40448. actual = strcmp(str, "http://127.0.0.1:22220");
  40449. AssertIntEQ(actual, 0);
  40450. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40451. #else
  40452. (void) aia; (void) ad; (void) adObj; (void) gn;
  40453. #endif
  40454. wolfSSL_X509_free(x509);
  40455. res = TEST_RES_CHECK(1);
  40456. #endif
  40457. return res;
  40458. }
  40459. static int test_wolfSSL_X509_get_extension_flags(void)
  40460. {
  40461. int res = TEST_SKIPPED;
  40462. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40463. XFILE f;
  40464. X509* x509;
  40465. unsigned int extFlags;
  40466. unsigned int keyUsageFlags;
  40467. unsigned int extKeyUsageFlags;
  40468. /* client-int-cert.pem has the following extension flags. */
  40469. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40470. /* and the following key usage flags. */
  40471. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40472. | KU_NON_REPUDIATION
  40473. | KU_KEY_ENCIPHERMENT;
  40474. /* and the following extended key usage flags. */
  40475. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40476. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  40477. AssertTrue(f != XBADFILE);
  40478. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40479. XFCLOSE(f);
  40480. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40481. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40482. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40483. X509_free(x509);
  40484. /* client-cert-ext.pem has the following extension flags. */
  40485. extFlags = EXFLAG_KUSAGE;
  40486. /* and the following key usage flags. */
  40487. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40488. | KU_KEY_CERT_SIGN
  40489. | KU_CRL_SIGN;
  40490. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  40491. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40492. XFCLOSE(f);
  40493. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40494. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40495. X509_free(x509);
  40496. res = TEST_RES_CHECK(1);
  40497. #endif /* OPENSSL_ALL */
  40498. return res;
  40499. }
  40500. static int test_wolfSSL_X509_get_ext(void)
  40501. {
  40502. int res = TEST_SKIPPED;
  40503. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40504. int ret = 0;
  40505. FILE* f;
  40506. WOLFSSL_X509* x509;
  40507. WOLFSSL_X509_EXTENSION* foundExtension;
  40508. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40509. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40510. fclose(f);
  40511. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40512. /* wolfSSL_X509_get_ext() valid input */
  40513. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40514. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  40515. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40516. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40517. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  40518. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40519. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40520. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  40521. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40522. wolfSSL_X509_free(x509);
  40523. res = TEST_RES_CHECK(1);
  40524. #endif
  40525. return res;
  40526. }
  40527. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40528. {
  40529. int res = TEST_SKIPPED;
  40530. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40531. int rc;
  40532. FILE* f;
  40533. WOLFSSL_X509* x509;
  40534. ASN1_OBJECT* obj = NULL;
  40535. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40536. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40537. fclose(f);
  40538. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40539. AssertIntGE(rc, 0);
  40540. /* Start search from last location (should fail) */
  40541. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  40542. AssertIntGE(rc, -1);
  40543. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  40544. AssertIntGE(rc, -1);
  40545. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  40546. AssertIntEQ(rc, -1);
  40547. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  40548. AssertIntEQ(rc, -1);
  40549. /* NID_ext_key_usage, check also its nid and oid */
  40550. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  40551. AssertIntGT(rc, -1);
  40552. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  40553. AssertIntEQ(obj->nid, NID_ext_key_usage);
  40554. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40555. wolfSSL_X509_free(x509);
  40556. res = TEST_RES_CHECK(1);
  40557. #endif
  40558. return res;
  40559. }
  40560. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40561. {
  40562. int res = TEST_SKIPPED;
  40563. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40564. int rc;
  40565. XFILE f;
  40566. WOLFSSL_X509* x509;
  40567. WOLFSSL_X509_EXTENSION* ext;
  40568. WOLFSSL_ASN1_STRING* sanString;
  40569. byte* sanDer;
  40570. const byte expectedDer[] = {
  40571. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  40572. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  40573. f = XFOPEN("./certs/server-cert.pem", "rb");
  40574. AssertTrue(f != XBADFILE);
  40575. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40576. fclose(f);
  40577. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  40578. AssertIntNE(rc, -1);
  40579. AssertNotNull(ext = X509_get_ext(x509, rc));
  40580. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  40581. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40582. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  40583. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40584. X509_free(x509);
  40585. res = TEST_RES_CHECK(1);
  40586. #endif
  40587. return res;
  40588. }
  40589. static int test_wolfSSL_X509_EXTENSION_new(void)
  40590. {
  40591. int res = TEST_SKIPPED;
  40592. #if defined (OPENSSL_ALL)
  40593. WOLFSSL_X509_EXTENSION* ext;
  40594. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40595. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40596. ext->obj->nid = WOLFSSL_SUCCESS;
  40597. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  40598. wolfSSL_X509_EXTENSION_free(ext);
  40599. res = TEST_RES_CHECK(1);
  40600. #endif
  40601. return res;
  40602. }
  40603. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40604. {
  40605. int res = TEST_SKIPPED;
  40606. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40607. WOLFSSL_X509* x509;
  40608. WOLFSSL_X509_EXTENSION* ext;
  40609. WOLFSSL_ASN1_OBJECT* o;
  40610. FILE* file;
  40611. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40612. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40613. fclose(file);
  40614. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  40615. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40616. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40617. AssertIntEQ(o->nid, 128);
  40618. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  40619. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40620. wolfSSL_X509_free(x509);
  40621. res = TEST_RES_CHECK(1);
  40622. #endif
  40623. return res;
  40624. }
  40625. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40626. {
  40627. int res = TEST_SKIPPED;
  40628. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40629. WOLFSSL_X509* x509;
  40630. WOLFSSL_X509_EXTENSION* ext;
  40631. WOLFSSL_ASN1_STRING* str;
  40632. FILE* file;
  40633. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40634. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40635. fclose(file);
  40636. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40637. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40638. wolfSSL_X509_free(x509);
  40639. res = TEST_RES_CHECK(1);
  40640. #endif
  40641. return res;
  40642. }
  40643. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40644. {
  40645. int res = TEST_SKIPPED;
  40646. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40647. WOLFSSL_X509* x509;
  40648. WOLFSSL_X509_EXTENSION* ext;
  40649. FILE* file;
  40650. int crit;
  40651. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40652. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40653. fclose(file);
  40654. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40655. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  40656. AssertIntEQ(crit, 0);
  40657. wolfSSL_X509_free(x509);
  40658. res = TEST_RES_CHECK(1);
  40659. #endif
  40660. return res;
  40661. }
  40662. static int test_wolfSSL_X509V3_EXT_print(void)
  40663. {
  40664. int res = TEST_SKIPPED;
  40665. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40666. !defined(NO_RSA)
  40667. {
  40668. FILE* f;
  40669. WOLFSSL_X509* x509;
  40670. X509_EXTENSION * ext = NULL;
  40671. int loc;
  40672. BIO *bio = NULL;
  40673. AssertNotNull(f = fopen(svrCertFile, "rb"));
  40674. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40675. fclose(f);
  40676. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40677. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40678. AssertIntGT(loc, -1);
  40679. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40680. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40681. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  40682. AssertIntGT(loc, -1);
  40683. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40684. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40685. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  40686. AssertIntGT(loc, -1);
  40687. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40688. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40689. wolfSSL_BIO_free(bio);
  40690. wolfSSL_X509_free(x509);
  40691. }
  40692. {
  40693. X509 *x509;
  40694. BIO *bio;
  40695. X509_EXTENSION *ext;
  40696. unsigned int i;
  40697. unsigned int idx;
  40698. /* Some NIDs to test with */
  40699. int nids[] = {
  40700. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40701. * string, which messes up X509V3_EXT_print */
  40702. /* NID_ext_key_usage, */
  40703. NID_subject_alt_name,
  40704. };
  40705. int* n;
  40706. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  40707. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40708. WOLFSSL_FILETYPE_PEM));
  40709. fprintf(stderr, "\nPrinting extension values:\n");
  40710. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40711. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40712. * update the index */
  40713. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40714. AssertNotNull(ext = X509_get_ext(x509, idx));
  40715. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40716. fprintf(stderr, "\n");
  40717. }
  40718. BIO_free(bio);
  40719. X509_free(x509);
  40720. }
  40721. res = TEST_RES_CHECK(1);
  40722. #endif
  40723. return res;
  40724. }
  40725. static int test_wolfSSL_X509_cmp(void)
  40726. {
  40727. int res = TEST_SKIPPED;
  40728. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40729. FILE* file1;
  40730. FILE* file2;
  40731. WOLFSSL_X509* cert1;
  40732. WOLFSSL_X509* cert2;
  40733. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40734. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40735. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40736. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40737. fclose(file1);
  40738. fclose(file2);
  40739. /* wolfSSL_X509_cmp() testing matching certs */
  40740. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40741. /* wolfSSL_X509_cmp() testing mismatched certs */
  40742. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40743. /* wolfSSL_X509_cmp() testing NULL, valid args */
  40744. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40745. /* wolfSSL_X509_cmp() testing valid, NULL args */
  40746. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40747. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  40748. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40749. wolfSSL_X509_free(cert1);
  40750. wolfSSL_X509_free(cert2);
  40751. res = TEST_RES_CHECK(1);
  40752. #endif
  40753. return res;
  40754. }
  40755. static int test_wolfSSL_PKEY_up_ref(void)
  40756. {
  40757. int res = TEST_SKIPPED;
  40758. #if defined(OPENSSL_ALL)
  40759. EVP_PKEY* pkey;
  40760. pkey = EVP_PKEY_new();
  40761. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40762. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40763. EVP_PKEY_free(pkey);
  40764. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40765. EVP_PKEY_free(pkey);
  40766. EVP_PKEY_free(pkey);
  40767. res = TEST_RES_CHECK(1);
  40768. #endif
  40769. return res;
  40770. }
  40771. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40772. {
  40773. int res = TEST_SKIPPED;
  40774. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40775. EVP_PKEY* pkey;
  40776. const unsigned char* p;
  40777. unsigned char* der = NULL;
  40778. int derLen;
  40779. p = client_keypub_der_2048;
  40780. /* Check that key can be successfully decoded. */
  40781. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40782. sizeof_client_keypub_der_2048));
  40783. /* Check that key can be successfully encoded. */
  40784. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40785. /* Ensure that the encoded version matches the original. */
  40786. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  40787. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40788. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40789. EVP_PKEY_free(pkey);
  40790. res = TEST_RES_CHECK(1);
  40791. #endif
  40792. return res;
  40793. }
  40794. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40795. {
  40796. int res = TEST_SKIPPED;
  40797. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40798. DSA* dsa;
  40799. char file[] = "./certs/dsaparams.der";
  40800. XFILE f;
  40801. int derInLen;
  40802. byte* derIn;
  40803. int derOutLen;
  40804. byte* derOut = NULL;
  40805. f = XFOPEN(file, "rb");
  40806. AssertTrue(f != XBADFILE);
  40807. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  40808. derInLen = (int)XFTELL(f);
  40809. XREWIND(f);
  40810. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  40811. DYNAMIC_TYPE_TMP_BUFFER));
  40812. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  40813. XFCLOSE(f);
  40814. /* Check that params can be successfully decoded. */
  40815. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  40816. /* Check that params can be successfully encoded. */
  40817. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  40818. /* Ensure that the encoded version matches the original. */
  40819. AssertIntEQ(derInLen, derOutLen);
  40820. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  40821. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40822. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40823. DSA_free(dsa);
  40824. res = TEST_RES_CHECK(1);
  40825. #endif
  40826. return res;
  40827. }
  40828. static int test_wolfSSL_i2d_PrivateKey(void)
  40829. {
  40830. int res = TEST_SKIPPED;
  40831. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  40832. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40833. {
  40834. EVP_PKEY* pkey;
  40835. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  40836. unsigned char buf[FOURK_BUF];
  40837. unsigned char* pt = NULL;
  40838. int bufSz;
  40839. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  40840. (long)sizeof_server_key_der_2048));
  40841. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  40842. pt = buf;
  40843. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  40844. AssertIntNE((pt - buf), 0);
  40845. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  40846. EVP_PKEY_free(pkey);
  40847. }
  40848. #endif
  40849. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40850. {
  40851. EVP_PKEY* pkey;
  40852. const unsigned char* client_key =
  40853. (const unsigned char*)ecc_clikey_der_256;
  40854. unsigned char buf[FOURK_BUF];
  40855. unsigned char* pt = NULL;
  40856. int bufSz;
  40857. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  40858. sizeof_ecc_clikey_der_256)));
  40859. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  40860. pt = buf;
  40861. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  40862. AssertIntNE((pt - buf), 0);
  40863. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  40864. EVP_PKEY_free(pkey);
  40865. }
  40866. #endif
  40867. res = TEST_RES_CHECK(1);
  40868. #endif
  40869. return res;
  40870. }
  40871. static int test_wolfSSL_OCSP_id_get0_info(void)
  40872. {
  40873. int res = TEST_SKIPPED;
  40874. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  40875. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40876. X509* cert;
  40877. X509* issuer;
  40878. OCSP_CERTID* id;
  40879. OCSP_CERTID* id2;
  40880. ASN1_STRING* name = NULL;
  40881. ASN1_OBJECT* pmd = NULL;
  40882. ASN1_STRING* keyHash = NULL;
  40883. ASN1_INTEGER* serial = NULL;
  40884. ASN1_INTEGER* x509Int;
  40885. AssertNotNull(cert =
  40886. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  40887. AssertNotNull(issuer =
  40888. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  40889. id = OCSP_cert_to_id(NULL, cert, issuer);
  40890. AssertNotNull(id);
  40891. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  40892. AssertNotNull(id2);
  40893. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  40894. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  40895. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  40896. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  40897. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  40898. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  40899. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  40900. AssertNotNull(serial);
  40901. /* compare serial number to one in cert, should be equal */
  40902. x509Int = X509_get_serialNumber(cert);
  40903. AssertNotNull(x509Int);
  40904. AssertIntEQ(x509Int->length, serial->length);
  40905. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  40906. /* test OCSP_id_cmp */
  40907. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  40908. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  40909. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  40910. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  40911. id->issuerHash[0] = ~id->issuerHash[0];
  40912. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  40913. OCSP_CERTID_free(id);
  40914. OCSP_CERTID_free(id2);
  40915. X509_free(cert); /* free's x509Int */
  40916. X509_free(issuer);
  40917. res = TEST_RES_CHECK(1);
  40918. #endif
  40919. return res;
  40920. }
  40921. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  40922. {
  40923. int res = TEST_SKIPPED;
  40924. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40925. WOLFSSL_OCSP_CERTID certId;
  40926. byte* targetBuffer;
  40927. byte* beginTargetBuffer;
  40928. /* OCSP CertID bytes taken from PCAP */
  40929. byte rawCertId[] = {
  40930. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40931. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40932. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40933. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40934. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40935. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40936. 0xcf, 0xbc, 0x91
  40937. };
  40938. int ret, i;
  40939. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  40940. certId.rawCertId = rawCertId;
  40941. certId.rawCertIdSize = sizeof(rawCertId);
  40942. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40943. beginTargetBuffer = targetBuffer;
  40944. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40945. /* If target buffer is not null, function increments targetBuffer to point
  40946. just past the end of the encoded data. */
  40947. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  40948. /* Function returns the size of the encoded data. */
  40949. AssertIntEQ(ret, sizeof(rawCertId));
  40950. for (i = 0; i < ret; ++i)
  40951. {
  40952. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  40953. }
  40954. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40955. targetBuffer = NULL;
  40956. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40957. /* If target buffer is null, function allocates memory for a buffer and
  40958. copies the encoded data into it. targetBuffer then points to the start of
  40959. this newly allocate buffer. */
  40960. AssertIntEQ(ret, sizeof(rawCertId));
  40961. for (i = 0; i < ret; ++i)
  40962. {
  40963. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  40964. }
  40965. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  40966. res = TEST_RES_CHECK(1);
  40967. #endif
  40968. return res;
  40969. }
  40970. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  40971. {
  40972. int res = TEST_SKIPPED;
  40973. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40974. WOLFSSL_OCSP_CERTID* certId;
  40975. WOLFSSL_OCSP_CERTID* certIdBad;
  40976. const unsigned char* rawCertIdPtr;
  40977. const unsigned char rawCertId[] = {
  40978. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40979. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40980. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40981. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40982. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40983. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40984. 0xcf, 0xbc, 0x91
  40985. };
  40986. rawCertIdPtr = &rawCertId[0];
  40987. /* If the cert ID is NULL the function should allocate it and copy the
  40988. * data to it. */
  40989. certId = NULL;
  40990. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  40991. AssertNotNull(certId);
  40992. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  40993. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  40994. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  40995. /* If the cert ID is not NULL the function will just copy the data to it. */
  40996. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  40997. DYNAMIC_TYPE_TMP_BUFFER);
  40998. AssertNotNull(certId);
  40999. XMEMSET(certId, 0, sizeof(*certId));
  41000. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  41001. rawCertIdPtr = &rawCertId[0];
  41002. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  41003. AssertNotNull(certId);
  41004. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41005. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41006. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41007. /* The below tests should fail when passed bad parameters. NULL should
  41008. * always be returned. */
  41009. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  41010. AssertNull(certIdBad);
  41011. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  41012. AssertNull(certIdBad);
  41013. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  41014. AssertNull(certIdBad);
  41015. res = TEST_RES_CHECK(1);
  41016. #endif
  41017. return res;
  41018. }
  41019. static int test_wolfSSL_OCSP_id_cmp(void)
  41020. {
  41021. int res = TEST_SKIPPED;
  41022. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41023. OCSP_CERTID id1;
  41024. OCSP_CERTID id2;
  41025. XMEMSET(&id1, 0, sizeof(id1));
  41026. XMEMSET(&id2, 0, sizeof(id2));
  41027. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  41028. res = TEST_RES_CHECK(1);
  41029. #endif
  41030. return res;
  41031. }
  41032. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  41033. {
  41034. int res = TEST_SKIPPED;
  41035. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41036. WOLFSSL_OCSP_SINGLERESP single;
  41037. const WOLFSSL_OCSP_CERTID* certId;
  41038. XMEMSET(&single, 0, sizeof(single));
  41039. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  41040. AssertPtrEq(&single, certId);
  41041. res = TEST_RES_CHECK(1);
  41042. #endif
  41043. return res;
  41044. }
  41045. static int test_wolfSSL_OCSP_single_get0_status(void)
  41046. {
  41047. int res = TEST_SKIPPED;
  41048. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41049. WOLFSSL_OCSP_SINGLERESP single;
  41050. CertStatus certStatus;
  41051. WOLFSSL_ASN1_TIME* thisDate;
  41052. WOLFSSL_ASN1_TIME* nextDate;
  41053. int ret, i;
  41054. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41055. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  41056. /* Fill the date fields with some dummy data. */
  41057. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  41058. certStatus.thisDateParsed.data[i] = i;
  41059. certStatus.nextDateParsed.data[i] = i;
  41060. }
  41061. certStatus.status = CERT_GOOD;
  41062. single.status = &certStatus;
  41063. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  41064. &nextDate);
  41065. AssertIntEQ(ret, CERT_GOOD);
  41066. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  41067. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  41068. res = TEST_RES_CHECK(1);
  41069. #endif
  41070. return res;
  41071. }
  41072. static int test_wolfSSL_OCSP_resp_count(void)
  41073. {
  41074. int res = TEST_SKIPPED;
  41075. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41076. WOLFSSL_OCSP_BASICRESP basicResp;
  41077. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41078. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41079. int count;
  41080. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41081. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41082. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41083. count = wolfSSL_OCSP_resp_count(&basicResp);
  41084. AssertIntEQ(count, 0);
  41085. basicResp.single = &singleRespOne;
  41086. count = wolfSSL_OCSP_resp_count(&basicResp);
  41087. AssertIntEQ(count, 1);
  41088. singleRespOne.next = &singleRespTwo;
  41089. count = wolfSSL_OCSP_resp_count(&basicResp);
  41090. AssertIntEQ(count, 2);
  41091. res = TEST_RES_CHECK(1);
  41092. #endif
  41093. return res;
  41094. }
  41095. static int test_wolfSSL_OCSP_resp_get0(void)
  41096. {
  41097. int res = TEST_SKIPPED;
  41098. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41099. WOLFSSL_OCSP_BASICRESP basicResp;
  41100. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41101. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41102. WOLFSSL_OCSP_SINGLERESP* ret;
  41103. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41104. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41105. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41106. basicResp.single = &singleRespOne;
  41107. singleRespOne.next = &singleRespTwo;
  41108. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  41109. AssertPtrEq(ret, &singleRespOne);
  41110. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  41111. AssertPtrEq(ret, &singleRespTwo);
  41112. res = TEST_RES_CHECK(1);
  41113. #endif
  41114. return res;
  41115. }
  41116. static int test_wolfSSL_EVP_PKEY_derive(void)
  41117. {
  41118. int res = TEST_SKIPPED;
  41119. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41120. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41121. EVP_PKEY_CTX *ctx;
  41122. unsigned char *skey;
  41123. size_t skeylen;
  41124. EVP_PKEY *pkey, *peerkey;
  41125. const unsigned char* key;
  41126. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41127. /* DH */
  41128. key = dh_key_der_2048;
  41129. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41130. sizeof_dh_key_der_2048)));
  41131. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41132. key = dh_key_der_2048;
  41133. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41134. sizeof_dh_key_der_2048)));
  41135. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41136. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41137. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41138. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41139. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41140. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41141. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41142. EVP_PKEY_CTX_free(ctx);
  41143. EVP_PKEY_free(peerkey);
  41144. EVP_PKEY_free(pkey);
  41145. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41146. #endif
  41147. #ifdef HAVE_ECC
  41148. /* ECDH */
  41149. key = ecc_clikey_der_256;
  41150. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41151. sizeof_ecc_clikey_der_256)));
  41152. key = ecc_clikeypub_der_256;
  41153. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41154. sizeof_ecc_clikeypub_der_256)));
  41155. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41156. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41157. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41158. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41159. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41160. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41161. EVP_PKEY_CTX_free(ctx);
  41162. EVP_PKEY_free(peerkey);
  41163. EVP_PKEY_free(pkey);
  41164. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41165. #endif /* HAVE_ECC */
  41166. res = TEST_RES_CHECK(1);
  41167. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41168. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41169. return res;
  41170. }
  41171. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41172. {
  41173. int res = TEST_SKIPPED;
  41174. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41175. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41176. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41177. int ret;
  41178. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41179. int pwdlen = sizeof(pwd);
  41180. const byte salt[] = {'N','a','C','l'};
  41181. int saltlen = sizeof(salt);
  41182. byte key[80];
  41183. word64 numOvr32 = (word64)INT32_MAX + 1;
  41184. /* expected derived key for N:16, r:1, p:1 */
  41185. const byte expectedKey[] = {
  41186. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41187. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41188. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41189. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41190. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41191. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41192. 0xE7, 0xE9, 0xC0, 0x9A};
  41193. /* N r p mx key keylen */
  41194. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41195. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  41196. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41197. AssertIntEQ(ret, 0); /* N must be power of 2 */
  41198. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41199. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  41200. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41201. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  41202. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41203. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  41204. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41205. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  41206. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41207. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  41208. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41209. AssertIntEQ(ret, 1); /* should succeed */
  41210. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41211. key, 64);
  41212. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41213. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41214. key, 64);
  41215. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41216. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41217. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41218. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41219. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41220. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41221. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41222. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41223. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41224. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41225. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41226. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41227. AssertIntEQ(ret, 1);
  41228. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41229. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41230. res = TEST_RES_CHECK(1);
  41231. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41232. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41233. return res;
  41234. }
  41235. static int test_wolfSSL_EC_get_builtin_curves(void)
  41236. {
  41237. int res = TEST_SKIPPED;
  41238. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  41239. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41240. EC_builtin_curve* curves = NULL;
  41241. size_t crv_len = 0;
  41242. size_t i = 0;
  41243. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  41244. AssertNotNull(curves = (EC_builtin_curve*)
  41245. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  41246. DYNAMIC_TYPE_TMP_BUFFER));
  41247. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  41248. for (i = 0; i < crv_len; i++)
  41249. {
  41250. if (curves[i].comment != NULL)
  41251. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  41252. }
  41253. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41254. res = TEST_RES_CHECK(1);
  41255. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41256. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  41257. return res;
  41258. }
  41259. static int test_no_op_functions(void)
  41260. {
  41261. int res = TEST_SKIPPED;
  41262. #if defined(OPENSSL_EXTRA)
  41263. /* this makes sure wolfSSL can compile and run these no-op functions */
  41264. SSL_load_error_strings();
  41265. ENGINE_load_builtin_engines();
  41266. OpenSSL_add_all_ciphers();
  41267. AssertIntEQ(CRYPTO_malloc_init(), 0);
  41268. res = TEST_RES_CHECK(1);
  41269. #endif
  41270. return res;
  41271. }
  41272. static int test_wolfSSL_CRYPTO_memcmp(void)
  41273. {
  41274. int res = TEST_SKIPPED;
  41275. #ifdef OPENSSL_EXTRA
  41276. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41277. "implementation of TLS/SSL for embedded devices to the cloud.";
  41278. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41279. "implementation of TLS/SSL for embedded devices to the cloud.";
  41280. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41281. "implementation of TLS/SSL for embedded devices to the cloud!";
  41282. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41283. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41284. res = TEST_RES_CHECK(1);
  41285. #endif
  41286. return res;
  41287. }
  41288. /*----------------------------------------------------------------------------*
  41289. | wolfCrypt ASN
  41290. *----------------------------------------------------------------------------*/
  41291. static int test_wc_CreateEncryptedPKCS8Key(void)
  41292. {
  41293. int res = TEST_SKIPPED;
  41294. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41295. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41296. WC_RNG rng;
  41297. byte* encKey = NULL;
  41298. word32 encKeySz = 0;
  41299. word32 decKeySz = 0;
  41300. const char password[] = "Lorem ipsum dolor sit amet";
  41301. word32 passwordSz = (word32)XSTRLEN(password);
  41302. word32 tradIdx = 0;
  41303. AssertIntEQ(wc_InitRng(&rng), 0);
  41304. /* Call with NULL for out buffer to get necessary length. */
  41305. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41306. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41307. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41308. LENGTH_ONLY_E);
  41309. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41310. DYNAMIC_TYPE_TMP_BUFFER));
  41311. /* Call with the allocated out buffer. */
  41312. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41313. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41314. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41315. 0);
  41316. /* Decrypt the encrypted PKCS8 key we just made. */
  41317. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41318. passwordSz)), 0);
  41319. /* encKey now holds the decrypted key (decrypted in place). */
  41320. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41321. /* Check that the decrypted key matches the key prior to encryption. */
  41322. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41323. sizeof_server_key_der_2048), 0);
  41324. if (encKey != NULL)
  41325. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41326. wc_FreeRng(&rng);
  41327. res = TEST_RES_CHECK(1);
  41328. #endif
  41329. return res;
  41330. }
  41331. static int test_wc_GetPkcs8TraditionalOffset(void)
  41332. {
  41333. int res = TEST_SKIPPED;
  41334. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41335. int length, derSz;
  41336. word32 inOutIdx;
  41337. const char* path = "./certs/server-keyPkcs8.der";
  41338. XFILE file;
  41339. byte der[2048];
  41340. file = XFOPEN(path, "rb");
  41341. AssertTrue(file != XBADFILE);
  41342. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  41343. XFCLOSE(file);
  41344. /* valid case */
  41345. inOutIdx = 0;
  41346. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41347. AssertIntGT(length, 0);
  41348. /* inOutIdx > sz */
  41349. inOutIdx = 4000;
  41350. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41351. AssertIntEQ(length, BAD_FUNC_ARG);
  41352. /* null input */
  41353. inOutIdx = 0;
  41354. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  41355. AssertIntEQ(length, BAD_FUNC_ARG);
  41356. /* invalid input, fill buffer with 1's */
  41357. XMEMSET(der, 1, sizeof(der));
  41358. inOutIdx = 0;
  41359. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41360. AssertIntEQ(length, ASN_PARSE_E);
  41361. res = TEST_RES_CHECK(1);
  41362. #endif /* NO_ASN */
  41363. return res;
  41364. }
  41365. static int test_wc_SetSubjectRaw(void)
  41366. {
  41367. int res = TEST_SKIPPED;
  41368. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41369. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41370. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41371. WOLFSSL_X509* x509;
  41372. int peerCertSz;
  41373. const byte* peerCertBuf;
  41374. Cert forgedCert;
  41375. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41376. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41377. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41378. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41379. wolfSSL_FreeX509(x509);
  41380. res = TEST_RES_CHECK(1);
  41381. #endif
  41382. return res;
  41383. }
  41384. static int test_wc_GetSubjectRaw(void)
  41385. {
  41386. int res = TEST_SKIPPED;
  41387. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41388. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41389. Cert cert;
  41390. byte *subjectRaw;
  41391. AssertIntEQ(0, wc_InitCert(&cert));
  41392. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41393. res = TEST_RES_CHECK(1);
  41394. #endif
  41395. return res;
  41396. }
  41397. static int test_wc_SetIssuerRaw(void)
  41398. {
  41399. int res = TEST_SKIPPED;
  41400. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41401. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41402. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41403. WOLFSSL_X509* x509;
  41404. int peerCertSz;
  41405. const byte* peerCertBuf;
  41406. Cert forgedCert;
  41407. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41408. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41409. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41410. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41411. wolfSSL_FreeX509(x509);
  41412. res = TEST_RES_CHECK(1);
  41413. #endif
  41414. return res;
  41415. }
  41416. static int test_wc_SetIssueBuffer(void)
  41417. {
  41418. int res = TEST_SKIPPED;
  41419. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41420. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41421. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41422. WOLFSSL_X509* x509;
  41423. int peerCertSz;
  41424. const byte* peerCertBuf;
  41425. Cert forgedCert;
  41426. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41427. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41428. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41429. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41430. wolfSSL_FreeX509(x509);
  41431. res = TEST_RES_CHECK(1);
  41432. #endif
  41433. return res;
  41434. }
  41435. /*
  41436. * Testing wc_SetSubjectKeyId
  41437. */
  41438. static int test_wc_SetSubjectKeyId(void)
  41439. {
  41440. int res = TEST_SKIPPED;
  41441. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41442. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41443. Cert cert;
  41444. const char* file = "certs/ecc-client-keyPub.pem";
  41445. AssertIntEQ(0, wc_InitCert(&cert));
  41446. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41447. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41448. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41449. res = TEST_RES_CHECK(1);
  41450. #endif
  41451. return res;
  41452. } /* END test_wc_SetSubjectKeyId */
  41453. /*
  41454. * Testing wc_SetSubject
  41455. */
  41456. static int test_wc_SetSubject(void)
  41457. {
  41458. int res = TEST_SKIPPED;
  41459. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41460. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41461. Cert cert;
  41462. const char* file = "./certs/ca-ecc-cert.pem";
  41463. AssertIntEQ(0, wc_InitCert(&cert));
  41464. AssertIntEQ(0, wc_SetSubject(&cert, file));
  41465. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41466. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41467. res = TEST_RES_CHECK(1);
  41468. #endif
  41469. return res;
  41470. } /* END test_wc_SetSubject */
  41471. static int test_CheckCertSignature(void)
  41472. {
  41473. int res = TEST_SKIPPED;
  41474. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41475. WOLFSSL_CERT_MANAGER* cm = NULL;
  41476. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41477. FILE* fp;
  41478. byte cert[4096];
  41479. int certSz;
  41480. #endif
  41481. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41482. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41483. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41484. #ifndef NO_RSA
  41485. #ifdef USE_CERT_BUFFERS_1024
  41486. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41487. sizeof_server_cert_der_1024, NULL, cm));
  41488. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41489. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41490. WOLFSSL_FILETYPE_ASN1));
  41491. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41492. sizeof_server_cert_der_1024, NULL, cm));
  41493. #elif defined(USE_CERT_BUFFERS_2048)
  41494. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41495. sizeof_server_cert_der_2048, NULL, cm));
  41496. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41497. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41498. WOLFSSL_FILETYPE_ASN1));
  41499. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41500. sizeof_server_cert_der_2048, NULL, cm));
  41501. #endif
  41502. #endif
  41503. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41504. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41505. sizeof_serv_ecc_der_256, NULL, cm));
  41506. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41507. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41508. WOLFSSL_FILETYPE_ASN1));
  41509. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41510. NULL, cm));
  41511. #endif
  41512. #if !defined(NO_FILESYSTEM)
  41513. wolfSSL_CertManagerFree(cm);
  41514. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41515. #ifndef NO_RSA
  41516. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  41517. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41518. XFCLOSE(fp);
  41519. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41520. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41521. "./certs/ca-cert.pem", NULL));
  41522. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41523. #endif
  41524. #ifdef HAVE_ECC
  41525. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  41526. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41527. XFCLOSE(fp);
  41528. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41529. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41530. "./certs/ca-ecc-cert.pem", NULL));
  41531. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41532. #endif
  41533. #endif
  41534. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41535. (void)fp;
  41536. (void)cert;
  41537. (void)certSz;
  41538. #endif
  41539. wolfSSL_CertManagerFree(cm);
  41540. res = TEST_RES_CHECK(1);
  41541. #endif
  41542. return res;
  41543. }
  41544. static int test_wc_ParseCert(void)
  41545. {
  41546. int res = TEST_SKIPPED;
  41547. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41548. DecodedCert decodedCert;
  41549. const byte* rawCert = client_cert_der_2048;
  41550. const int rawCertSize = sizeof_client_cert_der_2048;
  41551. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41552. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41553. #ifndef IGNORE_NAME_CONSTRAINTS
  41554. /* check that the subjects emailAddress was not put in the alt name list */
  41555. AssertNotNull(decodedCert.subjectEmail);
  41556. AssertNull(decodedCert.altEmailNames);
  41557. #endif
  41558. wc_FreeDecodedCert(&decodedCert);
  41559. res = TEST_RES_CHECK(1);
  41560. #endif
  41561. return res;
  41562. }
  41563. static int test_MakeCertWithPathLen(void)
  41564. {
  41565. int res = TEST_SKIPPED;
  41566. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41567. const byte expectedPathLen = 7;
  41568. Cert cert;
  41569. DecodedCert decodedCert;
  41570. byte der[FOURK_BUF];
  41571. int derSize = 0;
  41572. WC_RNG rng;
  41573. ecc_key key;
  41574. AssertIntEQ(wc_InitRng(&rng), 0);
  41575. AssertIntEQ(wc_ecc_init(&key), 0);
  41576. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41577. AssertIntEQ(wc_InitCert(&cert), 0);
  41578. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41579. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41580. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41581. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41582. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41583. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  41584. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  41585. cert.selfSigned = 1;
  41586. cert.isCA = 1;
  41587. cert.pathLen = expectedPathLen;
  41588. cert.pathLenSet = 1;
  41589. cert.sigType = CTC_SHA256wECDSA;
  41590. #ifdef WOLFSSL_CERT_EXT
  41591. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41592. #endif
  41593. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41594. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  41595. &key, &rng);
  41596. AssertIntGE(derSize, 0);
  41597. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41598. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41599. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  41600. wc_FreeDecodedCert(&decodedCert);
  41601. AssertIntEQ(wc_ecc_free(&key), 0);
  41602. AssertIntEQ(wc_FreeRng(&rng), 0);
  41603. res = TEST_RES_CHECK(1);
  41604. #endif
  41605. return res;
  41606. }
  41607. /*----------------------------------------------------------------------------*
  41608. | wolfCrypt ECC
  41609. *----------------------------------------------------------------------------*/
  41610. static int test_wc_ecc_get_curve_size_from_name(void)
  41611. {
  41612. int res = TEST_SKIPPED;
  41613. #ifdef HAVE_ECC
  41614. int ret;
  41615. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41616. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  41617. AssertIntEQ(ret, 32);
  41618. #endif
  41619. /* invalid case */
  41620. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  41621. AssertIntEQ(ret, -1);
  41622. /* NULL input */
  41623. ret = wc_ecc_get_curve_size_from_name(NULL);
  41624. AssertIntEQ(ret, BAD_FUNC_ARG);
  41625. res = TEST_RES_CHECK(1);
  41626. #endif /* HAVE_ECC */
  41627. return res;
  41628. }
  41629. static int test_wc_ecc_get_curve_id_from_name(void)
  41630. {
  41631. int res = TEST_SKIPPED;
  41632. #ifdef HAVE_ECC
  41633. int id;
  41634. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41635. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41636. AssertIntEQ(id, ECC_SECP256R1);
  41637. #endif
  41638. /* invalid case */
  41639. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  41640. AssertIntEQ(id, -1);
  41641. /* NULL input */
  41642. id = wc_ecc_get_curve_id_from_name(NULL);
  41643. AssertIntEQ(id, BAD_FUNC_ARG);
  41644. res = TEST_RES_CHECK(1);
  41645. #endif /* HAVE_ECC */
  41646. return res;
  41647. }
  41648. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41649. !defined(HAVE_SELFTEST) && \
  41650. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41651. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  41652. {
  41653. int id;
  41654. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41655. int curve_id;
  41656. ecc_key* key;
  41657. const ecc_set_type* params;
  41658. int ret;
  41659. #endif
  41660. WOLFSSL_EC_KEY *ecKey = NULL;
  41661. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41662. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41663. AssertIntEQ(id, ECC_SECP256R1);
  41664. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41665. AssertNotNull(ecKey);
  41666. ret = EC_KEY_generate_key(ecKey);
  41667. if (ret == 0) {
  41668. /* normal test */
  41669. key = (ecc_key*)ecKey->internal;
  41670. params = key->dp;
  41671. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  41672. AssertIntEQ(curve_id, id);
  41673. }
  41674. #endif
  41675. /* invalid case, NULL input*/
  41676. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  41677. AssertIntEQ(id, BAD_FUNC_ARG);
  41678. wolfSSL_EC_KEY_free(ecKey);
  41679. return TEST_RES_CHECK(1);
  41680. }
  41681. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41682. static int test_wc_ecc_get_curve_id_from_params(void)
  41683. {
  41684. int res = TEST_SKIPPED;
  41685. #ifdef HAVE_ECC
  41686. int id;
  41687. const byte prime[] =
  41688. {
  41689. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41691. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41692. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  41693. };
  41694. const byte primeInvalid[] =
  41695. {
  41696. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41698. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41699. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  41700. };
  41701. const byte Af[] =
  41702. {
  41703. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41704. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41705. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41706. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  41707. };
  41708. const byte Bf[] =
  41709. {
  41710. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  41711. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  41712. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  41713. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  41714. };
  41715. const byte order[] =
  41716. {
  41717. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  41718. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41719. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  41720. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  41721. };
  41722. const byte Gx[] =
  41723. {
  41724. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  41725. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  41726. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  41727. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  41728. };
  41729. const byte Gy[] =
  41730. {
  41731. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  41732. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  41733. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  41734. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  41735. };
  41736. int cofactor = 1;
  41737. int fieldSize = 256;
  41738. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41739. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  41740. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41741. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41742. AssertIntEQ(id, ECC_SECP256R1);
  41743. #endif
  41744. /* invalid case, fieldSize = 0 */
  41745. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  41746. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41747. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41748. AssertIntEQ(id, ECC_CURVE_INVALID);
  41749. /* invalid case, NULL prime */
  41750. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  41751. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41752. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41753. AssertIntEQ(id, BAD_FUNC_ARG);
  41754. /* invalid case, invalid prime */
  41755. id = wc_ecc_get_curve_id_from_params(fieldSize,
  41756. primeInvalid, sizeof(primeInvalid),
  41757. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41758. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41759. AssertIntEQ(id, ECC_CURVE_INVALID);
  41760. res = TEST_RES_CHECK(1);
  41761. #endif
  41762. return res;
  41763. }
  41764. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  41765. {
  41766. int res = TEST_SKIPPED;
  41767. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41768. !defined(HAVE_FAST_RSA)
  41769. WOLFSSL_RSA* rsa = NULL;
  41770. WOLFSSL_EVP_PKEY* pkey = NULL;
  41771. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41772. const char* in = "What is easy to do is easy not to do.";
  41773. size_t inlen = XSTRLEN(in);
  41774. size_t outEncLen = 0;
  41775. byte* outEnc = NULL;
  41776. byte* outDec = NULL;
  41777. size_t outDecLen = 0;
  41778. size_t rsaKeySz = 2048/8; /* Bytes */
  41779. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41780. byte* inTmp = NULL;
  41781. byte* outEncTmp = NULL;
  41782. byte* outDecTmp = NULL;
  41783. #endif
  41784. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41785. XMEMSET(outEnc, 0, rsaKeySz);
  41786. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41787. XMEMSET(outDec, 0, rsaKeySz);
  41788. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41789. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41790. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41791. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41792. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41793. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41794. WOLFSSL_SUCCESS);
  41795. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  41796. since ctx uses it.*/
  41797. AssertIntEQ(pkey->references, 2);
  41798. EVP_PKEY_free(pkey);
  41799. AssertIntEQ(pkey->references, 1);
  41800. /* Encrypt data */
  41801. /* Check that we can get the required output buffer length by passing in a
  41802. * NULL output buffer. */
  41803. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  41804. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41805. AssertIntEQ(rsaKeySz, outEncLen);
  41806. /* Now do the actual encryption. */
  41807. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  41808. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41809. /* Decrypt data */
  41810. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41811. /* Check that we can get the required output buffer length by passing in a
  41812. * NULL output buffer. */
  41813. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  41814. WOLFSSL_SUCCESS);
  41815. AssertIntEQ(rsaKeySz, outDecLen);
  41816. /* Now do the actual decryption. */
  41817. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  41818. WOLFSSL_SUCCESS);
  41819. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  41820. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41821. /* The input length must be the same size as the RSA key.*/
  41822. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41823. XMEMSET(inTmp, 9, rsaKeySz);
  41824. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41825. XMEMSET(outEncTmp, 0, rsaKeySz);
  41826. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41827. XMEMSET(outDecTmp, 0, rsaKeySz);
  41828. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41829. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41830. WOLFSSL_SUCCESS);
  41831. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  41832. WOLFSSL_SUCCESS);
  41833. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41834. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  41835. WOLFSSL_SUCCESS);
  41836. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  41837. #endif
  41838. EVP_PKEY_CTX_free(ctx);
  41839. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41840. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41841. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41842. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41843. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41844. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41845. #endif
  41846. res = TEST_RES_CHECK(1);
  41847. #endif
  41848. return res;
  41849. }
  41850. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  41851. {
  41852. int res = TEST_SKIPPED;
  41853. #if defined(OPENSSL_EXTRA)
  41854. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41855. WOLFSSL_DSA* dsa = NULL;
  41856. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41857. WOLFSSL_EVP_PKEY* pkey = NULL;
  41858. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41859. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  41860. const char* in = "What is easy to do is easy not to do.";
  41861. size_t inlen = XSTRLEN(in);
  41862. byte hash[SHA256_DIGEST_LENGTH] = {0};
  41863. byte zero[SHA256_DIGEST_LENGTH] = {0};
  41864. SHA256_CTX c;
  41865. byte* sig = NULL;
  41866. byte* sigVerify = NULL;
  41867. size_t siglen;
  41868. size_t siglenOnlyLen;
  41869. size_t keySz = 2048/8; /* Bytes */
  41870. int i;
  41871. int encs[3] = {0};
  41872. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41873. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41874. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41875. encs[0] = EVP_PKEY_RSA;
  41876. #endif
  41877. #endif
  41878. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41879. encs[1] = EVP_PKEY_DSA;
  41880. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41881. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41882. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41883. encs[2] = EVP_PKEY_EC;
  41884. #endif
  41885. #endif
  41886. AssertNotNull(sig =
  41887. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41888. AssertNotNull(sigVerify =
  41889. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41890. for (i = 0; i < 3; i++) {
  41891. if (encs[i] == 0)
  41892. continue;
  41893. siglen = keySz;
  41894. XMEMSET(sig, 0, keySz);
  41895. XMEMSET(sigVerify, 0, keySz);
  41896. /* Generate hash */
  41897. SHA256_Init(&c);
  41898. SHA256_Update(&c, in, inlen);
  41899. SHA256_Final(hash, &c);
  41900. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41901. /* workaround for small stack cache case */
  41902. wc_Sha256Free((wc_Sha256*)&c);
  41903. #endif
  41904. /* Generate key */
  41905. AssertNotNull(pkey = EVP_PKEY_new());
  41906. switch (encs[i]) {
  41907. case EVP_PKEY_RSA:
  41908. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41909. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41910. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41911. {
  41912. WOLFSSL_RSA* rsa = NULL;
  41913. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41914. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41915. }
  41916. #endif
  41917. #endif
  41918. break;
  41919. case EVP_PKEY_DSA:
  41920. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41921. AssertNotNull(dsa = DSA_new());
  41922. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41923. NULL, 0, NULL, NULL, NULL), 1);
  41924. AssertIntEQ(DSA_generate_key(dsa), 1);
  41925. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  41926. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41927. break;
  41928. case EVP_PKEY_EC:
  41929. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41930. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41931. {
  41932. WOLFSSL_EC_KEY* ecKey = NULL;
  41933. AssertNotNull(ecKey = EC_KEY_new());
  41934. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  41935. AssertIntEQ(
  41936. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41937. }
  41938. #endif
  41939. #endif
  41940. break;
  41941. }
  41942. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41943. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41944. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41945. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41946. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41947. if (encs[i] == EVP_PKEY_RSA)
  41948. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41949. WOLFSSL_SUCCESS);
  41950. #endif
  41951. #endif
  41952. /* Check returning only length */
  41953. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  41954. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41955. AssertIntGT(siglenOnlyLen, 0);
  41956. /* Sign data */
  41957. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  41958. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41959. AssertIntGE(siglenOnlyLen, siglen);
  41960. /* Verify signature */
  41961. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  41962. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41963. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41964. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41965. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41966. if (encs[i] == EVP_PKEY_RSA)
  41967. AssertIntEQ(
  41968. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  41969. WOLFSSL_SUCCESS);
  41970. #endif
  41971. #endif
  41972. AssertIntEQ(EVP_PKEY_verify(
  41973. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  41974. WOLFSSL_SUCCESS);
  41975. AssertIntEQ(EVP_PKEY_verify(
  41976. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  41977. WOLFSSL_FAILURE);
  41978. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41979. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41980. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41981. if (encs[i] == EVP_PKEY_RSA) {
  41982. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  41983. /* Try RSA sign/verify with no padding. */
  41984. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41985. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41986. WOLFSSL_SUCCESS);
  41987. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41988. siglen), WOLFSSL_SUCCESS);
  41989. AssertIntGE(siglenOnlyLen, siglen);
  41990. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41991. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41992. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  41993. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41994. siglen), WOLFSSL_SUCCESS);
  41995. #endif
  41996. /* Wrong padding schemes. */
  41997. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41998. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  41999. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42000. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42001. siglen), WOLFSSL_SUCCESS);
  42002. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42003. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42004. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42005. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42006. siglen), WOLFSSL_SUCCESS);
  42007. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42008. WOLFSSL_SUCCESS);
  42009. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42010. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  42011. }
  42012. #endif
  42013. #endif
  42014. /* error cases */
  42015. siglen = keySz; /* Reset because sig size may vary slightly */
  42016. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  42017. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42018. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  42019. WOLFSSL_SUCCESS);
  42020. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  42021. WOLFSSL_SUCCESS);
  42022. EVP_PKEY_free(pkey);
  42023. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42024. DSA_free(dsa);
  42025. dsa = NULL;
  42026. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42027. EVP_PKEY_CTX_free(ctx_verify);
  42028. EVP_PKEY_CTX_free(ctx);
  42029. }
  42030. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42031. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42032. res = TEST_RES_CHECK(1);
  42033. #endif /* OPENSSL_EXTRA */
  42034. return res;
  42035. }
  42036. static int test_EVP_PKEY_rsa(void)
  42037. {
  42038. int res = TEST_SKIPPED;
  42039. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42040. WOLFSSL_RSA* rsa;
  42041. WOLFSSL_EVP_PKEY* pkey;
  42042. AssertNotNull(rsa = wolfSSL_RSA_new());
  42043. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42044. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  42045. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  42046. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42047. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  42048. wolfSSL_EVP_PKEY_free(pkey);
  42049. res = TEST_RES_CHECK(1);
  42050. #endif
  42051. return res;
  42052. }
  42053. static int test_EVP_PKEY_ec(void)
  42054. {
  42055. int res = TEST_SKIPPED;
  42056. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42057. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42058. WOLFSSL_EC_KEY* ecKey;
  42059. WOLFSSL_EVP_PKEY* pkey;
  42060. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42061. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42062. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42063. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42064. /* Should fail since ecKey is empty */
  42065. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42066. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42067. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42068. wolfSSL_EVP_PKEY_free(pkey);
  42069. res = TEST_RES_CHECK(1);
  42070. #endif
  42071. #endif
  42072. return res;
  42073. }
  42074. static int test_EVP_PKEY_cmp(void)
  42075. {
  42076. int res = TEST_SKIPPED;
  42077. #if defined(OPENSSL_EXTRA)
  42078. EVP_PKEY *a, *b;
  42079. const unsigned char *in;
  42080. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42081. in = client_key_der_2048;
  42082. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42083. &in, (long)sizeof_client_key_der_2048));
  42084. in = client_key_der_2048;
  42085. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42086. &in, (long)sizeof_client_key_der_2048));
  42087. /* Test success case RSA */
  42088. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42089. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42090. #else
  42091. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42092. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42093. EVP_PKEY_free(b);
  42094. EVP_PKEY_free(a);
  42095. #endif
  42096. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42097. in = ecc_clikey_der_256;
  42098. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42099. &in, (long)sizeof_ecc_clikey_der_256));
  42100. in = ecc_clikey_der_256;
  42101. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42102. &in, (long)sizeof_ecc_clikey_der_256));
  42103. /* Test success case ECC */
  42104. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42105. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  42106. #else
  42107. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42108. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42109. EVP_PKEY_free(b);
  42110. EVP_PKEY_free(a);
  42111. #endif
  42112. /* Test failure cases */
  42113. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  42114. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42115. in = client_key_der_2048;
  42116. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42117. &in, (long)sizeof_client_key_der_2048));
  42118. in = ecc_clikey_der_256;
  42119. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42120. &in, (long)sizeof_ecc_clikey_der_256));
  42121. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42122. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  42123. #else
  42124. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42125. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42126. EVP_PKEY_free(b);
  42127. EVP_PKEY_free(a);
  42128. #endif
  42129. /* invalid or empty failure cases */
  42130. a = EVP_PKEY_new();
  42131. b = EVP_PKEY_new();
  42132. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42133. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42134. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42135. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42136. #ifdef NO_RSA
  42137. /* Type check will fail since RSA is the default EVP key type */
  42138. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  42139. #else
  42140. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42141. #endif
  42142. #else
  42143. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42144. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42145. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42146. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42147. #endif
  42148. EVP_PKEY_free(b);
  42149. EVP_PKEY_free(a);
  42150. (void)in;
  42151. res = TEST_RES_CHECK(1);
  42152. #endif
  42153. return res;
  42154. }
  42155. static int test_ERR_load_crypto_strings(void)
  42156. {
  42157. int res = TEST_SKIPPED;
  42158. #if defined(OPENSSL_ALL)
  42159. ERR_load_crypto_strings();
  42160. res = TEST_RES_CHECK(1);
  42161. #endif
  42162. return res;
  42163. }
  42164. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42165. static void free_x509(X509* x)
  42166. {
  42167. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42168. }
  42169. #endif
  42170. static int test_sk_X509(void)
  42171. {
  42172. int res = TEST_SKIPPED;
  42173. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42174. {
  42175. STACK_OF(X509)* s;
  42176. AssertNotNull(s = sk_X509_new_null());
  42177. AssertIntEQ(sk_X509_num(s), 0);
  42178. sk_X509_pop_free(s, NULL);
  42179. AssertNotNull(s = sk_X509_new_null());
  42180. AssertIntEQ(sk_X509_num(s), 0);
  42181. sk_X509_pop_free(s, NULL);
  42182. AssertNotNull(s = sk_X509_new_null());
  42183. sk_X509_push(s, (X509*)1);
  42184. AssertIntEQ(sk_X509_num(s), 1);
  42185. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42186. sk_X509_push(s, (X509*)2);
  42187. AssertIntEQ(sk_X509_num(s), 2);
  42188. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42189. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42190. sk_X509_push(s, (X509*)2);
  42191. sk_X509_pop_free(s, free_x509);
  42192. }
  42193. {
  42194. /* Push a list of 10 X509s onto stack, then verify that
  42195. * value(), push(), shift(), and pop() behave as expected. */
  42196. STACK_OF(X509)* s;
  42197. X509* xList[10];
  42198. int i = 0;
  42199. const int len = (sizeof(xList) / sizeof(xList[0]));
  42200. for (i = 0; i < len; ++i)
  42201. AssertNotNull(xList[i] = X509_new());
  42202. /* test push, pop, and free */
  42203. AssertNotNull(s = sk_X509_new_null());
  42204. for (i = 0; i < len; ++i) {
  42205. sk_X509_push(s, xList[i]);
  42206. AssertIntEQ(sk_X509_num(s), i + 1);
  42207. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42208. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42209. }
  42210. /* pop returns and removes last pushed on stack, which is index 0
  42211. * in sk_x509_value */
  42212. for (i = 0; i < len; ++i) {
  42213. X509 * x = sk_X509_value(s, 0);
  42214. X509 * y = sk_X509_pop(s);
  42215. X509 * z = xList[len - 1 - i];
  42216. AssertIntEQ((x == y), 1);
  42217. AssertIntEQ((x == z), 1);
  42218. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42219. }
  42220. sk_free(s);
  42221. /* test push, shift, and free */
  42222. AssertNotNull(s = sk_X509_new_null());
  42223. for (i = 0; i < len; ++i) {
  42224. sk_X509_push(s, xList[i]);
  42225. AssertIntEQ(sk_X509_num(s), i + 1);
  42226. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42227. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42228. }
  42229. /* shift returns and removes first pushed on stack, which is index i
  42230. * in sk_x509_value() */
  42231. for (i = 0; i < len; ++i) {
  42232. X509 * x = sk_X509_value(s, len - 1 - i);
  42233. X509 * y = sk_X509_shift(s);
  42234. X509 * z = xList[i];
  42235. AssertIntEQ((x == y), 1);
  42236. AssertIntEQ((x == z), 1);
  42237. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42238. }
  42239. sk_free(s);
  42240. for (i = 0; i < len; ++i)
  42241. X509_free(xList[i]);
  42242. }
  42243. res = TEST_RES_CHECK(1);
  42244. #endif
  42245. return res;
  42246. }
  42247. static int test_sk_X509_CRL(void)
  42248. {
  42249. int res = TEST_SKIPPED;
  42250. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42251. X509_CRL* crl;
  42252. XFILE fp;
  42253. STACK_OF(X509_CRL)* s;
  42254. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  42255. AssertTrue((fp != XBADFILE));
  42256. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  42257. XFCLOSE(fp);
  42258. AssertNotNull(s = sk_X509_CRL_new());
  42259. AssertIntEQ(sk_X509_CRL_num(s), 0);
  42260. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  42261. AssertIntEQ(sk_X509_CRL_num(s), 1);
  42262. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  42263. sk_X509_CRL_free(s);
  42264. res = TEST_RES_CHECK(1);
  42265. #endif
  42266. return res;
  42267. }
  42268. static int test_X509_get_signature_nid(void)
  42269. {
  42270. int res = TEST_SKIPPED;
  42271. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42272. X509* x509;
  42273. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  42274. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42275. SSL_FILETYPE_PEM));
  42276. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42277. X509_free(x509);
  42278. res = TEST_RES_CHECK(1);
  42279. #endif
  42280. return res;
  42281. }
  42282. static int test_X509_REQ(void)
  42283. {
  42284. int res = TEST_SKIPPED;
  42285. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42286. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42287. X509_NAME* name;
  42288. #ifndef NO_RSA
  42289. X509_NAME* subject;
  42290. #endif
  42291. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42292. X509_REQ* req;
  42293. EVP_PKEY* priv;
  42294. EVP_PKEY* pub;
  42295. unsigned char* der = NULL;
  42296. int len;
  42297. #endif
  42298. #ifndef NO_RSA
  42299. EVP_MD_CTX *mctx = NULL;
  42300. EVP_PKEY_CTX *pkctx = NULL;
  42301. #ifdef USE_CERT_BUFFERS_1024
  42302. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42303. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42304. #elif defined(USE_CERT_BUFFERS_2048)
  42305. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42306. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42307. #endif
  42308. #endif
  42309. #ifdef HAVE_ECC
  42310. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42311. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42312. #endif
  42313. AssertNotNull(name = X509_NAME_new());
  42314. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42315. (byte*)"wolfssl.com", 11, 0, 1),
  42316. WOLFSSL_SUCCESS);
  42317. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42318. (byte*)"support@wolfssl.com", 19, -1,
  42319. 1), WOLFSSL_SUCCESS);
  42320. #ifndef NO_RSA
  42321. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42322. (long)sizeof_client_key_der_2048));
  42323. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42324. (long)sizeof_client_keypub_der_2048));
  42325. AssertNotNull(req = X509_REQ_new());
  42326. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42327. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42328. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42329. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42330. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42331. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42332. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42333. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42334. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42335. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42336. len = i2d_X509_REQ(req, &der);
  42337. DEBUG_WRITE_DER(der, len, "req.der");
  42338. #ifdef USE_CERT_BUFFERS_1024
  42339. AssertIntEQ(len, 381);
  42340. #else
  42341. AssertIntEQ(len, 643);
  42342. #endif
  42343. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42344. der = NULL;
  42345. mctx = EVP_MD_CTX_new();
  42346. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  42347. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42348. EVP_MD_CTX_free(mctx);
  42349. X509_REQ_free(NULL);
  42350. X509_REQ_free(req);
  42351. /* Test getting the subject from a newly created X509_REQ */
  42352. AssertNotNull(req = X509_REQ_new());
  42353. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  42354. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42355. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42356. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42357. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42358. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42359. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42360. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42361. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42362. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42363. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42364. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42365. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42366. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42367. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42368. len = i2d_X509_REQ(req, &der);
  42369. DEBUG_WRITE_DER(der, len, "req2.der");
  42370. #ifdef USE_CERT_BUFFERS_1024
  42371. AssertIntEQ(len, 435);
  42372. #else
  42373. AssertIntEQ(len, 696);
  42374. #endif
  42375. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42376. der = NULL;
  42377. EVP_PKEY_free(pub);
  42378. EVP_PKEY_free(priv);
  42379. X509_REQ_free(req);
  42380. #endif
  42381. #ifdef HAVE_ECC
  42382. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42383. sizeof_ecc_clikey_der_256));
  42384. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42385. sizeof_ecc_clikeypub_der_256));
  42386. AssertNotNull(req = X509_REQ_new());
  42387. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42388. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42389. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42390. /* Signature is random and may be shorter or longer. */
  42391. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42392. AssertIntLE(len, 253);
  42393. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42394. X509_REQ_free(req);
  42395. EVP_PKEY_free(pub);
  42396. EVP_PKEY_free(priv);
  42397. #ifdef FP_ECC
  42398. wc_ecc_fp_free();
  42399. #endif
  42400. #endif /* HAVE_ECC */
  42401. X509_NAME_free(name);
  42402. res = TEST_RES_CHECK(1);
  42403. #endif
  42404. return res;
  42405. }
  42406. static int test_wolfssl_PKCS7(void)
  42407. {
  42408. int res = TEST_SKIPPED;
  42409. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  42410. PKCS7* pkcs7;
  42411. byte data[FOURK_BUF];
  42412. word32 len = sizeof(data);
  42413. const byte* p = data;
  42414. byte content[] = "Test data to encode.";
  42415. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42416. BIO* bio;
  42417. byte key[sizeof(client_key_der_2048)];
  42418. word32 keySz = (word32)sizeof(key);
  42419. byte* out = NULL;
  42420. #endif
  42421. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  42422. (word32)sizeof(content),
  42423. 0, 0, 0, RSA_TYPE)), 0);
  42424. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42425. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42426. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42427. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42428. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42429. PKCS7_free(pkcs7);
  42430. /* fail case, without PKCS7_NOVERIFY */
  42431. p = data;
  42432. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42433. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42434. 0), WOLFSSL_FAILURE);
  42435. PKCS7_free(pkcs7);
  42436. /* success case, with PKCS7_NOVERIFY */
  42437. p = data;
  42438. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42439. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42440. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42441. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42442. /* test i2d */
  42443. XMEMCPY(key, client_key_der_2048, keySz);
  42444. pkcs7->privateKey = key;
  42445. pkcs7->privateKeySz = (word32)sizeof(key);
  42446. pkcs7->encryptOID = RSAk;
  42447. #ifdef NO_SHA
  42448. pkcs7->hashOID = SHA256h;
  42449. #else
  42450. pkcs7->hashOID = SHAh;
  42451. #endif
  42452. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42453. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42454. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42455. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42456. BIO_free(bio);
  42457. #endif
  42458. PKCS7_free(NULL);
  42459. PKCS7_free(pkcs7);
  42460. res = TEST_RES_CHECK(1);
  42461. #endif
  42462. return res;
  42463. }
  42464. static int test_wolfSSL_PKCS7_sign(void)
  42465. {
  42466. int res = TEST_SKIPPED;
  42467. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42468. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42469. PKCS7* p7 = NULL;
  42470. PKCS7* p7Ver = NULL;
  42471. byte* out = NULL;
  42472. byte* tmpPtr = NULL;
  42473. int outLen = 0;
  42474. int flags = 0;
  42475. byte data[] = "Test data to encode.";
  42476. const char* cert = "./certs/server-cert.pem";
  42477. const char* key = "./certs/server-key.pem";
  42478. const char* ca = "./certs/ca-cert.pem";
  42479. WOLFSSL_BIO* certBio = NULL;
  42480. WOLFSSL_BIO* keyBio = NULL;
  42481. WOLFSSL_BIO* caBio = NULL;
  42482. WOLFSSL_BIO* inBio = NULL;
  42483. X509* signCert = NULL;
  42484. EVP_PKEY* signKey = NULL;
  42485. X509* caCert = NULL;
  42486. X509_STORE* store = NULL;
  42487. /* read signer cert/key into BIO */
  42488. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42489. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42490. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42491. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42492. /* read CA cert into store (for verify) */
  42493. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42494. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42495. AssertNotNull(store = X509_STORE_new());
  42496. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42497. /* data to be signed into BIO */
  42498. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42499. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42500. /* PKCS7_sign, bad args: signer NULL */
  42501. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42502. /* PKCS7_sign, bad args: signer key NULL */
  42503. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42504. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42505. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42506. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42507. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42508. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42509. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42510. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42511. {
  42512. flags = PKCS7_BINARY;
  42513. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42514. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42515. /* verify with d2i_PKCS7 */
  42516. tmpPtr = out;
  42517. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42518. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42519. PKCS7_free(p7Ver);
  42520. /* verify with wc_PKCS7_VerifySignedData */
  42521. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42522. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42523. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42524. /* compare the signer found to expected signer */
  42525. AssertIntNE(p7Ver->verifyCertSz, 0);
  42526. tmpPtr = NULL;
  42527. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42528. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42529. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42530. tmpPtr = NULL;
  42531. wc_PKCS7_Free(p7Ver);
  42532. AssertNotNull(out);
  42533. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42534. out = NULL;
  42535. PKCS7_free(p7);
  42536. }
  42537. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42538. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42539. {
  42540. /* re-populate input BIO, may have been consumed */
  42541. BIO_free(inBio);
  42542. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42543. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42544. flags = PKCS7_BINARY | PKCS7_STREAM;
  42545. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42546. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42547. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42548. /* PKCS7_final, bad args: PKCS7 null */
  42549. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42550. /* PKCS7_final, bad args: PKCS7 null */
  42551. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42552. tmpPtr = out;
  42553. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42554. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42555. PKCS7_free(p7Ver);
  42556. AssertNotNull(out);
  42557. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42558. out = NULL;
  42559. PKCS7_free(p7);
  42560. }
  42561. /* TEST SUCCESS: Detached, not streaming, not MIME */
  42562. {
  42563. /* re-populate input BIO, may have been consumed */
  42564. BIO_free(inBio);
  42565. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42566. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42567. flags = PKCS7_BINARY | PKCS7_DETACHED;
  42568. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42569. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42570. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42571. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42572. p7Ver->content = data;
  42573. p7Ver->contentSz = sizeof(data);
  42574. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42575. wc_PKCS7_Free(p7Ver);
  42576. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  42577. * yet support detached content */
  42578. tmpPtr = out;
  42579. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42580. PKCS7_free(p7Ver);
  42581. AssertNotNull(out);
  42582. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42583. out = NULL;
  42584. PKCS7_free(p7);
  42585. }
  42586. /* TEST SUCCESS: Detached, streaming, not MIME */
  42587. {
  42588. /* re-populate input BIO, may have been consumed */
  42589. BIO_free(inBio);
  42590. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42591. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42592. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  42593. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42594. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42595. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42596. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42597. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42598. p7Ver->content = data;
  42599. p7Ver->contentSz = sizeof(data);
  42600. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42601. wc_PKCS7_Free(p7Ver);
  42602. AssertNotNull(out);
  42603. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42604. PKCS7_free(p7);
  42605. }
  42606. X509_STORE_free(store);
  42607. X509_free(caCert);
  42608. X509_free(signCert);
  42609. EVP_PKEY_free(signKey);
  42610. BIO_free(inBio);
  42611. BIO_free(keyBio);
  42612. BIO_free(certBio);
  42613. BIO_free(caBio);
  42614. res = TEST_RES_CHECK(1);
  42615. #endif
  42616. return res;
  42617. }
  42618. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  42619. {
  42620. int res = TEST_SKIPPED;
  42621. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  42622. PKCS7_SIGNED* pkcs7;
  42623. pkcs7 = PKCS7_SIGNED_new();
  42624. AssertNotNull(pkcs7);
  42625. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  42626. PKCS7_SIGNED_free(pkcs7);
  42627. res = TEST_RES_CHECK(1);
  42628. #endif
  42629. return res;
  42630. }
  42631. #ifndef NO_BIO
  42632. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  42633. {
  42634. int res = TEST_SKIPPED;
  42635. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  42636. PKCS7* pkcs7 = NULL;
  42637. BIO* bio = NULL;
  42638. const byte* cert_buf = NULL;
  42639. int ret = 0;
  42640. WC_RNG rng;
  42641. const byte data[] = { /* Hello World */
  42642. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42643. 0x72,0x6c,0x64
  42644. };
  42645. #ifndef NO_RSA
  42646. #if defined(USE_CERT_BUFFERS_2048)
  42647. byte key[sizeof(client_key_der_2048)];
  42648. byte cert[sizeof(client_cert_der_2048)];
  42649. word32 keySz = (word32)sizeof(key);
  42650. word32 certSz = (word32)sizeof(cert);
  42651. XMEMSET(key, 0, keySz);
  42652. XMEMSET(cert, 0, certSz);
  42653. XMEMCPY(key, client_key_der_2048, keySz);
  42654. XMEMCPY(cert, client_cert_der_2048, certSz);
  42655. #elif defined(USE_CERT_BUFFERS_1024)
  42656. byte key[sizeof_client_key_der_1024];
  42657. byte cert[sizeof(sizeof_client_cert_der_1024)];
  42658. word32 keySz = (word32)sizeof(key);
  42659. word32 certSz = (word32)sizeof(cert);
  42660. XMEMSET(key, 0, keySz);
  42661. XMEMSET(cert, 0, certSz);
  42662. XMEMCPY(key, client_key_der_1024, keySz);
  42663. XMEMCPY(cert, client_cert_der_1024, certSz);
  42664. #else
  42665. unsigned char cert[ONEK_BUF];
  42666. unsigned char key[ONEK_BUF];
  42667. XFILE fp;
  42668. int certSz;
  42669. int keySz;
  42670. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  42671. AssertTrue((fp != XBADFILE));
  42672. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  42673. XFCLOSE(fp);
  42674. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  42675. AssertTrue(fp != XBADFILE);
  42676. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  42677. XFCLOSE(fp);
  42678. #endif
  42679. #elif defined(HAVE_ECC)
  42680. #if defined(USE_CERT_BUFFERS_256)
  42681. unsigned char cert[sizeof(cliecc_cert_der_256)];
  42682. unsigned char key[sizeof(ecc_clikey_der_256)];
  42683. int certSz = (int)sizeof(cert);
  42684. int keySz = (int)sizeof(key);
  42685. XMEMSET(cert, 0, certSz);
  42686. XMEMSET(key, 0, keySz);
  42687. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  42688. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  42689. #else
  42690. unsigned char cert[ONEK_BUF];
  42691. unsigned char key[ONEK_BUF];
  42692. XFILE fp;
  42693. int certSz, keySz;
  42694. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  42695. AssertTrue(fp != XBADFILE);
  42696. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  42697. XFCLOSE(fp);
  42698. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  42699. AssertTrue(fp != XBADFILE);
  42700. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  42701. XFCLOSE(fp);
  42702. #endif
  42703. #else
  42704. #error PKCS7 requires ECC or RSA
  42705. #endif
  42706. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  42707. /* initialize with DER encoded cert */
  42708. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  42709. /* init rng */
  42710. AssertIntEQ(wc_InitRng(&rng), 0);
  42711. pkcs7->rng = &rng;
  42712. pkcs7->content = (byte*)data; /* not used for ex */
  42713. pkcs7->contentSz = (word32)sizeof(data);
  42714. pkcs7->contentOID = SIGNED_DATA;
  42715. pkcs7->privateKey = key;
  42716. pkcs7->privateKeySz = (word32)sizeof(key);
  42717. pkcs7->encryptOID = RSAk;
  42718. #ifdef NO_SHA
  42719. pkcs7->hashOID = SHA256h;
  42720. #else
  42721. pkcs7->hashOID = SHAh;
  42722. #endif
  42723. pkcs7->signedAttribs = NULL;
  42724. pkcs7->signedAttribsSz = 0;
  42725. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42726. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  42727. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  42728. /* Read PKCS#7 PEM from BIO */
  42729. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  42730. AssertIntGE(ret, 0);
  42731. BIO_free(bio);
  42732. wc_PKCS7_Free(pkcs7);
  42733. wc_FreeRng(&rng);
  42734. res = TEST_RES_CHECK(1);
  42735. #endif
  42736. return res;
  42737. }
  42738. #ifdef HAVE_SMIME
  42739. static int test_wolfSSL_SMIME_read_PKCS7(void)
  42740. {
  42741. int res = TEST_SKIPPED;
  42742. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  42743. !defined(NO_RSA)
  42744. PKCS7* pkcs7 = NULL;
  42745. BIO* bio = NULL;
  42746. BIO* bcont = NULL;
  42747. BIO* out = NULL;
  42748. const byte* outBuf = NULL;
  42749. int outBufLen = 0;
  42750. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  42751. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  42752. /* smime-test.p7s */
  42753. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  42754. AssertNotNull(bio);
  42755. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42756. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42757. AssertNotNull(pkcs7);
  42758. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42759. PKCS7_NOVERIFY), SSL_SUCCESS);
  42760. XFCLOSE(smimeTestFile);
  42761. if (bcont) BIO_free(bcont);
  42762. wolfSSL_PKCS7_free(pkcs7);
  42763. /* smime-test-multipart.p7s */
  42764. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  42765. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42766. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42767. AssertNotNull(pkcs7);
  42768. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42769. PKCS7_NOVERIFY), SSL_SUCCESS);
  42770. XFCLOSE(smimeTestFile);
  42771. if (bcont) BIO_free(bcont);
  42772. wolfSSL_PKCS7_free(pkcs7);
  42773. /* smime-test-multipart-badsig.p7s */
  42774. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  42775. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42776. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42777. AssertNull(pkcs7);
  42778. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42779. PKCS7_NOVERIFY), SSL_FAILURE);
  42780. XFCLOSE(smimeTestFile);
  42781. if (bcont) BIO_free(bcont);
  42782. wolfSSL_PKCS7_free(pkcs7);
  42783. /* smime-test-canon.p7s */
  42784. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42785. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42786. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42787. AssertNotNull(pkcs7);
  42788. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42789. PKCS7_NOVERIFY), SSL_SUCCESS);
  42790. XFCLOSE(smimeTestFile);
  42791. if (bcont) BIO_free(bcont);
  42792. wolfSSL_PKCS7_free(pkcs7);
  42793. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  42794. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42795. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42796. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42797. AssertNotNull(pkcs7);
  42798. out = wolfSSL_BIO_new(BIO_s_mem());
  42799. AssertNotNull(out);
  42800. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  42801. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  42802. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  42803. /* Content-Type should not show up at beginning of output buffer */
  42804. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  42805. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  42806. BIO_free(out);
  42807. BIO_free(bio);
  42808. if (bcont) BIO_free(bcont);
  42809. wolfSSL_PKCS7_free(pkcs7);
  42810. res = TEST_RES_CHECK(1);
  42811. #endif
  42812. return res;
  42813. }
  42814. static int test_wolfSSL_SMIME_write_PKCS7(void)
  42815. {
  42816. int res = TEST_SKIPPED;
  42817. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  42818. PKCS7* p7 = NULL;
  42819. PKCS7* p7Ver = NULL;
  42820. int flags = 0;
  42821. byte data[] = "Test data to encode.";
  42822. const char* cert = "./certs/server-cert.pem";
  42823. const char* key = "./certs/server-key.pem";
  42824. const char* ca = "./certs/ca-cert.pem";
  42825. WOLFSSL_BIO* certBio = NULL;
  42826. WOLFSSL_BIO* keyBio = NULL;
  42827. WOLFSSL_BIO* caBio = NULL;
  42828. WOLFSSL_BIO* inBio = NULL;
  42829. WOLFSSL_BIO* outBio = NULL;
  42830. WOLFSSL_BIO* content = NULL;
  42831. X509* signCert = NULL;
  42832. EVP_PKEY* signKey = NULL;
  42833. X509* caCert = NULL;
  42834. X509_STORE* store = NULL;
  42835. /* read signer cert/key into BIO */
  42836. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42837. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42838. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42839. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42840. /* read CA cert into store (for verify) */
  42841. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42842. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42843. AssertNotNull(store = X509_STORE_new());
  42844. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42845. /* generate and verify SMIME: not detached */
  42846. {
  42847. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42848. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42849. flags = PKCS7_STREAM;
  42850. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42851. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42852. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42853. /* bad arg: out NULL */
  42854. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  42855. /* bad arg: pkcs7 NULL */
  42856. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  42857. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42858. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42859. BIO_free(content);
  42860. BIO_free(inBio);
  42861. BIO_free(outBio);
  42862. PKCS7_free(p7Ver);
  42863. PKCS7_free(p7);
  42864. }
  42865. /* generate and verify SMIME: not detached, add Content-Type */
  42866. {
  42867. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42868. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42869. flags = PKCS7_STREAM | PKCS7_TEXT;
  42870. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42871. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42872. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42873. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42874. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42875. BIO_free(content);
  42876. BIO_free(inBio);
  42877. BIO_free(outBio);
  42878. PKCS7_free(p7Ver);
  42879. PKCS7_free(p7);
  42880. }
  42881. /* generate and verify SMIME: detached */
  42882. {
  42883. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42884. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42885. flags = PKCS7_DETACHED | PKCS7_STREAM;
  42886. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42887. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42888. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42889. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42890. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42891. BIO_free(content);
  42892. BIO_free(inBio);
  42893. BIO_free(outBio);
  42894. PKCS7_free(p7Ver);
  42895. PKCS7_free(p7);
  42896. }
  42897. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  42898. {
  42899. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42900. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42901. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  42902. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42903. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42904. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42905. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42906. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42907. BIO_free(content);
  42908. BIO_free(inBio);
  42909. BIO_free(outBio);
  42910. PKCS7_free(p7Ver);
  42911. PKCS7_free(p7);
  42912. }
  42913. X509_STORE_free(store);
  42914. X509_free(caCert);
  42915. X509_free(signCert);
  42916. EVP_PKEY_free(signKey);
  42917. BIO_free(keyBio);
  42918. BIO_free(certBio);
  42919. BIO_free(caBio);
  42920. res = TEST_RES_CHECK(1);
  42921. #endif
  42922. return res;
  42923. }
  42924. #endif /* HAVE_SMIME */
  42925. #endif /* !NO_BIO */
  42926. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  42927. * returns 0) */
  42928. static int test_X509_STORE_No_SSL_CTX(void)
  42929. {
  42930. int res = TEST_SKIPPED;
  42931. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42932. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42933. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42934. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42935. X509_STORE * store;
  42936. X509_STORE_CTX * storeCtx;
  42937. X509_CRL * crl;
  42938. X509 * ca;
  42939. X509 * cert;
  42940. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  42941. const char srvCert[] = "./certs/server-cert.pem";
  42942. const char caCert[] = "./certs/ca-cert.pem";
  42943. const char caDir[] = "./certs/crl/hash_pem";
  42944. XFILE fp;
  42945. X509_LOOKUP * lookup;
  42946. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42947. /* Set up store with CA */
  42948. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42949. SSL_FILETYPE_PEM)));
  42950. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42951. /* Add CRL lookup directory to store
  42952. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  42953. * of crl.pem */
  42954. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42955. X509_LOOKUP_hash_dir())));
  42956. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  42957. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  42958. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42959. SSL_SUCCESS);
  42960. /* Add CRL to store NOT containing the verified certificate, which
  42961. * forces use of the CRL lookup directory */
  42962. fp = XFOPEN(cliCrlPem, "rb");
  42963. AssertTrue((fp != XBADFILE));
  42964. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42965. NULL, NULL));
  42966. XFCLOSE(fp);
  42967. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42968. /* Create verification context outside of an SSL session */
  42969. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42970. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42971. SSL_FILETYPE_PEM)));
  42972. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42973. /* Perform verification, which should NOT indicate CRL missing due to the
  42974. * store CM's X509 store pointer being NULL */
  42975. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  42976. X509_CRL_free(crl);
  42977. X509_STORE_free(store);
  42978. X509_STORE_CTX_free(storeCtx);
  42979. X509_free(cert);
  42980. X509_free(ca);
  42981. res = TEST_RES_CHECK(1);
  42982. #endif
  42983. return res;
  42984. }
  42985. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  42986. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  42987. static int test_X509_LOOKUP_add_dir(void)
  42988. {
  42989. int res = TEST_SKIPPED;
  42990. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42991. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42992. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42993. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42994. X509_STORE * store;
  42995. X509_STORE_CTX * storeCtx;
  42996. X509_CRL * crl;
  42997. X509 * ca;
  42998. X509 * cert;
  42999. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43000. const char srvCert[] = "./certs/server-cert.pem";
  43001. const char caCert[] = "./certs/ca-cert.pem";
  43002. const char caDir[] = "./certs/crl/hash_der";
  43003. XFILE fp;
  43004. X509_LOOKUP * lookup;
  43005. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  43006. /* Set up store with CA */
  43007. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43008. SSL_FILETYPE_PEM)));
  43009. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43010. /* Add CRL lookup directory to store.
  43011. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  43012. * of crl.der */
  43013. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43014. X509_LOOKUP_hash_dir())));
  43015. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  43016. SSL_SUCCESS);
  43017. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43018. SSL_SUCCESS);
  43019. /* Add CRL to store NOT containing the verified certificate, which
  43020. * forces use of the CRL lookup directory */
  43021. fp = XFOPEN(cliCrlPem, "rb");
  43022. AssertTrue((fp != XBADFILE));
  43023. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43024. NULL, NULL));
  43025. XFCLOSE(fp);
  43026. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43027. /* Create verification context outside of an SSL session */
  43028. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43029. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43030. SSL_FILETYPE_PEM)));
  43031. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43032. /* Perform verification, which should NOT return CRL missing */
  43033. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43034. X509_CRL_free(crl);
  43035. X509_STORE_free(store);
  43036. X509_STORE_CTX_free(storeCtx);
  43037. X509_free(cert);
  43038. X509_free(ca);
  43039. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  43040. * We should get CRL_MISSING at the end, because the lookup
  43041. * dir has only ASN1 CRLs. */
  43042. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  43043. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43044. SSL_FILETYPE_PEM)));
  43045. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43046. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  43047. X509_LOOKUP_hash_dir())));
  43048. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  43049. SSL_SUCCESS);
  43050. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43051. SSL_SUCCESS);
  43052. fp = XFOPEN(cliCrlPem, "rb");
  43053. AssertTrue((fp != XBADFILE));
  43054. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43055. NULL, NULL));
  43056. XFCLOSE(fp);
  43057. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43058. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  43059. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43060. SSL_FILETYPE_PEM)));
  43061. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43062. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  43063. * in dir containing only ASN1/DER. */
  43064. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  43065. X509_CRL_free(crl);
  43066. X509_STORE_free(store);
  43067. X509_STORE_CTX_free(storeCtx);
  43068. X509_free(cert);
  43069. X509_free(ca);
  43070. res = TEST_RES_CHECK(1);
  43071. #endif
  43072. return res;
  43073. }
  43074. /*----------------------------------------------------------------------------*
  43075. | Certificate Failure Checks
  43076. *----------------------------------------------------------------------------*/
  43077. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  43078. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  43079. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  43080. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  43081. int type)
  43082. {
  43083. int ret;
  43084. WOLFSSL_CERT_MANAGER* cm = NULL;
  43085. switch (type) {
  43086. case TESTING_RSA:
  43087. #ifdef NO_RSA
  43088. fprintf(stderr, "RSA disabled, skipping test\n");
  43089. return ASN_SIG_CONFIRM_E;
  43090. #else
  43091. break;
  43092. #endif
  43093. case TESTING_ECC:
  43094. #ifndef HAVE_ECC
  43095. fprintf(stderr, "ECC disabled, skipping test\n");
  43096. return ASN_SIG_CONFIRM_E;
  43097. #else
  43098. break;
  43099. #endif
  43100. default:
  43101. fprintf(stderr, "Bad function argument\n");
  43102. return BAD_FUNC_ARG;
  43103. }
  43104. cm = wolfSSL_CertManagerNew();
  43105. if (cm == NULL) {
  43106. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  43107. return -1;
  43108. }
  43109. #ifndef NO_FILESYSTEM
  43110. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  43111. if (ret != WOLFSSL_SUCCESS) {
  43112. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  43113. wolfSSL_CertManagerFree(cm);
  43114. return ret;
  43115. }
  43116. #else
  43117. (void)ca;
  43118. #endif
  43119. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  43120. /* Let AssertIntEQ handle return code */
  43121. wolfSSL_CertManagerFree(cm);
  43122. return ret;
  43123. }
  43124. #if !defined(NO_FILESYSTEM)
  43125. static int test_RsaSigFailure_cm(void)
  43126. {
  43127. int ret = 0;
  43128. const char* ca_cert = "./certs/ca-cert.pem";
  43129. const char* server_cert = "./certs/server-cert.der";
  43130. byte* cert_buf = NULL;
  43131. size_t cert_sz = 0;
  43132. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43133. if (ret == 0) {
  43134. /* corrupt DER - invert last byte, which is signature */
  43135. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43136. /* test bad cert */
  43137. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  43138. }
  43139. if (cert_buf)
  43140. free(cert_buf);
  43141. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43142. if (ret == WOLFSSL_FATAL_ERROR) {
  43143. ret = 0;
  43144. }
  43145. #else
  43146. if (ret == ASN_SIG_CONFIRM_E) {
  43147. ret = 0;
  43148. }
  43149. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43150. return TEST_RES_CHECK(ret == 0);
  43151. }
  43152. static int test_EccSigFailure_cm(void)
  43153. {
  43154. int ret = 0;
  43155. /* self-signed ECC cert, so use server cert as CA */
  43156. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43157. const char* server_cert = "./certs/server-ecc.der";
  43158. byte* cert_buf = NULL;
  43159. size_t cert_sz = 0;
  43160. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43161. if (ret == 0) {
  43162. /* corrupt DER - invert last byte, which is signature */
  43163. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43164. /* test bad cert */
  43165. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  43166. }
  43167. if (cert_buf)
  43168. free(cert_buf);
  43169. #ifdef FP_ECC
  43170. wc_ecc_fp_free();
  43171. #endif
  43172. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43173. if (ret == WOLFSSL_FATAL_ERROR) {
  43174. ret = 0;
  43175. }
  43176. #else
  43177. if (ret == ASN_SIG_CONFIRM_E) {
  43178. ret = 0;
  43179. }
  43180. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43181. return TEST_RES_CHECK(ret == 0);
  43182. }
  43183. #endif /* !NO_FILESYSTEM */
  43184. #endif /* NO_CERTS */
  43185. #ifdef WOLFSSL_TLS13
  43186. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43187. #ifdef WC_SHA384_DIGEST_SIZE
  43188. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43189. #else
  43190. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43191. #endif
  43192. #endif
  43193. #ifdef WOLFSSL_EARLY_DATA
  43194. static const char earlyData[] = "Early Data";
  43195. static char earlyDataBuffer[1];
  43196. #endif
  43197. static int test_tls13_apis(void)
  43198. {
  43199. int ret = 0;
  43200. #ifndef WOLFSSL_NO_TLS12
  43201. #ifndef NO_WOLFSSL_CLIENT
  43202. WOLFSSL_CTX* clientTls12Ctx;
  43203. WOLFSSL* clientTls12Ssl;
  43204. #endif
  43205. #ifndef NO_WOLFSSL_SERVER
  43206. WOLFSSL_CTX* serverTls12Ctx;
  43207. WOLFSSL* serverTls12Ssl;
  43208. #endif
  43209. #endif
  43210. #ifndef NO_WOLFSSL_CLIENT
  43211. WOLFSSL_CTX* clientCtx;
  43212. WOLFSSL* clientSsl;
  43213. #endif
  43214. #ifndef NO_WOLFSSL_SERVER
  43215. WOLFSSL_CTX* serverCtx;
  43216. WOLFSSL* serverSsl;
  43217. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43218. const char* ourCert = svrCertFile;
  43219. const char* ourKey = svrKeyFile;
  43220. #endif
  43221. #endif
  43222. int required;
  43223. #ifdef WOLFSSL_EARLY_DATA
  43224. int outSz;
  43225. #endif
  43226. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43227. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43228. #ifdef HAVE_PQC
  43229. WOLFSSL_KYBER_LEVEL1
  43230. #else
  43231. WOLFSSL_ECC_SECP256R1
  43232. #endif
  43233. };
  43234. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43235. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43236. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43237. int numGroups = 2;
  43238. #endif
  43239. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43240. char groupList[] =
  43241. #ifndef NO_ECC_SECP
  43242. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43243. "P-521:"
  43244. #endif
  43245. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43246. "P-384:"
  43247. #endif
  43248. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43249. "P-256"
  43250. #ifdef HAVE_PQC
  43251. ":P256_KYBER_LEVEL1"
  43252. #endif
  43253. #endif
  43254. #ifdef HAVE_PQC
  43255. ":KYBER_LEVEL1"
  43256. #endif
  43257. "";
  43258. #endif /* !defined(NO_ECC_SECP) */
  43259. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43260. (void)ret;
  43261. #ifndef WOLFSSL_NO_TLS12
  43262. #ifndef NO_WOLFSSL_CLIENT
  43263. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43264. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43265. #endif
  43266. #ifndef NO_WOLFSSL_SERVER
  43267. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43268. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43269. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43270. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  43271. #endif
  43272. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43273. #endif
  43274. #endif
  43275. #ifndef NO_WOLFSSL_CLIENT
  43276. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43277. clientSsl = wolfSSL_new(clientCtx);
  43278. #endif
  43279. #ifndef NO_WOLFSSL_SERVER
  43280. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43281. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43282. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43283. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43284. #endif
  43285. serverSsl = wolfSSL_new(serverCtx);
  43286. #endif
  43287. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43288. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43289. #ifndef NO_WOLFSSL_CLIENT
  43290. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43291. #endif
  43292. #ifndef NO_WOLFSSL_SERVER
  43293. #ifndef WOLFSSL_NO_TLS12
  43294. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  43295. #endif
  43296. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43297. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43298. WOLFSSL_SUCCESS);
  43299. #endif
  43300. #endif
  43301. #ifdef HAVE_SUPPORTED_CURVES
  43302. #ifdef HAVE_ECC
  43303. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43304. #ifndef NO_WOLFSSL_SERVER
  43305. do {
  43306. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43307. #ifdef WOLFSSL_ASYNC_CRYPT
  43308. if (ret == WC_PENDING_E)
  43309. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43310. #endif
  43311. } while (ret == WC_PENDING_E);
  43312. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43313. #endif
  43314. #ifndef NO_WOLFSSL_CLIENT
  43315. #ifndef WOLFSSL_NO_TLS12
  43316. do {
  43317. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43318. #ifdef WOLFSSL_ASYNC_CRYPT
  43319. if (ret == WC_PENDING_E)
  43320. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43321. #endif
  43322. } while (ret == WC_PENDING_E);
  43323. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43324. #endif
  43325. do {
  43326. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43327. #ifdef WOLFSSL_ASYNC_CRYPT
  43328. if (ret == WC_PENDING_E)
  43329. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43330. #endif
  43331. } while (ret == WC_PENDING_E);
  43332. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43333. #endif
  43334. #elif defined(HAVE_CURVE25519)
  43335. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43336. #ifndef NO_WOLFSSL_SERVER
  43337. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43338. WOLFSSL_SUCCESS);
  43339. #endif
  43340. #ifndef NO_WOLFSSL_CLIENT
  43341. #ifndef WOLFSSL_NO_TLS12
  43342. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43343. WOLFSSL_SUCCESS);
  43344. #endif
  43345. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43346. WOLFSSL_SUCCESS);
  43347. #endif
  43348. #elif defined(HAVE_CURVE448)
  43349. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43350. #ifndef NO_WOLFSSL_SERVER
  43351. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43352. WOLFSSL_SUCCESS);
  43353. #endif
  43354. #ifndef NO_WOLFSSL_CLIENT
  43355. #ifndef WOLFSSL_NO_TLS12
  43356. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43357. WOLFSSL_SUCCESS);
  43358. #endif
  43359. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43360. WOLFSSL_SUCCESS);
  43361. #endif
  43362. #else
  43363. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43364. #ifndef NO_WOLFSSL_CLIENT
  43365. #ifndef WOLFSSL_NO_TLS12
  43366. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43367. NOT_COMPILED_IN);
  43368. #endif
  43369. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43370. NOT_COMPILED_IN);
  43371. #endif
  43372. #endif
  43373. #if defined(HAVE_PQC)
  43374. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43375. #ifndef NO_WOLFSSL_SERVER
  43376. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43377. WOLFSSL_SUCCESS);
  43378. #endif
  43379. #ifndef NO_WOLFSSL_CLIENT
  43380. #ifndef WOLFSSL_NO_TLS12
  43381. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43382. BAD_FUNC_ARG);
  43383. #endif
  43384. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43385. WOLFSSL_SUCCESS);
  43386. #endif
  43387. #endif
  43388. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43389. #ifndef NO_WOLFSSL_SERVER
  43390. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43391. #endif
  43392. #ifndef NO_WOLFSSL_CLIENT
  43393. #ifndef WOLFSSL_NO_TLS12
  43394. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43395. #endif
  43396. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43397. #endif
  43398. #endif /* HAVE_SUPPORTED_CURVES */
  43399. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43400. #ifndef NO_WOLFSSL_CLIENT
  43401. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43402. #endif
  43403. #ifndef NO_WOLFSSL_SERVER
  43404. #ifndef WOLFSSL_NO_TLS12
  43405. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43406. #endif
  43407. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43408. #endif
  43409. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43410. #ifndef NO_WOLFSSL_CLIENT
  43411. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43412. #endif
  43413. #ifndef NO_WOLFSSL_SERVER
  43414. #ifndef WOLFSSL_NO_TLS12
  43415. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43416. #endif
  43417. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43418. #endif
  43419. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43420. #ifndef NO_WOLFSSL_CLIENT
  43421. #ifndef WOLFSSL_NO_TLS12
  43422. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43423. #endif
  43424. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43425. #endif
  43426. #ifndef NO_WOLFSSL_SERVER
  43427. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43428. #endif
  43429. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43430. #ifndef NO_WOLFSSL_CLIENT
  43431. #ifndef WOLFSSL_NO_TLS12
  43432. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43433. #endif
  43434. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43435. #endif
  43436. #ifndef NO_WOLFSSL_SERVER
  43437. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43438. #endif
  43439. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43440. #ifndef NO_WOLFSSL_CLIENT
  43441. #ifndef WOLFSSL_NO_TLS12
  43442. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43443. #endif
  43444. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43445. #endif
  43446. #ifndef NO_WOLFSSL_SERVER
  43447. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43448. #endif
  43449. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43450. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43451. #ifndef NO_WOLFSSL_CLIENT
  43452. #ifndef WOLFSSL_NO_TLS12
  43453. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43454. BAD_FUNC_ARG);
  43455. #endif
  43456. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43457. #endif
  43458. #ifndef NO_WOLFSSL_SERVER
  43459. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43460. #endif
  43461. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43462. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43463. #ifndef NO_WOLFSSL_SERVER
  43464. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43465. #endif
  43466. #ifndef NO_WOLFSSL_CLIENT
  43467. #ifndef WOLFSSL_NO_TLS12
  43468. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43469. BAD_FUNC_ARG);
  43470. #endif
  43471. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43472. #endif
  43473. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43474. #ifndef NO_WOLFSSL_SERVER
  43475. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43476. #endif
  43477. #ifndef NO_WOLFSSL_CLIENT
  43478. #ifndef WOLFSSL_NO_TLS12
  43479. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43480. BAD_FUNC_ARG);
  43481. #endif
  43482. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  43483. #endif
  43484. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  43485. #ifndef NO_WOLFSSL_CLIENT
  43486. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  43487. #endif
  43488. #ifndef NO_WOLFSSL_SERVER
  43489. #ifndef WOLFSSL_NO_TLS12
  43490. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  43491. BAD_FUNC_ARG);
  43492. #endif
  43493. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  43494. #endif
  43495. #endif
  43496. #ifdef HAVE_ECC
  43497. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  43498. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  43499. #ifndef NO_WOLFSSL_SERVER
  43500. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  43501. #endif
  43502. #ifndef NO_WOLFSSL_CLIENT
  43503. #ifndef WOLFSSL_NO_TLS12
  43504. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  43505. #endif
  43506. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  43507. #endif
  43508. #endif
  43509. #ifdef HAVE_SUPPORTED_CURVES
  43510. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43511. #ifndef NO_WOLFSSL_CLIENT
  43512. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  43513. #endif
  43514. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43515. #ifndef NO_WOLFSSL_CLIENT
  43516. #ifndef WOLFSSL_NO_TLS12
  43517. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  43518. BAD_FUNC_ARG);
  43519. #endif
  43520. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  43521. WOLFSSL_MAX_GROUP_COUNT + 1),
  43522. BAD_FUNC_ARG);
  43523. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  43524. WOLFSSL_SUCCESS);
  43525. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  43526. BAD_FUNC_ARG);
  43527. #endif
  43528. #ifndef NO_WOLFSSL_SERVER
  43529. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  43530. WOLFSSL_SUCCESS);
  43531. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  43532. BAD_FUNC_ARG);
  43533. #endif
  43534. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43535. #ifndef NO_WOLFSSL_CLIENT
  43536. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  43537. #endif
  43538. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43539. #ifndef NO_WOLFSSL_CLIENT
  43540. #ifndef WOLFSSL_NO_TLS12
  43541. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  43542. BAD_FUNC_ARG);
  43543. #endif
  43544. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  43545. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43546. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  43547. WOLFSSL_SUCCESS);
  43548. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  43549. BAD_FUNC_ARG);
  43550. #endif
  43551. #ifndef NO_WOLFSSL_SERVER
  43552. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  43553. WOLFSSL_SUCCESS);
  43554. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  43555. BAD_FUNC_ARG);
  43556. #endif
  43557. #ifdef OPENSSL_EXTRA
  43558. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43559. #ifndef NO_WOLFSSL_CLIENT
  43560. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  43561. #endif
  43562. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43563. #ifndef NO_WOLFSSL_CLIENT
  43564. #ifndef WOLFSSL_NO_TLS12
  43565. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  43566. WOLFSSL_FAILURE);
  43567. #endif
  43568. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  43569. WOLFSSL_SUCCESS);
  43570. #endif
  43571. #ifndef NO_WOLFSSL_SERVER
  43572. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  43573. WOLFSSL_SUCCESS);
  43574. #endif
  43575. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43576. #ifndef NO_WOLFSSL_CLIENT
  43577. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  43578. #endif
  43579. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43580. #ifndef NO_WOLFSSL_CLIENT
  43581. #ifndef WOLFSSL_NO_TLS12
  43582. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  43583. WOLFSSL_FAILURE);
  43584. #endif
  43585. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  43586. WOLFSSL_SUCCESS);
  43587. #endif
  43588. #ifndef NO_WOLFSSL_SERVER
  43589. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  43590. WOLFSSL_SUCCESS);
  43591. #endif
  43592. #endif /* OPENSSL_EXTRA */
  43593. #endif /* HAVE_SUPPORTED_CURVES */
  43594. #endif /* HAVE_ECC */
  43595. #ifdef WOLFSSL_EARLY_DATA
  43596. #ifndef OPENSSL_EXTRA
  43597. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43598. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43599. #else
  43600. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43601. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43602. #endif
  43603. #ifndef NO_WOLFSSL_CLIENT
  43604. #ifndef OPENSSL_EXTRA
  43605. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43606. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43607. #else
  43608. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43609. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43610. #endif
  43611. #endif
  43612. #ifndef NO_WOLFSSL_SERVER
  43613. #ifndef WOLFSSL_NO_TLS12
  43614. #ifndef OPENSSL_EXTRA
  43615. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43616. BAD_FUNC_ARG);
  43617. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43618. #else
  43619. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43620. BAD_FUNC_ARG);
  43621. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43622. #endif
  43623. #endif
  43624. #ifndef OPENSSL_EXTRA
  43625. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  43626. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  43627. #else
  43628. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  43629. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  43630. #endif
  43631. #endif
  43632. #ifndef OPENSSL_EXTRA
  43633. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43634. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43635. #else
  43636. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43637. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43638. #endif
  43639. #ifndef NO_WOLFSSL_CLIENT
  43640. #ifndef OPENSSL_EXTRA
  43641. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  43642. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  43643. #else
  43644. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  43645. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  43646. #endif
  43647. #endif
  43648. #ifndef NO_WOLFSSL_SERVER
  43649. #ifndef WOLFSSL_NO_TLS12
  43650. #ifndef OPENSSL_EXTRA
  43651. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43652. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43653. #else
  43654. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43655. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43656. #endif
  43657. #endif
  43658. #ifndef OPENSSL_EXTRA
  43659. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  43660. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  43661. #else
  43662. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  43663. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  43664. #endif
  43665. #endif
  43666. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  43667. &outSz), BAD_FUNC_ARG);
  43668. #ifndef NO_WOLFSSL_CLIENT
  43669. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  43670. &outSz), BAD_FUNC_ARG);
  43671. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  43672. BAD_FUNC_ARG);
  43673. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43674. sizeof(earlyData), NULL),
  43675. BAD_FUNC_ARG);
  43676. #endif
  43677. #ifndef NO_WOLFSSL_SERVER
  43678. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  43679. sizeof(earlyData), &outSz),
  43680. SIDE_ERROR);
  43681. #endif
  43682. #ifndef NO_WOLFSSL_CLIENT
  43683. #ifndef WOLFSSL_NO_TLS12
  43684. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  43685. sizeof(earlyData), &outSz),
  43686. BAD_FUNC_ARG);
  43687. #endif
  43688. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43689. sizeof(earlyData), &outSz),
  43690. WOLFSSL_FATAL_ERROR);
  43691. #endif
  43692. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  43693. sizeof(earlyDataBuffer), &outSz),
  43694. BAD_FUNC_ARG);
  43695. #ifndef NO_WOLFSSL_SERVER
  43696. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  43697. sizeof(earlyDataBuffer), &outSz),
  43698. BAD_FUNC_ARG);
  43699. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  43700. BAD_FUNC_ARG);
  43701. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43702. sizeof(earlyDataBuffer), NULL),
  43703. BAD_FUNC_ARG);
  43704. #endif
  43705. #ifndef NO_WOLFSSL_CLIENT
  43706. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  43707. sizeof(earlyDataBuffer), &outSz),
  43708. SIDE_ERROR);
  43709. #endif
  43710. #ifndef NO_WOLFSSL_SERVER
  43711. #ifndef WOLFSSL_NO_TLS12
  43712. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  43713. sizeof(earlyDataBuffer), &outSz),
  43714. BAD_FUNC_ARG);
  43715. #endif
  43716. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43717. sizeof(earlyDataBuffer), &outSz),
  43718. WOLFSSL_FATAL_ERROR);
  43719. #endif
  43720. #endif
  43721. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  43722. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  43723. #endif
  43724. #ifndef NO_WOLFSSL_SERVER
  43725. wolfSSL_free(serverSsl);
  43726. wolfSSL_CTX_free(serverCtx);
  43727. #endif
  43728. #ifndef NO_WOLFSSL_CLIENT
  43729. wolfSSL_free(clientSsl);
  43730. wolfSSL_CTX_free(clientCtx);
  43731. #endif
  43732. #ifndef WOLFSSL_NO_TLS12
  43733. #ifndef NO_WOLFSSL_SERVER
  43734. wolfSSL_free(serverTls12Ssl);
  43735. wolfSSL_CTX_free(serverTls12Ctx);
  43736. #endif
  43737. #ifndef NO_WOLFSSL_CLIENT
  43738. wolfSSL_free(clientTls12Ssl);
  43739. wolfSSL_CTX_free(clientTls12Ctx);
  43740. #endif
  43741. #endif
  43742. return TEST_RES_CHECK(1);
  43743. }
  43744. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43745. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43746. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43747. /* Called when writing. */
  43748. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43749. {
  43750. (void)ssl;
  43751. (void)buf;
  43752. (void)sz;
  43753. (void)ctx;
  43754. /* Force error return from wolfSSL_accept_TLSv13(). */
  43755. return WANT_WRITE;
  43756. }
  43757. /* Called when reading. */
  43758. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43759. {
  43760. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  43761. int len = (int)msg->length;
  43762. (void)ssl;
  43763. (void)sz;
  43764. /* Pass back as much of message as will fit in buffer. */
  43765. if (len > sz)
  43766. len = sz;
  43767. XMEMCPY(buf, msg->buffer, len);
  43768. /* Move over returned data. */
  43769. msg->buffer += len;
  43770. msg->length -= len;
  43771. /* Amount actually copied. */
  43772. return len;
  43773. }
  43774. #endif
  43775. static int test_tls13_cipher_suites(void)
  43776. {
  43777. int res = TEST_SKIPPED;
  43778. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43779. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43780. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43781. WOLFSSL_CTX* ctx;
  43782. WOLFSSL *ssl;
  43783. int i;
  43784. byte clientHello[] = {
  43785. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  43786. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  43787. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  43788. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  43789. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  43790. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  43791. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  43792. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  43793. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43794. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  43795. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  43796. 0x13, 0x01,
  43797. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  43798. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  43799. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  43800. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  43801. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  43802. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  43803. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  43804. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  43805. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  43806. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  43807. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  43808. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  43809. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  43810. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  43811. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  43812. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  43813. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  43814. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  43815. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  43816. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  43817. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  43818. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  43819. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  43820. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  43821. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  43822. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  43823. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  43824. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  43825. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  43826. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  43827. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  43828. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  43829. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  43830. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  43831. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  43832. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43834. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  43835. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  43836. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  43837. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  43838. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  43839. };
  43840. WOLFSSL_BUFFER_INFO msg;
  43841. /* Offset into ClientHello message data of first cipher suite. */
  43842. const int csOff = 78;
  43843. /* Server cipher list. */
  43844. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  43845. /* Suite list with duplicates. */
  43846. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  43847. "TLS13-AES128-GCM-SHA256:"
  43848. "TLS13-AES256-GCM-SHA384:"
  43849. "TLS13-AES256-GCM-SHA384:"
  43850. "TLS13-AES128-GCM-SHA256";
  43851. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43852. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43853. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43854. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43855. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43856. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  43857. #endif
  43858. /* Set up wolfSSL context. */
  43859. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  43860. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  43861. WOLFSSL_FILETYPE_PEM));
  43862. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  43863. WOLFSSL_FILETYPE_PEM));
  43864. /* Read from 'msg'. */
  43865. wolfSSL_SetIORecv(ctx, CsRecv);
  43866. /* No where to send to - dummy sender. */
  43867. wolfSSL_SetIOSend(ctx, CsSend);
  43868. /* Test cipher suite list with many copies of a cipher suite. */
  43869. AssertNotNull(ssl = wolfSSL_new(ctx));
  43870. msg.buffer = clientHello;
  43871. msg.length = (unsigned int)sizeof(clientHello);
  43872. wolfSSL_SetIOReadCtx(ssl, &msg);
  43873. /* Force server to have as many occurrences of same cipher suite as
  43874. * possible. */
  43875. ssl->suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  43876. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  43877. ssl->suites->suites[i + 0] = TLS13_BYTE;
  43878. ssl->suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  43879. }
  43880. /* Test multiple occurrences of same cipher suite. */
  43881. wolfSSL_accept_TLSv13(ssl);
  43882. wolfSSL_free(ssl);
  43883. /* Set client order opposite to server order:
  43884. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  43885. clientHello[csOff + 0] = TLS13_BYTE;
  43886. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  43887. clientHello[csOff + 2] = TLS13_BYTE;
  43888. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  43889. /* Test server order negotiation. */
  43890. AssertNotNull(ssl = wolfSSL_new(ctx));
  43891. msg.buffer = clientHello;
  43892. msg.length = (unsigned int)sizeof(clientHello);
  43893. wolfSSL_SetIOReadCtx(ssl, &msg);
  43894. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43895. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43896. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  43897. wolfSSL_accept_TLSv13(ssl);
  43898. /* Check refined order - server order. */
  43899. AssertIntEQ(ssl->suites->suiteSz, 4);
  43900. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43901. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43902. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43903. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43904. wolfSSL_free(ssl);
  43905. /* Test client order negotiation. */
  43906. AssertNotNull(ssl = wolfSSL_new(ctx));
  43907. msg.buffer = clientHello;
  43908. msg.length = (unsigned int)sizeof(clientHello);
  43909. wolfSSL_SetIOReadCtx(ssl, &msg);
  43910. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43911. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43912. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  43913. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  43914. wolfSSL_accept_TLSv13(ssl);
  43915. /* Check refined order - client order. */
  43916. AssertIntEQ(ssl->suites->suiteSz, 4);
  43917. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43918. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43919. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43920. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43921. wolfSSL_free(ssl);
  43922. /* Check duplicate detection is working. */
  43923. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  43924. AssertIntEQ(ctx->suites->suiteSz, 4);
  43925. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43926. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43927. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43928. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43929. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43930. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  43931. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  43932. AssertIntEQ(ctx->suites->suiteSz, 4);
  43933. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43934. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43935. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43936. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43937. #endif
  43938. wolfSSL_CTX_free(ctx);
  43939. res = TEST_RES_CHECK(1);
  43940. #endif
  43941. return res;
  43942. }
  43943. #endif
  43944. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  43945. !defined(NO_OLD_TLS))
  43946. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  43947. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  43948. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  43949. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  43950. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  43951. const unsigned char* priv, unsigned int privSz,
  43952. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  43953. unsigned char* out, unsigned int* outlen,
  43954. void* ctx)
  43955. {
  43956. int result;
  43957. /* Test fail when context associated with WOLFSSL is NULL */
  43958. if (ctx == NULL) {
  43959. return -1;
  43960. }
  43961. (void)ssl;
  43962. /* return 0 on success */
  43963. PRIVATE_KEY_UNLOCK();
  43964. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  43965. PRIVATE_KEY_LOCK();
  43966. return result;
  43967. }
  43968. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  43969. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  43970. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  43971. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  43972. WOLFSSL_SUCCESS);
  43973. #endif
  43974. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  43975. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  43976. WOLFSSL_SUCCESS);
  43977. #endif
  43978. }
  43979. static void test_dh_ssl_setup(WOLFSSL* ssl)
  43980. {
  43981. static int dh_test_ctx = 1;
  43982. int ret;
  43983. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  43984. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  43985. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43986. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43987. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43988. }
  43989. }
  43990. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  43991. {
  43992. int ret;
  43993. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  43994. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  43995. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43996. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43997. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43998. }
  43999. }
  44000. #endif
  44001. static int test_DhCallbacks(void)
  44002. {
  44003. int res = TEST_SKIPPED;
  44004. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  44005. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  44006. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44007. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  44008. WOLFSSL_CTX *ctx;
  44009. WOLFSSL *ssl;
  44010. tcp_ready ready;
  44011. func_args server_args;
  44012. func_args client_args;
  44013. THREAD_TYPE serverThread;
  44014. callback_functions func_cb_client;
  44015. callback_functions func_cb_server;
  44016. int test;
  44017. #ifndef NO_WOLFSSL_CLIENT
  44018. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44019. #else
  44020. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  44021. #endif
  44022. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  44023. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  44024. /* load client ca cert */
  44025. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  44026. WOLFSSL_SUCCESS);
  44027. /* test with NULL arguments */
  44028. wolfSSL_SetDhAgreeCtx(NULL, &test);
  44029. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  44030. /* test success case */
  44031. test = 1;
  44032. AssertNotNull(ssl = wolfSSL_new(ctx));
  44033. wolfSSL_SetDhAgreeCtx(ssl, &test);
  44034. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  44035. wolfSSL_free(ssl);
  44036. wolfSSL_CTX_free(ctx);
  44037. /* test a connection where callback is used */
  44038. #ifdef WOLFSSL_TIRTOS
  44039. fdOpenSession(Task_self());
  44040. #endif
  44041. XMEMSET(&server_args, 0, sizeof(func_args));
  44042. XMEMSET(&client_args, 0, sizeof(func_args));
  44043. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  44044. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  44045. StartTCP();
  44046. InitTcpReady(&ready);
  44047. #if defined(USE_WINDOWS_API)
  44048. /* use RNG to get random port if using windows */
  44049. ready.port = GetRandomPort();
  44050. #endif
  44051. server_args.signal = &ready;
  44052. client_args.signal = &ready;
  44053. server_args.return_code = TEST_FAIL;
  44054. client_args.return_code = TEST_FAIL;
  44055. /* set callbacks to use DH functions */
  44056. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44057. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  44058. #ifndef WOLFSSL_NO_TLS12
  44059. func_cb_client.method = wolfTLSv1_2_client_method;
  44060. #else
  44061. func_cb_client.method = wolfTLSv1_3_client_method;
  44062. #endif
  44063. client_args.callbacks = &func_cb_client;
  44064. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44065. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  44066. #ifndef WOLFSSL_NO_TLS12
  44067. func_cb_server.method = wolfTLSv1_2_server_method;
  44068. #else
  44069. func_cb_server.method = wolfTLSv1_3_server_method;
  44070. #endif
  44071. server_args.callbacks = &func_cb_server;
  44072. start_thread(test_server_nofail, &server_args, &serverThread);
  44073. wait_tcp_ready(&server_args);
  44074. test_client_nofail(&client_args, NULL);
  44075. join_thread(serverThread);
  44076. AssertTrue(client_args.return_code);
  44077. AssertTrue(server_args.return_code);
  44078. FreeTcpReady(&ready);
  44079. #ifdef WOLFSSL_TIRTOS
  44080. fdOpenSession(Task_self());
  44081. #endif
  44082. /* now set user ctx to not be 1 so that the callback returns fail case */
  44083. #ifdef WOLFSSL_TIRTOS
  44084. fdOpenSession(Task_self());
  44085. #endif
  44086. XMEMSET(&server_args, 0, sizeof(func_args));
  44087. XMEMSET(&client_args, 0, sizeof(func_args));
  44088. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  44089. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  44090. StartTCP();
  44091. InitTcpReady(&ready);
  44092. #if defined(USE_WINDOWS_API)
  44093. /* use RNG to get random port if using windows */
  44094. ready.port = GetRandomPort();
  44095. #endif
  44096. server_args.signal = &ready;
  44097. client_args.signal = &ready;
  44098. server_args.return_code = TEST_FAIL;
  44099. client_args.return_code = TEST_FAIL;
  44100. /* set callbacks to use DH functions */
  44101. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44102. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  44103. #ifndef WOLFSSL_NO_TLS12
  44104. func_cb_client.method = wolfTLSv1_2_client_method;
  44105. #else
  44106. func_cb_client.method = wolfTLSv1_3_client_method;
  44107. #endif
  44108. client_args.callbacks = &func_cb_client;
  44109. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44110. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  44111. #ifndef WOLFSSL_NO_TLS12
  44112. func_cb_server.method = wolfTLSv1_2_server_method;
  44113. #else
  44114. func_cb_server.method = wolfTLSv1_3_server_method;
  44115. #endif
  44116. server_args.callbacks = &func_cb_server;
  44117. start_thread(test_server_nofail, &server_args, &serverThread);
  44118. wait_tcp_ready(&server_args);
  44119. test_client_nofail(&client_args, NULL);
  44120. join_thread(serverThread);
  44121. AssertIntEQ(client_args.return_code, TEST_FAIL);
  44122. AssertIntEQ(server_args.return_code, TEST_FAIL);
  44123. FreeTcpReady(&ready);
  44124. #ifdef WOLFSSL_TIRTOS
  44125. fdOpenSession(Task_self());
  44126. #endif
  44127. res = TEST_RES_CHECK(1);
  44128. #endif
  44129. return res;
  44130. }
  44131. #endif /* HAVE_PK_CALLBACKS */
  44132. #ifdef HAVE_HASHDRBG
  44133. #ifdef TEST_RESEED_INTERVAL
  44134. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44135. {
  44136. int i, ret;
  44137. WC_RNG rng;
  44138. byte key[32];
  44139. ret = wc_InitRng(&rng);
  44140. if (ret == 0) {
  44141. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44142. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44143. if (ret != 0) {
  44144. break;
  44145. }
  44146. }
  44147. }
  44148. wc_FreeRng(&rng);
  44149. return TEST_RES_CHECK(ret == 0);
  44150. }
  44151. #endif /* TEST_RESEED_INTERVAL */
  44152. static int test_wc_RNG_GenerateBlock(void)
  44153. {
  44154. int i, ret;
  44155. WC_RNG rng;
  44156. byte key[32];
  44157. ret = wc_InitRng(&rng);
  44158. if (ret == 0) {
  44159. for (i = 0; i < 10; i++) {
  44160. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44161. if (ret != 0) {
  44162. break;
  44163. }
  44164. }
  44165. }
  44166. wc_FreeRng(&rng);
  44167. (void)rng; /* for WC_NO_RNG case */
  44168. (void)key;
  44169. return TEST_RES_CHECK(ret == 0);
  44170. }
  44171. #endif
  44172. /*
  44173. * Testing get_rand_digit
  44174. */
  44175. static int test_get_rand_digit(void)
  44176. {
  44177. int res = TEST_SKIPPED;
  44178. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44179. int ret = 0;
  44180. WC_RNG rng;
  44181. mp_digit d;
  44182. ret = wc_InitRng(&rng);
  44183. if (ret == 0) {
  44184. ret = get_rand_digit(&rng, &d);
  44185. }
  44186. if (ret == 0) {
  44187. ret = get_rand_digit(NULL, NULL);
  44188. if (ret == BAD_FUNC_ARG) {
  44189. ret = 0;
  44190. }
  44191. }
  44192. if (ret == 0) {
  44193. ret = get_rand_digit(NULL, &d);
  44194. if (ret == BAD_FUNC_ARG) {
  44195. ret = 0;
  44196. }
  44197. }
  44198. if (ret == 0) {
  44199. ret = get_rand_digit(&rng, NULL);
  44200. if (ret == BAD_FUNC_ARG) {
  44201. ret = 0;
  44202. }
  44203. }
  44204. if (ret == 0) {
  44205. ret = wc_FreeRng(&rng);
  44206. }
  44207. res = TEST_RES_CHECK(ret == 0);
  44208. #endif
  44209. return res;
  44210. }/* End test_get_rand_digit*/
  44211. /*
  44212. * Testing get_digit_count
  44213. */
  44214. static int test_get_digit_count(void)
  44215. {
  44216. int res = TEST_SKIPPED;
  44217. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44218. int ret = 0;
  44219. mp_int a;
  44220. if (mp_init(&a) != MP_OKAY) {
  44221. ret = -1;
  44222. }
  44223. if (ret == 0) {
  44224. ret = get_digit_count(NULL);
  44225. }
  44226. if (ret == 0) {
  44227. ret = get_digit_count(&a);
  44228. }
  44229. mp_clear(&a);
  44230. res = TEST_RES_CHECK(ret == 0);
  44231. #endif
  44232. return res;
  44233. }/* End test_get_digit_count*/
  44234. /*
  44235. * Testing mp_cond_copy
  44236. */
  44237. static int test_mp_cond_copy(void)
  44238. {
  44239. int res = TEST_SKIPPED;
  44240. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44241. defined(WOLFSSL_PUBLIC_MP)
  44242. int ret = 0;
  44243. mp_int a;
  44244. mp_int b;
  44245. int copy = 0;
  44246. if (mp_init(&a) != MP_OKAY) {
  44247. ret = -1;
  44248. }
  44249. if (ret == 0) {
  44250. if (mp_init(&b) != MP_OKAY) {
  44251. ret = -1;
  44252. }
  44253. }
  44254. if (ret == 0) {
  44255. ret = mp_cond_copy(NULL, copy, NULL);
  44256. if (ret == BAD_FUNC_ARG) {
  44257. ret = 0;
  44258. }
  44259. }
  44260. if (ret == 0) {
  44261. ret = mp_cond_copy(NULL, copy, &b);
  44262. if (ret == BAD_FUNC_ARG) {
  44263. ret = 0;
  44264. }
  44265. }
  44266. if (ret == 0) {
  44267. ret = mp_cond_copy(&a, copy, NULL);
  44268. if (ret == BAD_FUNC_ARG) {
  44269. ret = 0;
  44270. }
  44271. }
  44272. if (ret == 0) {
  44273. ret = mp_cond_copy(&a, copy, &b);
  44274. }
  44275. mp_clear(&a);
  44276. mp_clear(&b);
  44277. res = TEST_RES_CHECK(ret == 0);
  44278. #endif
  44279. return res;
  44280. }/* End test_mp_cond_copy*/
  44281. /*
  44282. * Testing mp_rand
  44283. */
  44284. static int test_mp_rand(void)
  44285. {
  44286. int res = TEST_SKIPPED;
  44287. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44288. int ret = 0;
  44289. mp_int a;
  44290. int digits = 1;
  44291. WC_RNG rng;
  44292. if (mp_init(&a) != MP_OKAY) {
  44293. ret = -1;
  44294. }
  44295. if (ret == 0) {
  44296. ret = wc_InitRng(&rng);
  44297. }
  44298. if (ret == 0) {
  44299. ret = mp_rand(&a, digits, NULL);
  44300. if (ret == MISSING_RNG_E) {
  44301. ret = 0;
  44302. }
  44303. }
  44304. if (ret == 0) {
  44305. ret = mp_rand(NULL, digits, &rng);
  44306. if (ret == BAD_FUNC_ARG) {
  44307. ret = 0;
  44308. }
  44309. }
  44310. if (ret == 0) {
  44311. ret = mp_rand(&a, 0, &rng);
  44312. if (ret == BAD_FUNC_ARG) {
  44313. ret = 0;
  44314. }
  44315. }
  44316. if (ret == 0) {
  44317. ret = mp_rand(&a, digits, &rng);
  44318. }
  44319. mp_clear(&a);
  44320. wc_FreeRng(&rng);
  44321. res = TEST_RES_CHECK(ret == 0);
  44322. #endif
  44323. return res;
  44324. }/* End test_mp_rand*/
  44325. /*
  44326. * Testing get_digit
  44327. */
  44328. static int test_get_digit(void)
  44329. {
  44330. int res = TEST_SKIPPED;
  44331. #if defined(WOLFSSL_PUBLIC_MP)
  44332. int ret = 0;
  44333. mp_int a;
  44334. int n = 0;
  44335. if (mp_init(&a) != MP_OKAY) {
  44336. ret = -1;
  44337. }
  44338. if (ret == 0) {
  44339. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  44340. ret = -1;
  44341. }
  44342. }
  44343. if (ret == 0) {
  44344. if (get_digit(NULL, n) == 0) { /* Should hit this */
  44345. ret = 0;
  44346. }
  44347. }
  44348. if (ret == 0) {
  44349. if (get_digit(&a, n) != 0) { /* Should not hit this */
  44350. ret = -1;
  44351. }
  44352. }
  44353. if (ret == 0) {
  44354. if (get_digit(&a, n) == 0) { /* Should hit this */
  44355. ret = 0;
  44356. }
  44357. }
  44358. mp_clear(&a);
  44359. res = TEST_RES_CHECK(ret == 0);
  44360. #endif
  44361. return res;
  44362. }/* End test_get_digit*/
  44363. /*
  44364. * Testing wc_export_int
  44365. */
  44366. static int test_wc_export_int(void)
  44367. {
  44368. int res = TEST_SKIPPED;
  44369. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44370. defined(WOLFSSL_PUBLIC_MP)
  44371. int ret = 0;
  44372. mp_int mp;
  44373. byte buf[32];
  44374. word32 keySz = (word32)sizeof(buf);
  44375. word32 len = (word32)sizeof(buf);
  44376. if (mp_init(&mp) != MP_OKAY) {
  44377. ret = -1;
  44378. }
  44379. if (ret == 0) {
  44380. ret = mp_set(&mp, 1234);
  44381. }
  44382. if (ret == 0) {
  44383. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44384. if (ret == BAD_FUNC_ARG) {
  44385. ret = 0;
  44386. }
  44387. }
  44388. if (ret == 0) {
  44389. len = sizeof(buf)-1;
  44390. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44391. if (ret == BUFFER_E) {
  44392. ret = 0;
  44393. }
  44394. }
  44395. if (ret == 0) {
  44396. len = sizeof(buf);
  44397. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44398. }
  44399. if (ret == 0) {
  44400. len = 4; /* test input too small */
  44401. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44402. if (ret == BUFFER_E) {
  44403. ret = 0;
  44404. }
  44405. }
  44406. if (ret == 0) {
  44407. len = sizeof(buf);
  44408. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44409. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44410. if (ret == 0 && len != 5) {
  44411. ret = BAD_FUNC_ARG;
  44412. }
  44413. }
  44414. mp_clear(&mp);
  44415. res = TEST_RES_CHECK(ret == 0);
  44416. #endif
  44417. return res;
  44418. }/* End test_wc_export_int*/
  44419. static int test_wc_InitRngNonce(void)
  44420. {
  44421. int res = TEST_SKIPPED;
  44422. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44423. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44424. int ret;
  44425. WC_RNG rng;
  44426. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44427. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44428. word32 nonceSz = sizeof(nonce);
  44429. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  44430. wc_FreeRng(&rng);
  44431. res = TEST_RES_CHECK(ret == 0);
  44432. #endif
  44433. return res;
  44434. }/* End test_wc_InitRngNonce*/
  44435. /*
  44436. * Testing wc_InitRngNonce_ex
  44437. */
  44438. static int test_wc_InitRngNonce_ex(void)
  44439. {
  44440. int res = TEST_SKIPPED;
  44441. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44442. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44443. int ret;
  44444. WC_RNG rng;
  44445. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44446. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44447. word32 nonceSz = sizeof(nonce);
  44448. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  44449. wc_FreeRng(&rng);
  44450. res = TEST_RES_CHECK(ret == 0);
  44451. #endif
  44452. return res;
  44453. }/*End test_wc_InitRngNonce_ex*/
  44454. static int test_wolfSSL_X509_CRL(void)
  44455. {
  44456. int res = TEST_SKIPPED;
  44457. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44458. X509_CRL *crl;
  44459. char pem[][100] = {
  44460. "./certs/crl/crl.pem",
  44461. "./certs/crl/crl2.pem",
  44462. "./certs/crl/caEccCrl.pem",
  44463. "./certs/crl/eccCliCRL.pem",
  44464. "./certs/crl/eccSrvCRL.pem",
  44465. ""
  44466. };
  44467. #ifndef NO_BIO
  44468. BIO *bio;
  44469. #endif
  44470. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44471. char der[][100] = {
  44472. "./certs/crl/crl.der",
  44473. "./certs/crl/crl2.der",
  44474. ""};
  44475. #endif
  44476. XFILE fp;
  44477. int i;
  44478. for (i = 0; pem[i][0] != '\0'; i++)
  44479. {
  44480. fp = XFOPEN(pem[i], "rb");
  44481. AssertTrue((fp != XBADFILE));
  44482. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  44483. AssertNotNull(crl);
  44484. X509_CRL_free(crl);
  44485. XFCLOSE(fp);
  44486. fp = XFOPEN(pem[i], "rb");
  44487. AssertTrue((fp != XBADFILE));
  44488. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  44489. AssertNotNull(crl);
  44490. X509_CRL_free(crl);
  44491. XFCLOSE(fp);
  44492. }
  44493. #ifndef NO_BIO
  44494. for (i = 0; pem[i][0] != '\0'; i++)
  44495. {
  44496. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  44497. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44498. X509_CRL_free(crl);
  44499. BIO_free(bio);
  44500. }
  44501. #endif
  44502. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44503. for (i = 0; der[i][0] != '\0'; i++) {
  44504. fp = XFOPEN(der[i], "rb");
  44505. AssertTrue((fp != XBADFILE));
  44506. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44507. AssertNotNull(crl);
  44508. X509_CRL_free(crl);
  44509. XFCLOSE(fp);
  44510. fp = XFOPEN(der[i], "rb");
  44511. AssertTrue((fp != XBADFILE));
  44512. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44513. AssertNotNull(crl);
  44514. X509_CRL_free(crl);
  44515. XFCLOSE(fp);
  44516. }
  44517. #endif
  44518. res = TEST_RES_CHECK(1);
  44519. #endif
  44520. return res;
  44521. }
  44522. static int test_wolfSSL_X509_load_crl_file(void)
  44523. {
  44524. int res = TEST_SKIPPED;
  44525. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44526. !defined(NO_RSA) && !defined(NO_BIO)
  44527. int i;
  44528. char pem[][100] = {
  44529. "./certs/crl/crl.pem",
  44530. "./certs/crl/crl2.pem",
  44531. "./certs/crl/caEccCrl.pem",
  44532. "./certs/crl/eccCliCRL.pem",
  44533. "./certs/crl/eccSrvCRL.pem",
  44534. ""
  44535. };
  44536. char der[][100] = {
  44537. "./certs/crl/crl.der",
  44538. "./certs/crl/crl2.der",
  44539. ""
  44540. };
  44541. WOLFSSL_X509_STORE* store;
  44542. WOLFSSL_X509_LOOKUP* lookup;
  44543. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44544. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44545. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44546. X509_FILETYPE_PEM), 1);
  44547. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44548. X509_FILETYPE_PEM), 1);
  44549. if (store) {
  44550. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44551. WOLFSSL_FILETYPE_PEM), 1);
  44552. /* since store hasn't yet known the revoked cert*/
  44553. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44554. WOLFSSL_FILETYPE_PEM), 1);
  44555. }
  44556. for (i = 0; pem[i][0] != '\0'; i++)
  44557. {
  44558. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  44559. }
  44560. if (store) {
  44561. /* since store knows crl list */
  44562. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44563. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44564. }
  44565. /* once feeing store */
  44566. X509_STORE_free(store);
  44567. store = NULL;
  44568. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44569. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44570. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44571. X509_FILETYPE_PEM), 1);
  44572. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44573. X509_FILETYPE_PEM), 1);
  44574. if (store) {
  44575. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44576. WOLFSSL_FILETYPE_PEM), 1);
  44577. /* since store hasn't yet known the revoked cert*/
  44578. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44579. WOLFSSL_FILETYPE_PEM), 1);
  44580. }
  44581. for (i = 0; der[i][0] != '\0'; i++)
  44582. {
  44583. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  44584. }
  44585. if (store) {
  44586. /* since store knows crl list */
  44587. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44588. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44589. }
  44590. /* test for incorrect parameter */
  44591. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44592. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44593. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44594. X509_STORE_free(store);
  44595. store = NULL;
  44596. res = TEST_RES_CHECK(1);
  44597. #endif
  44598. return res;
  44599. }
  44600. static int test_wolfSSL_d2i_X509_REQ(void)
  44601. {
  44602. int res = TEST_SKIPPED;
  44603. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44604. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44605. !defined(WOLFSSL_SP_MATH)
  44606. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  44607. * generated by libest
  44608. * ./certs/csr.attr.der contains sample attributes
  44609. * ./certs/csr.ext.der contains sample extensions */
  44610. const char* csrFile = "./certs/csr.signed.der";
  44611. const char* csrPopFile = "./certs/csr.attr.der";
  44612. const char* csrExtFile = "./certs/csr.ext.der";
  44613. /* ./certs/csr.dsa.pem is generated using
  44614. * openssl req -newkey dsa:certs/dsaparams.pem \
  44615. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44616. * -outform PEM
  44617. * with the passphrase "wolfSSL"
  44618. */
  44619. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44620. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44621. XFILE f;
  44622. #endif
  44623. BIO* bio = NULL;
  44624. X509* req = NULL;
  44625. EVP_PKEY *pub_key = NULL;
  44626. {
  44627. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  44628. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44629. /*
  44630. * Extract the public key from the CSR
  44631. */
  44632. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44633. /*
  44634. * Verify the signature in the CSR
  44635. */
  44636. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44637. X509_free(req);
  44638. BIO_free(bio);
  44639. EVP_PKEY_free(pub_key);
  44640. }
  44641. {
  44642. #ifdef OPENSSL_ALL
  44643. X509_ATTRIBUTE* attr;
  44644. ASN1_TYPE *at;
  44645. #endif
  44646. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  44647. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44648. /*
  44649. * Extract the public key from the CSR
  44650. */
  44651. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44652. /*
  44653. * Verify the signature in the CSR
  44654. */
  44655. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44656. #ifdef OPENSSL_ALL
  44657. /*
  44658. * Obtain the challenge password from the CSR
  44659. */
  44660. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44661. 1);
  44662. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  44663. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44664. AssertNotNull(at->value.asn1_string);
  44665. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  44666. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  44667. #endif
  44668. X509_free(req);
  44669. BIO_free(bio);
  44670. EVP_PKEY_free(pub_key);
  44671. }
  44672. {
  44673. #ifdef OPENSSL_ALL
  44674. X509_ATTRIBUTE* attr;
  44675. ASN1_TYPE *at;
  44676. STACK_OF(X509_EXTENSION) *exts = NULL;
  44677. #endif
  44678. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  44679. /* This CSR contains an Extension Request attribute so
  44680. * we test extension parsing in a CSR attribute here. */
  44681. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44682. /*
  44683. * Extract the public key from the CSR
  44684. */
  44685. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44686. /*
  44687. * Verify the signature in the CSR
  44688. */
  44689. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44690. #ifdef OPENSSL_ALL
  44691. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  44692. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  44693. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  44694. /*
  44695. * Obtain the challenge password from the CSR
  44696. */
  44697. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44698. 0);
  44699. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  44700. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44701. AssertNotNull(at->value.asn1_string);
  44702. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  44703. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  44704. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  44705. #endif
  44706. X509_free(req);
  44707. BIO_free(bio);
  44708. EVP_PKEY_free(pub_key);
  44709. }
  44710. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44711. {
  44712. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  44713. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  44714. /*
  44715. * Extract the public key from the CSR
  44716. */
  44717. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44718. /*
  44719. * Verify the signature in the CSR
  44720. */
  44721. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44722. X509_free(req);
  44723. BIO_free(bio);
  44724. /* Run the same test, but with a file pointer instead of a BIO.
  44725. * (PEM_read_X509_REQ)*/
  44726. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  44727. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  44728. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44729. X509_free(req);
  44730. EVP_PKEY_free(pub_key);
  44731. }
  44732. res = TEST_RES_CHECK(1);
  44733. #endif /* !NO_DSA && !HAVE_SELFTEST */
  44734. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  44735. return res;
  44736. }
  44737. static int test_wolfSSL_PEM_read_X509(void)
  44738. {
  44739. int res = TEST_SKIPPED;
  44740. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44741. !defined(NO_RSA)
  44742. X509 *x509 = NULL;
  44743. XFILE fp;
  44744. fp = XFOPEN(svrCertFile, "rb");
  44745. AssertTrue((fp != XBADFILE));
  44746. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  44747. X509_free(x509);
  44748. XFCLOSE(fp);
  44749. res = TEST_RES_CHECK(1);
  44750. #endif
  44751. return res;
  44752. }
  44753. static int test_wolfSSL_PEM_read(void)
  44754. {
  44755. int res = TEST_SKIPPED;
  44756. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44757. const char* filename = "./certs/server-keyEnc.pem";
  44758. XFILE fp;
  44759. char* name = NULL;
  44760. char* header = NULL;
  44761. byte* data = NULL;
  44762. long len;
  44763. EVP_CIPHER_INFO cipher;
  44764. WOLFSSL_BIO* bio;
  44765. byte* fileData;
  44766. size_t fileDataSz;
  44767. byte* out;
  44768. fp = XFOPEN(filename, "rb");
  44769. AssertTrue((fp != XBADFILE));
  44770. /* Fail cases. */
  44771. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  44772. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  44773. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  44774. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  44775. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44776. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44777. AssertIntGT(XSTRLEN(header), 0);
  44778. AssertIntGT(len, 0);
  44779. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44780. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44781. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44782. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44783. DYNAMIC_TYPE_TMP_BUFFER));
  44784. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44785. XFCLOSE(fp);
  44786. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44787. /* Fail cases. */
  44788. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  44789. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  44790. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  44791. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  44792. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44793. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44794. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44795. /* Fail cases. */
  44796. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  44797. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  44798. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  44799. #ifndef NO_DES3
  44800. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  44801. #endif
  44802. /* Fail cases. */
  44803. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  44804. (void*)"yassl123"), WOLFSSL_FAILURE);
  44805. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  44806. (void*)"yassl123"), WOLFSSL_FAILURE);
  44807. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  44808. (void*)"yassl123"), WOLFSSL_FAILURE);
  44809. #if !defined(NO_DES3) && !defined(NO_MD5)
  44810. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  44811. (void*)"yassl123"), WOLFSSL_SUCCESS);
  44812. #endif
  44813. BIO_free(bio);
  44814. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44815. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44816. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44817. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44818. name = NULL;
  44819. header = NULL;
  44820. data = NULL;
  44821. fp = XFOPEN(svrKeyFile, "rb");
  44822. AssertTrue((fp != XBADFILE));
  44823. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44824. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44825. AssertIntEQ(XSTRLEN(header), 0);
  44826. AssertIntGT(len, 0);
  44827. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44828. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44829. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44830. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44831. DYNAMIC_TYPE_TMP_BUFFER));
  44832. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44833. XFCLOSE(fp);
  44834. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44835. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44836. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44837. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44838. BIO_free(bio);
  44839. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44840. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44841. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44842. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44843. res = TEST_RES_CHECK(1);
  44844. #endif
  44845. return res;
  44846. }
  44847. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  44848. {
  44849. int res = TEST_SKIPPED;
  44850. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44851. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44852. const byte iv[12] = { 0 };
  44853. const byte key[16] = { 0 };
  44854. const byte cleartext[16] = { 0 };
  44855. const byte aad[] = {
  44856. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  44857. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  44858. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  44859. 0x4f
  44860. };
  44861. byte out1Part[16];
  44862. byte outTag1Part[16];
  44863. byte out2Part[16];
  44864. byte outTag2Part[16];
  44865. byte decryptBuf[16];
  44866. int len;
  44867. int tlen;
  44868. EVP_CIPHER_CTX* ctx = NULL;
  44869. /* ENCRYPT */
  44870. /* Send AAD and data in 1 part */
  44871. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44872. tlen = 0;
  44873. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44874. 1);
  44875. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44876. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44877. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  44878. sizeof(cleartext)), 1);
  44879. tlen += len;
  44880. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  44881. tlen += len;
  44882. AssertIntEQ(tlen, sizeof(cleartext));
  44883. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44884. outTag1Part), 1);
  44885. EVP_CIPHER_CTX_free(ctx);
  44886. /* DECRYPT */
  44887. /* Send AAD and data in 1 part */
  44888. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44889. tlen = 0;
  44890. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44891. 1);
  44892. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44893. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44894. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  44895. sizeof(cleartext)), 1);
  44896. tlen += len;
  44897. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44898. outTag1Part), 1);
  44899. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  44900. tlen += len;
  44901. AssertIntEQ(tlen, sizeof(cleartext));
  44902. EVP_CIPHER_CTX_free(ctx);
  44903. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44904. /* ENCRYPT */
  44905. /* Send AAD and data in 2 parts */
  44906. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44907. tlen = 0;
  44908. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44909. 1);
  44910. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44911. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44912. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44913. 1);
  44914. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  44915. tlen += len;
  44916. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  44917. sizeof(cleartext) - 1), 1);
  44918. tlen += len;
  44919. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  44920. tlen += len;
  44921. AssertIntEQ(tlen, sizeof(cleartext));
  44922. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44923. outTag2Part), 1);
  44924. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  44925. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  44926. EVP_CIPHER_CTX_free(ctx);
  44927. /* DECRYPT */
  44928. /* Send AAD and data in 2 parts */
  44929. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44930. tlen = 0;
  44931. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44932. 1);
  44933. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44934. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44935. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44936. 1);
  44937. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  44938. tlen += len;
  44939. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  44940. sizeof(cleartext) - 1), 1);
  44941. tlen += len;
  44942. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44943. outTag1Part), 1);
  44944. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  44945. tlen += len;
  44946. AssertIntEQ(tlen, sizeof(cleartext));
  44947. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44948. /* Test AAD re-use */
  44949. EVP_CIPHER_CTX_free(ctx);
  44950. res = TEST_RES_CHECK(1);
  44951. #endif
  44952. return res;
  44953. }
  44954. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  44955. {
  44956. int res = TEST_SKIPPED;
  44957. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44958. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44959. /* Zero length plain text */
  44960. byte key[] = {
  44961. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44962. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44963. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44964. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44965. }; /* align */
  44966. byte iv[] = {
  44967. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44968. }; /* align */
  44969. byte plaintxt[1];
  44970. int ivSz = 12;
  44971. int plaintxtSz = 0;
  44972. unsigned char tag[16];
  44973. unsigned char tag_kat[] =
  44974. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  44975. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  44976. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  44977. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  44978. int ciphertxtSz = 0;
  44979. int decryptedtxtSz = 0;
  44980. int len = 0;
  44981. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  44982. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  44983. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  44984. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44985. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  44986. plaintxtSz));
  44987. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  44988. ciphertxtSz += len;
  44989. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  44990. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  44991. AssertIntEQ(0, ciphertxtSz);
  44992. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  44993. EVP_CIPHER_CTX_init(de);
  44994. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  44995. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44996. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  44997. decryptedtxtSz = len;
  44998. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  44999. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45000. decryptedtxtSz += len;
  45001. AssertIntEQ(0, decryptedtxtSz);
  45002. EVP_CIPHER_CTX_free(en);
  45003. EVP_CIPHER_CTX_free(de);
  45004. res = TEST_RES_CHECK(1);
  45005. #endif
  45006. return res;
  45007. }
  45008. static int test_wolfssl_EVP_aes_gcm(void)
  45009. {
  45010. int res = TEST_SKIPPED;
  45011. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45012. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45013. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45014. byte *key = (byte*)"01234567890123456789012345678901";
  45015. /* A 128 bit IV */
  45016. byte *iv = (byte*)"0123456789012345";
  45017. int ivSz = AES_BLOCK_SIZE;
  45018. /* Message to be encrypted */
  45019. byte *plaintxt = (byte*)"for things to change you have to change";
  45020. /* Additional non-confidential data */
  45021. byte *aad = (byte*)"Don't spend major time on minor things.";
  45022. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45023. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45024. int aadSz = (int)XSTRLEN((char*)aad);
  45025. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45026. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45027. int ciphertxtSz = 0;
  45028. int decryptedtxtSz = 0;
  45029. int len = 0;
  45030. int i = 0;
  45031. EVP_CIPHER_CTX en[2];
  45032. EVP_CIPHER_CTX de[2];
  45033. for (i = 0; i < 2; i++) {
  45034. EVP_CIPHER_CTX_init(&en[i]);
  45035. if (i == 0) {
  45036. /* Default uses 96-bits IV length */
  45037. #ifdef WOLFSSL_AES_128
  45038. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  45039. #elif defined(WOLFSSL_AES_192)
  45040. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  45041. #elif defined(WOLFSSL_AES_256)
  45042. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  45043. #endif
  45044. }
  45045. else {
  45046. #ifdef WOLFSSL_AES_128
  45047. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45048. #elif defined(WOLFSSL_AES_192)
  45049. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45050. #elif defined(WOLFSSL_AES_256)
  45051. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45052. #endif
  45053. /* non-default must to set the IV length first */
  45054. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45055. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45056. }
  45057. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45058. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  45059. ciphertxtSz = len;
  45060. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45061. ciphertxtSz += len;
  45062. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  45063. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45064. EVP_CIPHER_CTX_init(&de[i]);
  45065. if (i == 0) {
  45066. /* Default uses 96-bits IV length */
  45067. #ifdef WOLFSSL_AES_128
  45068. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  45069. #elif defined(WOLFSSL_AES_192)
  45070. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  45071. #elif defined(WOLFSSL_AES_256)
  45072. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  45073. #endif
  45074. }
  45075. else {
  45076. #ifdef WOLFSSL_AES_128
  45077. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  45078. #elif defined(WOLFSSL_AES_192)
  45079. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  45080. #elif defined(WOLFSSL_AES_256)
  45081. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  45082. #endif
  45083. /* non-default must to set the IV length first */
  45084. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45085. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45086. }
  45087. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45088. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45089. decryptedtxtSz = len;
  45090. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45091. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45092. decryptedtxtSz += len;
  45093. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  45094. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45095. /* modify tag*/
  45096. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45097. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45098. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  45099. /* fail due to wrong tag */
  45100. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45101. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45102. AssertIntEQ(0, len);
  45103. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45104. }
  45105. res = TEST_RES_CHECK(1);
  45106. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45107. return res;
  45108. }
  45109. static int test_wolfssl_EVP_chacha20_poly1305(void)
  45110. {
  45111. int res = TEST_SKIPPED;
  45112. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45113. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45114. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45115. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45116. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45117. byte cipherText[sizeof(plainText)];
  45118. byte decryptedText[sizeof(plainText)];
  45119. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45120. EVP_CIPHER_CTX* ctx;
  45121. int outSz;
  45122. /* Encrypt. */
  45123. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45124. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45125. NULL), WOLFSSL_SUCCESS);
  45126. /* Invalid IV length. */
  45127. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45128. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45129. /* Valid IV length. */
  45130. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45131. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45132. /* Invalid tag length. */
  45133. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45134. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45135. /* Valid tag length. */
  45136. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45137. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45138. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45139. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45140. WOLFSSL_SUCCESS);
  45141. AssertIntEQ(outSz, sizeof(aad));
  45142. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45143. sizeof(plainText)), WOLFSSL_SUCCESS);
  45144. AssertIntEQ(outSz, sizeof(plainText));
  45145. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45146. AssertIntEQ(outSz, 0);
  45147. /* Invalid tag length. */
  45148. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45149. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45150. /* Valid tag length. */
  45151. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45152. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45153. EVP_CIPHER_CTX_free(ctx);
  45154. /* Decrypt. */
  45155. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45156. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45157. NULL), WOLFSSL_SUCCESS);
  45158. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45159. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45160. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45161. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45162. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45163. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45164. WOLFSSL_SUCCESS);
  45165. AssertIntEQ(outSz, sizeof(aad));
  45166. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45167. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45168. AssertIntEQ(outSz, sizeof(cipherText));
  45169. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45170. WOLFSSL_SUCCESS);
  45171. AssertIntEQ(outSz, 0);
  45172. EVP_CIPHER_CTX_free(ctx);
  45173. /* Test partial Inits. CipherInit() allow setting of key and iv
  45174. * in separate calls. */
  45175. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45176. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45177. key, NULL, 1), WOLFSSL_SUCCESS);
  45178. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45179. WOLFSSL_SUCCESS);
  45180. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45181. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45182. AssertIntEQ(outSz, sizeof(aad));
  45183. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45184. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45185. AssertIntEQ(outSz, sizeof(cipherText));
  45186. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45187. WOLFSSL_SUCCESS);
  45188. AssertIntEQ(outSz, 0);
  45189. EVP_CIPHER_CTX_free(ctx);
  45190. res = TEST_RES_CHECK(1);
  45191. #endif
  45192. return res;
  45193. }
  45194. static int test_wolfssl_EVP_chacha20(void)
  45195. {
  45196. int res = TEST_SKIPPED;
  45197. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45198. byte key[CHACHA_MAX_KEY_SZ];
  45199. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45200. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45201. byte cipherText[sizeof(plainText)];
  45202. byte decryptedText[sizeof(plainText)];
  45203. EVP_CIPHER_CTX* ctx;
  45204. int outSz;
  45205. /* Encrypt. */
  45206. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45207. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45208. NULL), WOLFSSL_SUCCESS);
  45209. /* Any tag length must fail - not an AEAD cipher. */
  45210. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45211. 16, NULL), WOLFSSL_FAILURE);
  45212. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45213. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45214. sizeof(plainText)), WOLFSSL_SUCCESS);
  45215. AssertIntEQ(outSz, sizeof(plainText));
  45216. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45217. AssertIntEQ(outSz, 0);
  45218. EVP_CIPHER_CTX_free(ctx);
  45219. /* Decrypt. */
  45220. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45221. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45222. NULL), WOLFSSL_SUCCESS);
  45223. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45224. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45225. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45226. AssertIntEQ(outSz, sizeof(cipherText));
  45227. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45228. WOLFSSL_SUCCESS);
  45229. AssertIntEQ(outSz, 0);
  45230. EVP_CIPHER_CTX_free(ctx);
  45231. /* Test partial Inits. CipherInit() allow setting of key and iv
  45232. * in separate calls. */
  45233. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45234. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45235. key, NULL, 1), WOLFSSL_SUCCESS);
  45236. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45237. WOLFSSL_SUCCESS);
  45238. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45239. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45240. AssertIntEQ(outSz, sizeof(cipherText));
  45241. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45242. WOLFSSL_SUCCESS);
  45243. AssertIntEQ(outSz, 0);
  45244. EVP_CIPHER_CTX_free(ctx);
  45245. res = TEST_RES_CHECK(1);
  45246. #endif
  45247. return res;
  45248. }
  45249. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  45250. {
  45251. int res = TEST_SKIPPED;
  45252. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  45253. EVP_PKEY_CTX* ctx;
  45254. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  45255. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  45256. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45257. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  45258. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  45259. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  45260. byte outKey[34];
  45261. size_t outKeySz = sizeof(outKey);
  45262. /* These expected outputs were gathered by running the same test below using
  45263. * OpenSSL. */
  45264. const byte extractAndExpand[] = {
  45265. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  45266. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  45267. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  45268. };
  45269. const byte extractOnly[] = {
  45270. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  45271. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  45272. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  45273. };
  45274. const byte expandOnly[] = {
  45275. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  45276. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  45277. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  45278. };
  45279. const byte extractAndExpandAddInfo[] = {
  45280. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  45281. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  45282. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  45283. };
  45284. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  45285. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  45286. /* NULL ctx. */
  45287. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45288. /* NULL md. */
  45289. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  45290. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  45291. /* NULL ctx. */
  45292. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  45293. WOLFSSL_FAILURE);
  45294. /* NULL salt is ok. */
  45295. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  45296. WOLFSSL_SUCCESS);
  45297. /* Salt length <= 0. */
  45298. /* Length 0 salt is ok. */
  45299. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  45300. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  45301. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  45302. WOLFSSL_SUCCESS);
  45303. /* NULL ctx. */
  45304. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  45305. WOLFSSL_FAILURE);
  45306. /* NULL key. */
  45307. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  45308. WOLFSSL_FAILURE);
  45309. /* Key length <= 0 */
  45310. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  45311. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  45312. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  45313. WOLFSSL_SUCCESS);
  45314. /* NULL ctx. */
  45315. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  45316. WOLFSSL_FAILURE);
  45317. /* NULL info is ok. */
  45318. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  45319. WOLFSSL_SUCCESS);
  45320. /* Info length <= 0 */
  45321. /* Length 0 info is ok. */
  45322. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  45323. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  45324. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  45325. WOLFSSL_SUCCESS);
  45326. /* NULL ctx. */
  45327. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45328. WOLFSSL_FAILURE);
  45329. /* Extract and expand (default). */
  45330. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45331. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  45332. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  45333. /* Extract only. */
  45334. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45335. WOLFSSL_SUCCESS);
  45336. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45337. AssertIntEQ(outKeySz, sizeof(extractOnly));
  45338. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  45339. outKeySz = sizeof(outKey);
  45340. /* Expand only. */
  45341. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  45342. WOLFSSL_SUCCESS);
  45343. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45344. AssertIntEQ(outKeySz, sizeof(expandOnly));
  45345. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  45346. outKeySz = sizeof(outKey);
  45347. /* Extract and expand with appended additional info. */
  45348. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  45349. WOLFSSL_SUCCESS);
  45350. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  45351. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  45352. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45353. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  45354. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  45355. EVP_PKEY_CTX_free(ctx);
  45356. res = TEST_RES_CHECK(1);
  45357. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  45358. return res;
  45359. }
  45360. #ifndef NO_BIO
  45361. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  45362. {
  45363. int res = TEST_SKIPPED;
  45364. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45365. BIO* bio;
  45366. X509_INFO* info;
  45367. STACK_OF(X509_INFO)* sk;
  45368. char* subject;
  45369. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45370. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45371. AssertNotNull(bio = BIO_new(BIO_s_file()));
  45372. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  45373. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  45374. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  45375. /* using dereference to maintain testing for Apache port*/
  45376. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45377. AssertNotNull(subject =
  45378. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45379. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  45380. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45381. X509_INFO_free(info);
  45382. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45383. AssertNotNull(subject =
  45384. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45385. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  45386. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45387. X509_INFO_free(info);
  45388. AssertNull(info = sk_X509_INFO_pop(sk));
  45389. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  45390. BIO_free(bio);
  45391. res = TEST_RES_CHECK(1);
  45392. #endif
  45393. return res;
  45394. }
  45395. #endif /* !NO_BIO */
  45396. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  45397. {
  45398. int res = TEST_SKIPPED;
  45399. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45400. X509 *x509;
  45401. X509_NAME* name;
  45402. int idx = 0;
  45403. X509_NAME_ENTRY *ne;
  45404. ASN1_OBJECT *object = NULL;
  45405. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  45406. AssertNotNull(x509);
  45407. name = X509_get_subject_name(x509);
  45408. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  45409. AssertIntGE(idx, 0);
  45410. ne = X509_NAME_get_entry(name, idx);
  45411. AssertNotNull(ne);
  45412. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  45413. X509_free(x509);
  45414. res = TEST_RES_CHECK(1);
  45415. #endif
  45416. return res;
  45417. }
  45418. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  45419. {
  45420. int res = TEST_SKIPPED;
  45421. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45422. ASN1_INTEGER *a;
  45423. long val;
  45424. int ret;
  45425. a = ASN1_INTEGER_new();
  45426. val = 0;
  45427. ret = ASN1_INTEGER_set(NULL, val);
  45428. AssertIntEQ(ret, 0);
  45429. ASN1_INTEGER_free(a);
  45430. /* 0 */
  45431. a = ASN1_INTEGER_new();
  45432. val = 0;
  45433. ret = ASN1_INTEGER_set(a, val);
  45434. AssertIntEQ(ret, 1);
  45435. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45436. ASN1_INTEGER_free(a);
  45437. /* 40 */
  45438. a = ASN1_INTEGER_new();
  45439. val = 40;
  45440. ret = ASN1_INTEGER_set(a, val);
  45441. AssertIntEQ(ret, 1);
  45442. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45443. ASN1_INTEGER_free(a);
  45444. /* -40 */
  45445. a = ASN1_INTEGER_new();
  45446. val = -40;
  45447. ret = ASN1_INTEGER_set(a, val);
  45448. AssertIntEQ(ret, 1);
  45449. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45450. ASN1_INTEGER_free(a);
  45451. /* 128 */
  45452. a = ASN1_INTEGER_new();
  45453. val = 128;
  45454. ret = ASN1_INTEGER_set(a, val);
  45455. AssertIntEQ(ret, 1);
  45456. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45457. ASN1_INTEGER_free(a);
  45458. /* -128 */
  45459. a = ASN1_INTEGER_new();
  45460. val = -128;
  45461. ret = ASN1_INTEGER_set(a, val);
  45462. AssertIntEQ(ret, 1);
  45463. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45464. ASN1_INTEGER_free(a);
  45465. /* 200 */
  45466. a = ASN1_INTEGER_new();
  45467. val = 200;
  45468. ret = ASN1_INTEGER_set(a, val);
  45469. AssertIntEQ(ret, 1);
  45470. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45471. ASN1_INTEGER_free(a);
  45472. /* int max (2147483647) */
  45473. a = ASN1_INTEGER_new();
  45474. val = 2147483647;
  45475. ret = ASN1_INTEGER_set(a, val);
  45476. AssertIntEQ(ret, 1);
  45477. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45478. ASN1_INTEGER_free(a);
  45479. /* int min (-2147483648) */
  45480. a = ASN1_INTEGER_new();
  45481. val = -2147483647 - 1;
  45482. ret = ASN1_INTEGER_set(a, val);
  45483. AssertIntEQ(ret, 1);
  45484. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45485. ASN1_INTEGER_free(a);
  45486. res = TEST_RES_CHECK(1);
  45487. #endif
  45488. return res;
  45489. }
  45490. #if defined(OPENSSL_EXTRA)
  45491. typedef struct ASN1IntTestVector {
  45492. const byte* der;
  45493. const size_t derSz;
  45494. const long value;
  45495. } ASN1IntTestVector;
  45496. #endif
  45497. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  45498. {
  45499. int res = TEST_SKIPPED;
  45500. #if defined(OPENSSL_EXTRA)
  45501. size_t i;
  45502. WOLFSSL_ASN1_INTEGER* a = NULL;
  45503. WOLFSSL_ASN1_INTEGER* b = NULL;
  45504. WOLFSSL_ASN1_INTEGER* c = NULL;
  45505. const byte* p = NULL;
  45506. byte* reEncoded = NULL;
  45507. int reEncodedSz;
  45508. static const byte zeroDer[] = {
  45509. 0x02, 0x01, 0x00
  45510. };
  45511. static const byte oneDer[] = {
  45512. 0x02, 0x01, 0x01
  45513. };
  45514. static const byte negativeDer[] = {
  45515. 0x02, 0x03, 0xC1, 0x16, 0x0D
  45516. };
  45517. static const byte positiveDer[] = {
  45518. 0x02, 0x03, 0x01, 0x00, 0x01
  45519. };
  45520. static const byte primeDer[] = {
  45521. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  45522. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  45523. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  45524. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  45525. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  45526. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  45527. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  45528. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  45529. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  45530. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  45531. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  45532. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  45533. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  45534. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  45535. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  45536. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  45537. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  45538. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  45539. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  45540. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  45541. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  45542. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  45543. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  45544. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  45545. };
  45546. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45547. static const ASN1IntTestVector testVectors[] = {
  45548. {zeroDer, sizeof(zeroDer), 0},
  45549. {oneDer, sizeof(oneDer), 1},
  45550. {negativeDer, sizeof(negativeDer), -4123123},
  45551. {positiveDer, sizeof(positiveDer), 65537},
  45552. {primeDer, sizeof(primeDer), 0}
  45553. };
  45554. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  45555. /* Check d2i error conditions */
  45556. /* NULL pointer to input. */
  45557. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  45558. AssertNull(b);
  45559. /* NULL input. */
  45560. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  45561. AssertNull(b);
  45562. /* 0 length. */
  45563. p = testVectors[0].der;
  45564. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  45565. AssertNull(b);
  45566. /* Negative length. */
  45567. p = testVectors[0].der;
  45568. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  45569. AssertNull(b);
  45570. /* Garbage DER input. */
  45571. p = garbageDer;
  45572. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  45573. AssertNull(b);
  45574. {
  45575. /* Check i2d error conditions */
  45576. /* NULL input. */
  45577. byte* p2 = NULL;
  45578. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  45579. /* 0 length input data buffer (a->length == 0). */
  45580. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  45581. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45582. a->data = NULL;
  45583. /* NULL input data buffer. */
  45584. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45585. /* Reset a->data. */
  45586. a->data = a->intData;
  45587. /* Set a to valid value. */
  45588. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  45589. /* NULL output buffer. */
  45590. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  45591. wolfSSL_ASN1_INTEGER_free(a);
  45592. }
  45593. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  45594. p = testVectors[i].der;
  45595. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  45596. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  45597. if (testVectors[i].derSz <= sizeof(long)) {
  45598. c = wolfSSL_ASN1_INTEGER_new();
  45599. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  45600. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  45601. wolfSSL_ASN1_INTEGER_free(c);
  45602. }
  45603. /* Convert to DER without a pre-allocated output buffer. */
  45604. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45605. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45606. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  45607. /* Convert to DER with a pre-allocated output buffer. In this case, the
  45608. * output buffer pointer should be incremented just past the end of the
  45609. * encoded data. */
  45610. p = reEncoded;
  45611. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45612. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45613. AssertPtrEq(p, reEncoded - reEncodedSz);
  45614. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  45615. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  45616. reEncoded = NULL;
  45617. wolfSSL_ASN1_INTEGER_free(a);
  45618. }
  45619. res = TEST_RES_CHECK(1);
  45620. #endif /* OPENSSL_EXTRA */
  45621. return res;
  45622. }
  45623. static int test_wolfSSL_X509_STORE_get1_certs(void)
  45624. {
  45625. int res = TEST_SKIPPED;
  45626. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  45627. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45628. X509_STORE_CTX *storeCtx;
  45629. X509_STORE *store;
  45630. X509 *caX509;
  45631. X509 *svrX509;
  45632. X509_NAME *subject;
  45633. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  45634. AssertNotNull(caX509 =
  45635. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  45636. AssertNotNull((svrX509 =
  45637. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  45638. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  45639. AssertNotNull(store = X509_STORE_new());
  45640. AssertNotNull(subject = X509_get_subject_name(caX509));
  45641. /* Errors */
  45642. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  45643. AssertNull(X509_STORE_get1_certs(NULL, subject));
  45644. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  45645. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  45646. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  45647. /* Should find the cert */
  45648. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45649. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  45650. sk_X509_pop_free(certs, NULL);
  45651. /* Should not find the cert */
  45652. AssertNotNull(subject = X509_get_subject_name(svrX509));
  45653. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45654. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  45655. sk_X509_pop_free(certs, NULL);
  45656. X509_STORE_free(store);
  45657. X509_STORE_CTX_free(storeCtx);
  45658. X509_free(svrX509);
  45659. X509_free(caX509);
  45660. res = TEST_RES_CHECK(1);
  45661. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  45662. return res;
  45663. }
  45664. /* Testing code used in dpp.c in hostap */
  45665. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45666. typedef struct {
  45667. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  45668. * as an OID identifying the curve */
  45669. X509_ALGOR *alg;
  45670. /* Compressed format public key per ANSI X9.63 */
  45671. ASN1_BIT_STRING *pub_key;
  45672. } DPP_BOOTSTRAPPING_KEY;
  45673. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  45674. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  45675. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  45676. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  45677. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  45678. #endif
  45679. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  45680. {
  45681. int res = TEST_SKIPPED;
  45682. /* Testing code used in dpp.c in hostap */
  45683. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45684. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45685. EC_KEY *eckey;
  45686. EVP_PKEY *key;
  45687. size_t len;
  45688. unsigned char *der = NULL;
  45689. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  45690. const unsigned char *in = ecc_clikey_der_256;
  45691. const EC_GROUP *group;
  45692. const EC_POINT *point;
  45693. int nid;
  45694. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  45695. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  45696. (long)sizeof_ecc_clikey_der_256));
  45697. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  45698. AssertNotNull(group = EC_KEY_get0_group(eckey));
  45699. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  45700. nid = EC_GROUP_get_curve_name(group);
  45701. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  45702. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  45703. #ifdef HAVE_COMP_KEY
  45704. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45705. NULL, 0, NULL)), 0);
  45706. #else
  45707. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45708. NULL, 0, NULL)), 0);
  45709. #endif
  45710. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  45711. #ifdef HAVE_COMP_KEY
  45712. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45713. der, len, NULL), len);
  45714. #else
  45715. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45716. der, len, NULL), len);
  45717. #endif
  45718. bootstrap->pub_key->data = der;
  45719. bootstrap->pub_key->length = (int)len;
  45720. /* Not actually used */
  45721. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  45722. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  45723. der = NULL;
  45724. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  45725. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  45726. EVP_PKEY_free(key);
  45727. EC_KEY_free(eckey);
  45728. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  45729. res = TEST_RES_CHECK(1);
  45730. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45731. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  45732. return res;
  45733. }
  45734. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  45735. {
  45736. int res = TEST_SKIPPED;
  45737. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45738. ASN1_INTEGER *a;
  45739. unsigned char *pp,*tpp;
  45740. int ret;
  45741. a = wolfSSL_ASN1_INTEGER_new();
  45742. /* 40 */
  45743. a->intData[0] = ASN_INTEGER;
  45744. a->intData[1] = 1;
  45745. a->intData[2] = 40;
  45746. ret = i2c_ASN1_INTEGER(a, NULL);
  45747. AssertIntEQ(ret, 1);
  45748. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45749. DYNAMIC_TYPE_TMP_BUFFER));
  45750. tpp = pp;
  45751. XMEMSET(pp, 0, ret + 1);
  45752. i2c_ASN1_INTEGER(a, &pp);
  45753. pp--;
  45754. AssertIntEQ(*pp, 40);
  45755. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45756. /* 128 */
  45757. a->intData[0] = ASN_INTEGER;
  45758. a->intData[1] = 1;
  45759. a->intData[2] = 128;
  45760. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45761. AssertIntEQ(ret, 2);
  45762. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45763. DYNAMIC_TYPE_TMP_BUFFER));
  45764. tpp = pp;
  45765. XMEMSET(pp, 0, ret + 1);
  45766. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45767. pp--;
  45768. AssertIntEQ(*(pp--), 128);
  45769. AssertIntEQ(*pp, 0);
  45770. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45771. /* -40 */
  45772. a->intData[0] = ASN_INTEGER;
  45773. a->intData[1] = 1;
  45774. a->intData[2] = 40;
  45775. a->negative = 1;
  45776. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45777. AssertIntEQ(ret, 1);
  45778. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45779. DYNAMIC_TYPE_TMP_BUFFER));
  45780. tpp = pp;
  45781. XMEMSET(pp, 0, ret + 1);
  45782. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45783. pp--;
  45784. AssertIntEQ(*pp, 216);
  45785. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45786. /* -128 */
  45787. a->intData[0] = ASN_INTEGER;
  45788. a->intData[1] = 1;
  45789. a->intData[2] = 128;
  45790. a->negative = 1;
  45791. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45792. AssertIntEQ(ret, 1);
  45793. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45794. DYNAMIC_TYPE_TMP_BUFFER));
  45795. tpp = pp;
  45796. XMEMSET(pp, 0, ret + 1);
  45797. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45798. pp--;
  45799. AssertIntEQ(*pp, 128);
  45800. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45801. /* -200 */
  45802. a->intData[0] = ASN_INTEGER;
  45803. a->intData[1] = 1;
  45804. a->intData[2] = 200;
  45805. a->negative = 1;
  45806. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45807. AssertIntEQ(ret, 2);
  45808. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45809. DYNAMIC_TYPE_TMP_BUFFER));
  45810. tpp = pp;
  45811. XMEMSET(pp, 0, ret + 1);
  45812. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45813. pp--;
  45814. AssertIntEQ(*(pp--), 56);
  45815. AssertIntEQ(*pp, 255);
  45816. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45817. wolfSSL_ASN1_INTEGER_free(a);
  45818. res = TEST_RES_CHECK(1);
  45819. #endif /* OPENSSL_EXTRA && !NO_ASN */
  45820. return res;
  45821. }
  45822. #ifndef NO_INLINE
  45823. #define WOLFSSL_MISC_INCLUDED
  45824. #include <wolfcrypt/src/misc.c>
  45825. #else
  45826. #include <wolfssl/wolfcrypt/misc.h>
  45827. #endif
  45828. static int test_ForceZero(void)
  45829. {
  45830. unsigned char data[32];
  45831. unsigned int i, j, len;
  45832. /* Test case with 0 length */
  45833. ForceZero(data, 0);
  45834. /* Test ForceZero */
  45835. for (i = 0; i < sizeof(data); i++) {
  45836. for (len = 1; len < sizeof(data) - i; len++) {
  45837. for (j = 0; j < sizeof(data); j++)
  45838. data[j] = j + 1;
  45839. ForceZero(data + i, len);
  45840. for (j = 0; j < sizeof(data); j++) {
  45841. if (j < i || j >= i + len) {
  45842. if (data[j] == 0x00)
  45843. return -10200;
  45844. }
  45845. else if (data[j] != 0x00)
  45846. return -10201;
  45847. }
  45848. }
  45849. }
  45850. return TEST_RES_CHECK(1);
  45851. }
  45852. #ifndef NO_BIO
  45853. static int test_wolfSSL_X509_print(void)
  45854. {
  45855. int res = TEST_SKIPPED;
  45856. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45857. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  45858. X509 *x509;
  45859. BIO *bio;
  45860. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45861. const X509_ALGOR *cert_sig_alg;
  45862. #endif
  45863. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  45864. AssertNotNull(x509);
  45865. /* print to memory */
  45866. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45867. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45868. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  45869. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  45870. /* Will print IP address subject alt name. */
  45871. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  45872. #else
  45873. /* Will print IP address subject alt name. */
  45874. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  45875. #endif
  45876. #else
  45877. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  45878. #endif
  45879. BIO_free(bio);
  45880. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  45881. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45882. /* Print signature */
  45883. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  45884. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  45885. #endif
  45886. /* print to stderr */
  45887. #if !defined(NO_WOLFSSL_DIR)
  45888. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45889. #endif
  45890. /* print again */
  45891. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  45892. X509_free(x509);
  45893. BIO_free(bio);
  45894. res = TEST_RES_CHECK(1);
  45895. #endif
  45896. return res;
  45897. }
  45898. static int test_wolfSSL_X509_CRL_print(void)
  45899. {
  45900. int res = TEST_SKIPPED;
  45901. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  45902. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  45903. X509_CRL* crl;
  45904. BIO *bio;
  45905. XFILE fp;
  45906. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  45907. AssertTrue((fp != XBADFILE));
  45908. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45909. NULL, NULL));
  45910. XFCLOSE(fp);
  45911. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45912. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  45913. X509_CRL_free(crl);
  45914. BIO_free(bio);
  45915. res = TEST_RES_CHECK(1);
  45916. #endif
  45917. return res;
  45918. }
  45919. static int test_wolfSSL_BIO_get_len(void)
  45920. {
  45921. int res = TEST_SKIPPED;
  45922. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  45923. BIO *bio = NULL;
  45924. const char txt[] = "Some example text to push to the BIO.";
  45925. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  45926. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45927. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  45928. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  45929. BIO_free(bio);
  45930. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  45931. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  45932. BIO_free(bio);
  45933. res = TEST_RES_CHECK(1);
  45934. #endif
  45935. return res;
  45936. }
  45937. static int test_wolfSSL_ASN1_STRING_print(void)
  45938. {
  45939. int res = TEST_SKIPPED;
  45940. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  45941. ASN1_STRING* asnStr = NULL;
  45942. const char HELLO_DATA[]= \
  45943. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  45944. #define MAX_UNPRINTABLE_CHAR 32
  45945. #define MAX_BUF 255
  45946. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  45947. unsigned char expected[sizeof(unprintableData)+1];
  45948. unsigned char rbuf[MAX_BUF];
  45949. BIO *bio;
  45950. int p_len, i;
  45951. /* setup */
  45952. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  45953. unprintableData[i] = HELLO_DATA[i];
  45954. expected[i] = HELLO_DATA[i];
  45955. }
  45956. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  45957. unprintableData[sizeof(HELLO_DATA)+i] = i;
  45958. if (i == (int)'\n' || i == (int)'\r')
  45959. expected[sizeof(HELLO_DATA)+i] = i;
  45960. else
  45961. expected[sizeof(HELLO_DATA)+i] = '.';
  45962. }
  45963. unprintableData[sizeof(unprintableData)-1] = '\0';
  45964. expected[sizeof(expected)-1] = '\0';
  45965. XMEMSET(rbuf, 0, MAX_BUF);
  45966. bio = BIO_new(BIO_s_mem());
  45967. BIO_set_write_buf_size(bio, MAX_BUF);
  45968. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  45969. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  45970. (int)sizeof(unprintableData));
  45971. /* test */
  45972. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  45973. AssertIntEQ(p_len, 46);
  45974. BIO_read(bio, (void*)rbuf, 46);
  45975. AssertStrEQ((char*)rbuf, (const char*)expected);
  45976. BIO_free(bio);
  45977. ASN1_STRING_free(asnStr);
  45978. res = TEST_RES_CHECK(1);
  45979. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  45980. return res;
  45981. }
  45982. #endif /* !NO_BIO */
  45983. static int test_wolfSSL_ASN1_get_object(void)
  45984. {
  45985. int res = TEST_SKIPPED;
  45986. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45987. const unsigned char* derBuf = cliecc_cert_der_256;
  45988. int len = sizeof_cliecc_cert_der_256;
  45989. long asnLen = 0;
  45990. int tag = 0, cls = 0;
  45991. ASN1_OBJECT *a;
  45992. /* Read a couple TLV triplets and make sure they match the expected values */
  45993. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  45994. AssertIntEQ(asnLen, 862);
  45995. AssertIntEQ(tag, 0x10);
  45996. AssertIntEQ(cls, 0);
  45997. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45998. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45999. AssertIntEQ(asnLen, 772);
  46000. AssertIntEQ(tag, 0x10);
  46001. AssertIntEQ(cls, 0);
  46002. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46003. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46004. AssertIntEQ(asnLen, 3);
  46005. AssertIntEQ(tag, 0);
  46006. AssertIntEQ(cls, 0x80);
  46007. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46008. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46009. AssertIntEQ(asnLen, 1);
  46010. AssertIntEQ(tag, 0x2);
  46011. AssertIntEQ(cls, 0);
  46012. derBuf += asnLen;
  46013. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46014. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46015. AssertIntEQ(asnLen, 20);
  46016. AssertIntEQ(tag, 0x2);
  46017. AssertIntEQ(cls, 0);
  46018. derBuf += asnLen;
  46019. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  46020. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  46021. AssertIntEQ(asnLen, 10);
  46022. AssertIntEQ(tag, 0x10);
  46023. AssertIntEQ(cls, 0);
  46024. /* Read an ASN OBJECT */
  46025. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  46026. ASN1_OBJECT_free(a);
  46027. res = TEST_RES_CHECK(1);
  46028. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  46029. return res;
  46030. }
  46031. static int test_wolfSSL_RSA(void)
  46032. {
  46033. int res = TEST_SKIPPED;
  46034. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  46035. defined(WOLFSSL_KEY_GEN)
  46036. RSA* rsa;
  46037. const BIGNUM *n;
  46038. const BIGNUM *e;
  46039. const BIGNUM *d;
  46040. const BIGNUM *p;
  46041. const BIGNUM *q;
  46042. const BIGNUM *dmp1;
  46043. const BIGNUM *dmq1;
  46044. const BIGNUM *iqmp;
  46045. AssertNotNull(rsa = RSA_new());
  46046. AssertIntEQ(RSA_size(NULL), 0);
  46047. AssertIntEQ(RSA_size(rsa), 0);
  46048. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  46049. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  46050. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  46051. #ifdef WOLFSSL_RSA_KEY_CHECK
  46052. AssertIntEQ(RSA_check_key(rsa), 0);
  46053. #endif
  46054. RSA_free(rsa);
  46055. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46056. AssertIntEQ(RSA_size(rsa), 256);
  46057. #ifdef WOLFSSL_RSA_KEY_CHECK
  46058. AssertIntEQ(RSA_check_key(NULL), 0);
  46059. AssertIntEQ(RSA_check_key(rsa), 1);
  46060. #endif
  46061. /* sanity check */
  46062. AssertIntEQ(RSA_bits(NULL), 0);
  46063. /* key */
  46064. AssertIntEQ(RSA_bits(rsa), 2048);
  46065. RSA_get0_key(rsa, &n, &e, &d);
  46066. AssertPtrEq(rsa->n, n);
  46067. AssertPtrEq(rsa->e, e);
  46068. AssertPtrEq(rsa->d, d);
  46069. AssertNotNull(n = BN_new());
  46070. AssertNotNull(e = BN_new());
  46071. AssertNotNull(d = BN_new());
  46072. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  46073. AssertPtrEq(rsa->n, n);
  46074. AssertPtrEq(rsa->e, e);
  46075. AssertPtrEq(rsa->d, d);
  46076. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  46077. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  46078. /* crt_params */
  46079. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  46080. AssertPtrEq(rsa->dmp1, dmp1);
  46081. AssertPtrEq(rsa->dmq1, dmq1);
  46082. AssertPtrEq(rsa->iqmp, iqmp);
  46083. AssertNotNull(dmp1 = BN_new());
  46084. AssertNotNull(dmq1 = BN_new());
  46085. AssertNotNull(iqmp = BN_new());
  46086. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46087. (BIGNUM*)iqmp), 1);
  46088. AssertPtrEq(rsa->dmp1, dmp1);
  46089. AssertPtrEq(rsa->dmq1, dmq1);
  46090. AssertPtrEq(rsa->iqmp, iqmp);
  46091. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  46092. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46093. (BIGNUM*)iqmp), 0);
  46094. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  46095. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  46096. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  46097. AssertNull(dmp1);
  46098. AssertNull(dmq1);
  46099. AssertNull(iqmp);
  46100. /* factors */
  46101. RSA_get0_factors(rsa, NULL, NULL);
  46102. RSA_get0_factors(rsa, &p, &q);
  46103. AssertPtrEq(rsa->p, p);
  46104. AssertPtrEq(rsa->q, q);
  46105. AssertNotNull(p = BN_new());
  46106. AssertNotNull(q = BN_new());
  46107. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  46108. AssertPtrEq(rsa->p, p);
  46109. AssertPtrEq(rsa->q, q);
  46110. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  46111. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  46112. RSA_get0_factors(NULL, NULL, NULL);
  46113. RSA_get0_factors(NULL, &p, &q);
  46114. AssertNull(p);
  46115. AssertNull(q);
  46116. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46117. AssertIntEQ(RSA_bits(rsa), 21);
  46118. RSA_free(rsa);
  46119. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46120. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46121. AssertIntEQ(RSA_size(rsa), 384);
  46122. AssertIntEQ(RSA_bits(rsa), 3072);
  46123. RSA_free(rsa);
  46124. #endif
  46125. /* remove for now with odd key size until adjusting rsa key size check with
  46126. wc_MakeRsaKey()
  46127. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46128. RSA_free(rsa);
  46129. */
  46130. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  46131. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46132. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46133. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  46134. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46135. {
  46136. byte buff[FOURK_BUF];
  46137. byte der[FOURK_BUF];
  46138. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46139. XFILE f;
  46140. int bytes;
  46141. /* test loading encrypted RSA private pem w/o password */
  46142. f = XFOPEN(PrivKeyPemFile, "rb");
  46143. AssertTrue((f != XBADFILE));
  46144. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  46145. XFCLOSE(f);
  46146. XMEMSET(der, 0, sizeof(der));
  46147. /* test that error value is returned with no password */
  46148. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  46149. }
  46150. #endif
  46151. res = TEST_RES_CHECK(1);
  46152. #endif
  46153. return res;
  46154. }
  46155. static int test_wolfSSL_RSA_DER(void)
  46156. {
  46157. int res = TEST_SKIPPED;
  46158. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46159. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46160. RSA *rsa;
  46161. int i;
  46162. const unsigned char *buff = NULL;
  46163. unsigned char *newBuff = NULL;
  46164. struct tbl_s
  46165. {
  46166. const unsigned char *der;
  46167. int sz;
  46168. } tbl[] = {
  46169. #ifdef USE_CERT_BUFFERS_1024
  46170. {client_key_der_1024, sizeof_client_key_der_1024},
  46171. {server_key_der_1024, sizeof_server_key_der_1024},
  46172. #endif
  46173. #ifdef USE_CERT_BUFFERS_2048
  46174. {client_key_der_2048, sizeof_client_key_der_2048},
  46175. {server_key_der_2048, sizeof_server_key_der_2048},
  46176. #endif
  46177. {NULL, 0}
  46178. };
  46179. /* Public Key DER */
  46180. struct tbl_s pub[] = {
  46181. #ifdef USE_CERT_BUFFERS_1024
  46182. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46183. #endif
  46184. #ifdef USE_CERT_BUFFERS_2048
  46185. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46186. #endif
  46187. {NULL, 0}
  46188. };
  46189. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46190. buff = pub[0].der;
  46191. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46192. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46193. buff = tbl[0].der;
  46194. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46195. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46196. rsa = RSA_new();
  46197. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46198. RSA_free(rsa);
  46199. for (i = 0; tbl[i].der != NULL; i++)
  46200. {
  46201. /* Passing in pointer results in pointer moving. */
  46202. buff = tbl[i].der;
  46203. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46204. AssertNotNull(rsa);
  46205. RSA_free(rsa);
  46206. }
  46207. for (i = 0; tbl[i].der != NULL; i++)
  46208. {
  46209. /* Passing in pointer results in pointer moving. */
  46210. buff = tbl[i].der;
  46211. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46212. AssertNotNull(rsa);
  46213. RSA_free(rsa);
  46214. }
  46215. for (i = 0; pub[i].der != NULL; i++)
  46216. {
  46217. buff = pub[i].der;
  46218. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46219. AssertNotNull(rsa);
  46220. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46221. newBuff = NULL;
  46222. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46223. AssertNotNull(newBuff);
  46224. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46225. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46226. RSA_free(rsa);
  46227. }
  46228. res = TEST_RES_CHECK(1);
  46229. #endif
  46230. return res;
  46231. }
  46232. static int test_wolfSSL_RSA_print(void)
  46233. {
  46234. int res = TEST_SKIPPED;
  46235. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46236. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46237. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  46238. BIO *bio;
  46239. WOLFSSL_RSA* rsa = NULL;
  46240. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46241. AssertNotNull(rsa = RSA_new());
  46242. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  46243. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46244. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  46245. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  46246. /* Some very large number of indent spaces. */
  46247. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  46248. /* RSA is empty. */
  46249. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  46250. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  46251. RSA_free(rsa);
  46252. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46253. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  46254. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  46255. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  46256. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  46257. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  46258. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  46259. BIO_free(bio);
  46260. RSA_free(rsa);
  46261. res = TEST_RES_CHECK(1);
  46262. #endif
  46263. return res;
  46264. }
  46265. #ifndef NO_RSA
  46266. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  46267. {
  46268. int res = TEST_SKIPPED;
  46269. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46270. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46271. RSA *rsa;
  46272. const unsigned char *derBuf = client_key_der_2048;
  46273. unsigned char em[256] = {0}; /* len = 2048/8 */
  46274. /* Random data simulating a hash */
  46275. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  46276. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46277. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46278. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46279. };
  46280. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46281. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  46282. RSA_PSS_SALTLEN_DIGEST), 0);
  46283. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  46284. RSA_PSS_SALTLEN_DIGEST), 0);
  46285. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  46286. RSA_PSS_SALTLEN_DIGEST), 0);
  46287. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  46288. RSA_PSS_SALTLEN_DIGEST), 0);
  46289. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  46290. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  46291. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46292. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  46293. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46294. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  46295. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46296. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  46297. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46298. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46299. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46300. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  46301. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46302. RSA_PSS_SALTLEN_DIGEST), 1);
  46303. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46304. RSA_PSS_SALTLEN_DIGEST), 1);
  46305. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46306. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46307. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46308. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46309. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46310. RSA_PSS_SALTLEN_MAX), 1);
  46311. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46312. RSA_PSS_SALTLEN_MAX), 1);
  46313. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  46314. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  46315. RSA_free(rsa);
  46316. res = TEST_RES_CHECK(1);
  46317. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46318. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46319. return res;
  46320. }
  46321. #endif
  46322. static int test_wolfSSL_RSA_sign_sha3(void)
  46323. {
  46324. int res = TEST_SKIPPED;
  46325. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  46326. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46327. RSA *rsa;
  46328. const unsigned char *derBuf = client_key_der_2048;
  46329. unsigned char sigRet[256] = {0};
  46330. unsigned int sigLen = sizeof(sigRet);
  46331. /* Random data simulating a hash */
  46332. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  46333. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46334. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46335. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46336. };
  46337. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46338. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  46339. &sigLen, rsa), 1);
  46340. RSA_free(rsa);
  46341. res = TEST_RES_CHECK(1);
  46342. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46343. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  46344. return res;
  46345. }
  46346. static int test_wolfSSL_RSA_get0_key(void)
  46347. {
  46348. int res = TEST_SKIPPED;
  46349. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  46350. RSA *rsa = NULL;
  46351. const BIGNUM* n = NULL;
  46352. const BIGNUM* e = NULL;
  46353. const BIGNUM* d = NULL;
  46354. const unsigned char* der;
  46355. int derSz;
  46356. #ifdef USE_CERT_BUFFERS_1024
  46357. der = client_key_der_1024;
  46358. derSz = sizeof_client_key_der_1024;
  46359. #elif defined(USE_CERT_BUFFERS_2048)
  46360. der = client_key_der_2048;
  46361. derSz = sizeof_client_key_der_2048;
  46362. #else
  46363. der = NULL;
  46364. derSz = 0;
  46365. #endif
  46366. if (der != NULL) {
  46367. RSA_get0_key(NULL, NULL, NULL, NULL);
  46368. RSA_get0_key(rsa, NULL, NULL, NULL);
  46369. RSA_get0_key(NULL, &n, &e, &d);
  46370. AssertNull(n);
  46371. AssertNull(e);
  46372. AssertNull(d);
  46373. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  46374. AssertNotNull(rsa);
  46375. RSA_get0_key(rsa, NULL, NULL, NULL);
  46376. RSA_get0_key(rsa, &n, NULL, NULL);
  46377. AssertNotNull(n);
  46378. RSA_get0_key(rsa, NULL, &e, NULL);
  46379. AssertNotNull(e);
  46380. RSA_get0_key(rsa, NULL, NULL, &d);
  46381. AssertNotNull(d);
  46382. RSA_get0_key(rsa, &n, &e, &d);
  46383. AssertNotNull(n);
  46384. AssertNotNull(e);
  46385. AssertNotNull(d);
  46386. RSA_free(rsa);
  46387. }
  46388. res = TEST_RES_CHECK(1);
  46389. #endif
  46390. return res;
  46391. }
  46392. static int test_wolfSSL_RSA_meth(void)
  46393. {
  46394. int res = TEST_SKIPPED;
  46395. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46396. RSA *rsa;
  46397. RSA_METHOD *rsa_meth;
  46398. #ifdef WOLFSSL_KEY_GEN
  46399. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46400. RSA_free(rsa);
  46401. #else
  46402. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46403. #endif
  46404. AssertNotNull(RSA_get_default_method());
  46405. wolfSSL_RSA_meth_free(NULL);
  46406. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  46407. AssertNotNull(rsa_meth =
  46408. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  46409. #ifndef NO_WOLFSSL_STUB
  46410. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  46411. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  46412. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  46413. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  46414. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  46415. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  46416. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  46417. #endif
  46418. AssertIntEQ(RSA_flags(NULL), 0);
  46419. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46420. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46421. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  46422. AssertNotNull(rsa = RSA_new());
  46423. /* No method set. */
  46424. AssertIntEQ(RSA_flags(rsa), 0);
  46425. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46426. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46427. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46428. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  46429. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  46430. AssertNull(RSA_get_method(NULL));
  46431. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  46432. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  46433. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46434. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46435. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  46436. RSA_METHOD_FLAG_NO_CHECK);
  46437. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46438. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46439. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  46440. /* rsa_meth is freed here */
  46441. RSA_free(rsa);
  46442. res = TEST_RES_CHECK(1);
  46443. #endif
  46444. return res;
  46445. }
  46446. static int test_wolfSSL_RSA_verify(void)
  46447. {
  46448. int res = TEST_SKIPPED;
  46449. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  46450. !defined(NO_FILESYSTEM)
  46451. #ifndef NO_BIO
  46452. XFILE fp;
  46453. RSA *pKey, *pubKey;
  46454. X509 *cert;
  46455. const char *text = "Hello wolfSSL !";
  46456. unsigned char hash[SHA256_DIGEST_LENGTH];
  46457. unsigned char signature[2048/8];
  46458. unsigned int signatureLength;
  46459. byte *buf;
  46460. BIO *bio;
  46461. SHA256_CTX c;
  46462. EVP_PKEY *evpPkey, *evpPubkey;
  46463. size_t sz;
  46464. /* generate hash */
  46465. SHA256_Init(&c);
  46466. SHA256_Update(&c, text, strlen(text));
  46467. SHA256_Final(hash, &c);
  46468. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46469. /* workaround for small stack cache case */
  46470. wc_Sha256Free((wc_Sha256*)&c);
  46471. #endif
  46472. /* read privete key file */
  46473. fp = XFOPEN(svrKeyFile, "rb");
  46474. AssertTrue((fp != XBADFILE));
  46475. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  46476. sz = XFTELL(fp);
  46477. XREWIND(fp);
  46478. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  46479. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  46480. XFCLOSE(fp);
  46481. /* read private key and sign hash data */
  46482. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  46483. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  46484. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  46485. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  46486. signature, &signatureLength, pKey), SSL_SUCCESS);
  46487. /* read public key and verify signed data */
  46488. fp = XFOPEN(svrCertFile,"rb");
  46489. AssertTrue((fp != XBADFILE));
  46490. cert = PEM_read_X509(fp, 0, 0, 0 );
  46491. XFCLOSE(fp);
  46492. evpPubkey = X509_get_pubkey(cert);
  46493. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  46494. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46495. signatureLength, pubKey), SSL_SUCCESS);
  46496. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  46497. signatureLength, NULL), SSL_FAILURE);
  46498. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  46499. signatureLength, pubKey), SSL_FAILURE);
  46500. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  46501. signatureLength, pubKey), SSL_FAILURE);
  46502. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46503. signatureLength, NULL), SSL_FAILURE);
  46504. RSA_free(pKey);
  46505. EVP_PKEY_free(evpPkey);
  46506. RSA_free(pubKey);
  46507. EVP_PKEY_free(evpPubkey);
  46508. X509_free(cert);
  46509. BIO_free(bio);
  46510. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  46511. res = TEST_RES_CHECK(1);
  46512. #endif
  46513. #endif
  46514. return res;
  46515. }
  46516. static int test_wolfSSL_RSA_sign(void)
  46517. {
  46518. int res = TEST_SKIPPED;
  46519. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46520. RSA *rsa;
  46521. unsigned char hash[SHA256_DIGEST_LENGTH];
  46522. #ifdef USE_CERT_BUFFERS_1024
  46523. const unsigned char* privDer = client_key_der_1024;
  46524. size_t privDerSz = sizeof_client_key_der_1024;
  46525. const unsigned char* pubDer = client_keypub_der_1024;
  46526. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46527. unsigned char signature[1024/8];
  46528. #else
  46529. const unsigned char* privDer = client_key_der_2048;
  46530. size_t privDerSz = sizeof_client_key_der_2048;
  46531. const unsigned char* pubDer = client_keypub_der_2048;
  46532. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46533. unsigned char signature[2048/8];
  46534. #endif
  46535. unsigned int signatureLen;
  46536. const unsigned char* der;
  46537. XMEMSET(hash, 0, sizeof(hash));
  46538. der = privDer;
  46539. rsa = NULL;
  46540. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46541. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  46542. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  46543. &signatureLen, rsa), 0);
  46544. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  46545. &signatureLen, rsa), 0);
  46546. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  46547. &signatureLen, rsa), 0);
  46548. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46549. NULL, rsa), 0);
  46550. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46551. &signatureLen, NULL), 0);
  46552. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46553. &signatureLen, rsa), 1);
  46554. RSA_free(rsa);
  46555. der = pubDer;
  46556. rsa = NULL;
  46557. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46558. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46559. signatureLen, rsa), 1);
  46560. RSA_free(rsa);
  46561. res = TEST_RES_CHECK(1);
  46562. #endif
  46563. return res;
  46564. }
  46565. static int test_wolfSSL_RSA_sign_ex(void)
  46566. {
  46567. int res = TEST_SKIPPED;
  46568. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46569. RSA *rsa;
  46570. unsigned char hash[SHA256_DIGEST_LENGTH];
  46571. #ifdef USE_CERT_BUFFERS_1024
  46572. const unsigned char* privDer = client_key_der_1024;
  46573. size_t privDerSz = sizeof_client_key_der_1024;
  46574. const unsigned char* pubDer = client_keypub_der_1024;
  46575. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46576. unsigned char signature[1024/8];
  46577. #else
  46578. const unsigned char* privDer = client_key_der_2048;
  46579. size_t privDerSz = sizeof_client_key_der_2048;
  46580. const unsigned char* pubDer = client_keypub_der_2048;
  46581. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46582. unsigned char signature[2048/8];
  46583. #endif
  46584. unsigned int signatureLen;
  46585. const unsigned char* der;
  46586. unsigned char encodedHash[51];
  46587. unsigned int encodedHashLen;
  46588. const unsigned char expEncHash[] = {
  46589. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  46590. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  46591. 0x00, 0x04, 0x20,
  46592. /* Hash data */
  46593. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46594. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46597. };
  46598. XMEMSET(hash, 0, sizeof(hash));
  46599. AssertNotNull(rsa = wolfSSL_RSA_new());
  46600. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46601. &signatureLen, rsa, 1), 0);
  46602. wolfSSL_RSA_free(rsa);
  46603. der = privDer;
  46604. rsa = NULL;
  46605. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46606. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  46607. -1), 0);
  46608. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  46609. signature, &signatureLen, rsa, 1), 0);
  46610. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46611. &signatureLen, rsa, 1), 0);
  46612. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46613. &signatureLen, rsa, 1), 0);
  46614. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46615. NULL, rsa, 1), 0);
  46616. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46617. &signatureLen, NULL, 1), 0);
  46618. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46619. &signatureLen, rsa, -1), 0);
  46620. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46621. &signatureLen, rsa, 0), 0);
  46622. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46623. &signatureLen, rsa, 0), 0);
  46624. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46625. NULL, rsa, 0), 0);
  46626. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46627. &signatureLen, rsa, 1), 1);
  46628. /* Test returning encoded hash. */
  46629. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  46630. &encodedHashLen, rsa, 0), 1);
  46631. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  46632. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  46633. RSA_free(rsa);
  46634. der = pubDer;
  46635. rsa = NULL;
  46636. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46637. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46638. signatureLen, rsa), 1);
  46639. RSA_free(rsa);
  46640. res = TEST_RES_CHECK(1);
  46641. #endif
  46642. return res;
  46643. }
  46644. static int test_wolfSSL_RSA_public_decrypt(void)
  46645. {
  46646. int res = TEST_SKIPPED;
  46647. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46648. RSA *rsa;
  46649. unsigned char msg[SHA256_DIGEST_LENGTH];
  46650. #ifdef USE_CERT_BUFFERS_1024
  46651. const unsigned char* pubDer = client_keypub_der_1024;
  46652. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46653. unsigned char decMsg[1024/8];
  46654. const unsigned char encMsg[] = {
  46655. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46656. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46657. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46658. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46659. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46660. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46661. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46662. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46663. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46664. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46665. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46666. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46667. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46668. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46669. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46670. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46671. };
  46672. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46673. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46674. defined(WC_RSA_NO_PADDING)
  46675. const unsigned char encMsgNoPad[] = {
  46676. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46677. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46678. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46679. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46680. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46681. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46682. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46683. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46684. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46685. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46686. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46687. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46688. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46689. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46690. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46691. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46692. };
  46693. #endif
  46694. #else
  46695. const unsigned char* pubDer = client_keypub_der_2048;
  46696. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46697. unsigned char decMsg[2048/8];
  46698. const unsigned char encMsg[] = {
  46699. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46700. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46701. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46702. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46703. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46704. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46705. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46706. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46707. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46708. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46709. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46710. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46711. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46712. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46713. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46714. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46715. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46716. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46717. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46718. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46719. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46720. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46721. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46722. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46723. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46724. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46725. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46726. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46727. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46728. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46729. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46730. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46731. };
  46732. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46733. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46734. defined(WC_RSA_NO_PADDING)
  46735. const unsigned char encMsgNoPad[] = {
  46736. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46737. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46738. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46739. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46740. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46741. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46742. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46743. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46744. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46745. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46746. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46747. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46748. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46749. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46750. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46751. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46752. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46753. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46754. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46755. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46756. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46757. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46758. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46759. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46760. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46761. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46762. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46763. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46764. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46765. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46766. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46767. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46768. };
  46769. #endif
  46770. #endif
  46771. const unsigned char* der;
  46772. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46773. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46774. defined(WC_RSA_NO_PADDING)
  46775. int i;
  46776. #endif
  46777. XMEMSET(msg, 0, sizeof(msg));
  46778. der = pubDer;
  46779. rsa = NULL;
  46780. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46781. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  46782. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  46783. RSA_PKCS1_PADDING), -1);
  46784. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  46785. RSA_PKCS1_PADDING), -1);
  46786. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  46787. RSA_PKCS1_PADDING), -1);
  46788. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  46789. RSA_PKCS1_PADDING), -1);
  46790. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46791. RSA_PKCS1_PSS_PADDING), -1);
  46792. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46793. RSA_PKCS1_PADDING), 32);
  46794. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  46795. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46796. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46797. defined(WC_RSA_NO_PADDING)
  46798. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  46799. rsa, RSA_NO_PADDING), sizeof(decMsg));
  46800. /* Zeros before actual data. */
  46801. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  46802. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46803. }
  46804. /* Check actual data. */
  46805. XMEMSET(msg, 0x01, sizeof(msg));
  46806. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46807. #endif
  46808. RSA_free(rsa);
  46809. res = TEST_RES_CHECK(1);
  46810. #endif
  46811. return res;
  46812. }
  46813. static int test_wolfSSL_RSA_private_encrypt(void)
  46814. {
  46815. int res = TEST_SKIPPED;
  46816. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46817. RSA *rsa;
  46818. unsigned char msg[SHA256_DIGEST_LENGTH];
  46819. #ifdef USE_CERT_BUFFERS_1024
  46820. const unsigned char* privDer = client_key_der_1024;
  46821. size_t privDerSz = sizeof_client_key_der_1024;
  46822. unsigned char encMsg[1024/8];
  46823. const unsigned char expEncMsg[] = {
  46824. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46825. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46826. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46827. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46828. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46829. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46830. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46831. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46832. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46833. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46834. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46835. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46836. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46837. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46838. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46839. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46840. };
  46841. #ifdef WC_RSA_NO_PADDING
  46842. const unsigned char expEncMsgNoPad[] = {
  46843. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46844. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46845. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46846. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46847. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46848. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46849. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46850. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46851. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46852. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46853. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46854. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46855. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46856. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46857. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46858. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46859. };
  46860. #endif
  46861. #else
  46862. const unsigned char* privDer = client_key_der_2048;
  46863. size_t privDerSz = sizeof_client_key_der_2048;
  46864. unsigned char encMsg[2048/8];
  46865. const unsigned char expEncMsg[] = {
  46866. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46867. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46868. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46869. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46870. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46871. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46872. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46873. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46874. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46875. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46876. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46877. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46878. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46879. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46880. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46881. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46882. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46883. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46884. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46885. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46886. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46887. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46888. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46889. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46890. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46891. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46892. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46893. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46894. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46895. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46896. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46897. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46898. };
  46899. #ifdef WC_RSA_NO_PADDING
  46900. const unsigned char expEncMsgNoPad[] = {
  46901. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46902. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46903. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46904. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46905. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46906. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46907. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46908. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46909. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46910. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46911. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46912. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46913. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46914. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46915. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46916. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46917. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46918. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46919. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46920. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46921. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46922. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46923. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46924. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46925. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46926. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46927. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46928. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46929. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46930. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46931. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46932. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46933. };
  46934. #endif
  46935. #endif
  46936. const unsigned char* der;
  46937. XMEMSET(msg, 0x00, sizeof(msg));
  46938. der = privDer;
  46939. rsa = NULL;
  46940. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46941. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  46942. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  46943. -1);
  46944. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46945. RSA_PKCS1_PADDING), -1);
  46946. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  46947. RSA_PKCS1_PADDING), -1);
  46948. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  46949. RSA_PKCS1_PADDING), -1);
  46950. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46951. RSA_PKCS1_PSS_PADDING), -1);
  46952. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46953. RSA_PKCS1_PADDING), sizeof(encMsg));
  46954. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  46955. #ifdef WC_RSA_NO_PADDING
  46956. /* Non-zero message. */
  46957. XMEMSET(msg, 0x01, sizeof(msg));
  46958. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46959. RSA_NO_PADDING), sizeof(encMsg));
  46960. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  46961. #endif
  46962. RSA_free(rsa);
  46963. res = TEST_RES_CHECK(1);
  46964. #endif
  46965. return res;
  46966. }
  46967. static int test_wolfSSL_RSA_public_encrypt(void)
  46968. {
  46969. int res = TEST_SKIPPED;
  46970. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46971. RSA* rsa;
  46972. const unsigned char msg[2048/8] = { 0 };
  46973. unsigned char encMsg[2048/8];
  46974. AssertNotNull(rsa = RSA_new());
  46975. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  46976. RSA_PKCS1_PADDING), -1);
  46977. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46978. RSA_PKCS1_PADDING), -1);
  46979. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  46980. RSA_PKCS1_PADDING), -1);
  46981. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  46982. RSA_PKCS1_PADDING), -1);
  46983. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46984. RSA_PKCS1_PSS_PADDING), -1);
  46985. /* Empty RSA key. */
  46986. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46987. RSA_PKCS1_PADDING), -1);
  46988. RSA_free(rsa);
  46989. res = TEST_RES_CHECK(1);
  46990. #endif
  46991. return res;
  46992. }
  46993. static int test_wolfSSL_RSA_private_decrypt(void)
  46994. {
  46995. int res = TEST_SKIPPED;
  46996. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46997. RSA* rsa;
  46998. unsigned char msg[2048/8];
  46999. const unsigned char encMsg[2048/8] = { 0 };
  47000. AssertNotNull(rsa = RSA_new());
  47001. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  47002. RSA_PKCS1_PADDING), -1);
  47003. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  47004. RSA_PKCS1_PADDING), -1);
  47005. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47006. RSA_PKCS1_PADDING), -1);
  47007. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  47008. RSA_PKCS1_PADDING), -1);
  47009. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47010. RSA_PKCS1_PSS_PADDING), -1);
  47011. /* Empty RSA key. */
  47012. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47013. RSA_PKCS1_PADDING), -1);
  47014. RSA_free(rsa);
  47015. res = TEST_RES_CHECK(1);
  47016. #endif
  47017. return res;
  47018. }
  47019. static int test_wolfSSL_RSA_GenAdd(void)
  47020. {
  47021. int res = TEST_SKIPPED;
  47022. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47023. RSA *rsa;
  47024. #ifdef USE_CERT_BUFFERS_1024
  47025. const unsigned char* privDer = client_key_der_1024;
  47026. size_t privDerSz = sizeof_client_key_der_1024;
  47027. const unsigned char* pubDer = client_keypub_der_1024;
  47028. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47029. #else
  47030. const unsigned char* privDer = client_key_der_2048;
  47031. size_t privDerSz = sizeof_client_key_der_2048;
  47032. const unsigned char* pubDer = client_keypub_der_2048;
  47033. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47034. #endif
  47035. const unsigned char* der;
  47036. der = privDer;
  47037. rsa = NULL;
  47038. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47039. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  47040. #ifndef RSA_LOW_MEM
  47041. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  47042. #else
  47043. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  47044. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47045. #endif
  47046. RSA_free(rsa);
  47047. der = pubDer;
  47048. rsa = NULL;
  47049. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47050. /* Need private values. */
  47051. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47052. RSA_free(rsa);
  47053. res = TEST_RES_CHECK(1);
  47054. #endif
  47055. return res;
  47056. }
  47057. static int test_wolfSSL_RSA_blinding_on(void)
  47058. {
  47059. int res = TEST_SKIPPED;
  47060. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  47061. RSA *rsa;
  47062. WOLFSSL_BN_CTX *bnCtx;
  47063. #ifdef USE_CERT_BUFFERS_1024
  47064. const unsigned char* privDer = client_key_der_1024;
  47065. size_t privDerSz = sizeof_client_key_der_1024;
  47066. #else
  47067. const unsigned char* privDer = client_key_der_2048;
  47068. size_t privDerSz = sizeof_client_key_der_2048;
  47069. #endif
  47070. const unsigned char* der;
  47071. der = privDer;
  47072. rsa = NULL;
  47073. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47074. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  47075. /* Does nothing so all parameters are valid. */
  47076. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  47077. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  47078. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  47079. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  47080. wolfSSL_BN_CTX_free(bnCtx);
  47081. RSA_free(rsa);
  47082. res = TEST_RES_CHECK(1);
  47083. #endif
  47084. return res;
  47085. }
  47086. static int test_wolfSSL_RSA_ex_data(void)
  47087. {
  47088. int res = TEST_SKIPPED;
  47089. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  47090. RSA* rsa;
  47091. unsigned char data[1];
  47092. rsa = RSA_new();
  47093. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  47094. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47095. #ifdef MAX_EX_DATA
  47096. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  47097. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  47098. #endif
  47099. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  47100. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  47101. #ifdef HAVE_EX_DATA
  47102. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  47103. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  47104. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  47105. #else
  47106. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  47107. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  47108. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47109. #endif
  47110. RSA_free(rsa);
  47111. res = TEST_RES_CHECK(1);
  47112. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47113. return res;
  47114. }
  47115. static int test_wolfSSL_RSA_LoadDer(void)
  47116. {
  47117. int res = TEST_SKIPPED;
  47118. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47119. defined(OPENSSL_EXTRA_X509_SMALL))
  47120. RSA *rsa;
  47121. #ifdef USE_CERT_BUFFERS_1024
  47122. const unsigned char* privDer = client_key_der_1024;
  47123. size_t privDerSz = sizeof_client_key_der_1024;
  47124. #else
  47125. const unsigned char* privDer = client_key_der_2048;
  47126. size_t privDerSz = sizeof_client_key_der_2048;
  47127. #endif
  47128. AssertNotNull(rsa = RSA_new());
  47129. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47130. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47131. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47132. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47133. RSA_free(rsa);
  47134. res = TEST_RES_CHECK(1);
  47135. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47136. return res;
  47137. }
  47138. /* Local API. */
  47139. static int test_wolfSSL_RSA_To_Der(void)
  47140. {
  47141. int res = TEST_SKIPPED;
  47142. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47143. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47144. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47145. RSA* rsa;
  47146. #ifdef USE_CERT_BUFFERS_1024
  47147. const unsigned char* privDer = client_key_der_1024;
  47148. size_t privDerSz = sizeof_client_key_der_1024;
  47149. const unsigned char* pubDer = client_keypub_der_1024;
  47150. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47151. unsigned char out[sizeof(client_key_der_1024)];
  47152. #else
  47153. const unsigned char* privDer = client_key_der_2048;
  47154. size_t privDerSz = sizeof_client_key_der_2048;
  47155. const unsigned char* pubDer = client_keypub_der_2048;
  47156. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47157. unsigned char out[sizeof(client_key_der_2048)];
  47158. #endif
  47159. const unsigned char* der;
  47160. unsigned char* outDer = NULL;
  47161. der = privDer;
  47162. rsa = NULL;
  47163. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47164. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47165. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47166. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47167. outDer = out;
  47168. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47169. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47170. outDer = NULL;
  47171. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47172. AssertNotNull(outDer);
  47173. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47174. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47175. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47176. outDer = out;
  47177. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47178. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47179. RSA_free(rsa);
  47180. AssertNotNull(rsa = RSA_new());
  47181. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47182. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47183. RSA_free(rsa);
  47184. der = pubDer;
  47185. rsa = NULL;
  47186. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47187. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47188. RSA_free(rsa);
  47189. res = TEST_RES_CHECK(1);
  47190. #endif
  47191. #endif
  47192. return res;
  47193. }
  47194. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47195. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47196. {
  47197. int res = TEST_SKIPPED;
  47198. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47199. XFILE file;
  47200. const char* fname = "./certs/server-keyPub.pem";
  47201. RSA *rsa;
  47202. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47203. file = XFOPEN(fname, "rb");
  47204. AssertTrue((file != XBADFILE));
  47205. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  47206. AssertIntEQ(RSA_size(rsa), 256);
  47207. RSA_free(rsa);
  47208. XFCLOSE(file);
  47209. res = TEST_RES_CHECK(1);
  47210. #endif
  47211. return res;
  47212. }
  47213. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47214. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  47215. {
  47216. int res = TEST_SKIPPED;
  47217. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  47218. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  47219. RSA* rsa = NULL;
  47220. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  47221. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  47222. /* Valid but stub so returns 0. */
  47223. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  47224. res = TEST_RES_CHECK(1);
  47225. #endif
  47226. return res;
  47227. }
  47228. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47229. {
  47230. int res = TEST_SKIPPED;
  47231. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47232. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47233. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47234. RSA* rsa;
  47235. #ifdef USE_CERT_BUFFERS_1024
  47236. const unsigned char* privDer = client_key_der_1024;
  47237. size_t privDerSz = sizeof_client_key_der_1024;
  47238. #else
  47239. const unsigned char* privDer = client_key_der_2048;
  47240. size_t privDerSz = sizeof_client_key_der_2048;
  47241. #endif
  47242. const unsigned char* der;
  47243. #ifndef NO_AES
  47244. unsigned char passwd[] = "password";
  47245. #endif
  47246. AssertNotNull(rsa = RSA_new());
  47247. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47248. NULL, NULL), 0);
  47249. RSA_free(rsa);
  47250. der = privDer;
  47251. rsa = NULL;
  47252. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47253. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47254. NULL, NULL), 0);
  47255. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  47256. NULL, NULL), 0);
  47257. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47258. NULL, NULL), 1);
  47259. #ifndef NO_AES
  47260. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47261. NULL, 0, NULL, NULL), 1);
  47262. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47263. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  47264. #endif
  47265. RSA_free(rsa);
  47266. res = TEST_RES_CHECK(1);
  47267. #endif
  47268. return res;
  47269. }
  47270. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  47271. {
  47272. int res = TEST_SKIPPED;
  47273. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47274. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47275. defined(WOLFSSL_DER_TO_PEM))
  47276. RSA* rsa;
  47277. #ifdef USE_CERT_BUFFERS_1024
  47278. const unsigned char* privDer = client_key_der_1024;
  47279. size_t privDerSz = sizeof_client_key_der_1024;
  47280. #else
  47281. const unsigned char* privDer = client_key_der_2048;
  47282. size_t privDerSz = sizeof_client_key_der_2048;
  47283. #endif
  47284. const unsigned char* der;
  47285. #ifndef NO_AES
  47286. unsigned char passwd[] = "password";
  47287. #endif
  47288. unsigned char* pem;
  47289. int plen;
  47290. AssertNotNull(rsa = RSA_new());
  47291. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47292. &plen), 0);
  47293. RSA_free(rsa);
  47294. der = privDer;
  47295. rsa = NULL;
  47296. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47297. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  47298. &plen), 0);
  47299. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  47300. &plen), 0);
  47301. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47302. NULL), 0);
  47303. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47304. &plen), 1);
  47305. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47306. #ifndef NO_AES
  47307. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47308. NULL, 0, &pem, &plen), 1);
  47309. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47310. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47311. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  47312. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47313. #endif
  47314. RSA_free(rsa);
  47315. res = TEST_RES_CHECK(1);
  47316. #endif
  47317. return res;
  47318. }
  47319. static int test_wolfSSL_DH(void)
  47320. {
  47321. int res = TEST_SKIPPED;
  47322. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47323. DH *dh = NULL;
  47324. BIGNUM* p;
  47325. BIGNUM* q;
  47326. BIGNUM* g;
  47327. BIGNUM* pub;
  47328. BIGNUM* priv;
  47329. #if defined(OPENSSL_ALL)
  47330. #if !defined(HAVE_FIPS) || \
  47331. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47332. FILE* f = NULL;
  47333. unsigned char buf[268];
  47334. const unsigned char* pt = buf;
  47335. long len = 0;
  47336. dh = NULL;
  47337. XMEMSET(buf, 0, sizeof(buf));
  47338. /* Test 2048 bit parameters */
  47339. f = XFOPEN("./certs/dh2048.der", "rb");
  47340. AssertTrue(f != XBADFILE);
  47341. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47342. XFCLOSE(f);
  47343. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47344. AssertNotNull(dh->p);
  47345. AssertNotNull(dh->g);
  47346. AssertTrue(pt == buf);
  47347. AssertIntEQ(DH_generate_key(dh), 1);
  47348. AssertIntEQ(DH_generate_key(dh), 1);
  47349. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  47350. AssertNotNull(pub = BN_new());
  47351. AssertIntEQ(BN_set_word(pub, 1), 1);
  47352. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  47353. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  47354. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  47355. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  47356. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  47357. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  47358. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  47359. BN_free(pub);
  47360. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47361. (const BIGNUM**)&q,
  47362. (const BIGNUM**)&g);
  47363. AssertPtrEq(p, dh->p);
  47364. AssertPtrEq(q, dh->q);
  47365. AssertPtrEq(g, dh->g);
  47366. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47367. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47368. AssertPtrEq(pub, dh->pub_key);
  47369. AssertPtrEq(priv, dh->priv_key);
  47370. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  47371. AssertPtrEq(pub, dh->pub_key);
  47372. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  47373. AssertPtrEq(priv, dh->priv_key);
  47374. AssertNotNull(pub = BN_new());
  47375. AssertNotNull(priv = BN_new());
  47376. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  47377. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47378. AssertNotNull(pub = BN_new());
  47379. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  47380. AssertNotNull(priv = BN_new());
  47381. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  47382. AssertPtrEq(pub, dh->pub_key);
  47383. AssertPtrEq(priv, dh->priv_key);
  47384. DH_free(dh);
  47385. AssertNotNull(dh = DH_new());
  47386. AssertNotNull(p = BN_new());
  47387. AssertIntEQ(BN_set_word(p, 1), 1);
  47388. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47389. AssertNotNull(pub = BN_new());
  47390. AssertNotNull(priv = BN_new());
  47391. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47392. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47393. BN_free(p);
  47394. DH_free(dh);
  47395. #ifdef WOLFSSL_KEY_GEN
  47396. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47397. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  47398. DH_free(dh);
  47399. #endif
  47400. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  47401. #endif /* OPENSSL_ALL */
  47402. (void)dh;
  47403. (void)p;
  47404. (void)q;
  47405. (void)g;
  47406. (void)pub;
  47407. (void)priv;
  47408. dh = wolfSSL_DH_new();
  47409. AssertNotNull(dh);
  47410. /* invalid parameters test */
  47411. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47412. (const BIGNUM**)&q,
  47413. (const BIGNUM**)&g);
  47414. DH_get0_pqg(dh, NULL,
  47415. (const BIGNUM**)&q,
  47416. (const BIGNUM**)&g);
  47417. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47418. DH_get0_pqg(dh, NULL, NULL, NULL);
  47419. AssertTrue(1);
  47420. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47421. (const BIGNUM**)&q,
  47422. (const BIGNUM**)&g);
  47423. AssertPtrEq(p, NULL);
  47424. AssertPtrEq(q, NULL);
  47425. AssertPtrEq(g, NULL);
  47426. DH_free(dh);
  47427. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  47428. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  47429. #if defined(OPENSSL_ALL) || \
  47430. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47431. dh = wolfSSL_DH_new();
  47432. AssertNotNull(dh);
  47433. p = wolfSSL_BN_new();
  47434. AssertNotNull(p);
  47435. AssertIntEQ(BN_set_word(p, 11), 1);
  47436. g = wolfSSL_BN_new();
  47437. AssertNotNull(g);
  47438. AssertIntEQ(BN_set_word(g, 2), 1);
  47439. q = wolfSSL_BN_new();
  47440. AssertNotNull(q);
  47441. AssertIntEQ(BN_set_word(q, 5), 1);
  47442. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  47443. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  47444. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  47445. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  47446. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  47447. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  47448. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  47449. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  47450. /* Don't need q. */
  47451. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47452. /* Setting again will free the p and g. */
  47453. wolfSSL_BN_free(q);
  47454. DH_free(dh);
  47455. dh = wolfSSL_DH_new();
  47456. AssertNotNull(dh);
  47457. p = wolfSSL_BN_new();
  47458. AssertNotNull(p);
  47459. AssertIntEQ(BN_set_word(p, 11), 1);
  47460. g = wolfSSL_BN_new();
  47461. AssertNotNull(g);
  47462. AssertIntEQ(BN_set_word(g, 2), 1);
  47463. q = wolfSSL_BN_new();
  47464. AssertNotNull(q);
  47465. AssertIntEQ(BN_set_word(q, 5), 1);
  47466. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  47467. /* p, q and g are now owned by dh - don't free. */
  47468. p = wolfSSL_BN_new();
  47469. AssertNotNull(p);
  47470. AssertIntEQ(BN_set_word(p, 11), 1);
  47471. g = wolfSSL_BN_new();
  47472. AssertNotNull(g);
  47473. AssertIntEQ(BN_set_word(g, 2), 1);
  47474. q = wolfSSL_BN_new();
  47475. AssertNotNull(q);
  47476. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  47477. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  47478. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  47479. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  47480. /* p, q and g are now owned by dh - don't free. */
  47481. DH_free(dh);
  47482. AssertIntEQ(DH_generate_key(NULL), 0);
  47483. AssertNotNull(dh = DH_new());
  47484. AssertIntEQ(DH_generate_key(dh), 0);
  47485. p = wolfSSL_BN_new();
  47486. AssertNotNull(p);
  47487. AssertIntEQ(BN_set_word(p, 0), 1);
  47488. g = wolfSSL_BN_new();
  47489. AssertNotNull(g);
  47490. AssertIntEQ(BN_set_word(g, 2), 1);
  47491. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47492. AssertIntEQ(DH_generate_key(dh), 0);
  47493. DH_free(dh);
  47494. #endif
  47495. #endif
  47496. /* Test DH_up_ref() */
  47497. dh = wolfSSL_DH_new();
  47498. AssertNotNull(dh);
  47499. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  47500. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  47501. DH_free(dh); /* decrease ref count */
  47502. DH_free(dh); /* free WOLFSSL_DH */
  47503. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  47504. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  47505. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  47506. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  47507. #ifdef HAVE_FFDHE_2048
  47508. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47509. DH_free(dh);
  47510. #endif
  47511. #ifdef HAVE_FFDHE_3072
  47512. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  47513. DH_free(dh);
  47514. #endif
  47515. #ifdef HAVE_FFDHE_4096
  47516. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  47517. DH_free(dh);
  47518. #endif
  47519. #else
  47520. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47521. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  47522. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  47523. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  47524. res = TEST_RES_CHECK(1);
  47525. #endif /* OPENSSL_EXTRA && !NO_DH */
  47526. return res;
  47527. }
  47528. static int test_wolfSSL_DH_dup(void)
  47529. {
  47530. int res = TEST_SKIPPED;
  47531. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  47532. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  47533. defined(OPENSSL_EXTRA)
  47534. DH *dh;
  47535. DH *dhDup;
  47536. WOLFSSL_BIGNUM* p;
  47537. WOLFSSL_BIGNUM* g;
  47538. AssertNotNull(p = wolfSSL_BN_new());
  47539. AssertNotNull(g = wolfSSL_BN_new());
  47540. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  47541. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  47542. dhDup = wolfSSL_DH_dup(NULL);
  47543. AssertNull(dhDup);
  47544. dh = wolfSSL_DH_new();
  47545. AssertNotNull(dh);
  47546. dhDup = wolfSSL_DH_dup(dh);
  47547. AssertNull(dhDup);
  47548. #if defined(OPENSSL_ALL) || \
  47549. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47550. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47551. dhDup = wolfSSL_DH_dup(dh);
  47552. AssertNotNull(dhDup);
  47553. wolfSSL_DH_free(dhDup);
  47554. #else
  47555. wolfSSL_BN_free(p);
  47556. wolfSSL_BN_free(g);
  47557. #endif
  47558. wolfSSL_DH_free(dh);
  47559. res = TEST_RES_CHECK(1);
  47560. #endif
  47561. #endif
  47562. return res;
  47563. }
  47564. static int test_wolfSSL_DH_check(void)
  47565. {
  47566. int res = TEST_SKIPPED;
  47567. #ifdef OPENSSL_ALL
  47568. #ifndef NO_DH
  47569. #ifndef NO_BIO
  47570. #ifndef NO_DSA
  47571. byte buf[6000];
  47572. char file[] = "./certs/dsaparams.pem";
  47573. XFILE f;
  47574. int bytes;
  47575. BIO* bio;
  47576. DSA* dsa;
  47577. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47578. static const byte dh2048[] = {
  47579. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  47580. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  47581. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  47582. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  47583. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  47584. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  47585. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  47586. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  47587. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  47588. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  47589. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  47590. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  47591. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  47592. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  47593. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  47594. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  47595. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  47596. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  47597. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  47598. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  47599. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  47600. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  47601. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  47602. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  47603. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  47604. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  47605. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  47606. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  47607. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  47608. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  47609. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  47610. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  47611. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  47612. 0x93, 0x02, 0x01, 0x02
  47613. };
  47614. const byte* params;
  47615. #endif
  47616. DH* dh = NULL;
  47617. WOLFSSL_BIGNUM* p;
  47618. WOLFSSL_BIGNUM* g;
  47619. WOLFSSL_BIGNUM* pTmp = NULL;
  47620. WOLFSSL_BIGNUM* gTmp = NULL;
  47621. int codes = -1;
  47622. #ifndef NO_DSA
  47623. /* Initialize DH */
  47624. f = XFOPEN(file, "rb");
  47625. AssertTrue((f != XBADFILE));
  47626. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  47627. XFCLOSE(f);
  47628. bio = BIO_new_mem_buf((void*)buf, bytes);
  47629. AssertNotNull(bio);
  47630. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  47631. AssertNotNull(dsa);
  47632. dh = wolfSSL_DSA_dup_DH(dsa);
  47633. AssertNotNull(dh);
  47634. BIO_free(bio);
  47635. DSA_free(dsa);
  47636. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47637. params = dh2048;
  47638. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  47639. AssertNotNull(dh);
  47640. #else
  47641. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  47642. AssertNotNull(dh);
  47643. #endif
  47644. /* Test assumed to be valid dh.
  47645. * Should return WOLFSSL_SUCCESS
  47646. * codes should be 0
  47647. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  47648. */
  47649. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47650. AssertIntEQ(codes, 0);
  47651. /* Test NULL dh: expected BAD_FUNC_ARG */
  47652. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  47653. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  47654. pTmp = dh->p;
  47655. dh->p = NULL;
  47656. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47657. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47658. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47659. /* set dh->p back to normal so it wont fail on next tests */
  47660. dh->p = pTmp;
  47661. pTmp = NULL;
  47662. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  47663. gTmp = dh->g;
  47664. dh->g = NULL;
  47665. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47666. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47667. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  47668. dh->g = gTmp;
  47669. gTmp = NULL;
  47670. /* Cleanup */
  47671. DH_free(dh);
  47672. dh = DH_new();
  47673. AssertNotNull(dh);
  47674. /* Check empty DH. */
  47675. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47676. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47677. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  47678. /* Check non-prime valued p. */
  47679. AssertNotNull(p = BN_new());
  47680. AssertIntEQ(BN_set_word(p, 4), 1);
  47681. AssertNotNull(g = BN_new());
  47682. AssertIntEQ(BN_set_word(g, 2), 1);
  47683. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  47684. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47685. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47686. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47687. DH_free(dh);
  47688. res = TEST_RES_CHECK(1);
  47689. #endif
  47690. #endif /* !NO_DH && !NO_DSA */
  47691. #endif
  47692. return res;
  47693. }
  47694. static int test_wolfSSL_DH_prime(void)
  47695. {
  47696. int res = TEST_SKIPPED;
  47697. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47698. WOLFSSL_BIGNUM* bn;
  47699. #if WOLFSSL_MAX_BN_BITS >= 768
  47700. WOLFSSL_BIGNUM* bn2;
  47701. #endif
  47702. bn = wolfSSL_DH_768_prime(NULL);
  47703. #if WOLFSSL_MAX_BN_BITS >= 768
  47704. AssertNotNull(bn);
  47705. bn2 = wolfSSL_DH_768_prime(bn);
  47706. AssertNotNull(bn2);
  47707. AssertTrue(bn == bn2);
  47708. wolfSSL_BN_free(bn);
  47709. #else
  47710. AssertNull(bn);
  47711. #endif
  47712. bn = wolfSSL_DH_1024_prime(NULL);
  47713. #if WOLFSSL_MAX_BN_BITS >= 1024
  47714. AssertNotNull(bn);
  47715. wolfSSL_BN_free(bn);
  47716. #else
  47717. AssertNull(bn);
  47718. #endif
  47719. bn = wolfSSL_DH_2048_prime(NULL);
  47720. #if WOLFSSL_MAX_BN_BITS >= 2048
  47721. AssertNotNull(bn);
  47722. wolfSSL_BN_free(bn);
  47723. #else
  47724. AssertNull(bn);
  47725. #endif
  47726. bn = wolfSSL_DH_3072_prime(NULL);
  47727. #if WOLFSSL_MAX_BN_BITS >= 3072
  47728. AssertNotNull(bn);
  47729. wolfSSL_BN_free(bn);
  47730. #else
  47731. AssertNull(bn);
  47732. #endif
  47733. bn = wolfSSL_DH_4096_prime(NULL);
  47734. #if WOLFSSL_MAX_BN_BITS >= 4096
  47735. AssertNotNull(bn);
  47736. wolfSSL_BN_free(bn);
  47737. #else
  47738. AssertNull(bn);
  47739. #endif
  47740. bn = wolfSSL_DH_6144_prime(NULL);
  47741. #if WOLFSSL_MAX_BN_BITS >= 6144
  47742. AssertNotNull(bn);
  47743. wolfSSL_BN_free(bn);
  47744. #else
  47745. AssertNull(bn);
  47746. #endif
  47747. bn = wolfSSL_DH_8192_prime(NULL);
  47748. #if WOLFSSL_MAX_BN_BITS >= 8192
  47749. AssertNotNull(bn);
  47750. wolfSSL_BN_free(bn);
  47751. #else
  47752. AssertNull(bn);
  47753. #endif
  47754. res = TEST_RES_CHECK(1);
  47755. #endif
  47756. return res;
  47757. }
  47758. static int test_wolfSSL_DH_1536_prime(void)
  47759. {
  47760. int res = TEST_SKIPPED;
  47761. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47762. BIGNUM* bn;
  47763. unsigned char bits[200];
  47764. int sz = 192; /* known binary size */
  47765. const byte expected[] = {
  47766. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47767. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  47768. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  47769. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  47770. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  47771. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  47772. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  47773. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  47774. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  47775. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  47776. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  47777. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  47778. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  47779. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  47780. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  47781. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  47782. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  47783. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  47784. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  47785. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  47786. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  47787. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  47788. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  47789. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47790. };
  47791. bn = get_rfc3526_prime_1536(NULL);
  47792. AssertNotNull(bn);
  47793. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  47794. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  47795. BN_free(bn);
  47796. res = TEST_RES_CHECK(1);
  47797. #endif
  47798. return res;
  47799. }
  47800. static int test_wolfSSL_DH_get_2048_256(void)
  47801. {
  47802. int res = TEST_SKIPPED;
  47803. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47804. WOLFSSL_DH* dh;
  47805. const WOLFSSL_BIGNUM* pBn;
  47806. const WOLFSSL_BIGNUM* gBn;
  47807. const WOLFSSL_BIGNUM* qBn;
  47808. const byte pExpected[] = {
  47809. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  47810. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  47811. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  47812. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  47813. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  47814. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  47815. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  47816. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  47817. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  47818. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  47819. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  47820. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  47821. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  47822. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  47823. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  47824. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  47825. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  47826. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  47827. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  47828. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  47829. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  47830. 0x1E, 0x1A, 0x15, 0x97
  47831. };
  47832. const byte gExpected[] = {
  47833. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  47834. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  47835. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  47836. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  47837. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  47838. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  47839. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  47840. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  47841. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  47842. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  47843. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  47844. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  47845. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  47846. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  47847. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  47848. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  47849. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  47850. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  47851. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  47852. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  47853. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  47854. 0x6C, 0xC4, 0x16, 0x59
  47855. };
  47856. const byte qExpected[] = {
  47857. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  47858. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  47859. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  47860. };
  47861. int pSz;
  47862. int qSz;
  47863. int gSz;
  47864. byte* pReturned;
  47865. byte* qReturned;
  47866. byte* gReturned;
  47867. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  47868. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  47869. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  47870. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47871. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  47872. AssertIntEQ(pSz, sizeof(pExpected));
  47873. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  47874. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  47875. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47876. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  47877. AssertIntEQ(qSz, sizeof(qExpected));
  47878. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  47879. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  47880. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47881. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  47882. AssertIntEQ(gSz, sizeof(gExpected));
  47883. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  47884. wolfSSL_DH_free(dh);
  47885. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47886. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47887. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47888. res = TEST_RES_CHECK(1);
  47889. #endif
  47890. return res;
  47891. }
  47892. static int test_wolfSSL_PEM_write_DHparams(void)
  47893. {
  47894. int res = TEST_SKIPPED;
  47895. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  47896. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  47897. DH* dh;
  47898. BIO* bio;
  47899. XFILE fp;
  47900. byte pem[2048];
  47901. int pemSz;
  47902. const char expected[] =
  47903. "-----BEGIN DH PARAMETERS-----\n"
  47904. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  47905. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  47906. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  47907. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  47908. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  47909. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  47910. "-----END DH PARAMETERS-----\n";
  47911. const char badPem[] =
  47912. "-----BEGIN DH PARAMETERS-----\n"
  47913. "-----END DH PARAMETERS-----\n";
  47914. const char emptySeqPem[] =
  47915. "-----BEGIN DH PARAMETERS-----\n"
  47916. "MAA=\n"
  47917. "-----END DH PARAMETERS-----\n";
  47918. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  47919. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47920. XFCLOSE(fp);
  47921. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  47922. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47923. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47924. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  47925. (int)sizeof(badPem));
  47926. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47927. BIO_free(bio);
  47928. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47929. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47930. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  47931. (int)sizeof(emptySeqPem));
  47932. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47933. BIO_free(bio);
  47934. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47935. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  47936. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47937. BIO_free(bio);
  47938. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  47939. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  47940. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  47941. DH_free(dh);
  47942. dh = wolfSSL_DH_new();
  47943. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  47944. XFCLOSE(fp);
  47945. wolfSSL_DH_free(dh);
  47946. /* check results */
  47947. XMEMSET(pem, 0, sizeof(pem));
  47948. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  47949. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47950. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  47951. XFCLOSE(fp);
  47952. res = TEST_RES_CHECK(1);
  47953. #endif
  47954. return res;
  47955. }
  47956. static int test_wolfSSL_d2i_DHparams(void)
  47957. {
  47958. int res = TEST_SKIPPED;
  47959. #ifdef OPENSSL_ALL
  47960. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  47961. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47962. FILE* f = NULL;
  47963. unsigned char buf[4096];
  47964. const unsigned char* pt = buf;
  47965. #ifdef HAVE_FFDHE_2048
  47966. const char* params1 = "./certs/dh2048.der";
  47967. #endif
  47968. #ifdef HAVE_FFDHE_3072
  47969. const char* params2 = "./certs/dh3072.der";
  47970. #endif
  47971. long len = 0;
  47972. WOLFSSL_DH* dh = NULL;
  47973. XMEMSET(buf, 0, sizeof(buf));
  47974. /* Test 2048 bit parameters */
  47975. #ifdef HAVE_FFDHE_2048
  47976. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  47977. f = XFOPEN(params1, "rb");
  47978. AssertTrue(f != XBADFILE);
  47979. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47980. XFCLOSE(f);
  47981. /* Valid case */
  47982. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  47983. AssertNotNull(dh->p);
  47984. AssertNotNull(dh->g);
  47985. AssertTrue(pt == buf);
  47986. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  47987. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  47988. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  47989. /* Invalid cases */
  47990. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  47991. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  47992. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  47993. DH_free(dh);
  47994. *buf = 0;
  47995. pt = buf;
  47996. res = TEST_RES_CHECK(1);
  47997. }
  47998. #endif /* HAVE_FFDHE_2048 */
  47999. /* Test 3072 bit parameters */
  48000. #ifdef HAVE_FFDHE_3072
  48001. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48002. f = XFOPEN(params2, "rb");
  48003. AssertTrue(f != XBADFILE);
  48004. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48005. XFCLOSE(f);
  48006. /* Valid case */
  48007. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  48008. AssertNotNull(dh->p);
  48009. AssertNotNull(dh->g);
  48010. AssertTrue(pt != buf);
  48011. AssertIntEQ(DH_generate_key(dh), 1);
  48012. /* Invalid cases */
  48013. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48014. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48015. DH_free(dh);
  48016. res = TEST_RES_CHECK(1);
  48017. }
  48018. #endif /* HAVE_FFDHE_3072 */
  48019. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48020. #endif /* !NO_DH */
  48021. #endif
  48022. return res;
  48023. }
  48024. static int test_wolfSSL_DH_LoadDer(void)
  48025. {
  48026. int res = TEST_SKIPPED;
  48027. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  48028. defined(OPENSSL_EXTRA)
  48029. static const byte dh2048[] = {
  48030. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48031. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48032. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48033. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48034. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48035. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48036. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48037. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48038. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48039. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48040. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48041. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48042. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48043. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48044. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48045. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48046. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48047. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48048. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48049. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48050. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48051. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48052. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48053. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48054. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48055. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48056. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48057. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48058. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48059. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48060. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48061. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48062. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48063. 0x93, 0x02, 0x01, 0x02
  48064. };
  48065. WOLFSSL_DH* dh;
  48066. dh = wolfSSL_DH_new();
  48067. AssertNotNull(dh);
  48068. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  48069. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  48070. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  48071. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  48072. wolfSSL_DH_free(dh);
  48073. res = TEST_RES_CHECK(1);
  48074. #endif
  48075. return res;
  48076. }
  48077. static int test_wolfSSL_i2d_DHparams(void)
  48078. {
  48079. int res = TEST_SKIPPED;
  48080. #ifdef OPENSSL_ALL
  48081. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48082. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48083. FILE* f;
  48084. unsigned char buf[4096];
  48085. const unsigned char* pt;
  48086. unsigned char* pt2;
  48087. #ifdef HAVE_FFDHE_2048
  48088. const char* params1 = "./certs/dh2048.der";
  48089. #endif
  48090. #ifdef HAVE_FFDHE_3072
  48091. const char* params2 = "./certs/dh3072.der";
  48092. #endif
  48093. long len;
  48094. WOLFSSL_DH* dh;
  48095. /* Test 2048 bit parameters */
  48096. #ifdef HAVE_FFDHE_2048
  48097. pt = buf;
  48098. pt2 = buf;
  48099. f = XFOPEN(params1, "rb");
  48100. AssertTrue(f != XBADFILE);
  48101. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48102. XFCLOSE(f);
  48103. /* Valid case */
  48104. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48105. AssertTrue(pt == buf);
  48106. AssertIntEQ(DH_generate_key(dh), 1);
  48107. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  48108. /* Invalid case */
  48109. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48110. /* Return length only */
  48111. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  48112. DH_free(dh);
  48113. *buf = 0;
  48114. #endif
  48115. /* Test 3072 bit parameters */
  48116. #ifdef HAVE_FFDHE_3072
  48117. pt = buf;
  48118. pt2 = buf;
  48119. f = XFOPEN(params2, "rb");
  48120. AssertTrue(f != XBADFILE);
  48121. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  48122. XFCLOSE(f);
  48123. /* Valid case */
  48124. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48125. AssertTrue(pt == buf);
  48126. AssertIntEQ(DH_generate_key(dh), 1);
  48127. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  48128. /* Invalid case */
  48129. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48130. /* Return length only */
  48131. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  48132. DH_free(dh);
  48133. #endif
  48134. dh = DH_new();
  48135. AssertNotNull(dh);
  48136. pt2 = buf;
  48137. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48138. DH_free(dh);
  48139. res = TEST_RES_CHECK(1);
  48140. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48141. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48142. #endif
  48143. return res;
  48144. }
  48145. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  48146. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48147. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  48148. {
  48149. X509* x509 = NULL;
  48150. BIGNUM* serial_number = NULL;
  48151. X509_NAME* name = NULL;
  48152. time_t epoch_off = 0;
  48153. ASN1_INTEGER* asn1_serial_number;
  48154. long not_before, not_after;
  48155. AssertNotNull(x509 = X509_new());
  48156. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  48157. AssertNotNull(serial_number = BN_new());
  48158. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  48159. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  48160. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  48161. /* version 3 */
  48162. AssertIntNE(X509_set_version(x509, 2L), 0);
  48163. AssertNotNull(name = X509_NAME_new());
  48164. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  48165. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  48166. AssertIntNE(X509_set_subject_name(x509, name), 0);
  48167. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  48168. not_before = (long)wc_Time(NULL);
  48169. not_after = not_before + (365 * 24 * 60 * 60);
  48170. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  48171. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  48172. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  48173. BN_free(serial_number);
  48174. X509_NAME_free(name);
  48175. X509_free(x509);
  48176. return 0;
  48177. }
  48178. #endif
  48179. static int test_openssl_generate_key_and_cert(void)
  48180. {
  48181. int res = TEST_SKIPPED;
  48182. #if defined(OPENSSL_EXTRA)
  48183. #if !defined(NO_RSA)
  48184. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48185. EVP_PKEY* pkey = EVP_PKEY_new();
  48186. int key_length = 2048;
  48187. BIGNUM* exponent = BN_new();
  48188. RSA* rsa = RSA_new();
  48189. AssertNotNull(pkey);
  48190. AssertNotNull(exponent);
  48191. AssertNotNull(rsa);
  48192. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  48193. #ifndef WOLFSSL_KEY_GEN
  48194. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48195. #if defined(USE_CERT_BUFFERS_1024)
  48196. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  48197. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48198. key_length = 1024;
  48199. #elif defined(USE_CERT_BUFFERS_2048)
  48200. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  48201. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48202. #else
  48203. RSA_free(rsa);
  48204. rsa = NULL;
  48205. #endif
  48206. #else
  48207. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  48208. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  48209. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  48210. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48211. #endif
  48212. if (rsa) {
  48213. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  48214. BN_free(exponent);
  48215. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48216. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48217. test_openssl_make_self_signed_certificate(pkey);
  48218. #endif
  48219. }
  48220. EVP_PKEY_free(pkey);
  48221. res = TEST_RES_CHECK(1);
  48222. }
  48223. #endif /* !NO_RSA */
  48224. #ifdef HAVE_ECC
  48225. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  48226. EVP_PKEY* pkey = EVP_PKEY_new();
  48227. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  48228. AssertNotNull(pkey);
  48229. AssertNotNull(ec_key);
  48230. #ifndef NO_WOLFSSL_STUB
  48231. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  48232. #endif
  48233. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  48234. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  48235. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48236. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48237. test_openssl_make_self_signed_certificate(pkey);
  48238. #endif
  48239. EVP_PKEY_free(pkey);
  48240. res = TEST_RES_CHECK(1);
  48241. }
  48242. #endif /* HAVE_ECC */
  48243. #endif /* OPENSSL_EXTRA */
  48244. return res;
  48245. }
  48246. static int test_stubs_are_stubs(void)
  48247. {
  48248. int res = TEST_SKIPPED;
  48249. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  48250. WOLFSSL_CTX* ctx = NULL;
  48251. WOLFSSL_CTX* ctxN = NULL;
  48252. #ifndef NO_WOLFSSL_CLIENT
  48253. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48254. AssertNotNull(ctx);
  48255. #elif !defined(NO_WOLFSSL_SERVER)
  48256. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  48257. AssertNotNull(ctx);
  48258. #else
  48259. return res;
  48260. #endif
  48261. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  48262. AssertIntEQ((int) x(z), 0)
  48263. /* test logic, all stubs return same result regardless of ctx being NULL
  48264. * as there are no sanity checks, it's just a stub! If at some
  48265. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  48266. * if invalid inputs are supplied. Test calling both
  48267. * with and without valid inputs, if a stub functionality remains unchanged.
  48268. */
  48269. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  48270. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  48271. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  48272. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  48273. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  48274. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  48275. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  48276. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  48277. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  48278. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  48279. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  48280. wolfSSL_CTX_free(ctx);
  48281. ctx = NULL;
  48282. res = TEST_RES_CHECK(1);
  48283. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  48284. return res;
  48285. }
  48286. static int test_CONF_modules_xxx(void)
  48287. {
  48288. int res = TEST_SKIPPED;
  48289. #if defined(OPENSSL_EXTRA)
  48290. CONF_modules_free();
  48291. AssertTrue(1); /* to confirm previous call gives no harm */
  48292. CONF_modules_unload(0);
  48293. AssertTrue(1);
  48294. CONF_modules_unload(1);
  48295. AssertTrue(1);
  48296. CONF_modules_unload(-1);
  48297. AssertTrue(1);
  48298. res = TEST_RES_CHECK(1);
  48299. #endif /* OPENSSL_EXTRA */
  48300. return res;
  48301. }
  48302. static int test_CRYPTO_set_dynlock_xxx(void)
  48303. {
  48304. int res = TEST_SKIPPED;
  48305. #if defined(OPENSSL_EXTRA)
  48306. CRYPTO_set_dynlock_create_callback(
  48307. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  48308. CRYPTO_set_dynlock_create_callback(
  48309. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  48310. CRYPTO_set_dynlock_destroy_callback(
  48311. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  48312. CRYPTO_set_dynlock_destroy_callback(
  48313. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  48314. CRYPTO_set_dynlock_lock_callback(
  48315. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  48316. CRYPTO_set_dynlock_lock_callback(
  48317. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  48318. AssertTrue(1); /* to confirm previous call gives no harm */
  48319. res = TEST_RES_CHECK(1);
  48320. #endif /* OPENSSL_EXTRA */
  48321. return res;
  48322. }
  48323. static int test_CRYPTO_THREADID_xxx(void)
  48324. {
  48325. int res = TEST_SKIPPED;
  48326. #if defined(OPENSSL_EXTRA)
  48327. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  48328. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  48329. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  48330. res = TEST_RES_CHECK(1);
  48331. #endif /* OPENSSL_EXTRA */
  48332. return res;
  48333. }
  48334. static int test_ENGINE_cleanup(void)
  48335. {
  48336. int res = TEST_SKIPPED;
  48337. #if defined(OPENSSL_EXTRA)
  48338. ENGINE_cleanup();
  48339. AssertTrue(1); /* to confirm previous call gives no harm */
  48340. res = TEST_RES_CHECK(1);
  48341. #endif /* OPENSSL_EXTRA */
  48342. return res;
  48343. }
  48344. static int test_wolfSSL_CTX_LoadCRL(void)
  48345. {
  48346. int res = TEST_SKIPPED;
  48347. #if defined(HAVE_CRL) && !defined(NO_RSA)
  48348. WOLFSSL_CTX* ctx = NULL;
  48349. WOLFSSL* ssl = NULL;
  48350. const char* badPath = "dummypath";
  48351. const char* validPath = "./certs/crl";
  48352. const char* validFilePath = "./certs/crl/cliCrl.pem";
  48353. const char* issuerCert = "./certs/client-cert.pem";
  48354. int derType = WOLFSSL_FILETYPE_ASN1;
  48355. int pemType = WOLFSSL_FILETYPE_PEM;
  48356. int monitor = WOLFSSL_CRL_MONITOR;
  48357. WOLFSSL_CERT_MANAGER* cm = NULL;
  48358. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48359. BAD_FUNC_ARG)
  48360. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48361. WOLFSSL_SUCCESS)
  48362. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48363. #ifndef NO_WOLFSSL_CLIENT
  48364. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48365. #elif !defined(NO_WOLFSSL_SERVER)
  48366. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48367. #else
  48368. return;
  48369. #endif
  48370. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48371. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  48372. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  48373. wolfSSL_CTX_free(ctx);
  48374. #ifndef NO_WOLFSSL_CLIENT
  48375. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48376. #elif !defined(NO_WOLFSSL_SERVER)
  48377. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48378. #else
  48379. return;
  48380. #endif
  48381. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48382. WOLFSSL_SUCCESS);
  48383. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  48384. wolfSSL_CTX_free(ctx);
  48385. #ifndef NO_WOLFSSL_CLIENT
  48386. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48387. #elif !defined(NO_WOLFSSL_SERVER)
  48388. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48389. #else
  48390. return;
  48391. #endif
  48392. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48393. WOLFSSL_SUCCESS);
  48394. AssertNotNull(ssl = wolfSSL_new(ctx));
  48395. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  48396. wolfSSL_free(ssl);
  48397. wolfSSL_CTX_free(ctx);
  48398. AssertNotNull(cm = wolfSSL_CertManagerNew());
  48399. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  48400. WOLFSSL_SUCCESS);
  48401. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  48402. wolfSSL_CertManagerFree(cm);
  48403. res = TEST_RES_CHECK(1);
  48404. #endif
  48405. return res;
  48406. }
  48407. static int test_SetTmpEC_DHE_Sz(void)
  48408. {
  48409. int res = TEST_SKIPPED;
  48410. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  48411. WOLFSSL_CTX *ctx;
  48412. WOLFSSL *ssl;
  48413. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48414. AssertNotNull(ctx);
  48415. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  48416. ssl = wolfSSL_new(ctx);
  48417. AssertNotNull(ssl);
  48418. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  48419. wolfSSL_free(ssl);
  48420. wolfSSL_CTX_free(ctx);
  48421. res = TEST_RES_CHECK(1);
  48422. #endif
  48423. return res;
  48424. }
  48425. static int test_wolfSSL_CTX_get0_privatekey(void)
  48426. {
  48427. int res = TEST_SKIPPED;
  48428. #ifdef OPENSSL_ALL
  48429. WOLFSSL_CTX* ctx = NULL;
  48430. #ifndef NO_RSA
  48431. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48432. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48433. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  48434. WOLFSSL_FILETYPE_PEM));
  48435. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48436. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  48437. WOLFSSL_FILETYPE_PEM));
  48438. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48439. wolfSSL_CTX_free(ctx);
  48440. #endif
  48441. #ifdef HAVE_ECC
  48442. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48443. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48444. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48445. WOLFSSL_FILETYPE_PEM));
  48446. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48447. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48448. WOLFSSL_FILETYPE_PEM));
  48449. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48450. wolfSSL_CTX_free(ctx);
  48451. #endif
  48452. res = TEST_RES_CHECK(1);
  48453. #endif
  48454. return res;
  48455. }
  48456. static int test_wolfSSL_dtls_set_mtu(void)
  48457. {
  48458. int res = TEST_SKIPPED;
  48459. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  48460. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  48461. WOLFSSL_CTX* ctx = NULL;
  48462. WOLFSSL* ssl = NULL;
  48463. const char* testCertFile;
  48464. const char* testKeyFile;
  48465. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  48466. #ifndef NO_RSA
  48467. testCertFile = svrCertFile;
  48468. testKeyFile = svrKeyFile;
  48469. #elif defined(HAVE_ECC)
  48470. testCertFile = eccCertFile;
  48471. testKeyFile = eccKeyFile;
  48472. #endif
  48473. if (testCertFile != NULL && testKeyFile != NULL) {
  48474. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  48475. WOLFSSL_FILETYPE_PEM));
  48476. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  48477. WOLFSSL_FILETYPE_PEM));
  48478. }
  48479. AssertNotNull(ssl = wolfSSL_new(ctx));
  48480. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48481. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48482. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  48483. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  48484. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  48485. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  48486. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  48487. wolfSSL_free(ssl);
  48488. wolfSSL_CTX_free(ctx);
  48489. res = TEST_RES_CHECK(1);
  48490. #endif
  48491. return res;
  48492. }
  48493. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48494. defined(WOLFSSL_DTLS)
  48495. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  48496. enum HandShakeType hsType, word16 length)
  48497. {
  48498. size_t idx = 0;
  48499. byte* l;
  48500. /* record layer */
  48501. /* handshake type */
  48502. out[idx++] = handshake;
  48503. /* protocol version */
  48504. out[idx++] = 0xfe;
  48505. out[idx++] = 0xfd; /* DTLS 1.2 */
  48506. /* epoch 0 */
  48507. XMEMSET(out + idx, 0, 2);
  48508. idx += 2;
  48509. /* sequence number */
  48510. XMEMSET(out + idx, 0, 6);
  48511. c32toa(seq, out + idx + 2);
  48512. idx += 6;
  48513. /* length in BE */
  48514. if (length)
  48515. c16toa(length, out + idx);
  48516. else
  48517. c16toa(outSz - idx - 2, out + idx);
  48518. idx += 2;
  48519. /* handshake layer */
  48520. /* handshake type */
  48521. out[idx++] = (byte)hsType;
  48522. /* length */
  48523. l = out + idx;
  48524. idx += 3;
  48525. /* message seq */
  48526. c16toa(0, out + idx);
  48527. idx += 2;
  48528. /* frag offset */
  48529. c32to24(0, out + idx);
  48530. idx += 3;
  48531. /* frag length */
  48532. c32to24((word32)outSz - (word32)idx - 3, l);
  48533. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  48534. idx += 3;
  48535. XMEMSET(out + idx, 0, outSz - idx);
  48536. }
  48537. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  48538. {
  48539. byte msg[] = "This is a msg for the client";
  48540. byte reply[40];
  48541. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48542. reply[sizeof(reply) - 1] = '\0';
  48543. fprintf(stderr, "Client message: %s\n", reply);
  48544. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48545. }
  48546. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  48547. {
  48548. byte ch[50];
  48549. int fd = wolfSSL_get_fd(ssl);
  48550. byte msg[] = "This is a msg for the server";
  48551. byte reply[40];
  48552. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  48553. /* Server should ignore this datagram */
  48554. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48555. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  48556. /* Server should ignore this datagram */
  48557. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48558. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48559. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48560. reply[sizeof(reply) - 1] = '\0';
  48561. fprintf(stderr, "Server response: %s\n", reply);
  48562. }
  48563. static int test_wolfSSL_dtls_plaintext(void)
  48564. {
  48565. callback_functions func_cb_client;
  48566. callback_functions func_cb_server;
  48567. size_t i;
  48568. struct test_params {
  48569. method_provider client_meth;
  48570. method_provider server_meth;
  48571. ssl_callback on_result_server;
  48572. ssl_callback on_result_client;
  48573. } params[] = {
  48574. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48575. test_wolfSSL_dtls_plaintext_server,
  48576. test_wolfSSL_dtls_plaintext_client},
  48577. };
  48578. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48579. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48580. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48581. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48582. func_cb_server.method = params[i].server_meth;
  48583. func_cb_client.method = params[i].client_meth;
  48584. func_cb_client.on_result = params[i].on_result_client;
  48585. func_cb_server.on_result = params[i].on_result_server;
  48586. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48587. if (!func_cb_client.return_code)
  48588. return TEST_FAIL;
  48589. if (!func_cb_server.return_code)
  48590. return TEST_FAIL;
  48591. }
  48592. return TEST_RES_CHECK(1);
  48593. }
  48594. #else
  48595. static int test_wolfSSL_dtls_plaintext(void) {
  48596. return TEST_SKIPPED;
  48597. }
  48598. #endif
  48599. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48600. defined(WOLFSSL_DTLS)
  48601. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  48602. {
  48603. byte b[1100]; /* buffer for the messages to send */
  48604. size_t idx = 0;
  48605. size_t seq_offset = 0;
  48606. size_t msg_offset = 0;
  48607. int i;
  48608. int fd = wolfSSL_get_fd(ssl);
  48609. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48610. word32 seq_number = 100; /* start high so server definitely reads this */
  48611. word16 msg_number = 50; /* start high so server has to buffer this */
  48612. AssertIntEQ(ret, 1);
  48613. /* Now let's start spamming the peer with fragments it needs to store */
  48614. XMEMSET(b, -1, sizeof(b));
  48615. /* record layer */
  48616. /* handshake type */
  48617. b[idx++] = 22;
  48618. /* protocol version */
  48619. b[idx++] = 0xfe;
  48620. b[idx++] = 0xfd; /* DTLS 1.2 */
  48621. /* epoch 0 */
  48622. XMEMSET(b + idx, 0, 2);
  48623. idx += 2;
  48624. /* sequence number */
  48625. XMEMSET(b + idx, 0, 6);
  48626. seq_offset = idx + 2; /* increment only the low 32 bits */
  48627. idx += 6;
  48628. /* static length in BE */
  48629. c16toa(42, b + idx);
  48630. idx += 2;
  48631. /* handshake layer */
  48632. /* cert type */
  48633. b[idx++] = 11;
  48634. /* length */
  48635. c32to24(1000, b + idx);
  48636. idx += 3;
  48637. /* message seq */
  48638. c16toa(0, b + idx);
  48639. msg_offset = idx;
  48640. idx += 2;
  48641. /* frag offset */
  48642. c32to24(500, b + idx);
  48643. idx += 3;
  48644. /* frag length */
  48645. c32to24(30, b + idx);
  48646. idx += 3;
  48647. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  48648. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  48649. seq_number++, msg_number++, i++) {
  48650. struct timespec delay;
  48651. XMEMSET(&delay, 0, sizeof(delay));
  48652. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48653. c32toa(seq_number, b + seq_offset);
  48654. c16toa(msg_number, b + msg_offset);
  48655. ret = (int)send(fd, b, 55, 0);
  48656. nanosleep(&delay, NULL);
  48657. }
  48658. }
  48659. #ifdef WOLFSSL_DTLS13
  48660. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  48661. {
  48662. byte b[150]; /* buffer for the messages to send */
  48663. size_t idx = 0;
  48664. size_t msg_offset = 0;
  48665. int fd = wolfSSL_get_fd(ssl);
  48666. word16 msg_number = 10; /* start high so server has to buffer this */
  48667. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48668. AssertIntEQ(ret, 1);
  48669. /* Now let's start spamming the peer with fragments it needs to store */
  48670. XMEMSET(b, -1, sizeof(b));
  48671. /* handshake type */
  48672. b[idx++] = 11;
  48673. /* length */
  48674. c32to24(10000, b + idx);
  48675. idx += 3;
  48676. /* message_seq */
  48677. msg_offset = idx;
  48678. idx += 2;
  48679. /* fragment_offset */
  48680. c32to24(5000, b + idx);
  48681. idx += 3;
  48682. /* fragment_length */
  48683. c32to24(100, b + idx);
  48684. idx += 3;
  48685. /* fragment contents */
  48686. idx += 100;
  48687. for (; ret > 0; msg_number++) {
  48688. byte sendBuf[150];
  48689. int sendSz = sizeof(sendBuf);
  48690. struct timespec delay;
  48691. XMEMSET(&delay, 0, sizeof(delay));
  48692. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48693. c16toa(msg_number, b + msg_offset);
  48694. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  48695. (int)idx, handshake, 0, 0, 0);
  48696. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  48697. nanosleep(&delay, NULL);
  48698. }
  48699. }
  48700. #endif
  48701. static int test_wolfSSL_dtls_fragments(void)
  48702. {
  48703. callback_functions func_cb_client;
  48704. callback_functions func_cb_server;
  48705. size_t i;
  48706. struct test_params {
  48707. method_provider client_meth;
  48708. method_provider server_meth;
  48709. ssl_callback spammer;
  48710. } params[] = {
  48711. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48712. test_wolfSSL_dtls12_fragments_spammer},
  48713. #ifdef WOLFSSL_DTLS13
  48714. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  48715. test_wolfSSL_dtls13_fragments_spammer},
  48716. #endif
  48717. };
  48718. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48719. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48720. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48721. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48722. func_cb_server.method = params[i].server_meth;
  48723. func_cb_client.method = params[i].client_meth;
  48724. func_cb_client.ssl_ready = params[i].spammer;
  48725. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48726. AssertFalse(func_cb_client.return_code);
  48727. AssertFalse(func_cb_server.return_code);
  48728. /* The socket should be closed by the server resulting in a
  48729. * socket error */
  48730. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  48731. /* Check the server returned an error indicating the msg buffer
  48732. * was full */
  48733. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  48734. }
  48735. return TEST_RES_CHECK(1);
  48736. }
  48737. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  48738. {
  48739. int fd, ret;
  48740. byte alert_msg[] = {
  48741. 0x15, /* alert type */
  48742. 0xfe, 0xfd, /* version */
  48743. 0x00, 0x00, /* epoch */
  48744. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  48745. 0x00, 0x02, /* length */
  48746. 0x02, /* level: fatal */
  48747. 0x46 /* protocol version */
  48748. };
  48749. fd = wolfSSL_get_fd(ssl);
  48750. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  48751. AssertIntGT(ret, 0);
  48752. }
  48753. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  48754. {
  48755. callback_functions client_cbs, server_cbs;
  48756. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48757. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48758. client_cbs.doUdp = server_cbs.doUdp = 1;
  48759. if (version12) {
  48760. client_cbs.method = wolfDTLSv1_2_client_method;
  48761. server_cbs.method = wolfDTLSv1_2_server_method;
  48762. }
  48763. else {
  48764. #ifdef WOLFSSL_DTLS13
  48765. client_cbs.method = wolfDTLSv1_3_client_method;
  48766. server_cbs.method = wolfDTLSv1_3_server_method;
  48767. #else
  48768. return TEST_SKIPPED;
  48769. #endif /* WOLFSSL_DTLS13 */
  48770. }
  48771. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  48772. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48773. if (!client_cbs.return_code)
  48774. return TEST_FAIL;
  48775. if (!server_cbs.return_code)
  48776. return TEST_FAIL;
  48777. return TEST_SUCCESS;
  48778. }
  48779. static int test_wolfSSL_ignore_alert_before_cookie(void)
  48780. {
  48781. int ret;
  48782. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  48783. if (ret != 0)
  48784. return ret;
  48785. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  48786. if (ret != 0)
  48787. return ret;
  48788. return 0;
  48789. }
  48790. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  48791. {
  48792. int ret;
  48793. int fd;
  48794. byte bad_msg[] = {
  48795. 0x17, /* app data */
  48796. 0xaa, 0xfd, /* bad version */
  48797. 0x00, 0x01, /* epoch 1 */
  48798. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  48799. 0x00, 0x26, /* length: 38 bytes */
  48800. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  48801. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  48802. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  48803. 0x03, 0x18, 0x72
  48804. };
  48805. fd = wolfSSL_get_fd(ssl);
  48806. AssertIntGE(fd, 0);
  48807. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  48808. AssertIntEQ(ret, sizeof(bad_msg));
  48809. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  48810. AssertIntEQ(ret, sizeof("badrecordtest"));
  48811. }
  48812. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  48813. {
  48814. byte buf[100];
  48815. int ret;
  48816. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  48817. AssertIntGT(ret, 0);
  48818. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  48819. }
  48820. static int _test_wolfSSL_dtls_bad_record(
  48821. method_provider client_method, method_provider server_method)
  48822. {
  48823. callback_functions client_cbs, server_cbs;
  48824. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48825. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48826. client_cbs.doUdp = server_cbs.doUdp = 1;
  48827. client_cbs.method = client_method;
  48828. server_cbs.method = server_method;
  48829. client_cbs.on_result = test_wolfSSL_send_bad_record;
  48830. server_cbs.on_result = test_wolfSSL_read_string;
  48831. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48832. if (!client_cbs.return_code)
  48833. return TEST_FAIL;
  48834. if (!server_cbs.return_code)
  48835. return TEST_FAIL;
  48836. return TEST_SUCCESS;
  48837. }
  48838. static int test_wolfSSL_dtls_bad_record(void)
  48839. {
  48840. int ret;
  48841. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  48842. wolfDTLSv1_2_server_method);
  48843. #ifdef WOLFSSL_DTLS13
  48844. if (ret != TEST_SUCCESS)
  48845. return ret;
  48846. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  48847. wolfDTLSv1_3_server_method);
  48848. #else
  48849. return ret;
  48850. #endif /* WOLFSSL_DTLS13 */
  48851. }
  48852. #else
  48853. static int test_wolfSSL_dtls_fragments(void) {
  48854. return TEST_SKIPPED;
  48855. }
  48856. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  48857. return TEST_SKIPPED;
  48858. }
  48859. static int test_wolfSSL_dtls_bad_record(void) {
  48860. return TEST_SKIPPED;
  48861. }
  48862. #endif
  48863. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  48864. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  48865. defined(HAVE_IO_TESTS_DEPENDENCIES)
  48866. static byte test_AEAD_fail_decryption = 0;
  48867. static byte test_AEAD_seq_num = 0;
  48868. static byte test_AEAD_done = 0;
  48869. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  48870. {
  48871. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  48872. if (ret > 0) {
  48873. if (test_AEAD_fail_decryption) {
  48874. /* Modify the packet to trigger a decryption failure */
  48875. buf[ret/2] ^= 0xFF;
  48876. if (test_AEAD_fail_decryption == 1)
  48877. test_AEAD_fail_decryption = 0;
  48878. }
  48879. }
  48880. (void)ctx;
  48881. return ret;
  48882. }
  48883. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  48884. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  48885. {
  48886. if (sendLimit)
  48887. w64Zero(sendLimit);
  48888. switch (ssl->specs.bulk_cipher_algorithm) {
  48889. case wolfssl_aes_gcm:
  48890. if (sendLimit)
  48891. *sendLimit = AEAD_AES_LIMIT;
  48892. FALL_THROUGH;
  48893. case wolfssl_chacha:
  48894. if (hardLimit)
  48895. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  48896. if (keyUpdateLimit)
  48897. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  48898. break;
  48899. case wolfssl_aes_ccm:
  48900. if (sendLimit)
  48901. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  48902. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  48903. if (hardLimit)
  48904. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  48905. if (keyUpdateLimit)
  48906. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  48907. }
  48908. else {
  48909. if (hardLimit)
  48910. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  48911. if (keyUpdateLimit)
  48912. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  48913. }
  48914. break;
  48915. default:
  48916. fprintf(stderr, "Unrecognized bulk cipher");
  48917. AssertFalse(1);
  48918. break;
  48919. }
  48920. }
  48921. static void test_AEAD_limit_client(WOLFSSL* ssl)
  48922. {
  48923. int ret;
  48924. int i;
  48925. int didReKey = 0;
  48926. char msgBuf[20];
  48927. w64wrapper hardLimit;
  48928. w64wrapper keyUpdateLimit;
  48929. w64wrapper counter;
  48930. w64wrapper sendLimit;
  48931. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  48932. w64Zero(&counter);
  48933. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  48934. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  48935. for (i = 0; i < 10; i++) {
  48936. /* Test some failed decryptions */
  48937. test_AEAD_fail_decryption = 1;
  48938. w64Increment(&counter);
  48939. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48940. /* Should succeed since decryption failures are dropped */
  48941. AssertIntGT(ret, 0);
  48942. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  48943. }
  48944. test_AEAD_fail_decryption = 1;
  48945. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  48946. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48947. /* 100 read calls should be enough to complete the key update */
  48948. w64Zero(&counter);
  48949. for (i = 0; i < 100; i++) {
  48950. /* Key update should be sent and negotiated */
  48951. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48952. AssertIntGT(ret, 0);
  48953. /* Epoch after one key update is 4 */
  48954. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  48955. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  48956. didReKey = 1;
  48957. break;
  48958. }
  48959. }
  48960. AssertTrue(didReKey);
  48961. if (!w64IsZero(sendLimit)) {
  48962. /* Test the sending limit for AEAD ciphers */
  48963. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  48964. test_AEAD_seq_num = 1;
  48965. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  48966. AssertIntGT(ret, 0);
  48967. didReKey = 0;
  48968. w64Zero(&counter);
  48969. /* 100 read calls should be enough to complete the key update */
  48970. for (i = 0; i < 100; i++) {
  48971. /* Key update should be sent and negotiated */
  48972. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48973. AssertIntGT(ret, 0);
  48974. /* Epoch after another key update is 5 */
  48975. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  48976. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  48977. didReKey = 1;
  48978. break;
  48979. }
  48980. }
  48981. AssertTrue(didReKey);
  48982. }
  48983. test_AEAD_fail_decryption = 2;
  48984. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  48985. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48986. /* Connection should fail with a DECRYPT_ERROR */
  48987. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48988. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  48989. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  48990. test_AEAD_done = 1;
  48991. }
  48992. int counter = 0;
  48993. static void test_AEAD_limit_server(WOLFSSL* ssl)
  48994. {
  48995. char msgBuf[] = "Sending data";
  48996. int ret = WOLFSSL_SUCCESS;
  48997. w64wrapper sendLimit;
  48998. SOCKET_T fd = wolfSSL_get_fd(ssl);
  48999. struct timespec delay;
  49000. XMEMSET(&delay, 0, sizeof(delay));
  49001. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  49002. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  49003. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  49004. while (!test_AEAD_done && ret > 0) {
  49005. counter++;
  49006. if (test_AEAD_seq_num) {
  49007. /* We need to update the seq number so that we can understand the
  49008. * peer. Otherwise we will incorrectly interpret the seq number. */
  49009. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  49010. AssertNotNull(e);
  49011. e->nextPeerSeqNumber = sendLimit;
  49012. test_AEAD_seq_num = 0;
  49013. }
  49014. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  49015. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  49016. nanosleep(&delay, NULL);
  49017. }
  49018. }
  49019. static int test_wolfSSL_dtls_AEAD_limit(void)
  49020. {
  49021. callback_functions func_cb_client;
  49022. callback_functions func_cb_server;
  49023. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  49024. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  49025. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  49026. func_cb_server.method = wolfDTLSv1_3_server_method;
  49027. func_cb_client.method = wolfDTLSv1_3_client_method;
  49028. func_cb_server.on_result = test_AEAD_limit_server;
  49029. func_cb_client.on_result = test_AEAD_limit_client;
  49030. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  49031. if (!func_cb_client.return_code)
  49032. return TEST_FAIL;
  49033. if (!func_cb_server.return_code)
  49034. return TEST_FAIL;
  49035. return TEST_SUCCESS;
  49036. }
  49037. #else
  49038. static int test_wolfSSL_dtls_AEAD_limit(void)
  49039. {
  49040. return TEST_SKIPPED;
  49041. }
  49042. #endif
  49043. #if defined(WOLFSSL_DTLS) && \
  49044. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  49045. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  49046. {
  49047. int fd, ret;
  49048. byte ch_msg[] = {
  49049. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49050. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49051. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  49052. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  49053. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  49054. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  49055. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  49056. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  49057. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  49058. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  49059. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  49060. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  49061. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  49062. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  49063. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  49064. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  49065. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  49066. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  49067. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  49068. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  49069. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  49070. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  49071. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  49072. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  49073. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  49074. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  49075. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  49076. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  49077. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  49078. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  49079. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  49080. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  49081. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  49082. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  49083. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  49084. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  49085. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  49086. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  49087. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  49088. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  49089. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  49090. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  49091. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  49092. 0x31, 0x68, 0x4c
  49093. };
  49094. fd = wolfSSL_get_fd(ssl);
  49095. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  49096. AssertIntGT(ret, 0);
  49097. /* consume the HRR otherwise handshake will fail */
  49098. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  49099. AssertIntGT(ret, 0);
  49100. }
  49101. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  49102. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  49103. {
  49104. int ret;
  49105. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  49106. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  49107. }
  49108. #endif
  49109. static int test_wolfSSL_dtls_stateless(void)
  49110. {
  49111. callback_functions client_cbs, server_cbs;
  49112. size_t i;
  49113. struct {
  49114. method_provider client_meth;
  49115. method_provider server_meth;
  49116. ssl_callback client_ssl_ready;
  49117. ssl_callback server_ssl_ready;
  49118. } test_params[] = {
  49119. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  49120. test_wolfSSL_dtls_send_ch, NULL},
  49121. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  49122. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  49123. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  49124. #endif
  49125. };
  49126. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  49127. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  49128. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  49129. client_cbs.doUdp = server_cbs.doUdp = 1;
  49130. client_cbs.method = test_params[i].client_meth;
  49131. server_cbs.method = test_params[i].server_meth;
  49132. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  49133. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  49134. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  49135. if (!client_cbs.return_code)
  49136. return TEST_FAIL;
  49137. if (!server_cbs.return_code)
  49138. return TEST_FAIL;
  49139. }
  49140. return TEST_SUCCESS;
  49141. }
  49142. #else
  49143. static int test_wolfSSL_dtls_stateless(void)
  49144. {
  49145. return TEST_SKIPPED;
  49146. }
  49147. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  49148. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  49149. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  49150. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49151. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49152. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49153. {
  49154. int ret;
  49155. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  49156. fprintf(stderr, "loading cert %s failed\n", certA);
  49157. fprintf(stderr, "Error: (%d): %s\n", ret,
  49158. wolfSSL_ERR_reason_error_string(ret));
  49159. return -1;
  49160. }
  49161. return 0;
  49162. }
  49163. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49164. {
  49165. int ret;
  49166. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  49167. != WOLFSSL_SUCCESS) {
  49168. fprintf(stderr, "could not verify the cert: %s\n", certA);
  49169. fprintf(stderr, "Error: (%d): %s\n", ret,
  49170. wolfSSL_ERR_reason_error_string(ret));
  49171. return -1;
  49172. }
  49173. else {
  49174. fprintf(stderr, "successfully verified: %s\n", certA);
  49175. }
  49176. return 0;
  49177. }
  49178. #define LOAD_ONE_CA(a, b, c, d) \
  49179. do { \
  49180. (a) = load_ca_into_cm(c, d); \
  49181. if ((a) != 0) \
  49182. return (b); \
  49183. else \
  49184. (b)--; \
  49185. } while(0)
  49186. #define VERIFY_ONE_CERT(a, b, c, d) \
  49187. do { \
  49188. (a) = verify_cert_with_cm(c, d); \
  49189. if ((a) != 0) \
  49190. return (b); \
  49191. else \
  49192. (b)--; \
  49193. } while(0)
  49194. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  49195. {
  49196. int ret;
  49197. int i = -1;
  49198. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  49199. char chainGArr[9][50] = {"certs/ca-cert.pem",
  49200. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  49201. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  49202. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  49203. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  49204. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  49205. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  49206. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  49207. "certs/test-pathlen/chainG-entity.pem"};
  49208. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  49209. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  49210. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  49211. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  49212. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  49213. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  49214. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  49215. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  49216. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  49217. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  49218. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  49219. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  49220. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  49221. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  49222. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  49223. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  49224. /* test validating the entity twice, should have no effect on pathLen since
  49225. * entity/leaf cert */
  49226. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  49227. return ret;
  49228. }
  49229. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  49230. {
  49231. int ret;
  49232. int i = -1;
  49233. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  49234. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  49235. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  49236. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  49237. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  49238. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49239. */
  49240. char chainHArr[6][50] = {"certs/ca-cert.pem",
  49241. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  49242. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  49243. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  49244. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  49245. "certs/test-pathlen/chainH-entity.pem"};
  49246. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  49247. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  49248. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  49249. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  49250. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  49251. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  49252. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  49253. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  49254. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  49255. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  49256. return ret;
  49257. }
  49258. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  49259. {
  49260. int ret;
  49261. int i = -1;
  49262. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  49263. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  49264. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  49265. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  49266. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49267. */
  49268. char chainIArr[5][50] = {"certs/ca-cert.pem",
  49269. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  49270. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  49271. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  49272. "certs/test-pathlen/chainI-entity.pem"};
  49273. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  49274. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  49275. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  49276. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  49277. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  49278. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  49279. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  49280. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  49281. return ret;
  49282. }
  49283. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  49284. {
  49285. int ret;
  49286. int i = -1;
  49287. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  49288. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  49289. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  49290. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  49291. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  49292. */
  49293. char chainJArr[6][50] = {"certs/ca-cert.pem",
  49294. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  49295. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  49296. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  49297. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  49298. "certs/test-pathlen/chainJ-entity.pem"};
  49299. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  49300. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  49301. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  49302. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  49303. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  49304. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  49305. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  49306. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  49307. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  49308. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  49309. return ret;
  49310. }
  49311. static int test_various_pathlen_chains(void)
  49312. {
  49313. int ret;
  49314. WOLFSSL_CERT_MANAGER* cm;
  49315. /* Test chain G (large chain with varying pathLens) */
  49316. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49317. fprintf(stderr, "cert manager new failed\n");
  49318. return -1;
  49319. }
  49320. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49321. AssertIntEQ(test_chainG(cm), -1);
  49322. #else
  49323. AssertIntEQ(test_chainG(cm), 0);
  49324. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49325. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49326. if (ret != WOLFSSL_SUCCESS)
  49327. return -1;
  49328. wolfSSL_CertManagerFree(cm);
  49329. /* end test chain G */
  49330. /* Test chain H (5 chain with same pathLens) */
  49331. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49332. fprintf(stderr, "cert manager new failed\n");
  49333. return -1;
  49334. }
  49335. AssertIntLT(test_chainH(cm), 0);
  49336. wolfSSL_CertManagerUnloadCAs(cm);
  49337. wolfSSL_CertManagerFree(cm);
  49338. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49339. fprintf(stderr, "cert manager new failed\n");
  49340. return -1;
  49341. }
  49342. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49343. if (ret != WOLFSSL_SUCCESS)
  49344. return -1;
  49345. wolfSSL_CertManagerFree(cm);
  49346. /* end test chain H */
  49347. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  49348. * followed by 2 ICA's, should pass) */
  49349. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49350. fprintf(stderr, "cert manager new failed\n");
  49351. return -1;
  49352. }
  49353. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49354. AssertIntEQ(test_chainI(cm), -1);
  49355. #else
  49356. AssertIntEQ(test_chainI(cm), 0);
  49357. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49358. wolfSSL_CertManagerUnloadCAs(cm);
  49359. wolfSSL_CertManagerFree(cm);
  49360. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49361. fprintf(stderr, "cert manager new failed\n");
  49362. return -1;
  49363. }
  49364. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49365. if (ret != WOLFSSL_SUCCESS)
  49366. return -1;
  49367. wolfSSL_CertManagerFree(cm);
  49368. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  49369. * this time followed by 3 ICA's, should fail */
  49370. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49371. fprintf(stderr, "cert manager new failed\n");
  49372. return -1;
  49373. }
  49374. AssertIntLT(test_chainJ(cm), 0);
  49375. wolfSSL_CertManagerUnloadCAs(cm);
  49376. wolfSSL_CertManagerFree(cm);
  49377. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49378. fprintf(stderr, "cert manager new failed\n");
  49379. return -1;
  49380. }
  49381. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49382. wolfSSL_CertManagerFree(cm);
  49383. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  49384. }
  49385. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  49386. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  49387. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  49388. {
  49389. byte ekm[100] = {0};
  49390. (void)ctx;
  49391. /* Succes Cases */
  49392. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49393. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  49394. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49395. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  49396. /* Use some random context */
  49397. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49398. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  49399. /* Failure cases */
  49400. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49401. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  49402. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49403. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  49404. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49405. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  49406. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49407. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  49408. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49409. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  49410. return TEST_RES_CHECK(1);
  49411. }
  49412. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  49413. {
  49414. wolfSSL_KeepArrays(ssl);
  49415. }
  49416. static int test_export_keying_material(void)
  49417. {
  49418. int res = TEST_SKIPPED;
  49419. #ifndef SINGLE_THREADED
  49420. tcp_ready ready;
  49421. callback_functions clientCb;
  49422. func_args client_args;
  49423. func_args server_args;
  49424. THREAD_TYPE serverThread;
  49425. XMEMSET(&client_args, 0, sizeof(func_args));
  49426. XMEMSET(&server_args, 0, sizeof(func_args));
  49427. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  49428. #ifdef WOLFSSL_TIRTOS
  49429. fdOpenSession(Task_self());
  49430. #endif
  49431. StartTCP();
  49432. InitTcpReady(&ready);
  49433. #if defined(USE_WINDOWS_API)
  49434. /* use RNG to get random port if using windows */
  49435. ready.port = GetRandomPort();
  49436. #endif
  49437. server_args.signal = &ready;
  49438. client_args.signal = &ready;
  49439. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  49440. client_args.callbacks = &clientCb;
  49441. start_thread(test_server_nofail, &server_args, &serverThread);
  49442. wait_tcp_ready(&server_args);
  49443. test_client_nofail(&client_args, test_export_keying_material_cb);
  49444. join_thread(serverThread);
  49445. AssertTrue(client_args.return_code);
  49446. AssertTrue(server_args.return_code);
  49447. FreeTcpReady(&ready);
  49448. #ifdef WOLFSSL_TIRTOS
  49449. fdOpenSession(Task_self());
  49450. #endif
  49451. res = TEST_RES_CHECK(1);
  49452. #endif /* !SINGLE_THREADED */
  49453. return res;
  49454. }
  49455. #endif /* HAVE_KEYING_MATERIAL */
  49456. static int test_wolfSSL_THREADID_hash(void)
  49457. {
  49458. int result = TEST_SKIPPED;
  49459. #if defined(OPENSSL_EXTRA)
  49460. unsigned long res;
  49461. CRYPTO_THREADID id;
  49462. CRYPTO_THREADID_current(NULL);
  49463. AssertTrue(1);
  49464. res = CRYPTO_THREADID_hash(NULL);
  49465. AssertTrue( res == 0UL);
  49466. XMEMSET(&id, 0, sizeof(id));
  49467. res = CRYPTO_THREADID_hash(&id);
  49468. AssertTrue( res == 0UL);
  49469. result = TEST_RES_CHECK(1);
  49470. #endif /* OPENSSL_EXTRA */
  49471. return result;
  49472. }
  49473. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  49474. {
  49475. int res = TEST_SKIPPED;
  49476. #if defined(OPENSSL_EXTRA)
  49477. WOLFSSL_CTX* ctx = NULL;
  49478. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  49479. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  49480. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  49481. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  49482. res = TEST_RES_CHECK(1);
  49483. #endif /* OPENSSL_EXTRA */
  49484. return res;
  49485. }
  49486. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49487. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49488. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  49489. {
  49490. callback_functions* callbacks = NULL;
  49491. WOLFSSL_CTX* ctx = NULL;
  49492. WOLFSSL* ssl = NULL;
  49493. SOCKET_T sfd = 0;
  49494. SOCKET_T cfd = 0;
  49495. word16 port;
  49496. char msg[] = "I hear you fa shizzle!";
  49497. int len = (int) XSTRLEN(msg);
  49498. char input[1024];
  49499. int ret, err;
  49500. if (!args)
  49501. return 0;
  49502. ((func_args*)args)->return_code = TEST_FAIL;
  49503. callbacks = ((func_args*)args)->callbacks;
  49504. ctx = wolfSSL_CTX_new(callbacks->method());
  49505. #if defined(USE_WINDOWS_API)
  49506. port = ((func_args*)args)->signal->port;
  49507. #else
  49508. /* Let tcp_listen assign port */
  49509. port = 0;
  49510. #endif
  49511. #ifdef WOLFSSL_TIRTOS
  49512. fdOpenSession(Task_self());
  49513. #endif
  49514. AssertIntEQ(WOLFSSL_SUCCESS,
  49515. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49516. AssertIntEQ(WOLFSSL_SUCCESS,
  49517. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  49518. WOLFSSL_FILETYPE_PEM));
  49519. AssertIntEQ(WOLFSSL_SUCCESS,
  49520. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  49521. WOLFSSL_FILETYPE_PEM));
  49522. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  49523. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  49524. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  49525. #elif !defined(NO_DH)
  49526. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  49527. #endif
  49528. if (callbacks->ctx_ready)
  49529. callbacks->ctx_ready(ctx);
  49530. ssl = wolfSSL_new(ctx);
  49531. AssertNotNull(ssl);
  49532. /* listen and accept */
  49533. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  49534. CloseSocket(sfd);
  49535. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  49536. if (callbacks->ssl_ready)
  49537. callbacks->ssl_ready(ssl);
  49538. do {
  49539. err = 0; /* Reset error */
  49540. ret = wolfSSL_accept(ssl);
  49541. if (ret != WOLFSSL_SUCCESS) {
  49542. err = wolfSSL_get_error(ssl, 0);
  49543. }
  49544. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49545. if (ret != WOLFSSL_SUCCESS) {
  49546. wolfSSL_free(ssl);
  49547. wolfSSL_CTX_free(ctx);
  49548. CloseSocket(cfd);
  49549. ((func_args*)args)->return_code = TEST_FAIL;
  49550. return 0;
  49551. }
  49552. /* read and write data */
  49553. XMEMSET( input, 0, sizeof(input));
  49554. while (1) {
  49555. ret = wolfSSL_read(ssl, input, sizeof(input));
  49556. if (ret > 0) {
  49557. break;
  49558. }
  49559. else {
  49560. err = wolfSSL_get_error(ssl,ret);
  49561. if (err == WOLFSSL_ERROR_WANT_READ) {
  49562. continue;
  49563. }
  49564. break;
  49565. }
  49566. }
  49567. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  49568. do {
  49569. ret = wolfSSL_write(ssl, msg, len);
  49570. if (ret > 0) {
  49571. break;
  49572. }
  49573. } while (ret < 0);
  49574. }
  49575. /* bidirectional shutdown */
  49576. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  49577. continue;
  49578. }
  49579. /* wait for the peer to disconnect the tcp connection */
  49580. do {
  49581. ret = wolfSSL_read(ssl, input, sizeof(input));
  49582. err = wolfSSL_get_error(ssl, ret);
  49583. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  49584. /* detect TCP disconnect */
  49585. AssertIntLE(ret,WOLFSSL_FAILURE);
  49586. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  49587. ((func_args*)args)->return_code = TEST_SUCCESS;
  49588. wolfSSL_free(ssl);
  49589. wolfSSL_CTX_free(ctx);
  49590. CloseSocket(cfd);
  49591. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49592. wc_ecc_fp_free(); /* free per thread cache */
  49593. #endif
  49594. return 0;
  49595. }
  49596. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  49597. {
  49598. callback_functions* callbacks = NULL;
  49599. WOLFSSL_CTX* ctx = NULL;
  49600. WOLFSSL* ssl = NULL;
  49601. SOCKET_T sfd = 0;
  49602. char msg[] = "hello wolfssl server!";
  49603. int len = (int) XSTRLEN(msg);
  49604. char input[1024];
  49605. int idx;
  49606. int ret, err;
  49607. if (!args)
  49608. return 0;
  49609. ((func_args*)args)->return_code = TEST_FAIL;
  49610. callbacks = ((func_args*)args)->callbacks;
  49611. ctx = wolfSSL_CTX_new(callbacks->method());
  49612. #ifdef WOLFSSL_TIRTOS
  49613. fdOpenSession(Task_self());
  49614. #endif
  49615. AssertIntEQ(WOLFSSL_SUCCESS,
  49616. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49617. AssertIntEQ(WOLFSSL_SUCCESS,
  49618. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  49619. WOLFSSL_FILETYPE_PEM));
  49620. AssertIntEQ(WOLFSSL_SUCCESS,
  49621. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  49622. WOLFSSL_FILETYPE_PEM));
  49623. AssertNotNull((ssl = wolfSSL_new(ctx)));
  49624. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  49625. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  49626. do {
  49627. err = 0; /* Reset error */
  49628. ret = wolfSSL_connect(ssl);
  49629. if (ret != WOLFSSL_SUCCESS) {
  49630. err = wolfSSL_get_error(ssl, 0);
  49631. }
  49632. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49633. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  49634. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  49635. input[idx] = 0;
  49636. }
  49637. ret = wolfSSL_shutdown(ssl);
  49638. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  49639. ret = wolfSSL_shutdown(ssl);
  49640. }
  49641. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  49642. ((func_args*)args)->return_code = TEST_SUCCESS;
  49643. wolfSSL_free(ssl);
  49644. wolfSSL_CTX_free(ctx);
  49645. CloseSocket(sfd);
  49646. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49647. wc_ecc_fp_free(); /* free per thread cache */
  49648. #endif
  49649. return 0;
  49650. }
  49651. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  49652. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  49653. /* This test is to check wolfSSL_read behaves as same as
  49654. * openSSL when it is called after SSL_shutdown completes.
  49655. */
  49656. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  49657. {
  49658. int res = TEST_SKIPPED;
  49659. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49660. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49661. tcp_ready ready;
  49662. func_args client_args;
  49663. func_args server_args;
  49664. THREAD_TYPE serverThread;
  49665. THREAD_TYPE clientThread;
  49666. callback_functions server_cbf;
  49667. callback_functions client_cbf;
  49668. #ifdef WOLFSSL_TIRTOS
  49669. fdOpenSession(Task_self());
  49670. #endif
  49671. StartTCP();
  49672. InitTcpReady(&ready);
  49673. #if defined(USE_WINDOWS_API)
  49674. /* use RNG to get random port if using windows */
  49675. ready.port = GetRandomPort();
  49676. #endif
  49677. XMEMSET(&client_args, 0, sizeof(func_args));
  49678. XMEMSET(&server_args, 0, sizeof(func_args));
  49679. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  49680. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  49681. server_cbf.method = wolfTLSv1_2_server_method;
  49682. client_cbf.method = wolfTLSv1_2_client_method;
  49683. server_args.callbacks = &server_cbf;
  49684. client_args.callbacks = &client_cbf;
  49685. server_args.signal = &ready;
  49686. client_args.signal = &ready;
  49687. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  49688. wait_tcp_ready(&server_args);
  49689. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  49690. join_thread(clientThread);
  49691. join_thread(serverThread);
  49692. AssertTrue(client_args.return_code);
  49693. AssertTrue(server_args.return_code);
  49694. FreeTcpReady(&ready);
  49695. res = TEST_RES_CHECK(1);
  49696. #endif
  49697. return res;
  49698. }
  49699. static int test_wolfSSL_CTX_get_min_proto_version(void)
  49700. {
  49701. int res = TEST_SKIPPED;
  49702. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  49703. WOLFSSL_CTX *ctx;
  49704. (void)ctx;
  49705. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49706. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  49707. #ifdef WOLFSSL_ALLOW_SSLV3
  49708. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49709. #else
  49710. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49711. #endif
  49712. wolfSSL_CTX_free(ctx);
  49713. #ifdef WOLFSSL_ALLOW_TLSV10
  49714. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  49715. #else
  49716. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49717. #endif
  49718. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  49719. #ifdef WOLFSSL_ALLOW_TLSV10
  49720. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49721. #else
  49722. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49723. #endif
  49724. wolfSSL_CTX_free(ctx);
  49725. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49726. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  49727. #ifndef NO_OLD_TLS
  49728. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49729. #else
  49730. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49731. #endif
  49732. wolfSSL_CTX_free(ctx);
  49733. #ifndef WOLFSSL_NO_TLS12
  49734. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  49735. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  49736. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  49737. wolfSSL_CTX_free(ctx);
  49738. #endif
  49739. #ifdef WOLFSSL_TLS13
  49740. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  49741. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  49742. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  49743. wolfSSL_CTX_free(ctx);
  49744. #endif
  49745. res = TEST_RES_CHECK(1);
  49746. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  49747. return res;
  49748. }
  49749. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  49750. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  49751. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  49752. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  49753. static int test_wolfSSL_set_SSL_CTX(void)
  49754. {
  49755. int res = TEST_SKIPPED;
  49756. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  49757. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13)
  49758. WOLFSSL_CTX *ctx1, *ctx2;
  49759. WOLFSSL *ssl;
  49760. const byte *session_id1 = (const byte *)"CTX1";
  49761. const byte *session_id2 = (const byte *)"CTX2";
  49762. AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  49763. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  49764. WOLFSSL_FILETYPE_PEM));
  49765. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  49766. WOLFSSL_FILETYPE_PEM));
  49767. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  49768. WOLFSSL_SUCCESS);
  49769. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  49770. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  49771. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  49772. WOLFSSL_SUCCESS);
  49773. AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  49774. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  49775. WOLFSSL_FILETYPE_PEM));
  49776. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  49777. WOLFSSL_FILETYPE_PEM));
  49778. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  49779. WOLFSSL_SUCCESS);
  49780. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  49781. WOLFSSL_SUCCESS);
  49782. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  49783. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  49784. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  49785. WOLFSSL_SUCCESS);
  49786. #ifdef HAVE_SESSION_TICKET
  49787. AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  49788. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  49789. AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  49790. #endif
  49791. AssertNotNull(ssl = wolfSSL_new(ctx2));
  49792. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  49793. #ifdef WOLFSSL_INT_H
  49794. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  49795. AssertTrue(ssl->buffers.certificate == ctx2->certificate);
  49796. AssertTrue(ssl->buffers.certChain == ctx2->certChain);
  49797. #endif
  49798. #ifdef HAVE_SESSION_TICKET
  49799. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  49800. #endif
  49801. /* Set the ctx1 that has TLSv1.3 as max proto version */
  49802. AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  49803. /* MUST not change proto versions of ssl */
  49804. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  49805. #ifdef HAVE_SESSION_TICKET
  49806. /* MUST not change */
  49807. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  49808. #endif
  49809. /* MUST change */
  49810. #ifdef WOLFSSL_INT_H
  49811. AssertTrue(ssl->buffers.certificate == ctx1->certificate);
  49812. AssertTrue(ssl->buffers.certChain == ctx1->certChain);
  49813. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  49814. #endif
  49815. wolfSSL_free(ssl);
  49816. wolfSSL_CTX_free(ctx1);
  49817. wolfSSL_CTX_free(ctx2);
  49818. res = TEST_RES_CHECK(1);
  49819. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  49820. return res;
  49821. }
  49822. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  49823. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  49824. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  49825. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  49826. static int test_wolfSSL_security_level(void)
  49827. {
  49828. int res = TEST_SKIPPED;
  49829. #if defined(OPENSSL_EXTRA)
  49830. SSL_CTX *ctx;
  49831. #ifdef WOLFSSL_TLS13
  49832. #ifdef NO_WOLFSSL_SERVER
  49833. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49834. #else
  49835. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49836. #endif
  49837. SSL_CTX_set_security_level(ctx, 1);
  49838. AssertTrue(1);
  49839. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  49840. SSL_CTX_free(ctx);
  49841. #else
  49842. (void)ctx;
  49843. #endif
  49844. res = TEST_RES_CHECK(1);
  49845. #endif
  49846. return res;
  49847. }
  49848. static int test_wolfSSL_SSL_in_init(void)
  49849. {
  49850. int res = TEST_SKIPPED;
  49851. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  49852. SSL_CTX* ctx;
  49853. SSL* ssl;
  49854. const char* testCertFile;
  49855. const char* testKeyFile;
  49856. #ifdef WOLFSSL_TLS13
  49857. #ifdef NO_WOLFSSL_SERVER
  49858. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49859. #else
  49860. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49861. #endif
  49862. #ifndef NO_RSA
  49863. testCertFile = svrCertFile;
  49864. testKeyFile = svrKeyFile;
  49865. #elif defined(HAVE_ECC)
  49866. testCertFile = eccCertFile;
  49867. testKeyFile = eccKeyFile;
  49868. #else
  49869. testCertFile = NULL;
  49870. testKeyFile = NULL;
  49871. #endif
  49872. if (testCertFile != NULL && testKeyFile != NULL) {
  49873. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  49874. SSL_FILETYPE_PEM));
  49875. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  49876. SSL_FILETYPE_PEM));
  49877. }
  49878. ssl = SSL_new(ctx);
  49879. AssertNotNull(ssl);
  49880. AssertIntEQ(SSL_in_init(ssl), 1);
  49881. SSL_CTX_free(ctx);
  49882. SSL_free(ssl);
  49883. #else
  49884. (void)ctx;
  49885. (void)ssl;
  49886. (void)testCertFile;
  49887. (void)testKeyFile;
  49888. #endif
  49889. res = TEST_RES_CHECK(1);
  49890. #endif
  49891. return res;
  49892. }
  49893. static int test_wolfSSL_EC_curve(void)
  49894. {
  49895. int res = TEST_SKIPPED;
  49896. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49897. int nid = NID_secp160k1;
  49898. const char* nid_name;
  49899. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  49900. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49901. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  49902. res = TEST_RES_CHECK(1);
  49903. #endif
  49904. return res;
  49905. }
  49906. static int test_wolfSSL_CTX_set_timeout(void)
  49907. {
  49908. int res = TEST_SKIPPED;
  49909. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  49910. int timeout;
  49911. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  49912. (void)timeout;
  49913. AssertNotNull(ctx);
  49914. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49915. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  49916. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  49917. */
  49918. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49919. /* giving 0 as timeout value sets default timeout */
  49920. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  49921. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  49922. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  49923. #else
  49924. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49925. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  49926. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  49927. #endif
  49928. wolfSSL_CTX_free(ctx);
  49929. res = TEST_RES_CHECK(1);
  49930. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  49931. return res;
  49932. }
  49933. static int test_wolfSSL_OpenSSL_version(void)
  49934. {
  49935. int res = TEST_SKIPPED;
  49936. #if defined(OPENSSL_EXTRA)
  49937. const char* ver;
  49938. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  49939. AssertNotNull(ver = OpenSSL_version(0));
  49940. #else
  49941. AssertNotNull(ver = OpenSSL_version());
  49942. #endif
  49943. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  49944. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  49945. res = TEST_RES_CHECK(1);
  49946. #endif
  49947. return res;
  49948. }
  49949. static int test_CONF_CTX_CMDLINE(void)
  49950. {
  49951. int res = TEST_SKIPPED;
  49952. #if defined(OPENSSL_ALL)
  49953. SSL_CTX* ctx = NULL;
  49954. SSL_CONF_CTX* cctx = NULL;
  49955. AssertNotNull(cctx = SSL_CONF_CTX_new());
  49956. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  49957. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  49958. AssertTrue(1);
  49959. /* set flags */
  49960. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  49961. WOLFSSL_CONF_FLAG_CMDLINE);
  49962. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  49963. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  49964. /* cmd invalid command */
  49965. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  49966. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  49967. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  49968. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  49969. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  49970. /* cmd Certificate and Private Key*/
  49971. {
  49972. #if !defined(NO_CERTS) && !defined(NO_RSA)
  49973. const char* ourCert = svrCertFile;
  49974. const char* ourKey = svrKeyFile;
  49975. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  49976. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  49977. WOLFSSL_SUCCESS);
  49978. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  49979. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  49980. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49981. #endif
  49982. }
  49983. /* cmd curves */
  49984. {
  49985. #if defined(HAVE_ECC)
  49986. const char* curve = "secp256r1";
  49987. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  49988. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  49989. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49990. #endif
  49991. }
  49992. /* cmd CipherString */
  49993. {
  49994. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  49995. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  49996. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  49997. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49998. }
  49999. /* cmd DH parameter */
  50000. {
  50001. #if !defined(NO_DH) && !defined(NO_BIO)
  50002. const char* ourdhcert = "./certs/dh2048.pem";
  50003. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  50004. -3);
  50005. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  50006. WOLFSSL_SUCCESS);
  50007. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  50008. #endif
  50009. }
  50010. SSL_CTX_free(ctx);
  50011. SSL_CONF_CTX_free(cctx);
  50012. res = TEST_RES_CHECK(1);
  50013. #endif /* OPENSSL_EXTRA */
  50014. return res;
  50015. }
  50016. static int test_CONF_CTX_FILE(void)
  50017. {
  50018. int res = TEST_SKIPPED;
  50019. #if defined(OPENSSL_ALL)
  50020. SSL_CTX* ctx = NULL;
  50021. SSL_CONF_CTX* cctx = NULL;
  50022. AssertNotNull(cctx = SSL_CONF_CTX_new());
  50023. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50024. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  50025. AssertTrue(1);
  50026. /* set flags */
  50027. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  50028. WOLFSSL_CONF_FLAG_FILE);
  50029. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  50030. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  50031. /* sanity check */
  50032. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  50033. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  50034. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  50035. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  50036. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  50037. /* cmd Certificate and Private Key*/
  50038. {
  50039. #if !defined(NO_CERTS) && !defined(NO_RSA)
  50040. const char* ourCert = svrCertFile;
  50041. const char* ourKey = svrKeyFile;
  50042. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  50043. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  50044. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  50045. WOLFSSL_SUCCESS);
  50046. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  50047. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  50048. #endif
  50049. }
  50050. /* cmd curves */
  50051. {
  50052. #if defined(HAVE_ECC)
  50053. const char* curve = "secp256r1";
  50054. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  50055. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  50056. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  50057. #endif
  50058. }
  50059. /* cmd CipherString */
  50060. {
  50061. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  50062. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  50063. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  50064. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  50065. }
  50066. /* cmd DH parameter */
  50067. {
  50068. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  50069. const char* ourdhcert = "./certs/dh3072.pem";
  50070. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  50071. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  50072. WOLFSSL_SUCCESS);
  50073. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  50074. #endif
  50075. }
  50076. SSL_CTX_free(ctx);
  50077. SSL_CONF_CTX_free(cctx);
  50078. res = TEST_RES_CHECK(1);
  50079. #endif /* OPENSSL_EXTRA */
  50080. return res;
  50081. }
  50082. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  50083. {
  50084. int res = TEST_SKIPPED;
  50085. #ifdef HAVE_EX_DATA
  50086. int idx1, idx2;
  50087. /* test for unsupported class index */
  50088. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  50089. 0,NULL, NULL, NULL, NULL ), -1);
  50090. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  50091. 0,NULL, NULL, NULL, NULL ), -1);
  50092. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  50093. 0,NULL, NULL, NULL, NULL ), -1);
  50094. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  50095. 0,NULL, NULL, NULL, NULL ), -1);
  50096. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  50097. 0,NULL, NULL, NULL, NULL ), -1);
  50098. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  50099. 0,NULL, NULL, NULL, NULL ), -1);
  50100. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  50101. 0,NULL, NULL, NULL, NULL ), -1);
  50102. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  50103. 0,NULL, NULL, NULL, NULL ), -1);
  50104. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  50105. 0,NULL, NULL, NULL, NULL ), -1);
  50106. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  50107. 0,NULL, NULL, NULL, NULL ), -1);
  50108. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  50109. 0,NULL, NULL, NULL, NULL ), -1);
  50110. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  50111. 0,NULL, NULL, NULL, NULL ), -1);
  50112. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  50113. /* test for supported class index */
  50114. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  50115. 0,NULL, NULL, NULL, NULL );
  50116. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  50117. 0,NULL, NULL, NULL, NULL );
  50118. AssertIntNE(idx1, -1);
  50119. AssertIntNE(idx2, -1);
  50120. AssertIntNE(idx1, idx2);
  50121. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  50122. 0,NULL, NULL, NULL, NULL );
  50123. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  50124. 0,NULL, NULL, NULL, NULL );
  50125. AssertIntNE(idx1, -1);
  50126. AssertIntNE(idx2, -1);
  50127. AssertIntNE(idx1, idx2);
  50128. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  50129. 0,NULL, NULL, NULL, NULL );
  50130. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  50131. 0,NULL, NULL, NULL, NULL );
  50132. AssertIntNE(idx1, -1);
  50133. AssertIntNE(idx2, -1);
  50134. AssertIntNE(idx1, idx2);
  50135. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  50136. 0,NULL, NULL, NULL, NULL );
  50137. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  50138. 0,NULL, NULL, NULL, NULL );
  50139. AssertIntNE(idx1, -1);
  50140. AssertIntNE(idx2, -1);
  50141. AssertIntNE(idx1, idx2);
  50142. res = TEST_RES_CHECK(1);
  50143. #endif /* HAVE_EX_DATA */
  50144. return res;
  50145. }
  50146. static int test_wolfSSL_set_psk_use_session_callback(void)
  50147. {
  50148. int res = TEST_SKIPPED;
  50149. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  50150. SSL_CTX* ctx;
  50151. SSL* ssl;
  50152. const char* testCertFile;
  50153. const char* testKeyFile;
  50154. #ifdef WOLFSSL_TLS13
  50155. #ifdef NO_WOLFSSL_SERVER
  50156. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  50157. #else
  50158. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  50159. #endif
  50160. #ifndef NO_RSA
  50161. testCertFile = svrCertFile;
  50162. testKeyFile = svrKeyFile;
  50163. #elif defined(HAVE_ECC)
  50164. testCertFile = eccCertFile;
  50165. testKeyFile = eccKeyFile;
  50166. #else
  50167. testCertFile = NULL;
  50168. testKeyFile = NULL;
  50169. #endif
  50170. if (testCertFile != NULL && testKeyFile != NULL) {
  50171. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  50172. SSL_FILETYPE_PEM));
  50173. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50174. SSL_FILETYPE_PEM));
  50175. }
  50176. ssl = SSL_new(ctx);
  50177. AssertNotNull(ssl);
  50178. SSL_set_psk_use_session_callback(ssl,
  50179. my_psk_use_session_cb);
  50180. AssertTrue(1);
  50181. SSL_CTX_free(ctx);
  50182. SSL_free(ssl);
  50183. #else
  50184. (void)ctx;
  50185. (void)ssl;
  50186. (void)testCertFile;
  50187. (void)testKeyFile;
  50188. #endif
  50189. res = TEST_RES_CHECK(1);
  50190. #endif
  50191. return res;
  50192. }
  50193. static int test_wolfSSL_ERR_strings(void)
  50194. {
  50195. int res = TEST_SKIPPED;
  50196. #if !defined(NO_ERROR_STRINGS)
  50197. const char* err1 = "unsupported cipher suite";
  50198. const char* err2 = "wolfSSL PEM routines";
  50199. const char* err = NULL;
  50200. (void)err;
  50201. (void)err1;
  50202. (void)err2;
  50203. #if defined(OPENSSL_EXTRA)
  50204. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  50205. AssertTrue(err != NULL);
  50206. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  50207. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  50208. AssertTrue(err != NULL);
  50209. AssertIntEQ((*err == '\0'), 1);
  50210. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  50211. AssertTrue(err != NULL);
  50212. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  50213. #else
  50214. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  50215. AssertTrue(err != NULL);
  50216. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  50217. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  50218. AssertTrue(err != NULL);
  50219. AssertIntEQ((*err == '\0'), 1);
  50220. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  50221. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  50222. AssertTrue(err != NULL);
  50223. AssertIntEQ((*err == '\0'), 1);
  50224. #endif
  50225. res = TEST_RES_CHECK(1);
  50226. #endif
  50227. return res;
  50228. }
  50229. static int test_wolfSSL_EVP_shake128(void)
  50230. {
  50231. int res = TEST_SKIPPED;
  50232. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50233. defined(WOLFSSL_SHAKE128)
  50234. const EVP_MD* md = NULL;
  50235. md = EVP_shake128();
  50236. AssertTrue(md != NULL);
  50237. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  50238. res = TEST_RES_CHECK(1);
  50239. #endif
  50240. return res;
  50241. }
  50242. static int test_wolfSSL_EVP_shake256(void)
  50243. {
  50244. int res = TEST_SKIPPED;
  50245. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50246. defined(WOLFSSL_SHAKE256)
  50247. const EVP_MD* md = NULL;
  50248. md = EVP_shake256();
  50249. AssertTrue(md != NULL);
  50250. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  50251. res = TEST_RES_CHECK(1);
  50252. #endif
  50253. return res;
  50254. }
  50255. static int test_EVP_blake2(void)
  50256. {
  50257. int res = TEST_SKIPPED;
  50258. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  50259. const EVP_MD* md = NULL;
  50260. (void)md;
  50261. #if defined(HAVE_BLAKE2)
  50262. md = EVP_blake2b512();
  50263. AssertTrue(md != NULL);
  50264. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  50265. #endif
  50266. #if defined(HAVE_BLAKE2S)
  50267. md = EVP_blake2s256();
  50268. AssertTrue(md != NULL);
  50269. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  50270. #endif
  50271. res = TEST_RES_CHECK(1);
  50272. #endif
  50273. return res;
  50274. }
  50275. #if defined(OPENSSL_EXTRA)
  50276. static void list_md_fn(const EVP_MD* m, const char* from,
  50277. const char* to, void* arg)
  50278. {
  50279. const char* mn;
  50280. BIO *bio;
  50281. (void) from;
  50282. (void) to;
  50283. (void) arg;
  50284. (void) mn;
  50285. (void) bio;
  50286. if (!m) {
  50287. /* alias */
  50288. AssertNull(m);
  50289. AssertNotNull(to);
  50290. }
  50291. else {
  50292. AssertNotNull(m);
  50293. AssertNull(to);
  50294. }
  50295. AssertNotNull(from);
  50296. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50297. mn = EVP_get_digestbyname(from);
  50298. /* print to stderr */
  50299. AssertNotNull(arg);
  50300. bio = BIO_new(BIO_s_file());
  50301. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50302. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  50303. BIO_free(bio);
  50304. #endif
  50305. }
  50306. #endif
  50307. static int test_EVP_MD_do_all(void)
  50308. {
  50309. int res = TEST_SKIPPED;
  50310. #if defined(OPENSSL_EXTRA)
  50311. EVP_MD_do_all(NULL, stderr);
  50312. /* to confirm previous call gives no harm */
  50313. AssertTrue(1);
  50314. EVP_MD_do_all(list_md_fn, stderr);
  50315. /* to confirm previous call gives no harm */
  50316. AssertTrue(1);
  50317. res = TEST_RES_CHECK(1);
  50318. #endif
  50319. return res;
  50320. }
  50321. #if defined(OPENSSL_EXTRA)
  50322. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  50323. {
  50324. (void)arg;
  50325. (void)nm;
  50326. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  50327. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50328. /* print to stderr */
  50329. AssertNotNull(arg);
  50330. bio = BIO_new(BIO_s_file());
  50331. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50332. BIO_printf(bio, "%s\n", mn);
  50333. BIO_free(bio);
  50334. #endif
  50335. }
  50336. #endif
  50337. static int test_OBJ_NAME_do_all(void)
  50338. {
  50339. int res = TEST_SKIPPED;
  50340. #if defined(OPENSSL_EXTRA)
  50341. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  50342. /* to confirm previous call gives no harm */
  50343. AssertTrue(1);
  50344. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  50345. /* to confirm previous call gives no harm */
  50346. AssertTrue(1);
  50347. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  50348. AssertTrue(1);
  50349. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  50350. AssertTrue(1);
  50351. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  50352. AssertTrue(1);
  50353. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  50354. AssertTrue(1);
  50355. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  50356. AssertTrue(1);
  50357. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  50358. AssertTrue(1);
  50359. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  50360. AssertTrue(1);
  50361. res = TEST_RES_CHECK(1);
  50362. #endif
  50363. return res;
  50364. }
  50365. static int test_SSL_CIPHER_get_xxx(void)
  50366. {
  50367. int res = TEST_SKIPPED;
  50368. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  50369. !defined(NO_FILESYSTEM)
  50370. const SSL_CIPHER* cipher = NULL;
  50371. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  50372. int i, numCiphers = 0;
  50373. SSL_CTX* ctx = NULL;
  50374. SSL* ssl = NULL;
  50375. const char* testCertFile;
  50376. const char* testKeyFile;
  50377. char buf[256] = {0};
  50378. const char* cipher_id = NULL;
  50379. int expect_nid1 = NID_undef;
  50380. int expect_nid2 = NID_undef;
  50381. int expect_nid3 = NID_undef;
  50382. int expect_nid4 = NID_undef;
  50383. int expect_nid5 = 0;
  50384. const char* cipher_id2 = NULL;
  50385. int expect_nid21 = NID_undef;
  50386. int expect_nid22 = NID_undef;
  50387. int expect_nid23 = NID_undef;
  50388. int expect_nid24 = NID_undef;
  50389. int expect_nid25 = 0;
  50390. (void)cipher;
  50391. (void)supportedCiphers;
  50392. (void)i;
  50393. (void)numCiphers;
  50394. (void)ctx;
  50395. (void)ssl;
  50396. (void)testCertFile;
  50397. (void)testKeyFile;
  50398. #if defined(WOLFSSL_TLS13)
  50399. cipher_id = "TLS13-AES128-GCM-SHA256";
  50400. expect_nid1 = NID_auth_rsa;
  50401. expect_nid2 = NID_aes_128_gcm;
  50402. expect_nid3 = NID_sha256;
  50403. expect_nid4 = NID_kx_any;
  50404. expect_nid5 = 1;
  50405. #if !defined(WOLFSSL_NO_TLS12)
  50406. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  50407. expect_nid21 = NID_auth_rsa;
  50408. expect_nid22 = NID_aes_256_gcm;
  50409. expect_nid23 = NID_sha384;
  50410. expect_nid24 = NID_kx_ecdhe;
  50411. expect_nid25 = 1;
  50412. #endif
  50413. #endif
  50414. #ifdef NO_WOLFSSL_SERVER
  50415. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50416. #else
  50417. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50418. #endif
  50419. if (cipher_id) {
  50420. #ifndef NO_RSA
  50421. testCertFile = svrCertFile;
  50422. testKeyFile = svrKeyFile;
  50423. #elif defined(HAVE_ECC)
  50424. testCertFile = eccCertFile;
  50425. testKeyFile = eccKeyFile;
  50426. #else
  50427. testCertFile = NULL;
  50428. testKeyFile = NULL;
  50429. #endif
  50430. if (testCertFile != NULL && testKeyFile != NULL) {
  50431. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  50432. SSL_FILETYPE_PEM));
  50433. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50434. SSL_FILETYPE_PEM));
  50435. }
  50436. ssl = SSL_new(ctx);
  50437. AssertNotNull(ssl);
  50438. AssertIntEQ(SSL_in_init(ssl), 1);
  50439. supportedCiphers = SSL_get_ciphers(ssl);
  50440. numCiphers = sk_num(supportedCiphers);
  50441. for (i = 0; i < numCiphers; ++i) {
  50442. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50443. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50444. }
  50445. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  50446. break;
  50447. }
  50448. }
  50449. /* test case for */
  50450. if (i != numCiphers) {
  50451. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  50452. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  50453. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  50454. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  50455. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  50456. }
  50457. if (cipher_id2) {
  50458. for (i = 0; i < numCiphers; ++i) {
  50459. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50460. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50461. }
  50462. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  50463. break;
  50464. }
  50465. }
  50466. /* test case for */
  50467. if (i != numCiphers) {
  50468. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  50469. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  50470. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  50471. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  50472. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  50473. }
  50474. }
  50475. }
  50476. if (ctx)
  50477. SSL_CTX_free(ctx);
  50478. if (ssl)
  50479. SSL_free(ssl);
  50480. res = TEST_RES_CHECK(1);
  50481. #endif
  50482. return res;
  50483. }
  50484. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50485. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  50486. int* keyFormat)
  50487. {
  50488. int ret;
  50489. byte* key_buf = NULL;
  50490. size_t key_sz = 0;
  50491. EncryptedInfo encInfo;
  50492. XMEMSET(&encInfo, 0, sizeof(encInfo));
  50493. ret = load_file(privKeyFile, &key_buf, &key_sz);
  50494. if (ret == 0) {
  50495. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  50496. NULL, &encInfo, keyFormat);
  50497. }
  50498. if (key_buf != NULL) {
  50499. free(key_buf); key_buf = NULL;
  50500. }
  50501. (void)encInfo; /* not used in this test */
  50502. #ifdef DEBUG_WOLFSSL
  50503. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  50504. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  50505. (*pDer)->length);
  50506. #endif
  50507. return ret;
  50508. }
  50509. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  50510. {
  50511. int ret = CRYPTOCB_UNAVAILABLE;
  50512. const char* privKeyFile = (const char*)ctx;
  50513. DerBuffer* pDer = NULL;
  50514. int keyFormat = 0;
  50515. if (info->algo_type == WC_ALGO_TYPE_PK) {
  50516. #ifdef DEBUG_WOLFSSL
  50517. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  50518. #endif
  50519. #ifndef NO_RSA
  50520. if (info->pk.type == WC_PK_TYPE_RSA) {
  50521. switch (info->pk.rsa.type) {
  50522. case RSA_PUBLIC_ENCRYPT:
  50523. case RSA_PUBLIC_DECRYPT:
  50524. /* perform software based RSA public op */
  50525. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  50526. break;
  50527. case RSA_PRIVATE_ENCRYPT:
  50528. case RSA_PRIVATE_DECRYPT:
  50529. {
  50530. RsaKey key;
  50531. /* perform software based RSA private op */
  50532. #ifdef DEBUG_WOLFSSL
  50533. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  50534. #endif
  50535. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  50536. &keyFormat);
  50537. if (ret != 0) {
  50538. return ret;
  50539. }
  50540. ret = wc_InitRsaKey(&key, HEAP_HINT);
  50541. if (ret == 0) {
  50542. word32 keyIdx = 0;
  50543. /* load RSA private key and perform private transform */
  50544. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  50545. &key, pDer->length);
  50546. if (ret == 0) {
  50547. ret = wc_RsaFunction(
  50548. info->pk.rsa.in, info->pk.rsa.inLen,
  50549. info->pk.rsa.out, info->pk.rsa.outLen,
  50550. info->pk.rsa.type, &key, info->pk.rsa.rng);
  50551. }
  50552. else {
  50553. /* if decode fails, then fall-back to software based crypto */
  50554. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  50555. "key decode failed %d, falling back to "
  50556. "software\n", ret);
  50557. ret = CRYPTOCB_UNAVAILABLE;
  50558. }
  50559. wc_FreeRsaKey(&key);
  50560. }
  50561. wc_FreeDer(&pDer); pDer = NULL;
  50562. break;
  50563. }
  50564. }
  50565. #ifdef DEBUG_WOLFSSL
  50566. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  50567. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  50568. #endif
  50569. }
  50570. #endif /* !NO_RSA */
  50571. #ifdef HAVE_ECC
  50572. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  50573. /* mark this key as ephemeral */
  50574. if (info->pk.eckg.key != NULL) {
  50575. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  50576. sizeof(info->pk.eckg.key->label));
  50577. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  50578. }
  50579. }
  50580. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  50581. ecc_key key;
  50582. /* perform software based ECC sign */
  50583. #ifdef DEBUG_WOLFSSL
  50584. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  50585. #endif
  50586. if (info->pk.eccsign.key != NULL &&
  50587. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  50588. /* this is an empheral key */
  50589. #ifdef DEBUG_WOLFSSL
  50590. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  50591. "ephemeral key\n");
  50592. #endif
  50593. return CRYPTOCB_UNAVAILABLE;
  50594. }
  50595. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50596. if (ret != 0) {
  50597. return ret;
  50598. }
  50599. ret = wc_ecc_init(&key);
  50600. if (ret == 0) {
  50601. word32 keyIdx = 0;
  50602. /* load ECC private key and perform private transform */
  50603. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  50604. &key, pDer->length);
  50605. if (ret == 0) {
  50606. ret = wc_ecc_sign_hash(
  50607. info->pk.eccsign.in, info->pk.eccsign.inlen,
  50608. info->pk.eccsign.out, info->pk.eccsign.outlen,
  50609. info->pk.eccsign.rng, &key);
  50610. }
  50611. else {
  50612. /* if decode fails, then fall-back to software based crypto */
  50613. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  50614. "decode failed %d, falling back to software\n", ret);
  50615. ret = CRYPTOCB_UNAVAILABLE;
  50616. }
  50617. wc_ecc_free(&key);
  50618. }
  50619. wc_FreeDer(&pDer); pDer = NULL;
  50620. #ifdef DEBUG_WOLFSSL
  50621. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  50622. ret, *info->pk.eccsign.outlen);
  50623. #endif
  50624. }
  50625. #endif /* HAVE_ECC */
  50626. #ifdef HAVE_ED25519
  50627. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  50628. ed25519_key key;
  50629. /* perform software based ED25519 sign */
  50630. #ifdef DEBUG_WOLFSSL
  50631. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  50632. #endif
  50633. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50634. if (ret != 0) {
  50635. return ret;
  50636. }
  50637. ret = wc_ed25519_init(&key);
  50638. if (ret == 0) {
  50639. word32 keyIdx = 0;
  50640. /* load ED25519 private key and perform private transform */
  50641. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  50642. &key, pDer->length);
  50643. if (ret == 0) {
  50644. /* calculate public key */
  50645. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  50646. if (ret == 0) {
  50647. key.pubKeySet = 1;
  50648. ret = wc_ed25519_sign_msg_ex(
  50649. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  50650. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  50651. &key, info->pk.ed25519sign.type,
  50652. info->pk.ed25519sign.context,
  50653. info->pk.ed25519sign.contextLen);
  50654. }
  50655. }
  50656. else {
  50657. /* if decode fails, then fall-back to software based crypto */
  50658. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  50659. "decode failed %d, falling back to software\n", ret);
  50660. ret = CRYPTOCB_UNAVAILABLE;
  50661. }
  50662. wc_ed25519_free(&key);
  50663. }
  50664. wc_FreeDer(&pDer); pDer = NULL;
  50665. #ifdef DEBUG_WOLFSSL
  50666. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  50667. ret, *info->pk.ed25519sign.outLen);
  50668. #endif
  50669. }
  50670. #endif /* HAVE_ED25519 */
  50671. }
  50672. (void)thisDevId;
  50673. (void)keyFormat;
  50674. return ret;
  50675. }
  50676. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50677. static void test_wc_CryptoCb_TLS(int tlsVer,
  50678. const char* cliCaPemFile, const char* cliCertPemFile,
  50679. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  50680. const char* svrCaPemFile, const char* svrCertPemFile,
  50681. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  50682. {
  50683. callback_functions client_cbf;
  50684. callback_functions server_cbf;
  50685. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50686. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50687. if (tlsVer == WOLFSSL_TLSV1_3) {
  50688. #ifdef WOLFSSL_TLS13
  50689. server_cbf.method = wolfTLSv1_3_server_method;
  50690. client_cbf.method = wolfTLSv1_3_client_method;
  50691. #endif
  50692. }
  50693. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50694. #ifndef WOLFSSL_NO_TLS12
  50695. server_cbf.method = wolfTLSv1_2_server_method;
  50696. client_cbf.method = wolfTLSv1_2_client_method;
  50697. #endif
  50698. }
  50699. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50700. #ifndef NO_OLD_TLS
  50701. server_cbf.method = wolfTLSv1_1_server_method;
  50702. client_cbf.method = wolfTLSv1_1_client_method;
  50703. #endif
  50704. }
  50705. else if (tlsVer == WOLFSSL_TLSV1) {
  50706. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50707. server_cbf.method = wolfTLSv1_server_method;
  50708. client_cbf.method = wolfTLSv1_client_method;
  50709. #endif
  50710. }
  50711. else if (tlsVer == WOLFSSL_SSLV3) {
  50712. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50713. defined(WOLFSSL_STATIC_RSA)
  50714. server_cbf.method = wolfSSLv3_server_method;
  50715. client_cbf.method = wolfSSLv3_client_method;
  50716. #endif
  50717. }
  50718. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50719. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50720. server_cbf.method = wolfDTLSv1_2_server_method;
  50721. client_cbf.method = wolfDTLSv1_2_client_method;
  50722. #endif
  50723. }
  50724. else if (tlsVer == WOLFSSL_DTLSV1) {
  50725. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50726. server_cbf.method = wolfDTLSv1_server_method;
  50727. client_cbf.method = wolfDTLSv1_client_method;
  50728. #endif
  50729. }
  50730. if (server_cbf.method == NULL) {
  50731. /* not enabled */
  50732. return;
  50733. }
  50734. /* Setup the keys for the TLS test */
  50735. client_cbf.certPemFile = cliCertPemFile;
  50736. client_cbf.keyPemFile = cliPubKeyPemFile;
  50737. client_cbf.caPemFile = cliCaPemFile;
  50738. server_cbf.certPemFile = svrCertPemFile;
  50739. server_cbf.keyPemFile = svrPubKeyPemFile;
  50740. server_cbf.caPemFile = svrCaPemFile;
  50741. /* Setup a crypto callback with pointer to private key file for testing */
  50742. client_cbf.devId = 1;
  50743. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  50744. (void*)cliPrivKeyPemFile);
  50745. server_cbf.devId = 2;
  50746. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  50747. (void*)svrPrivKeyPemFile);
  50748. /* Perform TLS server and client test */
  50749. /* First test is at WOLFSSL_CTX level */
  50750. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50751. /* Check for success */
  50752. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50753. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50754. /* Second test is a WOLFSSL object level */
  50755. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50756. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50757. /* Check for success */
  50758. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50759. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50760. /* Un register the devId's */
  50761. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  50762. client_cbf.devId = INVALID_DEVID;
  50763. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  50764. server_cbf.devId = INVALID_DEVID;
  50765. }
  50766. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  50767. static int test_wc_CryptoCb(void)
  50768. {
  50769. int res = TEST_SKIPPED;
  50770. #ifdef WOLF_CRYPTO_CB
  50771. /* TODO: Add crypto callback API tests */
  50772. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50773. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50774. int tlsVer;
  50775. #endif
  50776. #ifndef NO_RSA
  50777. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50778. test_wc_CryptoCb_TLS(tlsVer,
  50779. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  50780. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  50781. }
  50782. #endif
  50783. #ifdef HAVE_ECC
  50784. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50785. test_wc_CryptoCb_TLS(tlsVer,
  50786. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  50787. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  50788. }
  50789. #endif
  50790. #ifdef HAVE_ED25519
  50791. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50792. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50793. test_wc_CryptoCb_TLS(tlsVer,
  50794. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  50795. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  50796. }
  50797. #endif
  50798. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  50799. res = TEST_RES_CHECK(1);
  50800. #endif /* WOLF_CRYPTO_CB */
  50801. return res;
  50802. }
  50803. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50804. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50805. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  50806. const char* cliCaPemFile, const char* cliCertPemFile,
  50807. const char* cliPrivKeyPemFile,
  50808. const char* svrCaPemFile, const char* svrCertPemFile,
  50809. const char* svrPrivKeyPemFile,
  50810. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  50811. {
  50812. callback_functions client_cbf;
  50813. callback_functions server_cbf;
  50814. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50815. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50816. if (tlsVer == WOLFSSL_TLSV1_3) {
  50817. #ifdef WOLFSSL_TLS13
  50818. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  50819. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  50820. #endif
  50821. }
  50822. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50823. #ifndef WOLFSSL_NO_TLS12
  50824. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  50825. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  50826. #endif
  50827. }
  50828. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50829. #ifndef NO_OLD_TLS
  50830. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  50831. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  50832. #endif
  50833. }
  50834. else if (tlsVer == WOLFSSL_TLSV1) {
  50835. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50836. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  50837. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  50838. #endif
  50839. }
  50840. else if (tlsVer == WOLFSSL_SSLV3) {
  50841. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50842. defined(WOLFSSL_STATIC_RSA)
  50843. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  50844. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  50845. #endif
  50846. }
  50847. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50848. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50849. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  50850. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  50851. #endif
  50852. }
  50853. else if (tlsVer == WOLFSSL_DTLSV1) {
  50854. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50855. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  50856. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  50857. #endif
  50858. }
  50859. if (server_cbf.method_ex == NULL) {
  50860. /* not enabled */
  50861. return;
  50862. }
  50863. /* Setup the keys for the TLS test */
  50864. client_cbf.certPemFile = cliCertPemFile;
  50865. client_cbf.keyPemFile = cliPrivKeyPemFile;
  50866. client_cbf.caPemFile = cliCaPemFile;
  50867. server_cbf.certPemFile = svrCertPemFile;
  50868. server_cbf.keyPemFile = svrPrivKeyPemFile;
  50869. server_cbf.caPemFile = svrCaPemFile;
  50870. client_cbf.mem = cliMem;
  50871. client_cbf.memSz = cliMemSz;
  50872. server_cbf.mem = svrMem;
  50873. server_cbf.memSz = svrMemSz;
  50874. client_cbf.devId = INVALID_DEVID;
  50875. server_cbf.devId = INVALID_DEVID;
  50876. /* Perform TLS server and client test */
  50877. /* First test is at WOLFSSL_CTX level */
  50878. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50879. /* Check for success */
  50880. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50881. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50882. /* Second test is a WOLFSSL object level */
  50883. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50884. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50885. /* Check for success */
  50886. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50887. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50888. }
  50889. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  50890. #ifdef WOLFSSL_STATIC_MEMORY
  50891. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  50892. defined(SESSION_CERTS)
  50893. #ifdef OPENSSL_EXTRA
  50894. #define TEST_TLS_STATIC_MEMSZ (400000)
  50895. #else
  50896. #define TEST_TLS_STATIC_MEMSZ (320000)
  50897. #endif
  50898. #else
  50899. #define TEST_TLS_STATIC_MEMSZ (80000)
  50900. #endif
  50901. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  50902. {
  50903. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  50904. WOLFSSL_MEM_STATS mem_stats;
  50905. WOLFSSL_MEM_CONN_STATS ssl_stats;
  50906. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  50907. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50908. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50909. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50910. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50911. #endif
  50912. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  50913. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  50914. /* this should fail because kMaxCtxClients == 2 */
  50915. AssertNull((ssl3 = wolfSSL_new(ctx)));
  50916. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  50917. #ifdef DEBUG_WOLFSSL
  50918. wolfSSL_PrintStatsConn(&ssl_stats);
  50919. #endif
  50920. (void)ssl_stats;
  50921. }
  50922. /* display collected statistics */
  50923. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  50924. #ifdef DEBUG_WOLFSSL
  50925. wolfSSL_PrintStats(&mem_stats);
  50926. #endif
  50927. (void)mem_stats;
  50928. }
  50929. wolfSSL_free(ssl1);
  50930. wolfSSL_free(ssl2);
  50931. return TEST_RES_CHECK(1);
  50932. }
  50933. #endif /* WOLFSSL_STATIC_MEMORY */
  50934. static int test_wolfSSL_CTX_StaticMemory(void)
  50935. {
  50936. int res = TEST_SKIPPED;
  50937. #ifdef WOLFSSL_STATIC_MEMORY
  50938. wolfSSL_method_func method_func;
  50939. WOLFSSL_CTX* ctx;
  50940. const int kMaxCtxClients = 2;
  50941. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50942. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50943. int tlsVer;
  50944. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  50945. #endif
  50946. #endif
  50947. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  50948. #ifndef NO_WOLFSSL_SERVER
  50949. #ifndef WOLFSSL_NO_TLS12
  50950. method_func = wolfTLSv1_2_server_method_ex;
  50951. #else
  50952. method_func = wolfTLSv1_3_server_method_ex;
  50953. #endif
  50954. #else
  50955. #ifndef WOLFSSL_NO_TLS12
  50956. method_func = wolfTLSv1_2_client_method_ex;
  50957. #else
  50958. method_func = wolfTLSv1_3_client_method_ex;
  50959. #endif
  50960. #endif
  50961. /* Test creating CTX directly from static memory pool */
  50962. ctx = NULL;
  50963. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  50964. &ctx, method_func, svrMem, sizeof(svrMem),
  50965. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50966. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50967. wolfSSL_CTX_free(ctx);
  50968. ctx = NULL;
  50969. /* Test for heap allocated CTX, then assigning static pool to it */
  50970. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  50971. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  50972. NULL, svrMem, sizeof(svrMem),
  50973. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50974. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50975. wolfSSL_CTX_free(ctx);
  50976. /* TLS Level Tests using static memory */
  50977. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50978. #ifndef NO_RSA
  50979. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50980. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50981. svrCertFile, cliCertFile, cliKeyFile,
  50982. cliCertFile, svrCertFile, svrKeyFile,
  50983. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50984. }
  50985. #endif
  50986. #ifdef HAVE_ECC
  50987. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50988. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50989. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  50990. cliEccCertFile, eccCertFile, eccKeyFile,
  50991. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50992. }
  50993. #endif
  50994. #ifdef HAVE_ED25519
  50995. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50996. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50997. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50998. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  50999. cliEdCertFile, edCertFile, edKeyFile,
  51000. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  51001. }
  51002. #endif
  51003. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  51004. res = TEST_RES_CHECK(1);
  51005. #endif
  51006. return res;
  51007. }
  51008. static int test_openssl_FIPS_drbg(void)
  51009. {
  51010. int res = TEST_SKIPPED;
  51011. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  51012. DRBG_CTX* dctx;
  51013. byte data1[32], data2[32], zeroData[32];
  51014. byte testSeed[16];
  51015. size_t dlen = sizeof(data1);
  51016. int i;
  51017. XMEMSET(data1, 0, dlen);
  51018. XMEMSET(data2, 0, dlen);
  51019. XMEMSET(zeroData, 0, sizeof(zeroData));
  51020. for (i=0; i<(int)sizeof(testSeed); i++) {
  51021. testSeed[i] = (byte)i;
  51022. }
  51023. AssertNotNull(dctx = FIPS_get_default_drbg());
  51024. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  51025. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  51026. WOLFSSL_SUCCESS);
  51027. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  51028. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  51029. WOLFSSL_SUCCESS);
  51030. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  51031. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  51032. WOLFSSL_SUCCESS);
  51033. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  51034. WOLFSSL_SUCCESS);
  51035. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  51036. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  51037. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  51038. res = TEST_RES_CHECK(1);
  51039. #endif
  51040. return res;
  51041. }
  51042. static int test_wolfSSL_FIPS_mode(void)
  51043. {
  51044. int res = TEST_SKIPPED;
  51045. #if defined(OPENSSL_ALL)
  51046. #ifdef HAVE_FIPS
  51047. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  51048. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  51049. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  51050. #else
  51051. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  51052. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  51053. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  51054. #endif
  51055. res = TEST_RES_CHECK(1);
  51056. #endif
  51057. return res;
  51058. }
  51059. #ifdef WOLFSSL_DTLS
  51060. /* Prints out the current window */
  51061. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  51062. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  51063. int i;
  51064. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  51065. word32 b = w[i];
  51066. int j;
  51067. /* Prints out a 32 bit binary number in big endian order */
  51068. for (j = 0; j < 32; j++, b <<= 1) {
  51069. if (b & (((word32)1) << 31))
  51070. fprintf(stderr, "1");
  51071. else
  51072. fprintf(stderr, "0");
  51073. }
  51074. fprintf(stderr, " ");
  51075. }
  51076. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  51077. #else
  51078. (void)h;
  51079. (void)l;
  51080. (void)w;
  51081. #endif
  51082. }
  51083. /* a - cur_hi
  51084. * b - cur_lo
  51085. * c - next_hi
  51086. * d - next_lo
  51087. * e - window
  51088. * f - expected next_hi
  51089. * g - expected next_lo
  51090. * h - expected window[1]
  51091. * i - expected window[0]
  51092. */
  51093. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  51094. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  51095. DUW_TEST_print_window_binary((a), (b), (e)); \
  51096. AssertIntEQ((c), (f)); \
  51097. AssertIntEQ((d), (g)); \
  51098. AssertIntEQ((e)[1], (h)); \
  51099. AssertIntEQ((e)[0], (i)); \
  51100. } while (0)
  51101. static int test_wolfSSL_DtlsUpdateWindow(void)
  51102. {
  51103. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  51104. word32 next_lo = 0;
  51105. word16 next_hi = 0;
  51106. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  51107. fprintf(stderr, "\n");
  51108. #endif
  51109. XMEMSET(window, 0, sizeof window);
  51110. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  51111. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  51112. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  51113. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  51114. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  51115. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  51116. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  51117. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  51118. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  51119. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  51120. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  51121. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  51122. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  51123. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  51124. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  51125. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  51126. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  51127. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  51128. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  51129. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  51130. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  51131. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  51132. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  51133. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  51134. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  51135. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  51136. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  51137. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51138. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51139. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  51140. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  51141. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  51142. return TEST_RES_CHECK(1);
  51143. }
  51144. #endif /* WOLFSSL_DTLS */
  51145. #ifdef WOLFSSL_DTLS
  51146. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  51147. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  51148. {
  51149. DtlsMsg* cur;
  51150. static byte msg[100];
  51151. static byte msgInit = 0;
  51152. if (!msgInit) {
  51153. int i;
  51154. for (i = 0; i < 100; i++)
  51155. msg[i] = i + 1;
  51156. msgInit = 1;
  51157. }
  51158. /* Sanitize test parameters */
  51159. if (len > sizeof(msg))
  51160. return -1;
  51161. if (f_offset + f_len > sizeof(msg))
  51162. return -1;
  51163. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  51164. if (ssl->dtls_rx_msg_list == NULL)
  51165. return -100;
  51166. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  51167. return -200;
  51168. if (cur->fragBucketListCount != f_count)
  51169. return -300;
  51170. if (cur->ready != ready)
  51171. return -400;
  51172. if (cur->bytesReceived != bytesReceived)
  51173. return -500;
  51174. if (ready) {
  51175. if (cur->fragBucketList != NULL)
  51176. return -600;
  51177. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  51178. return -700;
  51179. }
  51180. else {
  51181. DtlsFragBucket* fb;
  51182. if (cur->fragBucketList == NULL)
  51183. return -800;
  51184. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  51185. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  51186. return -900;
  51187. }
  51188. }
  51189. return 0;
  51190. }
  51191. static void DFB_TEST_RESET(WOLFSSL* ssl)
  51192. {
  51193. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  51194. ssl->dtls_rx_msg_list = NULL;
  51195. ssl->dtls_rx_msg_list_sz = 0;
  51196. }
  51197. static int test_wolfSSL_DTLS_fragment_buckets(void)
  51198. {
  51199. WOLFSSL ssl[1];
  51200. XMEMSET(ssl, 0, sizeof(*ssl));
  51201. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  51202. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51203. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  51204. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  51205. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  51206. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  51207. /* Test all permutations of 3 regions */
  51208. /* 1 2 3 */
  51209. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51210. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  51211. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  51212. /* 1 3 2 */
  51213. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51214. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  51215. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  51216. /* 2 1 3 */
  51217. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51218. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  51219. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  51220. /* 2 3 1 */
  51221. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51222. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  51223. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51224. /* 3 1 2 */
  51225. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51226. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  51227. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  51228. /* 3 2 1 */
  51229. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51230. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  51231. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51232. /* Test overlapping regions */
  51233. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51234. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  51235. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  51236. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  51237. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  51238. /* Test overlapping multiple regions */
  51239. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51240. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  51241. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  51242. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  51243. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  51244. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  51245. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  51246. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  51247. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  51248. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  51249. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51250. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  51251. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  51252. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  51253. DFB_TEST_RESET(ssl);
  51254. return TEST_RES_CHECK(1);
  51255. }
  51256. #endif
  51257. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51258. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  51259. static int test_wolfSSL_dtls_stateless2(void)
  51260. {
  51261. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51262. struct test_memio_ctx test_ctx;
  51263. WOLFSSL_CTX *ctx_c, *ctx_s;
  51264. int ret;
  51265. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51266. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51267. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51268. if (ret != 0)
  51269. return -1;
  51270. ssl_c2 = wolfSSL_new(ctx_c);
  51271. if (ssl_c2 == NULL)
  51272. return -2;
  51273. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51274. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51275. /* send CH */
  51276. ret = wolfSSL_connect(ssl_c2);
  51277. if (ret == 0 || ssl_c2->error != WANT_READ)
  51278. return -3;
  51279. ret = wolfSSL_accept(ssl_s);
  51280. if (ret == 0 || ssl_s->error != WANT_READ)
  51281. return -4;
  51282. if (test_ctx.c_len == 0)
  51283. return -5;
  51284. /* consume HRR */
  51285. test_ctx.c_len = 0;
  51286. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51287. if (ret != 0)
  51288. return -6;
  51289. wolfSSL_free(ssl_c2);
  51290. wolfSSL_free(ssl_c);
  51291. wolfSSL_free(ssl_s);
  51292. wolfSSL_CTX_free(ctx_c);
  51293. wolfSSL_CTX_free(ctx_s);
  51294. return TEST_SUCCESS;
  51295. }
  51296. #ifdef HAVE_MAX_FRAGMENT
  51297. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  51298. {
  51299. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51300. struct test_memio_ctx test_ctx;
  51301. WOLFSSL_CTX *ctx_c, *ctx_s;
  51302. word16 max_fragment;
  51303. int ret;
  51304. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51305. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51306. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51307. if (ret != 0)
  51308. return -1;
  51309. ssl_c2 = wolfSSL_new(ctx_c);
  51310. if (ssl_c2 == NULL)
  51311. return -2;
  51312. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  51313. if (ret != WOLFSSL_SUCCESS)
  51314. return -3;
  51315. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51316. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51317. max_fragment = ssl_s->max_fragment;
  51318. /* send CH */
  51319. ret = wolfSSL_connect(ssl_c2);
  51320. if (ret == 0 || ssl_c2->error != WANT_READ)
  51321. return -4;
  51322. ret = wolfSSL_accept(ssl_s);
  51323. if (ret == 0 || ssl_s->error != WANT_READ)
  51324. return -5;
  51325. /* CH without cookie shouldn't change state */
  51326. if (ssl_s->max_fragment != max_fragment)
  51327. return -6;
  51328. if (test_ctx.c_len == 0)
  51329. return -7;
  51330. /* consume HRR from buffer */
  51331. test_ctx.c_len = 0;
  51332. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51333. if (ret != 0)
  51334. return -8;
  51335. wolfSSL_free(ssl_c2);
  51336. wolfSSL_free(ssl_c);
  51337. wolfSSL_free(ssl_s);
  51338. wolfSSL_CTX_free(ctx_c);
  51339. wolfSSL_CTX_free(ctx_s);
  51340. return TEST_SUCCESS;
  51341. }
  51342. #endif /* HAVE_MAX_FRAGMENT */
  51343. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  51344. #define ROUNDS_WITH_HVR 4
  51345. #define ROUNDS_WITHOUT_HVR 2
  51346. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  51347. static int buf_is_hvr(const byte *data, int len)
  51348. {
  51349. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  51350. return 0;
  51351. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  51352. }
  51353. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  51354. {
  51355. struct test_memio_ctx test_ctx;
  51356. WOLFSSL_CTX *ctx_c, *ctx_s;
  51357. WOLFSSL *ssl_c, *ssl_s;
  51358. WOLFSSL_SESSION *sess;
  51359. int ret, round_trips;
  51360. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51361. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51362. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51363. if (ret != 0)
  51364. return -1;
  51365. #ifdef HAVE_SESSION_TICKET
  51366. if (useticket) {
  51367. ret = wolfSSL_UseSessionTicket(ssl_c);
  51368. if (ret != WOLFSSL_SUCCESS)
  51369. return -2;
  51370. }
  51371. #endif
  51372. round_trips = ROUNDS_WITH_HVR;
  51373. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  51374. if (ret != 0)
  51375. return -3;
  51376. if (round_trips != ROUNDS_WITH_HVR)
  51377. return -4;
  51378. sess = wolfSSL_get1_session(ssl_c);
  51379. if (sess == NULL)
  51380. return -5;
  51381. wolfSSL_shutdown(ssl_c);
  51382. wolfSSL_shutdown(ssl_s);
  51383. wolfSSL_free(ssl_c);
  51384. wolfSSL_free(ssl_s);
  51385. test_ctx.c_len = test_ctx.s_len = 0;
  51386. /* make resumption invalid */
  51387. if (bad) {
  51388. if (useticket) {
  51389. #ifdef HAVE_SESSION_TICKET
  51390. sess->ticket[0] = !sess->ticket[0];
  51391. #endif /* HAVE_SESSION_TICKET */
  51392. }
  51393. else {
  51394. sess->sessionID[0] = !sess->sessionID[0];
  51395. }
  51396. }
  51397. ssl_c = wolfSSL_new(ctx_c);
  51398. ssl_s = wolfSSL_new(ctx_s);
  51399. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  51400. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  51401. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  51402. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  51403. ret = wolfSSL_set_session(ssl_c, sess);
  51404. if (ret != WOLFSSL_SUCCESS)
  51405. return -6;
  51406. ret = wolfSSL_connect(ssl_c);
  51407. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  51408. return -7;
  51409. ret = wolfSSL_accept(ssl_s);
  51410. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  51411. return -8;
  51412. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  51413. return -9;
  51414. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  51415. return -10;
  51416. if (!useticket) {
  51417. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  51418. if (ret != 0)
  51419. return -11;
  51420. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  51421. return -12;
  51422. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  51423. return -13;
  51424. }
  51425. wolfSSL_SESSION_free(sess);
  51426. wolfSSL_free(ssl_c);
  51427. wolfSSL_free(ssl_s);
  51428. wolfSSL_CTX_free(ctx_c);
  51429. wolfSSL_CTX_free(ctx_s);
  51430. return TEST_SUCCESS;
  51431. }
  51432. static int test_wolfSSL_dtls_stateless_resume(void)
  51433. {
  51434. int ret;
  51435. #ifdef HAVE_SESSION_TICKET
  51436. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  51437. if (ret != 0)
  51438. return ret;
  51439. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  51440. if (ret != 0)
  51441. return ret - 100;
  51442. #endif /* HAVE_SESION_TICKET */
  51443. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  51444. if (ret != 0)
  51445. return ret - 200;
  51446. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  51447. if (ret != 0)
  51448. return ret - 300;
  51449. return TEST_SUCCESS;
  51450. }
  51451. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  51452. #if !defined(NO_OLD_TLS)
  51453. static int test_wolfSSL_dtls_stateless_downgrade(void)
  51454. {
  51455. WOLFSSL_CTX *ctx_c, *ctx_c2, *ctx_s;
  51456. WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
  51457. struct test_memio_ctx test_ctx;
  51458. int ret;
  51459. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51460. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51461. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  51462. if (ret != 0)
  51463. return -1;
  51464. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  51465. if (ret != WOLFSSL_SUCCESS)
  51466. return -2;
  51467. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  51468. if (ctx_c2 == NULL)
  51469. return -3;
  51470. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  51471. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  51472. ssl_c2 = wolfSSL_new(ctx_c2);
  51473. if (ssl_c2 == NULL)
  51474. return -4;
  51475. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  51476. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  51477. /* send CH */
  51478. ret = wolfSSL_connect(ssl_c2);
  51479. if (ret == 0 || ssl_c2->error != WANT_READ)
  51480. return -5;
  51481. ret = wolfSSL_accept(ssl_s);
  51482. if (ret == 0 || ssl_s->error != WANT_READ)
  51483. return -6;
  51484. if (test_ctx.c_len == 0)
  51485. return -7;
  51486. /* consume HRR */
  51487. test_ctx.c_len = 0;
  51488. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  51489. if (ret != 0)
  51490. return -8;
  51491. wolfSSL_free(ssl_c2);
  51492. wolfSSL_free(ssl_c);
  51493. wolfSSL_free(ssl_s);
  51494. wolfSSL_CTX_free(ctx_c);
  51495. wolfSSL_CTX_free(ctx_c2);
  51496. wolfSSL_CTX_free(ctx_s);
  51497. return TEST_SUCCESS;
  51498. }
  51499. #endif /* !defined(NO_OLD_TLS) */
  51500. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51501. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  51502. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51503. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51504. !defined(NO_OLD_TLS)
  51505. static int test_WOLFSSL_dtls_version_alert(void)
  51506. {
  51507. struct test_memio_ctx test_ctx;
  51508. WOLFSSL_CTX *ctx_c, *ctx_s;
  51509. WOLFSSL *ssl_c, *ssl_s;
  51510. int ret;
  51511. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51512. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51513. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  51514. if (ret != 0)
  51515. return -1;
  51516. /* client hello */
  51517. ret = wolfSSL_connect(ssl_c);
  51518. if (ret == 0 || ssl_c->error != WANT_READ )
  51519. return -2;
  51520. /* hrr */
  51521. ret = wolfSSL_accept(ssl_s);
  51522. if (ret == 0 || ssl_s->error != WANT_READ )
  51523. return -3;
  51524. /* client hello 1 */
  51525. ret = wolfSSL_connect(ssl_c);
  51526. if (ret == 0 || ssl_c->error != WANT_READ )
  51527. return -4;
  51528. /* server hello */
  51529. ret = wolfSSL_accept(ssl_s);
  51530. if (ret == 0 || ssl_s->error != WANT_READ )
  51531. return -5;
  51532. /* should fail */
  51533. ret = wolfSSL_connect(ssl_c);
  51534. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  51535. return -6;
  51536. /* shuould fail */
  51537. ret = wolfSSL_accept(ssl_s);
  51538. if (ret == 0 ||
  51539. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  51540. return -7;
  51541. wolfSSL_free(ssl_c);
  51542. wolfSSL_free(ssl_s);
  51543. wolfSSL_CTX_free(ctx_c);
  51544. wolfSSL_CTX_free(ctx_s);
  51545. return TEST_RES_CHECK(1);
  51546. }
  51547. #else
  51548. static int test_WOLFSSL_dtls_version_alert(void)
  51549. {
  51550. return TEST_SKIPPED;
  51551. }
  51552. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  51553. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  51554. * !defined(NO_OLD_TLS)
  51555. */
  51556. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  51557. && defined(WOLFSSL_TLS13) && \
  51558. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  51559. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  51560. {
  51561. struct test_memio_ctx *test_ctx;
  51562. byte buf[2048];
  51563. int idx, sz;
  51564. word32 tmp;
  51565. int ret;
  51566. idx = 5; /* space for record header */
  51567. buf[idx] = session_ticket; /* type */
  51568. idx++;
  51569. tmp = OPAQUE32_LEN +
  51570. OPAQUE32_LEN +
  51571. OPAQUE8_LEN + nonceLength +
  51572. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  51573. c32to24(tmp, buf + idx);
  51574. idx += OPAQUE24_LEN;
  51575. c32toa((word32)12345, buf+idx); /* lifetime */
  51576. idx += OPAQUE32_LEN;
  51577. c32toa((word32)12345, buf+idx); /* add */
  51578. idx += OPAQUE32_LEN;
  51579. buf[idx] = nonceLength; /* nonce length */
  51580. idx++;
  51581. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  51582. idx += nonceLength;
  51583. tmp = 1; /* ticket len */
  51584. c16toa((word16)tmp, buf+idx);
  51585. idx += 2;
  51586. buf[idx] = 0xFF; /* ticket */
  51587. idx++;
  51588. tmp = 0; /* ext len */
  51589. c16toa((word16)tmp, buf+idx);
  51590. idx += 2;
  51591. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  51592. handshake, 0, 0, 0);
  51593. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  51594. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  51595. return !(ret == sz);
  51596. }
  51597. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  51598. {
  51599. int i;
  51600. if (sess == NULL)
  51601. return -1;
  51602. if (sess->ticketNonce.len != len)
  51603. return -1;
  51604. for (i = 0; i < len; i++)
  51605. if (sess->ticketNonce.data[i] != len)
  51606. return -1;
  51607. return 0;
  51608. }
  51609. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51610. {
  51611. char *buf[1024];
  51612. int ret;
  51613. ret = send_new_session_ticket(ssl_s, len, len);
  51614. if (ret != 0)
  51615. return -1;
  51616. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  51617. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51618. return -1;
  51619. return test_ticket_nonce_check(ssl_c->session, len);
  51620. }
  51621. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51622. {
  51623. WOLFSSL_SESSION *sess, *cached;
  51624. WOLFSSL_CTX *ctx;
  51625. int ret;
  51626. ctx = ssl_c->ctx;
  51627. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  51628. if (ret != 0)
  51629. return -1;
  51630. sess = wolfSSL_get1_session(ssl_c);
  51631. if (sess == NULL)
  51632. return -1;
  51633. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  51634. NULL, ssl_c->options.side, 1,NULL);
  51635. if (ret != 0)
  51636. return -1;
  51637. cached = wolfSSL_SESSION_new();
  51638. if (cached == NULL)
  51639. return -1;
  51640. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  51641. if (ret != WOLFSSL_SUCCESS)
  51642. return -1;
  51643. ret = test_ticket_nonce_check(cached, len);
  51644. if (ret != 0)
  51645. return -1;
  51646. wolfSSL_SESSION_free(cached);
  51647. wolfSSL_SESSION_free(sess);
  51648. return 0;
  51649. }
  51650. static int test_ticket_nonce_malloc(void)
  51651. {
  51652. struct test_memio_ctx test_ctx;
  51653. WOLFSSL_CTX *ctx_c, *ctx_s;
  51654. byte small, medium, big;
  51655. WOLFSSL *ssl_c, *ssl_s;
  51656. int ret;
  51657. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51658. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51659. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  51660. if (ret != 0)
  51661. return -1;
  51662. /* will send ticket manually */
  51663. wolfSSL_no_ticket_TLSv13(ssl_s);
  51664. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  51665. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  51666. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  51667. ret = wolfSSL_connect(ssl_c);
  51668. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51669. return -2;
  51670. ret = wolfSSL_accept(ssl_s);
  51671. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  51672. return -3;
  51673. }
  51674. small = TLS13_TICKET_NONCE_STATIC_SZ;
  51675. medium = small + 20 <= 255 ? small + 20 : 255;
  51676. big = medium + 20 <= 255 ? small + 20 : 255;
  51677. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51678. return -1;
  51679. if (ssl_c->session->ticketNonce.data !=
  51680. ssl_c->session->ticketNonce.dataStatic)
  51681. return -1;
  51682. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51683. return -1;
  51684. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  51685. return -1;
  51686. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51687. return -5;
  51688. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51689. return -6;
  51690. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51691. return -1;
  51692. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51693. return -1;
  51694. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  51695. return -1;
  51696. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51697. return -1;
  51698. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51699. return -1;
  51700. wolfSSL_free(ssl_c);
  51701. wolfSSL_free(ssl_s);
  51702. wolfSSL_CTX_free(ctx_c);
  51703. wolfSSL_CTX_free(ctx_s);
  51704. return 0;
  51705. }
  51706. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  51707. /*----------------------------------------------------------------------------*
  51708. | Main
  51709. *----------------------------------------------------------------------------*/
  51710. typedef int (*TEST_FUNC)(void);
  51711. typedef struct {
  51712. const char *name;
  51713. TEST_FUNC func;
  51714. byte run:1;
  51715. } TEST_CASE;
  51716. #define TEST_DECL(func) { #func, func, 0 }
  51717. int testAll = 1;
  51718. TEST_CASE testCases[] = {
  51719. TEST_DECL(test_fileAccess),
  51720. TEST_DECL(test_wolfSSL_Init),
  51721. TEST_DECL(test_wolfSSL_Method_Allocators),
  51722. #ifndef NO_WOLFSSL_SERVER
  51723. TEST_DECL(test_wolfSSL_CTX_new),
  51724. #endif
  51725. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  51726. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  51727. TEST_DECL(test_for_double_Free),
  51728. #endif
  51729. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51730. TEST_DECL(test_wolfSSL_get_finished),
  51731. TEST_DECL(test_wolfSSL_CTX_add_session),
  51732. #endif
  51733. TEST_DECL(test_SSL_CIPHER_get_xxx),
  51734. TEST_DECL(test_wolfSSL_ERR_strings),
  51735. TEST_DECL(test_wolfSSL_EVP_shake128),
  51736. TEST_DECL(test_wolfSSL_EVP_shake256),
  51737. TEST_DECL(test_EVP_blake2),
  51738. TEST_DECL(test_EVP_MD_do_all),
  51739. TEST_DECL(test_OBJ_NAME_do_all),
  51740. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  51741. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  51742. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  51743. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  51744. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  51745. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  51746. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  51747. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  51748. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  51749. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  51750. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  51751. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  51752. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  51753. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  51754. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  51755. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  51756. TEST_DECL(test_wolfSSL_FPKI),
  51757. TEST_DECL(test_wolfSSL_OtherName),
  51758. TEST_DECL(test_wolfSSL_CertRsaPss),
  51759. TEST_DECL(test_wolfSSL_CertManagerCRL),
  51760. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  51761. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  51762. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  51763. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  51764. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  51765. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  51766. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  51767. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  51768. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  51769. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  51770. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  51771. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  51772. TEST_DECL(test_server_wolfSSL_new),
  51773. TEST_DECL(test_client_wolfSSL_new),
  51774. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  51775. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  51776. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  51777. TEST_DECL(test_SetTmpEC_DHE_Sz),
  51778. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  51779. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  51780. TEST_DECL(test_wolfSSL_dtls_plaintext),
  51781. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51782. defined(HAVE_IO_TESTS_DEPENDENCIES)
  51783. TEST_DECL(test_wolfSSL_read_write),
  51784. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  51785. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  51786. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  51787. TEST_DECL(test_wolfSSL_dtls_export),
  51788. TEST_DECL(test_wolfSSL_tls_export),
  51789. #endif
  51790. TEST_DECL(test_wolfSSL_SetMinVersion),
  51791. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  51792. /* TLS extensions tests */
  51793. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51794. #ifdef HAVE_SNI
  51795. TEST_DECL(test_wolfSSL_UseSNI_params),
  51796. TEST_DECL(test_wolfSSL_UseSNI_connection),
  51797. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  51798. #endif /* HAVE_SNI */
  51799. #endif
  51800. TEST_DECL(test_wolfSSL_UseTrustedCA),
  51801. TEST_DECL(test_wolfSSL_UseMaxFragment),
  51802. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  51803. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  51804. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51805. TEST_DECL(test_wolfSSL_UseALPN_connection),
  51806. TEST_DECL(test_wolfSSL_UseALPN_params),
  51807. #endif
  51808. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  51809. TEST_DECL(test_wolfSSL_set_alpn_protos),
  51810. #endif
  51811. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  51812. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  51813. TEST_DECL(test_tls_ext_duplicate),
  51814. /* X509 tests */
  51815. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  51816. TEST_DECL(test_wolfSSL_PKCS12),
  51817. TEST_DECL(test_wolfSSL_no_password_cb),
  51818. TEST_DECL(test_wolfSSL_PKCS8),
  51819. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  51820. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  51821. TEST_DECL(test_wolfSSL_PKCS5),
  51822. TEST_DECL(test_wolfSSL_URI),
  51823. TEST_DECL(test_wolfSSL_TBS),
  51824. TEST_DECL(test_wolfSSL_X509_verify),
  51825. TEST_DECL(test_wolfSSL_X509_TLS_version),
  51826. TEST_DECL(test_wc_PemToDer),
  51827. TEST_DECL(test_wc_AllocDer),
  51828. TEST_DECL(test_wc_CertPemToDer),
  51829. TEST_DECL(test_wc_PubKeyPemToDer),
  51830. TEST_DECL(test_wc_PemPubKeyToDer),
  51831. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  51832. TEST_DECL(test_wc_CheckCertSigPubKey),
  51833. /* OCSP Stapling */
  51834. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  51835. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  51836. /* Multicast */
  51837. TEST_DECL(test_wolfSSL_mcast),
  51838. /* compatibility tests */
  51839. TEST_DECL(test_wolfSSL_lhash),
  51840. TEST_DECL(test_wolfSSL_X509_NAME),
  51841. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  51842. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  51843. #ifndef NO_BIO
  51844. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  51845. TEST_DECL(test_wolfSSL_X509_INFO),
  51846. #endif
  51847. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  51848. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  51849. TEST_DECL(test_wolfSSL_X509_check_host),
  51850. TEST_DECL(test_wolfSSL_X509_check_email),
  51851. TEST_DECL(test_wolfSSL_DES),
  51852. TEST_DECL(test_wolfSSL_certs),
  51853. TEST_DECL(test_wolfSSL_X509_check_private_key),
  51854. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  51855. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  51856. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  51857. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  51858. TEST_DECL(test_wolfSSL_private_keys),
  51859. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  51860. #ifndef NO_BIO
  51861. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  51862. #endif
  51863. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  51864. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  51865. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  51866. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  51867. #ifndef NO_BIO
  51868. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  51869. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  51870. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  51871. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  51872. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  51873. #endif
  51874. TEST_DECL(test_DSA_do_sign_verify),
  51875. TEST_DECL(test_wolfSSL_tmp_dh),
  51876. TEST_DECL(test_wolfSSL_ctrl),
  51877. TEST_DECL(test_wolfSSL_EVP_MD_size),
  51878. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  51879. TEST_DECL(test_wolfSSL_EVP_Digest),
  51880. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  51881. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  51882. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  51883. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  51884. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  51885. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  51886. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  51887. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  51888. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  51889. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  51890. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  51891. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  51892. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  51893. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  51894. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  51895. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  51896. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  51897. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  51898. #endif
  51899. #ifndef NO_BIO
  51900. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  51901. TEST_DECL(test_wolfSSL_GetLoggingCb),
  51902. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  51903. TEST_DECL(test_wc_ERR_remove_state),
  51904. TEST_DECL(test_wc_ERR_print_errors_fp),
  51905. #endif
  51906. TEST_DECL(test_wolfSSL_set_options),
  51907. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  51908. TEST_DECL(test_wolfSSL_set1_curves_list),
  51909. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  51910. TEST_DECL(test_wolfSSL_PKCS7_certs),
  51911. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  51912. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  51913. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  51914. TEST_DECL(test_wolfSSL_msgCb),
  51915. TEST_DECL(test_wolfSSL_either_side),
  51916. TEST_DECL(test_wolfSSL_DTLS_either_side),
  51917. TEST_DECL(test_wolfSSL_dtls_fragments),
  51918. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  51919. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  51920. TEST_DECL(test_wolfSSL_dtls_bad_record),
  51921. TEST_DECL(test_wolfSSL_dtls_stateless),
  51922. TEST_DECL(test_generate_cookie),
  51923. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  51924. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  51925. TEST_DECL(test_wolfSSL_X509_Name_canon),
  51926. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  51927. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  51928. TEST_DECL(test_wolfSSL_X509_NID),
  51929. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  51930. TEST_DECL(test_wolfSSL_get0_param),
  51931. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  51932. TEST_DECL(test_wolfSSL_set1_host),
  51933. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  51934. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  51935. TEST_DECL(test_wolfSSL_X509_STORE),
  51936. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  51937. TEST_DECL(test_X509_STORE_get0_objects),
  51938. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  51939. TEST_DECL(test_wolfSSL_BN),
  51940. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  51941. #ifndef NO_BIO
  51942. TEST_DECL(test_wolfSSL_PEM_read_bio),
  51943. TEST_DECL(test_wolfSSL_BIO),
  51944. #endif
  51945. TEST_DECL(test_wolfSSL_ASN1_STRING),
  51946. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  51947. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  51948. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  51949. TEST_DECL(test_wolfSSL_X509),
  51950. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  51951. TEST_DECL(test_wolfSSL_X509_sign),
  51952. TEST_DECL(test_wolfSSL_X509_sign2),
  51953. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  51954. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  51955. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51956. TEST_DECL(test_wolfSSL_check_domain),
  51957. #endif
  51958. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  51959. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  51960. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  51961. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  51962. TEST_DECL(test_wolfSSL_RAND),
  51963. TEST_DECL(test_wolfSSL_BUF),
  51964. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  51965. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  51966. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  51967. TEST_DECL(test_wolfSSL_X509_cmp_time),
  51968. TEST_DECL(test_wolfSSL_X509_time_adj),
  51969. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  51970. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  51971. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  51972. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  51973. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  51974. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  51975. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  51976. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  51977. TEST_DECL(test_wolfSSL_Tls13_postauth),
  51978. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  51979. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  51980. TEST_DECL(test_wolfSSL_THREADID_hash),
  51981. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  51982. TEST_DECL(test_wolfSSL_RAND_bytes),
  51983. TEST_DECL(test_wolfSSL_BN_rand),
  51984. TEST_DECL(test_wolfSSL_pseudo_rand),
  51985. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  51986. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  51987. TEST_DECL(test_error_queue_per_thread),
  51988. TEST_DECL(test_wolfSSL_ERR_put_error),
  51989. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  51990. #ifndef NO_BIO
  51991. TEST_DECL(test_wolfSSL_ERR_print_errors),
  51992. #endif
  51993. TEST_DECL(test_wolfSSL_HMAC),
  51994. TEST_DECL(test_wolfSSL_CMAC),
  51995. TEST_DECL(test_wolfSSL_OBJ),
  51996. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  51997. TEST_DECL(test_wolfSSL_OBJ_cmp),
  51998. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  51999. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  52000. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  52001. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  52002. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  52003. TEST_DECL(test_wolfSSL_X509_set_name),
  52004. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  52005. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  52006. TEST_DECL(test_wolfSSL_X509_set_version),
  52007. #ifndef NO_BIO
  52008. TEST_DECL(test_wolfSSL_BIO_gets),
  52009. TEST_DECL(test_wolfSSL_BIO_puts),
  52010. TEST_DECL(test_wolfSSL_BIO_dump),
  52011. TEST_DECL(test_wolfSSL_BIO_should_retry),
  52012. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  52013. TEST_DECL(test_wolfSSL_BIO_write),
  52014. TEST_DECL(test_wolfSSL_BIO_connect),
  52015. TEST_DECL(test_wolfSSL_BIO_accept),
  52016. TEST_DECL(test_wolfSSL_BIO_printf),
  52017. TEST_DECL(test_wolfSSL_BIO_f_md),
  52018. TEST_DECL(test_wolfSSL_BIO_up_ref),
  52019. TEST_DECL(test_wolfSSL_BIO_tls),
  52020. #endif
  52021. TEST_DECL(test_wolfSSL_cert_cb),
  52022. TEST_DECL(test_wolfSSL_SESSION),
  52023. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  52024. TEST_DECL(test_wolfSSL_ticket_keys),
  52025. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  52026. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  52027. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  52028. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  52029. TEST_DECL(test_wolfSSL_MD4),
  52030. TEST_DECL(test_wolfSSL_verify_mode),
  52031. TEST_DECL(test_wolfSSL_verify_depth),
  52032. TEST_DECL(test_wolfSSL_HMAC_CTX),
  52033. TEST_DECL(test_wolfSSL_msg_callback),
  52034. TEST_DECL(test_wolfSSL_SHA),
  52035. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  52036. TEST_DECL(test_wolfSSL_MD5),
  52037. TEST_DECL(test_wolfSSL_MD5_Transform),
  52038. TEST_DECL(test_wolfSSL_SHA_Transform),
  52039. TEST_DECL(test_wolfSSL_SHA256),
  52040. TEST_DECL(test_wolfSSL_SHA256_Transform),
  52041. TEST_DECL(test_wolfSSL_SHA224),
  52042. TEST_DECL(test_wolfSSL_SHA512_Transform),
  52043. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  52044. TEST_DECL(test_wolfSSL_X509_CRL),
  52045. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  52046. TEST_DECL(test_wolfSSL_PEM_read_X509),
  52047. TEST_DECL(test_wolfSSL_PEM_read),
  52048. #ifndef NO_BIO
  52049. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  52050. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  52051. #endif
  52052. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  52053. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  52054. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  52055. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  52056. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  52057. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  52058. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  52059. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  52060. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  52061. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  52062. TEST_DECL(test_wolfSSL_X509_check_ca),
  52063. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  52064. TEST_DECL(test_wolfSSL_make_cert),
  52065. TEST_DECL(test_wolfSSL_DES_ncbc),
  52066. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  52067. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  52068. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  52069. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  52070. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  52071. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  52072. TEST_DECL(test_wolfssl_EVP_chacha20),
  52073. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  52074. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  52075. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  52076. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  52077. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  52078. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  52079. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  52080. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  52081. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  52082. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  52083. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  52084. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  52085. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  52086. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  52087. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  52088. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  52089. TEST_DECL(test_CONF_modules_xxx),
  52090. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  52091. TEST_DECL(test_CRYPTO_THREADID_xxx),
  52092. TEST_DECL(test_ENGINE_cleanup),
  52093. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  52094. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  52095. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  52096. #ifdef OPENSSL_ALL
  52097. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  52098. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  52099. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  52100. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  52101. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  52102. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  52103. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  52104. TEST_DECL(test_wolfSSL_DSA_SIG),
  52105. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  52106. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  52107. TEST_DECL(test_wolfSSL_CTX_ctrl),
  52108. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  52109. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  52110. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  52111. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  52112. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  52113. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  52114. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  52115. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  52116. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  52117. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  52118. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  52119. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  52120. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  52121. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  52122. TEST_DECL(test_wolfSSL_EVP_rc4),
  52123. TEST_DECL(test_wolfSSL_EVP_enc_null),
  52124. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  52125. TEST_DECL(test_wolfSSL_EVP_mdc2),
  52126. TEST_DECL(test_wolfSSL_EVP_md4),
  52127. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  52128. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  52129. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  52130. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  52131. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  52132. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  52133. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  52134. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  52135. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  52136. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  52137. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  52138. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  52139. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  52140. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  52141. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  52142. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  52143. TEST_DECL(test_evp_cipher_aes_gcm),
  52144. TEST_DECL(test_wolfSSL_OBJ_ln),
  52145. TEST_DECL(test_wolfSSL_OBJ_sn),
  52146. TEST_DECL(test_wolfSSL_TXT_DB),
  52147. TEST_DECL(test_wolfSSL_NCONF),
  52148. #endif /* OPENSSL_ALL */
  52149. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  52150. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  52151. #ifndef NO_BIO
  52152. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  52153. #endif /* !NO_BIO */
  52154. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  52155. TEST_DECL(test_wolfSSL_X509_CA_num),
  52156. TEST_DECL(test_wolfSSL_X509_get_version),
  52157. #ifndef NO_BIO
  52158. TEST_DECL(test_wolfSSL_X509_print),
  52159. TEST_DECL(test_wolfSSL_X509_CRL_print),
  52160. TEST_DECL(test_wolfSSL_BIO_get_len),
  52161. #endif
  52162. TEST_DECL(test_wolfSSL_RSA),
  52163. TEST_DECL(test_wolfSSL_RSA_DER),
  52164. TEST_DECL(test_wolfSSL_RSA_print),
  52165. #ifndef NO_RSA
  52166. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  52167. #endif
  52168. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  52169. TEST_DECL(test_wolfSSL_RSA_get0_key),
  52170. TEST_DECL(test_wolfSSL_RSA_meth),
  52171. TEST_DECL(test_wolfSSL_RSA_verify),
  52172. TEST_DECL(test_wolfSSL_RSA_sign),
  52173. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  52174. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  52175. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  52176. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  52177. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  52178. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  52179. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  52180. TEST_DECL(test_wolfSSL_RSA_ex_data),
  52181. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  52182. TEST_DECL(test_wolfSSL_RSA_To_Der),
  52183. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  52184. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  52185. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  52186. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  52187. TEST_DECL(test_wolfSSL_DH),
  52188. TEST_DECL(test_wolfSSL_DH_dup),
  52189. TEST_DECL(test_wolfSSL_DH_check),
  52190. TEST_DECL(test_wolfSSL_DH_prime),
  52191. TEST_DECL(test_wolfSSL_DH_1536_prime),
  52192. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  52193. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  52194. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  52195. TEST_DECL(test_wolfSSL_d2i_DHparams),
  52196. TEST_DECL(test_wolfSSL_DH_LoadDer),
  52197. TEST_DECL(test_wolfSSL_i2d_DHparams),
  52198. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  52199. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  52200. TEST_DECL(test_wolfSSL_X509V3_EXT),
  52201. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  52202. TEST_DECL(test_wolfSSL_X509_get_ext),
  52203. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  52204. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  52205. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  52206. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  52207. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  52208. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  52209. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  52210. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  52211. TEST_DECL(test_wolfSSL_X509_cmp),
  52212. #ifndef NO_BIO
  52213. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  52214. #endif
  52215. TEST_DECL(test_wolfSSL_ASN1_get_object),
  52216. TEST_DECL(test_openssl_generate_key_and_cert),
  52217. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  52218. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  52219. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  52220. /* test the no op functions for compatibility */
  52221. TEST_DECL(test_no_op_functions),
  52222. /* OpenSSL EVP_PKEY API tests */
  52223. TEST_DECL(test_EVP_PKEY_rsa),
  52224. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  52225. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  52226. TEST_DECL(test_EVP_PKEY_ec),
  52227. TEST_DECL(test_EVP_PKEY_cmp),
  52228. /* OpenSSL error API tests */
  52229. TEST_DECL(test_ERR_load_crypto_strings),
  52230. /* OpenSSL sk_X509 API test */
  52231. TEST_DECL(test_sk_X509),
  52232. /* OpenSSL sk_X509_CRL API test */
  52233. TEST_DECL(test_sk_X509_CRL),
  52234. /* OpenSSL X509 API test */
  52235. TEST_DECL(test_X509_get_signature_nid),
  52236. /* OpenSSL X509 REQ API test */
  52237. TEST_DECL(test_X509_REQ),
  52238. /* OpenSSL PKCS7 API test */
  52239. TEST_DECL(test_wolfssl_PKCS7),
  52240. TEST_DECL(test_wolfSSL_PKCS7_sign),
  52241. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  52242. #ifndef NO_BIO
  52243. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  52244. #ifdef HAVE_SMIME
  52245. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  52246. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  52247. #endif /* HAVE_SMIME */
  52248. #endif /* !NO_BIO */
  52249. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  52250. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  52251. TEST_DECL(test_X509_LOOKUP_add_dir),
  52252. /* wolfCrypt ASN tests */
  52253. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  52254. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  52255. TEST_DECL(test_wc_SetSubjectRaw),
  52256. TEST_DECL(test_wc_GetSubjectRaw),
  52257. TEST_DECL(test_wc_SetIssuerRaw),
  52258. TEST_DECL(test_wc_SetIssueBuffer),
  52259. TEST_DECL(test_wc_SetSubjectKeyId),
  52260. TEST_DECL(test_wc_SetSubject),
  52261. TEST_DECL(test_CheckCertSignature),
  52262. TEST_DECL(test_wc_ParseCert),
  52263. TEST_DECL(test_MakeCertWithPathLen),
  52264. /* wolfCrypt ECC tests */
  52265. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  52266. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  52267. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  52268. #ifdef WOLFSSL_TLS13
  52269. /* TLS v1.3 API tests */
  52270. TEST_DECL(test_tls13_apis),
  52271. TEST_DECL(test_tls13_cipher_suites),
  52272. #endif
  52273. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  52274. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  52275. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  52276. /* Bad certificate signature tests */
  52277. TEST_DECL(test_EccSigFailure_cm),
  52278. TEST_DECL(test_RsaSigFailure_cm),
  52279. #endif /* NO_CERTS */
  52280. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  52281. !defined(NO_OLD_TLS))
  52282. TEST_DECL(test_DhCallbacks),
  52283. #endif
  52284. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52285. TEST_DECL(test_export_keying_material),
  52286. #endif
  52287. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  52288. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52289. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52290. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52291. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  52292. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  52293. #endif
  52294. TEST_DECL(test_wolfSSL_security_level),
  52295. TEST_DECL(test_wolfSSL_SSL_in_init),
  52296. TEST_DECL(test_wolfSSL_EC_curve),
  52297. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  52298. TEST_DECL(test_wolfSSL_OpenSSL_version),
  52299. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  52300. TEST_DECL(test_CONF_CTX_FILE),
  52301. TEST_DECL(test_CONF_CTX_CMDLINE),
  52302. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  52303. /* wolfcrypt */
  52304. TEST_DECL(test_wolfCrypt_Init),
  52305. TEST_DECL(test_wc_InitMd5),
  52306. TEST_DECL(test_wc_Md5Update),
  52307. TEST_DECL(test_wc_Md5Final),
  52308. TEST_DECL(test_wc_InitSha),
  52309. TEST_DECL(test_wc_ShaUpdate),
  52310. TEST_DECL(test_wc_ShaFinal),
  52311. TEST_DECL(test_wc_InitSha256),
  52312. TEST_DECL(test_wc_Sha256Update),
  52313. TEST_DECL(test_wc_Sha256Final),
  52314. TEST_DECL(test_wc_Sha256FinalRaw),
  52315. TEST_DECL(test_wc_Sha256GetFlags),
  52316. TEST_DECL(test_wc_Sha256Free),
  52317. TEST_DECL(test_wc_Sha256GetHash),
  52318. TEST_DECL(test_wc_Sha256Copy),
  52319. TEST_DECL(test_wc_InitSha512),
  52320. TEST_DECL(test_wc_Sha512Update),
  52321. TEST_DECL(test_wc_Sha512Final),
  52322. TEST_DECL(test_wc_Sha512GetFlags),
  52323. TEST_DECL(test_wc_Sha512FinalRaw),
  52324. TEST_DECL(test_wc_Sha512Free),
  52325. TEST_DECL(test_wc_Sha512GetHash),
  52326. TEST_DECL(test_wc_Sha512Copy),
  52327. TEST_DECL(test_wc_InitSha512_224),
  52328. TEST_DECL(test_wc_Sha512_224Update),
  52329. TEST_DECL(test_wc_Sha512_224Final),
  52330. TEST_DECL(test_wc_Sha512_224GetFlags),
  52331. TEST_DECL(test_wc_Sha512_224FinalRaw),
  52332. TEST_DECL(test_wc_Sha512_224Free),
  52333. TEST_DECL(test_wc_Sha512_224GetHash),
  52334. TEST_DECL(test_wc_Sha512_224Copy),
  52335. TEST_DECL(test_wc_InitSha512_256),
  52336. TEST_DECL(test_wc_Sha512_256Update),
  52337. TEST_DECL(test_wc_Sha512_256Final),
  52338. TEST_DECL(test_wc_Sha512_256GetFlags),
  52339. TEST_DECL(test_wc_Sha512_256FinalRaw),
  52340. TEST_DECL(test_wc_Sha512_256Free),
  52341. TEST_DECL(test_wc_Sha512_256GetHash),
  52342. TEST_DECL(test_wc_Sha512_256Copy),
  52343. TEST_DECL(test_wc_InitSha384),
  52344. TEST_DECL(test_wc_Sha384Update),
  52345. TEST_DECL(test_wc_Sha384Final),
  52346. TEST_DECL(test_wc_Sha384GetFlags),
  52347. TEST_DECL(test_wc_Sha384FinalRaw),
  52348. TEST_DECL(test_wc_Sha384Free),
  52349. TEST_DECL(test_wc_Sha384GetHash),
  52350. TEST_DECL(test_wc_Sha384Copy),
  52351. TEST_DECL(test_wc_InitSha224),
  52352. TEST_DECL(test_wc_Sha224Update),
  52353. TEST_DECL(test_wc_Sha224Final),
  52354. TEST_DECL(test_wc_Sha224SetFlags),
  52355. TEST_DECL(test_wc_Sha224GetFlags),
  52356. TEST_DECL(test_wc_Sha224Free),
  52357. TEST_DECL(test_wc_Sha224GetHash),
  52358. TEST_DECL(test_wc_Sha224Copy),
  52359. TEST_DECL(test_wc_InitBlake2b),
  52360. TEST_DECL(test_wc_InitBlake2b_WithKey),
  52361. TEST_DECL(test_wc_InitBlake2s_WithKey),
  52362. TEST_DECL(test_wc_InitRipeMd),
  52363. TEST_DECL(test_wc_RipeMdUpdate),
  52364. TEST_DECL(test_wc_RipeMdFinal),
  52365. TEST_DECL(test_wc_InitSha3),
  52366. TEST_DECL(testing_wc_Sha3_Update),
  52367. TEST_DECL(test_wc_Sha3_224_Final),
  52368. TEST_DECL(test_wc_Sha3_256_Final),
  52369. TEST_DECL(test_wc_Sha3_384_Final),
  52370. TEST_DECL(test_wc_Sha3_512_Final),
  52371. TEST_DECL(test_wc_Sha3_224_Copy),
  52372. TEST_DECL(test_wc_Sha3_256_Copy),
  52373. TEST_DECL(test_wc_Sha3_384_Copy),
  52374. TEST_DECL(test_wc_Sha3_512_Copy),
  52375. TEST_DECL(test_wc_Sha3_GetFlags),
  52376. TEST_DECL(test_wc_InitShake256),
  52377. TEST_DECL(testing_wc_Shake256_Update),
  52378. TEST_DECL(test_wc_Shake256_Final),
  52379. TEST_DECL(test_wc_Shake256_Copy),
  52380. TEST_DECL(test_wc_Shake256Hash),
  52381. TEST_DECL(test_wc_Md5HmacSetKey),
  52382. TEST_DECL(test_wc_Md5HmacUpdate),
  52383. TEST_DECL(test_wc_Md5HmacFinal),
  52384. TEST_DECL(test_wc_ShaHmacSetKey),
  52385. TEST_DECL(test_wc_ShaHmacUpdate),
  52386. TEST_DECL(test_wc_ShaHmacFinal),
  52387. TEST_DECL(test_wc_Sha224HmacSetKey),
  52388. TEST_DECL(test_wc_Sha224HmacUpdate),
  52389. TEST_DECL(test_wc_Sha224HmacFinal),
  52390. TEST_DECL(test_wc_Sha256HmacSetKey),
  52391. TEST_DECL(test_wc_Sha256HmacUpdate),
  52392. TEST_DECL(test_wc_Sha256HmacFinal),
  52393. TEST_DECL(test_wc_Sha384HmacSetKey),
  52394. TEST_DECL(test_wc_Sha384HmacUpdate),
  52395. TEST_DECL(test_wc_Sha384HmacFinal),
  52396. TEST_DECL(test_wc_HashInit),
  52397. TEST_DECL(test_wc_HashSetFlags),
  52398. TEST_DECL(test_wc_HashGetFlags),
  52399. TEST_DECL(test_wc_InitCmac),
  52400. TEST_DECL(test_wc_CmacUpdate),
  52401. TEST_DECL(test_wc_CmacFinal),
  52402. TEST_DECL(test_wc_AesCmacGenerate),
  52403. TEST_DECL(test_wc_AesGcmStream),
  52404. TEST_DECL(test_wc_Des3_SetIV),
  52405. TEST_DECL(test_wc_Des3_SetKey),
  52406. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  52407. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  52408. TEST_DECL(test_wc_Des3_EcbEncrypt),
  52409. TEST_DECL(test_wc_Chacha_SetKey),
  52410. TEST_DECL(test_wc_Chacha_Process),
  52411. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  52412. TEST_DECL(test_wc_Poly1305SetKey),
  52413. TEST_DECL(test_wc_CamelliaSetKey),
  52414. TEST_DECL(test_wc_CamelliaSetIV),
  52415. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  52416. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  52417. TEST_DECL(test_wc_Arc4SetKey),
  52418. TEST_DECL(test_wc_Arc4Process),
  52419. TEST_DECL(test_wc_Rc2SetKey),
  52420. TEST_DECL(test_wc_Rc2SetIV),
  52421. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  52422. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  52423. TEST_DECL(test_wc_AesSetKey),
  52424. TEST_DECL(test_wc_AesSetIV),
  52425. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  52426. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  52427. TEST_DECL(test_wc_AesGcmSetKey),
  52428. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  52429. TEST_DECL(test_wc_GmacSetKey),
  52430. TEST_DECL(test_wc_GmacUpdate),
  52431. TEST_DECL(test_wc_InitRsaKey),
  52432. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  52433. TEST_DECL(test_wc_RsaPublicKeyDecode),
  52434. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  52435. TEST_DECL(test_wc_MakeRsaKey),
  52436. TEST_DECL(test_wc_SetKeyUsage),
  52437. TEST_DECL(test_wc_CheckProbablePrime),
  52438. TEST_DECL(test_wc_RsaPSS_Verify),
  52439. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  52440. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  52441. TEST_DECL(test_wc_SetMutexCb),
  52442. TEST_DECL(test_wc_LockMutex_ex),
  52443. TEST_DECL(test_wc_RsaKeyToDer),
  52444. TEST_DECL(test_wc_RsaKeyToPublicDer),
  52445. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  52446. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  52447. TEST_DECL(test_wc_RsaEncryptSize),
  52448. TEST_DECL(test_wc_RsaSSL_SignVerify),
  52449. TEST_DECL(test_wc_RsaFlattenPublicKey),
  52450. TEST_DECL(test_RsaDecryptBoundsCheck),
  52451. TEST_DECL(test_wc_AesCcmSetKey),
  52452. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  52453. TEST_DECL(test_wc_InitDsaKey),
  52454. TEST_DECL(test_wc_DsaSignVerify),
  52455. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  52456. TEST_DECL(test_wc_MakeDsaKey),
  52457. TEST_DECL(test_wc_DsaKeyToDer),
  52458. TEST_DECL(test_wc_DsaKeyToPublicDer),
  52459. TEST_DECL(test_wc_DsaImportParamsRaw),
  52460. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  52461. TEST_DECL(test_wc_DsaExportParamsRaw),
  52462. TEST_DECL(test_wc_DsaExportKeyRaw),
  52463. TEST_DECL(test_wc_SignatureGetSize_ecc),
  52464. TEST_DECL(test_wc_SignatureGetSize_rsa),
  52465. /*
  52466. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  52467. * avoid memory leaks.
  52468. */
  52469. TEST_DECL(test_wolfCrypt_Cleanup),
  52470. #ifdef OPENSSL_EXTRA
  52471. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  52472. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  52473. TEST_DECL(test_wolfSSL_EC),
  52474. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  52475. TEST_DECL(test_ECDSA_size_sign),
  52476. TEST_DECL(test_ED25519),
  52477. TEST_DECL(test_ED448),
  52478. TEST_DECL(test_EC_i2d),
  52479. #endif
  52480. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  52481. !defined(HAVE_SELFTEST) && \
  52482. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  52483. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  52484. #endif
  52485. #ifdef HAVE_HASHDRBG
  52486. #ifdef TEST_RESEED_INTERVAL
  52487. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  52488. #endif
  52489. TEST_DECL(test_wc_RNG_GenerateBlock),
  52490. #endif
  52491. TEST_DECL(test_get_rand_digit),
  52492. TEST_DECL(test_get_digit_count),
  52493. TEST_DECL(test_mp_cond_copy),
  52494. TEST_DECL(test_mp_rand),
  52495. TEST_DECL(test_get_digit),
  52496. TEST_DECL(test_wc_export_int),
  52497. TEST_DECL(test_wc_InitRngNonce),
  52498. TEST_DECL(test_wc_InitRngNonce_ex),
  52499. TEST_DECL(test_wc_ed25519_make_key),
  52500. TEST_DECL(test_wc_ed25519_init),
  52501. TEST_DECL(test_wc_ed25519_sign_msg),
  52502. TEST_DECL(test_wc_ed25519_import_public),
  52503. TEST_DECL(test_wc_ed25519_import_private_key),
  52504. TEST_DECL(test_wc_ed25519_export),
  52505. TEST_DECL(test_wc_ed25519_size),
  52506. TEST_DECL(test_wc_ed25519_exportKey),
  52507. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  52508. TEST_DECL(test_wc_curve25519_init),
  52509. TEST_DECL(test_wc_curve25519_size),
  52510. TEST_DECL(test_wc_curve25519_export_key_raw),
  52511. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  52512. TEST_DECL(test_wc_curve25519_make_key),
  52513. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  52514. TEST_DECL(test_wc_curve25519_make_pub),
  52515. TEST_DECL(test_wc_curve25519_export_public_ex),
  52516. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  52517. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  52518. TEST_DECL(test_wc_curve25519_import_private),
  52519. TEST_DECL(test_wc_ed448_make_key),
  52520. TEST_DECL(test_wc_ed448_init),
  52521. TEST_DECL(test_wc_ed448_sign_msg),
  52522. TEST_DECL(test_wc_ed448_import_public),
  52523. TEST_DECL(test_wc_ed448_import_private_key),
  52524. TEST_DECL(test_wc_ed448_export),
  52525. TEST_DECL(test_wc_ed448_size),
  52526. TEST_DECL(test_wc_ed448_exportKey),
  52527. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  52528. TEST_DECL(test_wc_curve448_make_key),
  52529. TEST_DECL(test_wc_curve448_shared_secret_ex),
  52530. TEST_DECL(test_wc_curve448_export_public_ex),
  52531. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  52532. TEST_DECL(test_wc_curve448_export_key_raw),
  52533. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  52534. TEST_DECL(test_wc_curve448_import_private),
  52535. TEST_DECL(test_wc_curve448_init),
  52536. TEST_DECL(test_wc_curve448_size),
  52537. TEST_DECL(test_wc_ecc_make_key),
  52538. TEST_DECL(test_wc_ecc_init),
  52539. TEST_DECL(test_wc_ecc_check_key),
  52540. TEST_DECL(test_wc_ecc_get_generator),
  52541. TEST_DECL(test_wc_ecc_size),
  52542. TEST_DECL(test_wc_ecc_params),
  52543. TEST_DECL(test_wc_ecc_signVerify_hash),
  52544. TEST_DECL(test_wc_ecc_shared_secret),
  52545. TEST_DECL(test_wc_ecc_export_x963),
  52546. TEST_DECL(test_wc_ecc_export_x963_ex),
  52547. TEST_DECL(test_wc_ecc_import_x963),
  52548. TEST_DECL(ecc_import_private_key),
  52549. TEST_DECL(test_wc_ecc_export_private_only),
  52550. TEST_DECL(test_wc_ecc_rs_to_sig),
  52551. TEST_DECL(test_wc_ecc_import_raw),
  52552. TEST_DECL(test_wc_ecc_import_unsigned),
  52553. TEST_DECL(test_wc_ecc_sig_size),
  52554. TEST_DECL(test_wc_ecc_ctx_new),
  52555. TEST_DECL(test_wc_ecc_ctx_reset),
  52556. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  52557. TEST_DECL(test_wc_ecc_ctx_set_info),
  52558. TEST_DECL(test_wc_ecc_encryptDecrypt),
  52559. TEST_DECL(test_wc_ecc_del_point),
  52560. TEST_DECL(test_wc_ecc_pointFns),
  52561. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  52562. TEST_DECL(test_wc_ecc_verify_hash_ex),
  52563. TEST_DECL(test_wc_ecc_mulmod),
  52564. TEST_DECL(test_wc_ecc_is_valid_idx),
  52565. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  52566. TEST_DECL(test_wc_ecc_sig_size_calc),
  52567. TEST_DECL(test_ToTraditional),
  52568. TEST_DECL(test_wc_EccPrivateKeyToDer),
  52569. TEST_DECL(test_wc_DhPublicKeyDecode),
  52570. TEST_DECL(test_wc_Ed25519KeyToDer),
  52571. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  52572. TEST_DECL(test_wc_Ed448KeyToDer),
  52573. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  52574. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  52575. TEST_DECL(test_wc_SetSubjectBuffer),
  52576. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  52577. TEST_DECL(test_wc_PKCS7_New),
  52578. TEST_DECL(test_wc_PKCS7_Init),
  52579. TEST_DECL(test_wc_PKCS7_InitWithCert),
  52580. TEST_DECL(test_wc_PKCS7_EncodeData),
  52581. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  52582. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  52583. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  52584. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  52585. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  52586. TEST_DECL(test_wc_PKCS7_Degenerate),
  52587. TEST_DECL(test_wc_PKCS7_BER),
  52588. TEST_DECL(test_PKCS7_signed_enveloped),
  52589. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  52590. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  52591. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  52592. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  52593. TEST_DECL(test_wc_i2d_PKCS12),
  52594. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  52595. TEST_DECL(test_openssl_FIPS_drbg),
  52596. TEST_DECL(test_wc_CryptoCb),
  52597. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  52598. TEST_DECL(test_wolfSSL_FIPS_mode),
  52599. #ifdef WOLFSSL_DTLS
  52600. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  52601. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  52602. #endif
  52603. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  52604. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  52605. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  52606. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  52607. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  52608. #ifdef HAVE_MAX_FRAGMENT
  52609. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  52610. #endif /* HAVE_MAX_FRAGMENT */
  52611. TEST_DECL(test_wolfSSL_dtls_stateless2),
  52612. #if !defined(NO_OLD_TLS)
  52613. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  52614. #endif /* !defined(NO_OLD_TLS) */
  52615. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  52616. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  52617. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  52618. TEST_DECL(test_ForceZero),
  52619. TEST_DECL(test_wolfSSL_Cleanup),
  52620. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  52621. && defined(WOLFSSL_TLS13) && \
  52622. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  52623. TEST_DECL(test_ticket_nonce_malloc),
  52624. #endif
  52625. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  52626. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  52627. !defined(WOLFSSL_NO_CLIENT_AUTH))
  52628. TEST_DECL(test_various_pathlen_chains),
  52629. #endif
  52630. /* If at some point a stub get implemented this test should fail indicating
  52631. * a need to implement a new test case
  52632. */
  52633. TEST_DECL(test_stubs_are_stubs)
  52634. };
  52635. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  52636. static void TestSetup(void)
  52637. {
  52638. /* Stub, for now. Add common test setup code here. */
  52639. }
  52640. static void TestCleanup(void)
  52641. {
  52642. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  52643. /* Clear any errors added to the error queue during the test run. */
  52644. wolfSSL_ERR_clear_error();
  52645. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  52646. }
  52647. /* Print out all API test cases with numeric identifier.
  52648. */
  52649. void ApiTest_PrintTestCases(void)
  52650. {
  52651. int i;
  52652. printf("All Test Cases:");
  52653. for (i = 0; i < TEST_CASE_CNT; i++) {
  52654. printf("%3d: %s\n", i + 1, testCases[i].name);
  52655. }
  52656. }
  52657. /* Add test case with index to the list to run.
  52658. *
  52659. * @param [in] idx Index of test case to run starting at 1.
  52660. * @return 0 on success.
  52661. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  52662. */
  52663. int ApiTest_RunIdx(int idx)
  52664. {
  52665. if (idx < 1 || idx > TEST_CASE_CNT) {
  52666. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  52667. return BAD_FUNC_ARG;
  52668. }
  52669. testAll = 0;
  52670. testCases[idx-1].run = 1;
  52671. return 0;
  52672. }
  52673. /* Add test case with name to the list to run.
  52674. *
  52675. * @param [in] name Name of test case to run.
  52676. * @return 0 on success.
  52677. * @return BAD_FUNC_ARG when name is not a known test case name.
  52678. */
  52679. int ApiTest_RunName(char* name)
  52680. {
  52681. int i;
  52682. for (i = 0; i < TEST_CASE_CNT; i++) {
  52683. if (XSTRCMP(testCases[i].name, name) == 0) {
  52684. testAll = 0;
  52685. testCases[i].run = 1;
  52686. return 0;
  52687. }
  52688. }
  52689. printf("Test case name not found: %s\n", name);
  52690. printf("Use -list to see all test case names.\n");
  52691. return BAD_FUNC_ARG;
  52692. }
  52693. /* Converts the result code to a string.
  52694. *
  52695. * @param [in] res Test result code.
  52696. * @return String describing test result.
  52697. */
  52698. static const char* apitest_res_string(int res)
  52699. {
  52700. const char* str = "invalid result";
  52701. switch (res) {
  52702. case TEST_SUCCESS:
  52703. str = "passed";
  52704. break;
  52705. case TEST_FAIL:
  52706. str = "failed";
  52707. break;
  52708. case TEST_SKIPPED:
  52709. str = "skipped";
  52710. break;
  52711. }
  52712. return str;
  52713. }
  52714. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52715. static double gettime_secs(void)
  52716. {
  52717. struct timeval tv;
  52718. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  52719. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  52720. }
  52721. #endif
  52722. void ApiTest(void)
  52723. {
  52724. int i;
  52725. int ret;
  52726. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52727. double timeDiff;
  52728. #endif
  52729. printf(" Begin API Tests\n");
  52730. fflush(stdout);
  52731. for (i = 0; i < TEST_CASE_CNT; ++i) {
  52732. /* When not testing all cases then skip if not marked for running. */
  52733. if (!testAll && !testCases[i].run) {
  52734. continue;
  52735. }
  52736. TestSetup();
  52737. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  52738. fflush(stdout);
  52739. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52740. timeDiff = gettime_secs();
  52741. #endif
  52742. ret = testCases[i].func();
  52743. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52744. timeDiff = gettime_secs() - timeDiff;
  52745. #endif
  52746. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  52747. if (ret != TEST_SKIPPED) {
  52748. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  52749. }
  52750. else
  52751. #endif
  52752. {
  52753. printf(" %s\n", apitest_res_string(ret));
  52754. }
  52755. fflush(stdout);
  52756. AssertIntNE(ret, TEST_FAIL);
  52757. TestCleanup();
  52758. }
  52759. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  52760. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  52761. wc_ecc_fp_free(); /* free per thread cache */
  52762. #endif
  52763. wolfSSL_Cleanup();
  52764. (void)testDevId;
  52765. printf(" End API Tests\n");
  52766. fflush(stdout);
  52767. }