api.c 2.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883
  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/ecdh.h>
  273. #include <wolfssl/openssl/engine.h>
  274. #include <wolfssl/openssl/hmac.h>
  275. #include <wolfssl/openssl/objects.h>
  276. #include <wolfssl/openssl/rand.h>
  277. #include <wolfssl/openssl/modes.h>
  278. #include <wolfssl/openssl/fips_rand.h>
  279. #include <wolfssl/openssl/kdf.h>
  280. #ifdef OPENSSL_ALL
  281. #include <wolfssl/openssl/txt_db.h>
  282. #include <wolfssl/openssl/lhash.h>
  283. #endif
  284. #ifndef NO_AES
  285. #include <wolfssl/openssl/aes.h>
  286. #endif
  287. #ifndef NO_DES3
  288. #include <wolfssl/openssl/des.h>
  289. #endif
  290. #ifdef HAVE_ECC
  291. #include <wolfssl/openssl/ecdsa.h>
  292. #endif
  293. #ifdef HAVE_PKCS7
  294. #include <wolfssl/openssl/pkcs7.h>
  295. #endif
  296. #ifdef HAVE_ED25519
  297. #include <wolfssl/openssl/ed25519.h>
  298. #endif
  299. #ifdef HAVE_ED448
  300. #include <wolfssl/openssl/ed448.h>
  301. #endif
  302. #endif /* OPENSSL_EXTRA */
  303. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  304. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  305. #include <wolfssl/wolfcrypt/srp.h>
  306. #endif
  307. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  308. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  309. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  310. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  311. defined(HAVE_ECH) || defined(HAVE_EX_DATA)
  312. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  313. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  314. * number tracking */
  315. #include "wolfssl/internal.h"
  316. #endif
  317. /* force enable test buffers */
  318. #ifndef USE_CERT_BUFFERS_2048
  319. #define USE_CERT_BUFFERS_2048
  320. #endif
  321. #ifndef USE_CERT_BUFFERS_256
  322. #define USE_CERT_BUFFERS_256
  323. #endif
  324. #include <wolfssl/certs_test.h>
  325. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  326. /* FIPS build has replaced ecc.h. */
  327. #define wc_ecc_key_get_priv(key) (&((key)->k))
  328. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  329. #endif
  330. typedef struct testVector {
  331. const char* input;
  332. const char* output;
  333. size_t inLen;
  334. size_t outLen;
  335. } testVector;
  336. #if defined(HAVE_PKCS7)
  337. typedef struct {
  338. const byte* content;
  339. word32 contentSz;
  340. int contentOID;
  341. int encryptOID;
  342. int keyWrapOID;
  343. int keyAgreeOID;
  344. byte* cert;
  345. size_t certSz;
  346. byte* privateKey;
  347. word32 privateKeySz;
  348. } pkcs7EnvelopedVector;
  349. #ifndef NO_PKCS7_ENCRYPTED_DATA
  350. typedef struct {
  351. const byte* content;
  352. word32 contentSz;
  353. int contentOID;
  354. int encryptOID;
  355. byte* encryptionKey;
  356. word32 encryptionKeySz;
  357. } pkcs7EncryptedVector;
  358. #endif
  359. #endif /* HAVE_PKCS7 */
  360. /*----------------------------------------------------------------------------*
  361. | Constants
  362. *----------------------------------------------------------------------------*/
  363. /* Test result constants and macros. */
  364. /* Test succeeded. */
  365. #define TEST_SUCCESS (1)
  366. /* Test failed. */
  367. #define TEST_FAIL (0)
  368. /* Test skipped - not run. */
  369. #define TEST_SKIPPED (-7777)
  370. /* Returns the result based on whether check is true.
  371. *
  372. * @param [in] check Condition for success.
  373. * @return When condition is true: TEST_SUCCESS.
  374. * @return When condition is false: TEST_FAIL.
  375. */
  376. #ifdef DEBUG_WOLFSSL_VERBOSE
  377. #define XSTRINGIFY(s) STRINGIFY(s)
  378. #define STRINGIFY(s) #s
  379. #define TEST_RES_CHECK(check) ({ \
  380. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  381. if (_ret == TEST_FAIL) { \
  382. fprintf(stderr, " check \"%s\" at %d ", \
  383. XSTRINGIFY(check), __LINE__); \
  384. } \
  385. _ret; })
  386. #else
  387. #define TEST_RES_CHECK(check) \
  388. ((check) ? TEST_SUCCESS : TEST_FAIL)
  389. #endif /* DEBUG_WOLFSSL_VERBOSE */
  390. #define TEST_STRING "Everyone gets Friday off."
  391. #define TEST_STRING_SZ 25
  392. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  393. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  394. #define TEST_RSA_BITS 1024
  395. #else
  396. #define TEST_RSA_BITS 2048
  397. #endif
  398. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  399. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  400. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  401. static const char* bogusFile =
  402. #ifdef _WIN32
  403. "NUL"
  404. #else
  405. "/dev/null"
  406. #endif
  407. ;
  408. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  409. enum {
  410. TESTING_RSA = 1,
  411. TESTING_ECC = 2
  412. };
  413. #ifdef WOLFSSL_QNX_CAAM
  414. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  415. static int testDevId = WOLFSSL_CAAM_DEVID;
  416. #else
  417. static int testDevId = INVALID_DEVID;
  418. #endif
  419. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  420. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  421. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  422. #define HAVE_IO_TESTS_DEPENDENCIES
  423. #endif
  424. /*----------------------------------------------------------------------------*
  425. | BIO with fixed read/write size
  426. *----------------------------------------------------------------------------*/
  427. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  428. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  429. int len)
  430. {
  431. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  432. len = 0;
  433. }
  434. else {
  435. if (bio->wrSz - bio->wrIdx < len) {
  436. len = bio->wrSz - bio->wrIdx;
  437. }
  438. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  439. bio->wrIdx += len;
  440. }
  441. return len;
  442. }
  443. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  444. {
  445. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  446. len = 0;
  447. }
  448. else {
  449. if (bio->wrSz - bio->rdIdx < len) {
  450. len = bio->wrSz - bio->rdIdx;
  451. }
  452. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  453. bio->rdIdx += len;
  454. }
  455. return len;
  456. }
  457. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  458. {
  459. static WOLFSSL_BIO_METHOD meth;
  460. meth.type = WOLFSSL_BIO_BIO;
  461. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  462. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  463. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  464. return &meth;
  465. }
  466. #endif
  467. /*----------------------------------------------------------------------------*
  468. | Setup
  469. *----------------------------------------------------------------------------*/
  470. static int test_wolfSSL_Init(void)
  471. {
  472. int result;
  473. result = wolfSSL_Init();
  474. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  475. return result;
  476. }
  477. static int test_wolfSSL_Cleanup(void)
  478. {
  479. int result;
  480. result = wolfSSL_Cleanup();
  481. result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
  482. return result;
  483. }
  484. /* Initialize the wolfCrypt state.
  485. * POST: 0 success.
  486. */
  487. static int test_wolfCrypt_Init(void)
  488. {
  489. int result;
  490. result = wolfCrypt_Init();
  491. result = TEST_RES_CHECK(result == 0);
  492. return result;
  493. } /* END test_wolfCrypt_Init */
  494. static int test_wolfCrypt_Cleanup(void)
  495. {
  496. int result;
  497. result = wolfCrypt_Cleanup();
  498. result = TEST_RES_CHECK(result == 0);
  499. return result;
  500. }
  501. /*----------------------------------------------------------------------------*
  502. | Platform dependent function test
  503. *----------------------------------------------------------------------------*/
  504. static int test_fileAccess(void)
  505. {
  506. int res = TEST_SKIPPED;
  507. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  508. const char *fname[] = {
  509. svrCertFile, svrKeyFile, caCertFile,
  510. eccCertFile, eccKeyFile, eccRsaCertFile,
  511. cliCertFile, cliCertDerFile, cliKeyFile,
  512. dhParamFile,
  513. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  514. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  515. NULL
  516. };
  517. const char derfile[] = "./certs/server-cert.der";
  518. XFILE f;
  519. size_t sz;
  520. byte *buff;
  521. int i;
  522. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  523. for (i=0; fname[i] != NULL ; i++) {
  524. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  525. XFCLOSE(f);
  526. }
  527. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  528. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  529. sz = (size_t) XFTELL(f);
  530. AssertTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  531. AssertTrue(sz == sizeof_server_cert_der_2048);
  532. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  533. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  534. XMEMCMP(server_cert_der_2048, buff, sz);
  535. res = TEST_RES_CHECK(1);
  536. #endif
  537. return res;
  538. }
  539. /*----------------------------------------------------------------------------*
  540. | Method Allocators
  541. *----------------------------------------------------------------------------*/
  542. static int test_wolfSSL_Method_Allocators(void)
  543. {
  544. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  545. do { \
  546. WOLFSSL_METHOD *method; \
  547. condition(method = allocator()); \
  548. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  549. } while(0)
  550. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  551. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  552. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  553. TEST_METHOD_ALLOCATOR(a, AssertNull)
  554. #ifndef NO_OLD_TLS
  555. #ifdef WOLFSSL_ALLOW_SSLV3
  556. #ifndef NO_WOLFSSL_SERVER
  557. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  558. #endif
  559. #ifndef NO_WOLFSSL_CLIENT
  560. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  561. #endif
  562. #endif
  563. #ifdef WOLFSSL_ALLOW_TLSV10
  564. #ifndef NO_WOLFSSL_SERVER
  565. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  566. #endif
  567. #ifndef NO_WOLFSSL_CLIENT
  568. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  569. #endif
  570. #endif
  571. #ifndef NO_WOLFSSL_SERVER
  572. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  573. #endif
  574. #ifndef NO_WOLFSSL_CLIENT
  575. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  576. #endif
  577. #endif /* !NO_OLD_TLS */
  578. #ifndef WOLFSSL_NO_TLS12
  579. #ifndef NO_WOLFSSL_SERVER
  580. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  581. #endif
  582. #ifndef NO_WOLFSSL_CLIENT
  583. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  584. #endif
  585. #endif /* !WOLFSSL_NO_TLS12 */
  586. #ifdef WOLFSSL_TLS13
  587. #ifndef NO_WOLFSSL_SERVER
  588. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  589. #endif
  590. #ifndef NO_WOLFSSL_CLIENT
  591. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  592. #endif
  593. #endif /* WOLFSSL_TLS13 */
  594. #ifndef NO_WOLFSSL_SERVER
  595. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  596. #endif
  597. #ifndef NO_WOLFSSL_CLIENT
  598. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  599. #endif
  600. #ifdef WOLFSSL_DTLS
  601. #ifndef NO_OLD_TLS
  602. #ifndef NO_WOLFSSL_SERVER
  603. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  604. #endif
  605. #ifndef NO_WOLFSSL_CLIENT
  606. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  607. #endif
  608. #endif
  609. #ifndef WOLFSSL_NO_TLS12
  610. #ifndef NO_WOLFSSL_SERVER
  611. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  612. #endif
  613. #ifndef NO_WOLFSSL_CLIENT
  614. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  615. #endif
  616. #endif
  617. #endif /* WOLFSSL_DTLS */
  618. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  619. /* Stubs */
  620. #ifndef NO_WOLFSSL_SERVER
  621. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  622. #endif
  623. #ifndef NO_WOLFSSL_CLIENT
  624. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  625. #endif
  626. #endif
  627. /* Test Either Method (client or server) */
  628. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  629. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  630. #ifndef NO_OLD_TLS
  631. #ifdef WOLFSSL_ALLOW_TLSV10
  632. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  633. #endif
  634. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  635. #endif /* !NO_OLD_TLS */
  636. #ifndef WOLFSSL_NO_TLS12
  637. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  638. #endif /* !WOLFSSL_NO_TLS12 */
  639. #ifdef WOLFSSL_TLS13
  640. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  641. #endif /* WOLFSSL_TLS13 */
  642. #ifdef WOLFSSL_DTLS
  643. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  644. #ifndef NO_OLD_TLS
  645. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  646. #endif /* !NO_OLD_TLS */
  647. #ifndef WOLFSSL_NO_TLS12
  648. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  649. #endif /* !WOLFSSL_NO_TLS12 */
  650. #endif /* WOLFSSL_DTLS */
  651. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  652. return TEST_SUCCESS;
  653. }
  654. /*----------------------------------------------------------------------------*
  655. | Context
  656. *----------------------------------------------------------------------------*/
  657. #ifndef NO_WOLFSSL_SERVER
  658. static int test_wolfSSL_CTX_new(void)
  659. {
  660. WOLFSSL_CTX *ctx;
  661. WOLFSSL_METHOD* method;
  662. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  663. AssertNotNull(method = wolfSSLv23_server_method());
  664. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  665. wolfSSL_CTX_free(ctx);
  666. return TEST_RES_CHECK(1);
  667. }
  668. #endif
  669. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  670. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  671. static int test_for_double_Free(void)
  672. {
  673. WOLFSSL_CTX* ctx;
  674. WOLFSSL* ssl;
  675. int skipTest = 0;
  676. const char* testCertFile;
  677. const char* testKeyFile;
  678. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  679. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  680. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  681. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  682. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  683. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  684. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  685. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  686. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  687. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  688. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  689. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  690. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  691. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  692. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  693. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  694. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  695. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  696. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  697. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  698. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  699. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  700. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  701. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  702. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  703. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  704. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  705. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  706. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  707. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  708. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  709. #ifdef OPENSSL_EXTRA
  710. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  711. "!SRP:!kRSA:!aNULL:!eNULL";
  712. #endif
  713. #ifndef NO_RSA
  714. testCertFile = svrCertFile;
  715. testKeyFile = svrKeyFile;
  716. #elif defined(HAVE_ECC)
  717. testCertFile = eccCertFile;
  718. testKeyFile = eccKeyFile;
  719. #else
  720. skipTest = 1;
  721. #endif
  722. if (skipTest != 1) {
  723. #ifndef NO_WOLFSSL_SERVER
  724. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  725. AssertNotNull(ctx);
  726. #else
  727. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  728. AssertNotNull(ctx);
  729. #endif
  730. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  731. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  732. ssl = wolfSSL_new(ctx);
  733. AssertNotNull(ssl);
  734. /* First test freeing SSL, then CTX */
  735. wolfSSL_free(ssl);
  736. wolfSSL_CTX_free(ctx);
  737. #ifndef NO_WOLFSSL_CLIENT
  738. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  739. AssertNotNull(ctx);
  740. #else
  741. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  742. AssertNotNull(ctx);
  743. #endif
  744. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  745. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  746. ssl = wolfSSL_new(ctx);
  747. AssertNotNull(ssl);
  748. /* Next test freeing CTX then SSL */
  749. wolfSSL_CTX_free(ctx);
  750. wolfSSL_free(ssl);
  751. #ifndef NO_WOLFSSL_SERVER
  752. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  753. AssertNotNull(ctx);
  754. #else
  755. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  756. AssertNotNull(ctx);
  757. #endif
  758. /* Test setting ciphers at ctx level */
  759. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  760. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  761. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  762. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  763. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  764. /* only update TLSv13 suites */
  765. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  766. #endif
  767. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  768. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  769. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  770. /* only update pre-TLSv13 suites */
  771. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  772. #endif
  773. #ifdef OPENSSL_EXTRA
  774. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  775. #endif
  776. AssertNotNull(ssl = wolfSSL_new(ctx));
  777. wolfSSL_CTX_free(ctx);
  778. wolfSSL_free(ssl);
  779. #ifndef NO_WOLFSSL_CLIENT
  780. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  781. AssertNotNull(ctx);
  782. #else
  783. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  784. AssertNotNull(ctx);
  785. #endif
  786. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  787. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  788. ssl = wolfSSL_new(ctx);
  789. AssertNotNull(ssl);
  790. /* test setting ciphers at SSL level */
  791. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  792. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  793. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  794. /* only update TLSv13 suites */
  795. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  796. #endif
  797. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  798. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  799. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  800. /* only update pre-TLSv13 suites */
  801. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  802. #endif
  803. wolfSSL_CTX_free(ctx);
  804. wolfSSL_free(ssl);
  805. }
  806. return TEST_RES_CHECK(1);
  807. }
  808. #endif
  809. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  810. {
  811. int res = TEST_SKIPPED;
  812. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  813. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  814. (!defined(NO_RSA) || defined(HAVE_ECC))
  815. const char* testCertFile;
  816. const char* testKeyFile;
  817. WOLFSSL_CTX* ctx;
  818. WOLFSSL* ssl;
  819. const byte cipherList[] =
  820. {
  821. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  822. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  823. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  824. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  825. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  826. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  827. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  828. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  829. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  830. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  831. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  832. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  833. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  834. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  835. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  836. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  837. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  838. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  839. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  840. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  841. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  842. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  843. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  844. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  845. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  846. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  847. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  848. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  849. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  850. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  851. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  852. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  853. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  854. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  855. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  856. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  857. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  858. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  859. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  860. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  861. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  862. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  863. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  864. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  865. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  866. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  867. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  868. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  869. /* SHA256 */
  870. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  871. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  872. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  873. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  874. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  875. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  876. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  877. /* SHA384 */
  878. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  879. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  880. /* AES-GCM */
  881. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  882. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  883. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  884. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  885. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  886. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  887. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  888. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  889. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  890. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  891. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  892. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  893. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  894. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  895. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  896. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  897. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  898. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  899. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  900. * also, in some of the other AES-CCM suites
  901. * there will be second byte number conflicts
  902. * with non-ECC AES-GCM */
  903. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  904. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  905. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  906. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  907. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  908. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  909. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  910. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  911. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  912. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  913. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  914. /* Camellia */
  915. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  916. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  917. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  918. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  919. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  920. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  921. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  922. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  923. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  924. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  925. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  926. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  927. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  928. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  929. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  930. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  931. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  932. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  933. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  934. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  935. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  936. /* TLS v1.3 cipher suites */
  937. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  938. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  939. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  940. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  941. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  942. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  943. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  944. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  945. };
  946. #ifndef NO_RSA
  947. testCertFile = svrCertFile;
  948. testKeyFile = svrKeyFile;
  949. #elif defined(HAVE_ECC)
  950. testCertFile = eccCertFile;
  951. testKeyFile = eccKeyFile;
  952. #endif
  953. #ifndef NO_WOLFSSL_SERVER
  954. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  955. AssertNotNull(ctx);
  956. #else
  957. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  958. AssertNotNull(ctx);
  959. #endif
  960. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  961. sizeof(cipherList)));
  962. wolfSSL_CTX_free(ctx);
  963. #ifndef NO_WOLFSSL_SERVER
  964. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  965. AssertNotNull(ctx);
  966. #else
  967. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  968. AssertNotNull(ctx);
  969. #endif
  970. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  971. WOLFSSL_FILETYPE_PEM));
  972. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  973. WOLFSSL_FILETYPE_PEM));
  974. ssl = wolfSSL_new(ctx);
  975. AssertNotNull(ssl);
  976. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  977. sizeof(cipherList)));
  978. wolfSSL_free(ssl);
  979. wolfSSL_CTX_free(ctx);
  980. res = TEST_RES_CHECK(1);
  981. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  982. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  983. return res;
  984. }
  985. static int test_wolfSSL_CTX_use_certificate_file(void)
  986. {
  987. int res = TEST_SKIPPED;
  988. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  989. WOLFSSL_CTX *ctx;
  990. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  991. /* invalid context */
  992. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  993. WOLFSSL_FILETYPE_PEM));
  994. /* invalid cert file */
  995. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  996. WOLFSSL_FILETYPE_PEM));
  997. /* invalid cert type */
  998. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  999. #ifdef NO_RSA
  1000. /* rsa needed */
  1001. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  1002. #else
  1003. /* success */
  1004. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1005. #endif
  1006. wolfSSL_CTX_free(ctx);
  1007. res = TEST_RES_CHECK(1);
  1008. #endif
  1009. return res;
  1010. }
  1011. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1012. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1013. {
  1014. int res = TEST_SKIPPED;
  1015. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1016. WOLFSSL_CTX* ctx;
  1017. int ret;
  1018. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1019. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1020. server_cert_der_2048);
  1021. wolfSSL_CTX_free(ctx);
  1022. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1023. #endif
  1024. return res;
  1025. }
  1026. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1027. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1028. * context using buffer.
  1029. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1030. * --enable-testcert flag.
  1031. */
  1032. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1033. {
  1034. int res = TEST_SKIPPED;
  1035. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1036. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1037. WOLFSSL_CTX* ctx;
  1038. int ret;
  1039. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1040. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1041. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1042. wolfSSL_CTX_free(ctx);
  1043. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1044. #endif
  1045. return res;
  1046. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  1047. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1048. {
  1049. int res = TEST_SKIPPED;
  1050. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1051. WOLFSSL_CTX *ctx;
  1052. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1053. /* invalid context */
  1054. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1055. WOLFSSL_FILETYPE_PEM));
  1056. /* invalid key file */
  1057. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1058. WOLFSSL_FILETYPE_PEM));
  1059. /* invalid key type */
  1060. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1061. /* success */
  1062. #ifdef NO_RSA
  1063. /* rsa needed */
  1064. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1065. #else
  1066. /* success */
  1067. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1068. #endif
  1069. wolfSSL_CTX_free(ctx);
  1070. res = TEST_RES_CHECK(1);
  1071. #endif
  1072. return res;
  1073. }
  1074. /* test both file and buffer versions along with unloading trusted peer certs */
  1075. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1076. {
  1077. int res = TEST_SKIPPED;
  1078. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1079. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1080. WOLFSSL_CTX *ctx;
  1081. WOLFSSL* ssl;
  1082. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1083. AssertNotNull(ssl = wolfSSL_new(ctx));
  1084. #if !defined(NO_FILESYSTEM)
  1085. /* invalid file */
  1086. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1087. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1088. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1089. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1090. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1092. /* success */
  1093. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1094. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1095. /* unload cert */
  1096. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1097. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1098. /* invalid file */
  1099. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1100. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1101. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1102. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1103. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1104. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1105. /* success */
  1106. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1107. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1108. #ifdef WOLFSSL_LOCAL_X509_STORE
  1109. /* unload cert */
  1110. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1111. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1112. #endif
  1113. #endif
  1114. /* Test of loading certs from buffers */
  1115. /* invalid buffer */
  1116. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1117. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1118. /* success */
  1119. #ifdef USE_CERT_BUFFERS_1024
  1120. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1121. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1122. #endif
  1123. #ifdef USE_CERT_BUFFERS_2048
  1124. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1125. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1126. #endif
  1127. /* unload cert */
  1128. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1129. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1130. wolfSSL_free(ssl);
  1131. wolfSSL_CTX_free(ctx);
  1132. res = TEST_RES_CHECK(1);
  1133. #endif
  1134. return res;
  1135. }
  1136. static int test_wolfSSL_CTX_load_verify_locations(void)
  1137. {
  1138. int res = TEST_SKIPPED;
  1139. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1140. WOLFSSL_CTX *ctx;
  1141. #ifndef NO_RSA
  1142. WOLFSSL_CERT_MANAGER* cm;
  1143. #ifdef PERSIST_CERT_CACHE
  1144. int cacheSz;
  1145. #endif
  1146. #endif
  1147. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1148. const char* load_certs_path = "./certs/external";
  1149. const char* load_no_certs_path = "./examples";
  1150. const char* load_expired_path = "./certs/test/expired";
  1151. #endif
  1152. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1153. /* invalid arguments */
  1154. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1155. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1156. /* invalid ca file */
  1157. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1158. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1159. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1160. (defined(WOLFSSL_QT) && \
  1161. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1162. /* invalid path */
  1163. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1164. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1165. #endif
  1166. /* load ca cert */
  1167. #ifdef NO_RSA
  1168. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1169. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1170. #else /* Skip the following test without RSA certs. */
  1171. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1172. #ifdef PERSIST_CERT_CACHE
  1173. /* Get cert cache size */
  1174. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1175. #endif
  1176. /* Test unloading CA's */
  1177. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1178. #ifdef PERSIST_CERT_CACHE
  1179. /* Verify no certs (result is less than cacheSz) */
  1180. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1181. #endif
  1182. /* load ca cert again */
  1183. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1184. /* Test getting CERT_MANAGER */
  1185. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1186. /* Test unloading CA's using CM */
  1187. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1188. #ifdef PERSIST_CERT_CACHE
  1189. /* Verify no certs (result is less than cacheSz) */
  1190. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1191. #endif
  1192. #endif
  1193. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1194. /* Test loading CA certificates using a path */
  1195. #ifdef NO_RSA
  1196. /* failure here okay since certs in external directory are RSA */
  1197. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1198. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1199. #else
  1200. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1201. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1202. #endif
  1203. /* Test loading path with no files */
  1204. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1205. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1206. /* Test loading expired CA certificates */
  1207. #ifdef NO_RSA
  1208. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1209. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1210. WOLFSSL_SUCCESS);
  1211. #else
  1212. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1213. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1214. WOLFSSL_SUCCESS);
  1215. #endif
  1216. /* Test loading CA certificates and ignoring all errors */
  1217. #ifdef NO_RSA
  1218. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1219. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1220. #else
  1221. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1222. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1223. #endif
  1224. #endif
  1225. wolfSSL_CTX_free(ctx);
  1226. res = TEST_RES_CHECK(1);
  1227. #endif
  1228. return res;
  1229. }
  1230. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1231. {
  1232. int res = TEST_SKIPPED;
  1233. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1234. (!defined(NO_RSA) || defined(HAVE_ECC))
  1235. WOLFSSL_CTX* ctx;
  1236. byte dirValid = 0;
  1237. int ret = 0;
  1238. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1239. if (ctx == NULL) {
  1240. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1241. ret = -1;
  1242. }
  1243. if (ret == 0) {
  1244. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1245. dirValid = 1;
  1246. #else
  1247. word32 numDirs;
  1248. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1249. if (caDirs == NULL || numDirs == 0) {
  1250. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1251. ret = -1;
  1252. }
  1253. else {
  1254. ReadDirCtx dirCtx;
  1255. word32 i;
  1256. for (i = 0; i < numDirs; ++i) {
  1257. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1258. /* Directory isn't empty. */
  1259. dirValid = 1;
  1260. wc_ReadDirClose(&dirCtx);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. #endif
  1266. }
  1267. /*
  1268. * If the directory isn't empty, we should be able to load CA
  1269. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1270. * usable.
  1271. */
  1272. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1273. WOLFSSL_SUCCESS) {
  1274. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1275. ret = -1;
  1276. }
  1277. #ifdef OPENSSL_EXTRA
  1278. if (ret == 0 &&
  1279. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1280. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1281. ret = -1;
  1282. }
  1283. #endif /* OPENSSL_EXTRA */
  1284. wolfSSL_CTX_free(ctx);
  1285. res = TEST_RES_CHECK(ret == 0);
  1286. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1287. return res;
  1288. }
  1289. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1290. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1291. {
  1292. int ret;
  1293. WOLFSSL_CERT_MANAGER* cm;
  1294. cm = wolfSSL_CertManagerNew();
  1295. if (cm == NULL) {
  1296. fprintf(stderr, "test_cm_load_ca failed\n");
  1297. return -1;
  1298. }
  1299. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1300. wolfSSL_CertManagerFree(cm);
  1301. return ret;
  1302. }
  1303. static int test_cm_load_ca_file(const char* ca_cert_file)
  1304. {
  1305. int ret = 0;
  1306. byte* cert_buf = NULL;
  1307. size_t cert_sz = 0;
  1308. #if defined(WOLFSSL_PEM_TO_DER)
  1309. DerBuffer* pDer = NULL;
  1310. #endif
  1311. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1312. if (ret == 0) {
  1313. /* normal test */
  1314. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1315. if (ret == WOLFSSL_SUCCESS) {
  1316. /* test including null terminator in length */
  1317. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1318. if (tmp == NULL) {
  1319. ret = MEMORY_E;
  1320. }
  1321. else {
  1322. cert_buf = tmp;
  1323. cert_buf[cert_sz] = '\0';
  1324. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1325. WOLFSSL_FILETYPE_PEM);
  1326. }
  1327. }
  1328. #if defined(WOLFSSL_PEM_TO_DER)
  1329. if (ret == WOLFSSL_SUCCESS) {
  1330. /* test loading DER */
  1331. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1332. if (ret == 0 && pDer != NULL) {
  1333. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1334. WOLFSSL_FILETYPE_ASN1);
  1335. wc_FreeDer(&pDer);
  1336. }
  1337. }
  1338. #endif
  1339. }
  1340. free(cert_buf);
  1341. return ret;
  1342. }
  1343. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1344. int file_type, word32 flags)
  1345. {
  1346. int ret;
  1347. WOLFSSL_CERT_MANAGER* cm;
  1348. cm = wolfSSL_CertManagerNew();
  1349. if (cm == NULL) {
  1350. fprintf(stderr, "test_cm_load_ca failed\n");
  1351. return -1;
  1352. }
  1353. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1354. 0, flags);
  1355. wolfSSL_CertManagerFree(cm);
  1356. return ret;
  1357. }
  1358. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1359. {
  1360. int ret = 0;
  1361. byte* cert_buf = NULL;
  1362. size_t cert_sz = 0;
  1363. #if defined(WOLFSSL_PEM_TO_DER)
  1364. DerBuffer* pDer = NULL;
  1365. #endif
  1366. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1367. if (ret == 0) {
  1368. /* normal test */
  1369. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1370. WOLFSSL_FILETYPE_PEM, flags);
  1371. if (ret == WOLFSSL_SUCCESS) {
  1372. /* test including null terminator in length */
  1373. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1374. if (tmp == NULL) {
  1375. ret = MEMORY_E;
  1376. }
  1377. else {
  1378. cert_buf = tmp;
  1379. cert_buf[cert_sz] = '\0';
  1380. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1381. WOLFSSL_FILETYPE_PEM, flags);
  1382. }
  1383. }
  1384. #if defined(WOLFSSL_PEM_TO_DER)
  1385. if (ret == WOLFSSL_SUCCESS) {
  1386. /* test loading DER */
  1387. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1388. if (ret == 0 && pDer != NULL) {
  1389. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1390. WOLFSSL_FILETYPE_ASN1, flags);
  1391. wc_FreeDer(&pDer);
  1392. }
  1393. }
  1394. #endif
  1395. }
  1396. free(cert_buf);
  1397. return ret;
  1398. }
  1399. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1400. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1401. {
  1402. int res = TEST_SKIPPED;
  1403. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1404. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1405. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1406. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1407. defined(HAVE_LIGHTY)
  1408. WOLFSSL_CERT_MANAGER* cm = NULL;
  1409. /* Raw OCSP response bytes captured using the following setup:
  1410. * - Run responder with
  1411. * openssl ocsp -port 9999 -ndays 9999
  1412. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1413. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1414. * -rkey certs/ocsp/ocsp-responder-key.pem
  1415. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1416. * - Run client with
  1417. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1418. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1419. * -cert certs/ocsp/server1-cert.pem
  1420. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1421. * - Copy raw response from Wireshark.
  1422. */
  1423. byte response[] = {
  1424. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1425. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1426. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1427. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1428. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1429. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1430. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1431. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1432. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1433. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1434. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1435. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1436. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1437. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1438. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1439. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1440. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1441. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1442. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1443. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1444. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1445. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1446. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1447. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1448. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1449. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1450. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1451. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1452. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1453. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1454. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1455. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1456. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1457. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1458. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1459. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1460. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1461. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1462. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1463. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1464. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1465. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1466. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1467. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1468. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1469. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1470. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1471. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1472. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1473. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1474. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1475. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1476. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1477. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1478. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1479. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1480. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1481. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1482. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1483. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1484. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1485. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1486. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1487. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1488. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1489. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1490. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1491. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1492. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1493. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1494. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1495. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1496. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1497. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1498. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1499. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1500. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1501. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1502. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1503. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1504. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1505. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1506. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1507. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1508. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1509. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1510. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1511. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1512. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1513. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1514. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1515. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1516. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1517. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1518. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1519. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1520. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1521. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1522. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1523. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1524. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1525. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1526. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1527. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1528. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1529. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1530. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1531. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1532. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1533. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1534. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1535. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1536. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1537. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1538. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1539. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1540. 0x59, 0x59, 0xa0, 0x07
  1541. };
  1542. OcspEntry entry[1];
  1543. CertStatus status[1];
  1544. OcspRequest* request;
  1545. byte serial[] = {0x05};
  1546. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1547. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1548. XMEMSET(entry, 0, sizeof(OcspEntry));
  1549. XMEMSET(status, 0, sizeof(CertStatus));
  1550. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1551. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1552. DYNAMIC_TYPE_OCSP_REQUEST);
  1553. AssertNotNull(request->serial);
  1554. request->serialSz = sizeof(serial);
  1555. XMEMCPY(request->serial, serial, sizeof(serial));
  1556. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1557. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1558. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1559. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1560. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1561. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1562. /* Response should be valid. */
  1563. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1564. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1565. /* Flip a byte in the request serial number, response should be invalid
  1566. * now. */
  1567. request->serial[0] ^= request->serial[0];
  1568. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1569. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1570. wolfSSL_OCSP_REQUEST_free(request);
  1571. wolfSSL_CertManagerFree(cm);
  1572. res = TEST_RES_CHECK(1);
  1573. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1574. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1575. #endif /* HAVE_OCSP */
  1576. return res;
  1577. }
  1578. static int test_wolfSSL_CheckOCSPResponse(void)
  1579. {
  1580. int result = TEST_SKIPPED;
  1581. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1582. const char* responseFile = "./certs/ocsp/test-response.der";
  1583. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1584. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1585. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1586. OcspResponse* res = NULL;
  1587. byte data[4096];
  1588. const unsigned char* pt;
  1589. int dataSz;
  1590. XFILE f;
  1591. WOLFSSL_OCSP_BASICRESP* bs;
  1592. WOLFSSL_X509_STORE* st;
  1593. WOLFSSL_X509* issuer;
  1594. f = XFOPEN(responseFile, "rb");
  1595. AssertTrue(f != XBADFILE);
  1596. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1597. AssertIntGT(dataSz, 0);
  1598. XFCLOSE(f);
  1599. pt = data;
  1600. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1601. AssertNotNull(res);
  1602. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1603. AssertNotNull(issuer);
  1604. st = wolfSSL_X509_STORE_new();
  1605. AssertNotNull(st);
  1606. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1607. bs = wolfSSL_OCSP_response_get1_basic(res);
  1608. AssertNotNull(bs);
  1609. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1610. wolfSSL_OCSP_BASICRESP_free(bs);
  1611. wolfSSL_OCSP_RESPONSE_free(res);
  1612. wolfSSL_X509_STORE_free(st);
  1613. wolfSSL_X509_free(issuer);
  1614. /* check loading a response with optional certs */
  1615. f = XFOPEN(responseNoInternFile, "rb");
  1616. AssertTrue(f != XBADFILE);
  1617. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1618. AssertIntGT(dataSz, 0);
  1619. XFCLOSE(f);
  1620. pt = data;
  1621. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1622. AssertNotNull(res);
  1623. wolfSSL_OCSP_RESPONSE_free(res);
  1624. /* check loading a response with multiple certs */
  1625. {
  1626. WOLFSSL_CERT_MANAGER* cm = NULL;
  1627. OcspEntry *entry;
  1628. CertStatus* status;
  1629. OcspRequest* request;
  1630. byte serial1[] = {0x01};
  1631. byte serial[] = {0x02};
  1632. byte issuerHash[] = {
  1633. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1634. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1635. 0x37, 0x15, 0x8A, 0x88
  1636. };
  1637. byte issuerKeyHash[] = {
  1638. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1639. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1640. 0x7E, 0x72, 0x15, 0x21
  1641. };
  1642. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1643. DYNAMIC_TYPE_OPENSSL);
  1644. AssertNotNull(entry);
  1645. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1646. DYNAMIC_TYPE_OPENSSL);
  1647. AssertNotNull(status);
  1648. XMEMSET(entry, 0, sizeof(OcspEntry));
  1649. XMEMSET(status, 0, sizeof(CertStatus));
  1650. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1651. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1652. DYNAMIC_TYPE_OCSP_REQUEST);
  1653. AssertNotNull(request->serial);
  1654. request->serialSz = sizeof(serial);
  1655. XMEMCPY(request->serial, serial, sizeof(serial));
  1656. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1657. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1658. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1659. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1660. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1661. WOLFSSL_SUCCESS);
  1662. f = XFOPEN(responseMultiFile, "rb");
  1663. AssertTrue(f != XBADFILE);
  1664. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1665. AssertIntGT(dataSz, 0);
  1666. XFCLOSE(f);
  1667. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1668. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1669. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1670. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1671. AssertNotNull(entry->status);
  1672. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1673. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1674. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1675. /* store both status's in the entry to check that "next" is not
  1676. * overwritten */
  1677. status->next = entry->status;
  1678. entry->status = status;
  1679. XMEMCPY(request->serial, serial, sizeof(serial));
  1680. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1681. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1682. AssertNotNull(entry->status->next);
  1683. /* compare the status found */
  1684. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1685. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1686. status->serialSz), 0);
  1687. wolfSSL_OCSP_CERTID_free(entry);
  1688. wolfSSL_OCSP_REQUEST_free(request);
  1689. wolfSSL_CertManagerFree(cm);
  1690. }
  1691. #if defined(WC_RSA_PSS)
  1692. {
  1693. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1694. /* check loading a response with RSA-PSS signature */
  1695. f = XFOPEN(responsePssFile, "rb");
  1696. AssertTrue(f != XBADFILE);
  1697. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1698. AssertIntGT(dataSz, 0);
  1699. XFCLOSE(f);
  1700. pt = data;
  1701. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1702. AssertNotNull(res);
  1703. /* try to verify the response */
  1704. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1705. AssertNotNull(issuer);
  1706. st = wolfSSL_X509_STORE_new();
  1707. AssertNotNull(st);
  1708. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1709. bs = wolfSSL_OCSP_response_get1_basic(res);
  1710. AssertNotNull(bs);
  1711. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1712. wolfSSL_OCSP_BASICRESP_free(bs);
  1713. wolfSSL_OCSP_RESPONSE_free(res);
  1714. wolfSSL_X509_STORE_free(st);
  1715. wolfSSL_X509_free(issuer);
  1716. }
  1717. #endif
  1718. result = TEST_RES_CHECK(1);
  1719. #endif /* HAVE_OCSP */
  1720. return result;
  1721. }
  1722. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1723. {
  1724. int res = TEST_SKIPPED;
  1725. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1726. const char* ca_cert = "./certs/ca-cert.pem";
  1727. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1728. int ret;
  1729. ret = test_cm_load_ca_file(ca_cert);
  1730. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1731. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1732. #elif defined(NO_RSA)
  1733. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1734. #else
  1735. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1736. #endif
  1737. ret = test_cm_load_ca_file(ca_expired_cert);
  1738. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1739. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1740. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1741. #elif defined(NO_RSA)
  1742. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1743. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1744. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1745. !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  1746. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1747. res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
  1748. #else
  1749. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1750. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1751. #endif
  1752. #endif
  1753. return res;
  1754. }
  1755. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1756. {
  1757. int res = TEST_SKIPPED;
  1758. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1759. const char* ca_cert = "./certs/ca-cert.pem";
  1760. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1761. int ret;
  1762. ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE);
  1763. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1764. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1765. #elif defined(NO_RSA)
  1766. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1767. #else
  1768. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1769. #endif
  1770. ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1771. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
  1772. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1773. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1774. res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
  1775. #elif defined(NO_RSA)
  1776. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1777. res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
  1778. #else
  1779. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1780. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  1781. #endif
  1782. #endif
  1783. return res;
  1784. }
  1785. static int test_wolfSSL_CertManagerGetCerts(void)
  1786. {
  1787. int res = TEST_SKIPPED;
  1788. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1789. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1790. defined(WOLFSSL_SIGNER_DER_CERT)
  1791. WOLFSSL_CERT_MANAGER* cm = NULL;
  1792. WOLFSSL_STACK* sk = NULL;
  1793. X509* x509 = NULL;
  1794. X509* cert1 = NULL;
  1795. FILE* file1 = NULL;
  1796. #ifdef DEBUG_WOLFSSL_VERBOSE
  1797. WOLFSSL_BIO* bio = NULL;
  1798. #endif
  1799. int i = 0;
  1800. int ret = 0;
  1801. const byte* der;
  1802. int derSz = 0;
  1803. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1804. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1805. fclose(file1);
  1806. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1807. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1808. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1809. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1810. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1811. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1812. * and full OpenSSL compatibility */
  1813. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1814. #else
  1815. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1816. #endif
  1817. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1818. "./certs/ca-cert.pem", NULL));
  1819. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1820. for (i = 0; i < sk_X509_num(sk); i++) {
  1821. x509 = sk_X509_value(sk, i);
  1822. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1823. #ifdef DEBUG_WOLFSSL_VERBOSE
  1824. bio = BIO_new(wolfSSL_BIO_s_file());
  1825. if (bio != NULL) {
  1826. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1827. X509_print(bio, x509);
  1828. BIO_free(bio);
  1829. }
  1830. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1831. }
  1832. wolfSSL_X509_free(cert1);
  1833. sk_X509_pop_free(sk, NULL);
  1834. wolfSSL_CertManagerFree(cm);
  1835. res = TEST_RES_CHECK(1);
  1836. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1837. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1838. defined(WOLFSSL_SIGNER_DER_CERT) */
  1839. return res;
  1840. }
  1841. static int test_wolfSSL_CertManagerSetVerify(void)
  1842. {
  1843. int res = TEST_SKIPPED;
  1844. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1845. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1846. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1847. int ret = 0;
  1848. WOLFSSL_CERT_MANAGER* cm;
  1849. int tmp = myVerifyAction;
  1850. const char* ca_cert = "./certs/ca-cert.pem";
  1851. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1852. cm = wolfSSL_CertManagerNew();
  1853. AssertNotNull(cm);
  1854. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1855. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1856. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1857. AssertIntEQ(ret, -1);
  1858. #else
  1859. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1860. #endif
  1861. /* Use the test CB that always accepts certs */
  1862. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1863. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1864. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1865. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1866. {
  1867. const char* verifyCert = "./certs/server-cert.pem";
  1868. /* Use the test CB that always fails certs */
  1869. myVerifyAction = VERIFY_FORCE_FAIL;
  1870. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1871. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1872. }
  1873. #endif
  1874. wolfSSL_CertManagerFree(cm);
  1875. myVerifyAction = tmp;
  1876. res = TEST_RES_CHECK(1);
  1877. #endif
  1878. return res;
  1879. }
  1880. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1881. defined(DEBUG_UNIT_TEST_CERTS)
  1882. /* Used when debugging name constraint tests. Not static to allow use in
  1883. * multiple locations with complex define guards. */
  1884. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1885. {
  1886. BIO* out = BIO_new_file(fileName, "wb");
  1887. if (out != NULL) {
  1888. PEM_write_bio_X509(out, x509);
  1889. BIO_free(out);
  1890. }
  1891. }
  1892. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1893. {
  1894. BIO* out = BIO_new_file(fileName, "wb");
  1895. if (out != NULL) {
  1896. BIO_write(out, der, derSz);
  1897. BIO_free(out);
  1898. }
  1899. }
  1900. #else
  1901. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1902. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1903. #endif
  1904. static int test_wolfSSL_CertManagerNameConstraint(void)
  1905. {
  1906. int res = TEST_SKIPPED;
  1907. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1908. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1909. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1910. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1911. !defined(NO_SHA256)
  1912. WOLFSSL_CERT_MANAGER* cm;
  1913. WOLFSSL_EVP_PKEY *priv;
  1914. WOLFSSL_X509_NAME* name;
  1915. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1916. const char* server_cert = "./certs/test/server-goodcn.pem";
  1917. int i = 0;
  1918. static const byte extNameConsOid[] = {85, 29, 30};
  1919. RsaKey key;
  1920. WC_RNG rng;
  1921. byte *der;
  1922. int derSz;
  1923. word32 idx = 0;
  1924. byte *pt;
  1925. WOLFSSL_X509 *x509, *ca;
  1926. wc_InitRng(&rng);
  1927. /* load in CA private key for signing */
  1928. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1929. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1930. sizeof_server_key_der_2048), 0);
  1931. /* get ca certificate then alter it */
  1932. AssertNotNull(der =
  1933. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1934. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1935. WOLFSSL_FILETYPE_ASN1));
  1936. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1937. XMEMCPY(der, pt, derSz);
  1938. /* find the name constraint extension and alter it */
  1939. pt = der;
  1940. for (i = 0; i < derSz - 3; i++) {
  1941. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1942. pt += 3;
  1943. break;
  1944. }
  1945. pt++;
  1946. }
  1947. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1948. /* go to the length value and set it to 0 */
  1949. while (i < derSz && *pt != 0x81) {
  1950. pt++;
  1951. i++;
  1952. }
  1953. AssertIntNE(i, derSz); /* did not place to alter */
  1954. pt++;
  1955. *pt = 0x00;
  1956. /* resign the altered certificate */
  1957. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1958. FOURK_BUF, &key, NULL, &rng)), 0);
  1959. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1960. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1961. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1962. wolfSSL_CertManagerFree(cm);
  1963. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1964. wolfSSL_X509_free(x509);
  1965. wc_FreeRsaKey(&key);
  1966. wc_FreeRng(&rng);
  1967. /* add email alt name to satisfy constraint */
  1968. pt = (byte*)server_key_der_2048;
  1969. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1970. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1971. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1972. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1973. WOLFSSL_FILETYPE_ASN1));
  1974. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1975. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1976. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1977. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1978. /* Good cert test with proper alt email name */
  1979. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1980. WOLFSSL_FILETYPE_PEM));
  1981. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1982. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1983. AssertNotNull(name = X509_NAME_new());
  1984. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1985. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1986. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1987. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1988. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1989. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1990. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1991. X509_NAME_free(name);
  1992. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1993. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1994. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1995. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1996. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1997. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1998. wolfSSL_X509_free(x509);
  1999. /* Cert with bad alt name list */
  2000. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2001. WOLFSSL_FILETYPE_PEM));
  2002. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2003. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2004. AssertNotNull(name = X509_NAME_new());
  2005. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2006. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2007. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2008. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2009. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2010. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2011. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2012. X509_NAME_free(name);
  2013. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2014. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2015. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2016. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2017. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2018. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2019. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2020. wolfSSL_CertManagerFree(cm);
  2021. wolfSSL_X509_free(x509);
  2022. wolfSSL_X509_free(ca);
  2023. wolfSSL_EVP_PKEY_free(priv);
  2024. res = TEST_RES_CHECK(1);
  2025. #endif
  2026. return res;
  2027. }
  2028. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2029. {
  2030. int res = TEST_SKIPPED;
  2031. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2032. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2033. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2034. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2035. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2036. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2037. const char* server_cert = "./certs/server-cert.pem";
  2038. WOLFSSL_CERT_MANAGER* cm;
  2039. WOLFSSL_X509 *x509, *ca;
  2040. const unsigned char *der;
  2041. const unsigned char *pt;
  2042. WOLFSSL_EVP_PKEY *priv;
  2043. WOLFSSL_X509_NAME* name;
  2044. int derSz;
  2045. /* C=US*/
  2046. char altName[] = {
  2047. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2048. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2049. };
  2050. /* C=ID */
  2051. char altNameFail[] = {
  2052. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2053. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2054. };
  2055. /* C=US ST=California*/
  2056. char altNameExc[] = {
  2057. 0x30, 0x22,
  2058. 0x31, 0x0B,
  2059. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2060. 0x31, 0x13,
  2061. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2062. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2063. };
  2064. /* load in CA private key for signing */
  2065. pt = ca_key_der_2048;
  2066. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2067. sizeof_ca_key_der_2048));
  2068. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2069. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2070. WOLFSSL_FILETYPE_ASN1));
  2071. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2072. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2073. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2074. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2075. WOLFSSL_FILETYPE_PEM));
  2076. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2077. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2078. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2079. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2080. #else
  2081. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2082. #endif
  2083. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2084. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2085. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2086. /* add in matching DIR alt name and resign */
  2087. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2088. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2089. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2090. #else
  2091. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2092. #endif
  2093. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2094. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2095. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2096. wolfSSL_X509_free(x509);
  2097. /* check verify fail */
  2098. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2099. WOLFSSL_FILETYPE_PEM));
  2100. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2101. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2102. /* add in miss matching DIR alt name and resign */
  2103. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2104. ASN_DIR_TYPE);
  2105. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2106. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2107. #else
  2108. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2109. #endif
  2110. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2111. #ifndef WOLFSSL_NO_ASN_STRICT
  2112. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2113. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2114. #else
  2115. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2116. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2117. #endif
  2118. /* check that it still fails if one bad altname and one good altname is in
  2119. * the certificate */
  2120. wolfSSL_X509_free(x509);
  2121. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2122. WOLFSSL_FILETYPE_PEM));
  2123. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2124. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2125. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2126. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2127. ASN_DIR_TYPE);
  2128. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2129. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2130. #else
  2131. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2132. #endif
  2133. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2134. #ifndef WOLFSSL_NO_ASN_STRICT
  2135. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2136. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2137. #else
  2138. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2139. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2140. #endif
  2141. /* check it fails with switching position of bad altname */
  2142. wolfSSL_X509_free(x509);
  2143. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2144. WOLFSSL_FILETYPE_PEM));
  2145. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2146. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2147. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2148. ASN_DIR_TYPE);
  2149. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2150. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2151. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2152. #else
  2153. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2154. #endif
  2155. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2156. #ifndef WOLFSSL_NO_ASN_STRICT
  2157. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2158. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2159. #else
  2160. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2161. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2162. #endif
  2163. wolfSSL_CertManagerFree(cm);
  2164. wolfSSL_X509_free(x509);
  2165. wolfSSL_X509_free(ca);
  2166. /* now test with excluded name constraint */
  2167. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2168. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2169. WOLFSSL_FILETYPE_ASN1));
  2170. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2171. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2172. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2173. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2174. WOLFSSL_FILETYPE_PEM));
  2175. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2176. ASN_DIR_TYPE);
  2177. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2178. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2179. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2180. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2181. #else
  2182. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2183. #endif
  2184. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2185. #ifndef WOLFSSL_NO_ASN_STRICT
  2186. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2187. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2188. #else
  2189. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2191. #endif
  2192. wolfSSL_CertManagerFree(cm);
  2193. wolfSSL_X509_free(x509);
  2194. wolfSSL_X509_free(ca);
  2195. wolfSSL_EVP_PKEY_free(priv);
  2196. res = TEST_RES_CHECK(1);
  2197. #endif
  2198. return res;
  2199. }
  2200. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2201. {
  2202. int res = TEST_SKIPPED;
  2203. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2204. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2205. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2206. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2207. !defined(NO_SHA256)
  2208. WOLFSSL_CERT_MANAGER* cm;
  2209. WOLFSSL_EVP_PKEY *priv;
  2210. WOLFSSL_X509_NAME* name;
  2211. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2212. const char* server_cert = "./certs/test/server-goodcn.pem";
  2213. byte *der;
  2214. int derSz;
  2215. byte *pt;
  2216. WOLFSSL_X509 *x509, *ca;
  2217. pt = (byte*)server_key_der_2048;
  2218. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2219. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2220. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2221. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2222. WOLFSSL_FILETYPE_ASN1));
  2223. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2224. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2225. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2226. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2227. /* check satisfying .wolfssl.com constraint passes */
  2228. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2229. WOLFSSL_FILETYPE_PEM));
  2230. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2231. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2232. AssertNotNull(name = X509_NAME_new());
  2233. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2234. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2235. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2236. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2237. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2238. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2239. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2240. X509_NAME_free(name);
  2241. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2242. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2243. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2244. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2245. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2246. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2247. wolfSSL_X509_free(x509);
  2248. /* check satisfying .random.com constraint passes */
  2249. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2250. WOLFSSL_FILETYPE_PEM));
  2251. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2252. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2253. AssertNotNull(name = X509_NAME_new());
  2254. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2255. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2256. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2257. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2258. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2259. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2260. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2261. X509_NAME_free(name);
  2262. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2263. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2264. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2265. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2266. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2267. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2268. wolfSSL_X509_free(x509);
  2269. /* check fail case when neither constraint is matched */
  2270. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2271. WOLFSSL_FILETYPE_PEM));
  2272. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2273. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2274. AssertNotNull(name = X509_NAME_new());
  2275. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2276. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2277. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2278. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2279. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2280. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2281. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2282. X509_NAME_free(name);
  2283. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2284. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2285. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2286. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2287. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2288. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2289. wolfSSL_CertManagerFree(cm);
  2290. wolfSSL_X509_free(x509);
  2291. wolfSSL_X509_free(ca);
  2292. wolfSSL_EVP_PKEY_free(priv);
  2293. res = TEST_RES_CHECK(1);
  2294. #endif
  2295. return res;
  2296. }
  2297. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2298. {
  2299. int res = TEST_SKIPPED;
  2300. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2301. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2302. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2303. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2304. !defined(NO_SHA256)
  2305. WOLFSSL_CERT_MANAGER* cm;
  2306. WOLFSSL_EVP_PKEY *priv;
  2307. WOLFSSL_X509_NAME* name;
  2308. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2309. const char* server_cert = "./certs/test/server-goodcn.pem";
  2310. byte *der;
  2311. int derSz;
  2312. byte *pt;
  2313. WOLFSSL_X509 *x509, *ca;
  2314. pt = (byte*)server_key_der_2048;
  2315. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2316. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2317. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2318. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2319. WOLFSSL_FILETYPE_ASN1));
  2320. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2321. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2322. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2323. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2324. /* check satisfying wolfssl.com constraint passes */
  2325. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2326. WOLFSSL_FILETYPE_PEM));
  2327. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2328. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2329. AssertNotNull(name = X509_NAME_new());
  2330. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2331. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2332. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2333. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2334. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2335. X509_NAME_free(name);
  2336. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2337. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2338. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2339. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2340. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2341. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2342. wolfSSL_X509_free(x509);
  2343. /* check satisfying example.com constraint passes */
  2344. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2345. WOLFSSL_FILETYPE_PEM));
  2346. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2347. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2348. AssertNotNull(name = X509_NAME_new());
  2349. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2350. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2351. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2352. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2353. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2354. X509_NAME_free(name);
  2355. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2356. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2357. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2358. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2359. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2360. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2361. wolfSSL_X509_free(x509);
  2362. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2363. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2364. WOLFSSL_FILETYPE_PEM));
  2365. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2366. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2367. AssertNotNull(name = X509_NAME_new());
  2368. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2369. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2370. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2371. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2372. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2373. X509_NAME_free(name);
  2374. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2375. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2376. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2377. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2378. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2379. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2380. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2381. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2382. wolfSSL_X509_free(x509);
  2383. /* check fail when one DNS in the list is bad */
  2384. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2385. WOLFSSL_FILETYPE_PEM));
  2386. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2387. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2388. AssertNotNull(name = X509_NAME_new());
  2389. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2390. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2391. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2392. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2393. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2394. X509_NAME_free(name);
  2395. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2396. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2397. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2398. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2399. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2400. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2401. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2402. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2403. wolfSSL_X509_free(x509);
  2404. /* check fail case when neither constraint is matched */
  2405. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2406. WOLFSSL_FILETYPE_PEM));
  2407. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2408. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2409. AssertNotNull(name = X509_NAME_new());
  2410. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2411. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2412. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2413. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2414. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2415. X509_NAME_free(name);
  2416. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2417. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2418. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2419. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2420. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2421. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2422. wolfSSL_CertManagerFree(cm);
  2423. wolfSSL_X509_free(x509);
  2424. wolfSSL_X509_free(ca);
  2425. wolfSSL_EVP_PKEY_free(priv);
  2426. res = TEST_RES_CHECK(1);
  2427. #endif
  2428. return res;
  2429. }
  2430. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2431. {
  2432. int res = TEST_SKIPPED;
  2433. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2434. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2435. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2436. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2437. !defined(NO_SHA256)
  2438. WOLFSSL_CERT_MANAGER* cm;
  2439. WOLFSSL_EVP_PKEY *priv;
  2440. WOLFSSL_X509_NAME* name;
  2441. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2442. const char* server_cert = "./certs/test/server-goodcn.pem";
  2443. byte *der;
  2444. int derSz;
  2445. byte *pt;
  2446. WOLFSSL_X509 *x509, *ca;
  2447. pt = (byte*)server_key_der_2048;
  2448. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2449. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2450. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2451. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2452. WOLFSSL_FILETYPE_ASN1));
  2453. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2454. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2455. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2456. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2457. /* check satisfying wolfssl.com constraint passes */
  2458. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2459. WOLFSSL_FILETYPE_PEM));
  2460. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2461. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2462. AssertNotNull(name = X509_NAME_new());
  2463. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2464. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2465. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2466. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2467. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2468. X509_NAME_free(name);
  2469. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2470. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2471. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2472. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2473. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2474. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2475. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2476. wolfSSL_X509_free(x509);
  2477. /* fail with DNS check because of common name */
  2478. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2479. WOLFSSL_FILETYPE_PEM));
  2480. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2481. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2482. AssertNotNull(name = X509_NAME_new());
  2483. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2484. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2485. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2486. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2487. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2488. X509_NAME_free(name);
  2489. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2490. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2491. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2492. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2493. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2494. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2495. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2496. wolfSSL_X509_free(x509);
  2497. /* fail on permitted DNS name constraint */
  2498. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2499. WOLFSSL_FILETYPE_PEM));
  2500. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2501. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2502. AssertNotNull(name = X509_NAME_new());
  2503. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2504. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2505. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2506. X509_NAME_free(name);
  2507. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2508. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2509. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2510. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2511. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2512. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2513. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2514. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2515. wolfSSL_X509_free(x509);
  2516. /* fail on permitted email name constraint */
  2517. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2518. WOLFSSL_FILETYPE_PEM));
  2519. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2520. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2521. AssertNotNull(name = X509_NAME_new());
  2522. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2523. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2524. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2525. X509_NAME_free(name);
  2526. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2527. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2528. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2529. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2530. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2531. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2532. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2533. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2534. wolfSSL_X509_free(x509);
  2535. /* success with empty email name */
  2536. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2537. WOLFSSL_FILETYPE_PEM));
  2538. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2539. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2540. AssertNotNull(name = X509_NAME_new());
  2541. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2542. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2543. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2544. X509_NAME_free(name);
  2545. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2546. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2547. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2548. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2549. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2550. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2551. wolfSSL_X509_free(x509);
  2552. wolfSSL_CertManagerFree(cm);
  2553. wolfSSL_X509_free(ca);
  2554. wolfSSL_EVP_PKEY_free(priv);
  2555. res = TEST_RES_CHECK(1);
  2556. #endif
  2557. return res;
  2558. }
  2559. static int test_wolfSSL_FPKI(void)
  2560. {
  2561. int res = TEST_SKIPPED;
  2562. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2563. XFILE f;
  2564. const char* fpkiCert = "./certs/fpki-cert.der";
  2565. DecodedCert cert;
  2566. byte buf[4096];
  2567. byte* uuid;
  2568. byte* fascn;
  2569. word32 fascnSz;
  2570. word32 uuidSz;
  2571. int bytes;
  2572. f = XFOPEN(fpkiCert, "rb");
  2573. AssertTrue((f != XBADFILE));
  2574. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2575. XFCLOSE(f);
  2576. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2577. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2578. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2579. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2580. AssertNotNull(fascn);
  2581. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2582. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2583. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2584. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2585. AssertNotNull(uuid);
  2586. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2587. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2588. wc_FreeDecodedCert(&cert);
  2589. res = TEST_RES_CHECK(1);
  2590. #endif
  2591. return res;
  2592. }
  2593. /* use RID in confuncture with other names to test parsing of unknown other
  2594. * names */
  2595. static int test_wolfSSL_OtherName(void)
  2596. {
  2597. int res = TEST_SKIPPED;
  2598. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2599. XFILE f;
  2600. const char* ridCert = "./certs/rid-cert.der";
  2601. DecodedCert cert;
  2602. byte buf[4096];
  2603. int bytes;
  2604. f = XFOPEN(ridCert, "rb");
  2605. AssertTrue((f != XBADFILE));
  2606. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2607. XFCLOSE(f);
  2608. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2609. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2610. wc_FreeDecodedCert(&cert);
  2611. res = TEST_RES_CHECK(1);
  2612. #endif
  2613. return res;
  2614. }
  2615. static int test_wolfSSL_CertRsaPss(void)
  2616. {
  2617. int res = TEST_SKIPPED;
  2618. /* FIPS v2 and below don't support long salts. */
  2619. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2620. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2621. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2622. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2623. XFILE f;
  2624. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2625. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2626. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2627. RSA_MAX_SIZE >= 3072
  2628. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2629. #endif
  2630. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2631. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2632. #endif
  2633. DecodedCert cert;
  2634. byte buf[4096];
  2635. int bytes;
  2636. WOLFSSL_CERT_MANAGER* cm;
  2637. cm = wolfSSL_CertManagerNew();
  2638. AssertNotNull(cm);
  2639. AssertIntEQ(WOLFSSL_SUCCESS,
  2640. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2641. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2642. AssertIntEQ(WOLFSSL_SUCCESS,
  2643. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2644. #endif
  2645. f = XFOPEN(rsaPssSha256Cert, "rb");
  2646. AssertTrue((f != XBADFILE));
  2647. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2648. XFCLOSE(f);
  2649. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2650. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2651. wc_FreeDecodedCert(&cert);
  2652. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2653. RSA_MAX_SIZE >= 3072
  2654. f = XFOPEN(rsaPssSha384Cert, "rb");
  2655. AssertTrue((f != XBADFILE));
  2656. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2657. XFCLOSE(f);
  2658. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2659. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2660. wc_FreeDecodedCert(&cert);
  2661. #endif
  2662. wolfSSL_CertManagerFree(cm);
  2663. res = TEST_RES_CHECK(1);
  2664. #endif
  2665. return res;
  2666. }
  2667. static int test_wolfSSL_CertManagerCRL(void)
  2668. {
  2669. int res = TEST_SKIPPED;
  2670. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2671. !defined(NO_RSA)
  2672. const char* ca_cert = "./certs/ca-cert.pem";
  2673. const char* crl1 = "./certs/crl/crl.pem";
  2674. const char* crl2 = "./certs/crl/crl2.pem";
  2675. WOLFSSL_CERT_MANAGER* cm = NULL;
  2676. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2677. AssertIntEQ(WOLFSSL_SUCCESS,
  2678. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2679. AssertIntEQ(WOLFSSL_SUCCESS,
  2680. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2681. AssertIntEQ(WOLFSSL_SUCCESS,
  2682. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2683. wolfSSL_CertManagerFreeCRL(cm);
  2684. AssertIntEQ(WOLFSSL_SUCCESS,
  2685. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2686. AssertIntEQ(WOLFSSL_SUCCESS,
  2687. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2688. wolfSSL_CertManagerFree(cm);
  2689. res = TEST_RES_CHECK(1);
  2690. #endif
  2691. return res;
  2692. }
  2693. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2694. {
  2695. int res = TEST_SKIPPED;
  2696. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2697. !defined(NO_WOLFSSL_CLIENT)
  2698. WOLFSSL_CTX* ctx;
  2699. const char* ca_cert = "./certs/ca-cert.pem";
  2700. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2701. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2702. AssertNotNull(ctx);
  2703. /* test good CA */
  2704. AssertTrue(WOLFSSL_SUCCESS ==
  2705. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2706. WOLFSSL_LOAD_FLAG_NONE));
  2707. /* test expired CA */
  2708. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2709. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2710. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2711. #else
  2712. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2713. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2714. #endif
  2715. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2716. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2717. wolfSSL_CTX_free(ctx);
  2718. res = TEST_RES_CHECK(1);
  2719. #endif
  2720. return res;
  2721. }
  2722. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2723. {
  2724. int res = TEST_SKIPPED;
  2725. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2726. defined(USE_CERT_BUFFERS_2048)
  2727. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2728. WOLFSSL_CTX* ctx;
  2729. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2730. byte ca_expired_cert[TWOK_BUF];
  2731. word32 sizeof_ca_expired_cert;
  2732. XFILE fp;
  2733. #ifndef NO_WOLFSSL_CLIENT
  2734. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2735. #else
  2736. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2737. #endif
  2738. AssertNotNull(ctx);
  2739. /* test good CA */
  2740. AssertTrue(WOLFSSL_SUCCESS ==
  2741. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2742. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2743. WOLFSSL_LOAD_FLAG_NONE));
  2744. /* load expired CA */
  2745. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2746. fp = XFOPEN(ca_expired_cert_file, "rb");
  2747. AssertTrue(fp != XBADFILE);
  2748. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2749. sizeof(ca_expired_cert), fp);
  2750. XFCLOSE(fp);
  2751. /* test expired CA failure */
  2752. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  2753. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2754. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2755. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2756. #else
  2757. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2758. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2759. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2760. #endif
  2761. /* test expired CA success */
  2762. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2763. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2764. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2765. wolfSSL_CTX_free(ctx);
  2766. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2767. res = TEST_RES_CHECK(1);
  2768. #endif
  2769. return res;
  2770. }
  2771. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2772. {
  2773. int res = TEST_SKIPPED;
  2774. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2775. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2776. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2777. WOLFSSL_CTX* ctx;
  2778. #ifndef NO_WOLFSSL_CLIENT
  2779. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2780. #else
  2781. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2782. #endif
  2783. AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  2784. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  2785. WOLFSSL_FILETYPE_ASN1));
  2786. wolfSSL_CTX_free(ctx);
  2787. res = TEST_RES_CHECK(1);
  2788. #endif
  2789. return res;
  2790. }
  2791. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2792. {
  2793. int res = TEST_SKIPPED;
  2794. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2795. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2796. WOLFSSL_CTX* ctx;
  2797. WOLFSSL* ssl;
  2798. const char *certChain[] = {
  2799. "./certs/intermediate/client-int-cert.pem",
  2800. "./certs/intermediate/ca-int2-cert.pem",
  2801. "./certs/intermediate/ca-int-cert.pem",
  2802. "./certs/ca-cert.pem",
  2803. NULL
  2804. };
  2805. const char** cert;
  2806. WOLFSSL_X509* x509;
  2807. WOLF_STACK_OF(X509)* chain = NULL;
  2808. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2809. AssertNotNull(ssl = wolfSSL_new(ctx));
  2810. for (cert = certChain; *cert != NULL; cert++) {
  2811. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2812. AssertNotNull(x509);
  2813. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2814. X509_free(x509);
  2815. }
  2816. for (cert = certChain; *cert != NULL; cert++) {
  2817. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2818. AssertNotNull(x509);
  2819. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2820. X509_free(x509);
  2821. }
  2822. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2823. AssertIntEQ(sk_X509_num(chain), 3);
  2824. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2825. AssertIntEQ(sk_X509_num(chain), 3);
  2826. SSL_free(ssl);
  2827. SSL_CTX_free(ctx);
  2828. res = TEST_RES_CHECK(1);
  2829. #endif
  2830. return res;
  2831. }
  2832. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2833. {
  2834. int res = TEST_SKIPPED;
  2835. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2836. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2837. const char* server_chain_der = "./certs/server-cert-chain.der";
  2838. const char* client_single_pem = "./certs/client-cert.pem";
  2839. WOLFSSL_CTX* ctx;
  2840. int ret = 0;
  2841. (void)server_chain_der;
  2842. (void)client_single_pem;
  2843. (void)ctx;
  2844. #ifndef NO_WOLFSSL_CLIENT
  2845. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2846. AssertNotNull(ctx);
  2847. #else
  2848. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2849. AssertNotNull(ctx);
  2850. #endif
  2851. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2852. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2853. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2854. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2855. wolfSSL_CTX_free(ctx);
  2856. res = TEST_RES_CHECK(ret == 0);
  2857. #endif
  2858. return res;
  2859. }
  2860. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2861. {
  2862. int res = TEST_SKIPPED;
  2863. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2864. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2865. WOLFSSL_CTX *ctx;
  2866. (void)ctx;
  2867. #ifndef NO_WOLFSSL_CLIENT
  2868. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2869. #else
  2870. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2871. #endif
  2872. /* invalid context */
  2873. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2874. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2875. /* invalid dhParamFile file */
  2876. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2877. NULL, WOLFSSL_FILETYPE_PEM));
  2878. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2879. bogusFile, WOLFSSL_FILETYPE_PEM));
  2880. /* success */
  2881. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2882. WOLFSSL_FILETYPE_PEM));
  2883. wolfSSL_CTX_free(ctx);
  2884. res = TEST_RES_CHECK(1);
  2885. #endif
  2886. return res;
  2887. }
  2888. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2889. {
  2890. int res = TEST_SKIPPED;
  2891. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2892. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2893. WOLFSSL_CTX *ctx;
  2894. (void)ctx;
  2895. #ifndef NO_WOLFSSL_CLIENT
  2896. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2897. #else
  2898. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2899. #endif
  2900. /* invalid context */
  2901. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2902. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2903. /* invalid dhParamFile file */
  2904. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2905. 0, WOLFSSL_FILETYPE_ASN1));
  2906. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2907. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2908. /* success */
  2909. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2910. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2911. wolfSSL_CTX_free(ctx);
  2912. res = TEST_RES_CHECK(1);
  2913. #endif
  2914. return res;
  2915. }
  2916. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2917. {
  2918. int res = TEST_SKIPPED;
  2919. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2920. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2921. WOLFSSL_CTX *ctx;
  2922. (void)ctx;
  2923. #ifndef NO_WOLFSSL_CLIENT
  2924. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2925. AssertNotNull(ctx);
  2926. #else
  2927. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2928. AssertNotNull(ctx);
  2929. #endif
  2930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2931. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2932. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2933. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2934. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2935. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2936. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2937. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2938. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2939. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2940. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2941. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2942. wolfSSL_CTX_free(ctx);
  2943. res = TEST_RES_CHECK(1);
  2944. #endif
  2945. return res;
  2946. }
  2947. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2948. {
  2949. int res = TEST_SKIPPED;
  2950. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  2951. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2952. WOLFSSL_CTX* ctx = NULL;
  2953. const char* derCert = "./certs/server-cert.der";
  2954. const char* nullPath = NULL;
  2955. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2956. const char* emptyPath = "";
  2957. /* der load Case 1 ctx NULL */
  2958. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2959. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2960. #ifndef NO_WOLFSSL_CLIENT
  2961. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2962. #else
  2963. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2964. #endif
  2965. /* Case 2 filePath NULL */
  2966. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2967. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2968. /* Case 3 invalid format */
  2969. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2970. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2971. /* Case 4 filePath not valid */
  2972. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2973. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2974. /* Case 5 filePath empty */
  2975. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2976. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2977. #ifndef NO_RSA
  2978. /* Case 6 success case */
  2979. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2980. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2981. #endif
  2982. wolfSSL_CTX_free(ctx);
  2983. res = TEST_RES_CHECK(1);
  2984. #endif
  2985. return res;
  2986. }
  2987. static int test_wolfSSL_CTX_enable_disable(void)
  2988. {
  2989. int res = TEST_SKIPPED;
  2990. #ifndef NO_CERTS
  2991. WOLFSSL_CTX* ctx = NULL;
  2992. #ifdef HAVE_CRL
  2993. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2994. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2995. #endif
  2996. #ifdef HAVE_OCSP
  2997. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2998. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2999. #endif
  3000. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3001. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3002. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3003. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3004. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3005. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3006. #endif
  3007. #ifndef NO_WOLFSSL_CLIENT
  3008. #ifdef HAVE_EXTENDED_MASTER
  3009. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3010. #endif
  3011. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3012. AssertNotNull(ctx);
  3013. #ifdef HAVE_EXTENDED_MASTER
  3014. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3015. #endif
  3016. #elif !defined(NO_WOLFSSL_SERVER)
  3017. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3018. #else
  3019. return TEST_SUCCESS;
  3020. #endif
  3021. #ifdef HAVE_CRL
  3022. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3023. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3024. #endif
  3025. #ifdef HAVE_OCSP
  3026. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3027. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3028. WOLFSSL_SUCCESS);
  3029. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3030. WOLFSSL_SUCCESS);
  3031. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3032. WOLFSSL_SUCCESS);
  3033. #endif
  3034. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3035. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3036. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3037. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3038. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3039. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3040. #endif
  3041. wolfSSL_CTX_free(ctx);
  3042. res = TEST_RES_CHECK(1);
  3043. #endif /* NO_CERTS */
  3044. return res;
  3045. }
  3046. static int test_wolfSSL_CTX_ticket_API(void)
  3047. {
  3048. int res = TEST_SKIPPED;
  3049. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3050. WOLFSSL_CTX* ctx = NULL;
  3051. void *userCtx = (void*)"this is my ctx";
  3052. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3053. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3054. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3055. wolfSSL_CTX_free(ctx);
  3056. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3057. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3058. res = TEST_RES_CHECK(1);
  3059. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3060. return res;
  3061. }
  3062. static int test_wolfSSL_set_minmax_proto_version(void)
  3063. {
  3064. int res = TEST_SKIPPED;
  3065. #ifdef OPENSSL_EXTRA
  3066. WOLFSSL_CTX *ctx;
  3067. WOLFSSL *ssl;
  3068. int ret;
  3069. (void)ret;
  3070. (void)ssl;
  3071. #ifndef NO_WOLFSSL_CLIENT
  3072. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3073. AssertNotNull(ssl = wolfSSL_new(ctx));
  3074. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3075. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3076. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3077. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3078. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3079. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3080. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3081. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3082. wolfSSL_free(ssl);
  3083. wolfSSL_CTX_free(ctx);
  3084. #endif
  3085. #ifndef NO_WOLFSSL_SERVER
  3086. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3087. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3088. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3089. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3090. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3091. wolfSSL_CTX_free(ctx);
  3092. #endif
  3093. res = TEST_RES_CHECK(1);
  3094. #endif
  3095. return res;
  3096. }
  3097. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3098. defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  3099. static void test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3100. {
  3101. AssertStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3102. }
  3103. static void test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3104. {
  3105. /* Set TLS 1.2 */
  3106. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3107. WOLFSSL_SUCCESS);
  3108. }
  3109. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3110. * what was set at ctx creation. */
  3111. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3112. {
  3113. callback_functions client_cbs, server_cbs;
  3114. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3115. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3116. client_cbs.method = wolfTLS_client_method;
  3117. server_cbs.method = wolfTLS_server_method;
  3118. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3119. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3120. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3121. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  3122. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  3123. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  3124. return TEST_RES_CHECK(1);
  3125. }
  3126. #else
  3127. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3128. {
  3129. return TEST_SKIPPED;
  3130. }
  3131. #endif
  3132. /*----------------------------------------------------------------------------*
  3133. | SSL
  3134. *----------------------------------------------------------------------------*/
  3135. static int test_server_wolfSSL_new(void)
  3136. {
  3137. int res = TEST_SKIPPED;
  3138. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3139. !defined(NO_WOLFSSL_SERVER)
  3140. WOLFSSL_CTX *ctx;
  3141. WOLFSSL_CTX *ctx_nocert;
  3142. WOLFSSL *ssl;
  3143. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3144. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3145. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  3146. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  3147. /* invalid context */
  3148. AssertNull(ssl = wolfSSL_new(NULL));
  3149. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  3150. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  3151. #endif
  3152. /* success */
  3153. AssertNotNull(ssl = wolfSSL_new(ctx));
  3154. wolfSSL_free(ssl);
  3155. wolfSSL_CTX_free(ctx);
  3156. wolfSSL_CTX_free(ctx_nocert);
  3157. res = TEST_RES_CHECK(1);
  3158. #endif
  3159. return res;
  3160. }
  3161. static int test_client_wolfSSL_new(void)
  3162. {
  3163. int res = TEST_SKIPPED;
  3164. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3165. !defined(NO_WOLFSSL_CLIENT)
  3166. WOLFSSL_CTX *ctx;
  3167. WOLFSSL_CTX *ctx_nocert;
  3168. WOLFSSL *ssl;
  3169. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3170. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3171. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3172. /* invalid context */
  3173. AssertNull(ssl = wolfSSL_new(NULL));
  3174. /* success */
  3175. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  3176. wolfSSL_free(ssl);
  3177. /* success */
  3178. AssertNotNull(ssl = wolfSSL_new(ctx));
  3179. wolfSSL_free(ssl);
  3180. wolfSSL_CTX_free(ctx);
  3181. wolfSSL_CTX_free(ctx_nocert);
  3182. res = TEST_RES_CHECK(1);
  3183. #endif
  3184. return res;
  3185. }
  3186. static int test_wolfSSL_SetTmpDH_file(void)
  3187. {
  3188. int res = TEST_SKIPPED;
  3189. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3190. !defined(NO_WOLFSSL_SERVER)
  3191. WOLFSSL_CTX *ctx;
  3192. WOLFSSL *ssl;
  3193. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3194. #ifndef NO_RSA
  3195. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3196. WOLFSSL_FILETYPE_PEM));
  3197. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3198. WOLFSSL_FILETYPE_PEM));
  3199. #elif defined(HAVE_ECC)
  3200. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3201. WOLFSSL_FILETYPE_PEM));
  3202. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3203. WOLFSSL_FILETYPE_PEM));
  3204. #elif defined(HAVE_ED25519)
  3205. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3206. WOLFSSL_FILETYPE_PEM));
  3207. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3208. WOLFSSL_FILETYPE_PEM));
  3209. #elif defined(HAVE_ED448)
  3210. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3211. WOLFSSL_FILETYPE_PEM));
  3212. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3213. WOLFSSL_FILETYPE_PEM));
  3214. #endif
  3215. AssertNotNull(ssl = wolfSSL_new(ctx));
  3216. /* invalid ssl */
  3217. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3218. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3219. /* invalid dhParamFile file */
  3220. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3221. NULL, WOLFSSL_FILETYPE_PEM));
  3222. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3223. bogusFile, WOLFSSL_FILETYPE_PEM));
  3224. /* success */
  3225. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3226. WOLFSSL_FILETYPE_PEM));
  3227. wolfSSL_free(ssl);
  3228. wolfSSL_CTX_free(ctx);
  3229. res = TEST_RES_CHECK(1);
  3230. #endif
  3231. return res;
  3232. }
  3233. static int test_wolfSSL_SetTmpDH_buffer(void)
  3234. {
  3235. int res = TEST_SKIPPED;
  3236. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3237. WOLFSSL_CTX *ctx;
  3238. WOLFSSL *ssl;
  3239. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3240. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3241. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3242. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3243. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3244. AssertNotNull(ssl = wolfSSL_new(ctx));
  3245. /* invalid ssl */
  3246. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3247. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3248. /* invalid dhParamFile file */
  3249. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3250. 0, WOLFSSL_FILETYPE_ASN1));
  3251. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3252. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3253. /* success */
  3254. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3255. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3256. wolfSSL_free(ssl);
  3257. wolfSSL_CTX_free(ctx);
  3258. res = TEST_RES_CHECK(1);
  3259. #endif
  3260. return res;
  3261. }
  3262. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3263. {
  3264. int res = TEST_SKIPPED;
  3265. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3266. WOLFSSL_CTX *ctx, *ctx2;
  3267. WOLFSSL *ssl, *ssl2;
  3268. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3269. AssertNotNull(ctx);
  3270. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3271. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3272. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3273. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3274. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3275. ssl = wolfSSL_new(ctx);
  3276. AssertNotNull(ssl);
  3277. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3278. AssertNotNull(ctx2);
  3279. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3280. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3281. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3282. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3283. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3284. ssl2 = wolfSSL_new(ctx2);
  3285. AssertNotNull(ssl2);
  3286. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3287. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3288. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3289. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3290. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3291. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3292. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3293. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3294. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3295. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3296. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3297. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3298. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3299. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3300. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3301. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3302. wolfSSL_free(ssl2);
  3303. wolfSSL_CTX_free(ctx2);
  3304. wolfSSL_free(ssl);
  3305. wolfSSL_CTX_free(ctx);
  3306. res = TEST_RES_CHECK(1);
  3307. #endif
  3308. return res;
  3309. }
  3310. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3311. * allowed.
  3312. * POST: return 1 on success.
  3313. */
  3314. static int test_wolfSSL_SetMinVersion(void)
  3315. {
  3316. int res = TEST_SKIPPED;
  3317. #ifndef NO_WOLFSSL_CLIENT
  3318. int failFlag = WOLFSSL_SUCCESS;
  3319. WOLFSSL_CTX* ctx;
  3320. WOLFSSL* ssl;
  3321. int itr;
  3322. #ifndef NO_OLD_TLS
  3323. const int versions[] = {
  3324. #ifdef WOLFSSL_ALLOW_TLSV10
  3325. WOLFSSL_TLSV1,
  3326. #endif
  3327. WOLFSSL_TLSV1_1,
  3328. WOLFSSL_TLSV1_2};
  3329. #elif !defined(WOLFSSL_NO_TLS12)
  3330. const int versions[] = { WOLFSSL_TLSV1_2 };
  3331. #else
  3332. const int versions[] = { WOLFSSL_TLSV1_3 };
  3333. #endif
  3334. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3335. ssl = wolfSSL_new(ctx);
  3336. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3337. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3338. failFlag = WOLFSSL_FAILURE;
  3339. }
  3340. }
  3341. wolfSSL_free(ssl);
  3342. wolfSSL_CTX_free(ctx);
  3343. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3344. #endif
  3345. return res;
  3346. } /* END test_wolfSSL_SetMinVersion */
  3347. #ifdef OPENSSL_EXTRA
  3348. static int test_ED25519(void)
  3349. {
  3350. int res = TEST_SKIPPED;
  3351. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3352. defined(WOLFSSL_KEY_GEN)
  3353. byte priv[ED25519_PRV_KEY_SIZE];
  3354. unsigned int privSz = (unsigned int)sizeof(priv);
  3355. byte pub[ED25519_PUB_KEY_SIZE];
  3356. unsigned int pubSz = (unsigned int)sizeof(pub);
  3357. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3358. const char* msg = TEST_STRING;
  3359. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3360. byte sig[ED25519_SIG_SIZE];
  3361. unsigned int sigSz = (unsigned int)sizeof(sig);
  3362. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3363. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3364. WOLFSSL_SUCCESS);
  3365. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3366. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3367. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3368. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3369. &sigSz), WOLFSSL_SUCCESS);
  3370. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3371. #ifdef HAVE_ED25519_VERIFY
  3372. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3373. sigSz), WOLFSSL_SUCCESS);
  3374. #endif /* HAVE_ED25519_VERIFY */
  3375. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3376. res = TEST_RES_CHECK(1);
  3377. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3378. return res;
  3379. }
  3380. static int test_ED448(void)
  3381. {
  3382. int res = TEST_SKIPPED;
  3383. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3384. defined(WOLFSSL_KEY_GEN)
  3385. byte priv[ED448_PRV_KEY_SIZE];
  3386. unsigned int privSz = (unsigned int)sizeof(priv);
  3387. byte pub[ED448_PUB_KEY_SIZE];
  3388. unsigned int pubSz = (unsigned int)sizeof(pub);
  3389. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3390. const char* msg = TEST_STRING;
  3391. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3392. byte sig[ED448_SIG_SIZE];
  3393. unsigned int sigSz = (unsigned int)sizeof(sig);
  3394. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3395. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3396. WOLFSSL_SUCCESS);
  3397. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3398. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3399. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3400. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3401. &sigSz), WOLFSSL_SUCCESS);
  3402. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3403. #ifdef HAVE_ED448_VERIFY
  3404. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3405. sigSz), WOLFSSL_SUCCESS);
  3406. #endif /* HAVE_ED448_VERIFY */
  3407. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3408. res = TEST_RES_CHECK(1);
  3409. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3410. return res;
  3411. }
  3412. #endif /* OPENSSL_EXTRA */
  3413. #include <wolfssl/openssl/pem.h>
  3414. /*----------------------------------------------------------------------------*
  3415. | EVP
  3416. *----------------------------------------------------------------------------*/
  3417. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3418. {
  3419. int res = TEST_SKIPPED;
  3420. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3421. WOLFSSL_BIO* rbio = NULL;
  3422. WOLFSSL_BIO* wbio = NULL;
  3423. WOLFSSL_EVP_PKEY* pkey = NULL;
  3424. char line[256] = { 0 };
  3425. char line1[256] = { 0 };
  3426. int i;
  3427. /* test error cases */
  3428. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3429. /*
  3430. * test RSA public key print
  3431. * in this test, pass '3' for indent
  3432. */
  3433. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3434. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3435. sizeof_client_keypub_der_1024);
  3436. AssertNotNull(rbio);
  3437. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3438. AssertNotNull(pkey);
  3439. wbio = BIO_new(BIO_s_mem());
  3440. AssertNotNull(wbio);
  3441. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3442. BIO_gets(wbio, line, sizeof(line));
  3443. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3444. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3445. BIO_gets(wbio, line, sizeof(line));
  3446. strcpy(line1, " Modulus:\n");
  3447. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3448. BIO_gets(wbio, line, sizeof(line));
  3449. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3450. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3451. /* skip to the end of modulus element*/
  3452. for (i = 0; i < 8 ;i++) {
  3453. BIO_gets(wbio, line, sizeof(line));
  3454. }
  3455. BIO_gets(wbio, line, sizeof(line));
  3456. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3457. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3458. /* should reach EOF */
  3459. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3460. EVP_PKEY_free(pkey);
  3461. pkey = NULL;
  3462. BIO_free(rbio);
  3463. BIO_free(wbio);
  3464. rbio = NULL;
  3465. wbio = NULL;
  3466. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3467. /*
  3468. * test DSA public key print
  3469. */
  3470. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3471. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3472. sizeof_dsa_pub_key_der_2048);
  3473. AssertNotNull(rbio);
  3474. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3475. AssertNotNull(pkey);
  3476. wbio = BIO_new(BIO_s_mem());
  3477. AssertNotNull(wbio);
  3478. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3479. BIO_gets(wbio, line, sizeof(line));
  3480. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3481. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3482. BIO_gets(wbio, line, sizeof(line));
  3483. strcpy(line1, "pub:\n");
  3484. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3485. BIO_gets(wbio, line, sizeof(line));
  3486. strcpy(line1,
  3487. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3488. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3489. /* skip to the end of pub element*/
  3490. for (i = 0; i < 17 ;i++) {
  3491. BIO_gets(wbio, line, sizeof(line));
  3492. }
  3493. BIO_gets(wbio, line, sizeof(line));
  3494. strcpy(line1, "P:\n");
  3495. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3496. /* skip to the end of P element*/
  3497. for (i = 0; i < 18 ;i++) {
  3498. BIO_gets(wbio, line, sizeof(line));
  3499. }
  3500. BIO_gets(wbio, line, sizeof(line));
  3501. strcpy(line1, "Q:\n");
  3502. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3503. /* skip to the end of Q element*/
  3504. for (i = 0; i < 3 ;i++) {
  3505. BIO_gets(wbio, line, sizeof(line));
  3506. }
  3507. BIO_gets(wbio, line, sizeof(line));
  3508. strcpy(line1, "G:\n");
  3509. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3510. /* skip to the end of G element*/
  3511. for (i = 0; i < 18 ;i++) {
  3512. BIO_gets(wbio, line, sizeof(line));
  3513. }
  3514. /* should reach EOF */
  3515. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3516. EVP_PKEY_free(pkey);
  3517. pkey = NULL;
  3518. BIO_free(rbio);
  3519. BIO_free(wbio);
  3520. rbio = NULL;
  3521. wbio = NULL;
  3522. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3523. /*
  3524. * test ECC public key print
  3525. */
  3526. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3527. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3528. sizeof_ecc_clikeypub_der_256);
  3529. AssertNotNull(rbio);
  3530. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3531. AssertNotNull(pkey);
  3532. wbio = BIO_new(BIO_s_mem());
  3533. AssertNotNull(wbio);
  3534. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3535. BIO_gets(wbio, line, sizeof(line));
  3536. strcpy(line1, "Public-Key: (256 bit)\n");
  3537. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3538. BIO_gets(wbio, line, sizeof(line));
  3539. strcpy(line1, "pub:\n");
  3540. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3541. BIO_gets(wbio, line, sizeof(line));
  3542. strcpy(line1,
  3543. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3544. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3545. /* skip to the end of pub element*/
  3546. for (i = 0; i < 4 ;i++) {
  3547. BIO_gets(wbio, line, sizeof(line));
  3548. }
  3549. BIO_gets(wbio, line, sizeof(line));
  3550. strcpy(line1, "ASN1 OID: prime256v1\n");
  3551. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3552. BIO_gets(wbio, line, sizeof(line));
  3553. strcpy(line1, "NIST CURVE: P-256\n");
  3554. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3555. /* should reach EOF */
  3556. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3557. EVP_PKEY_free(pkey);
  3558. pkey = NULL;
  3559. BIO_free(rbio);
  3560. BIO_free(wbio);
  3561. rbio = NULL;
  3562. wbio = NULL;
  3563. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3564. /*
  3565. * test DH public key print
  3566. */
  3567. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3568. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3569. sizeof_dh_pub_key_der_2048);
  3570. AssertNotNull(rbio);
  3571. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3572. AssertNotNull(pkey);
  3573. wbio = BIO_new(BIO_s_mem());
  3574. AssertNotNull(wbio);
  3575. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3576. BIO_gets(wbio, line, sizeof(line));
  3577. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3578. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3579. BIO_gets(wbio, line, sizeof(line));
  3580. strcpy(line1, "public-key:\n");
  3581. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3582. BIO_gets(wbio, line, sizeof(line));
  3583. strcpy(line1,
  3584. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3585. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3586. /* skip to the end of public-key element*/
  3587. for (i = 0; i < 17 ;i++) {
  3588. BIO_gets(wbio, line, sizeof(line));
  3589. }
  3590. BIO_gets(wbio, line, sizeof(line));
  3591. strcpy(line1, "prime:\n");
  3592. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3593. BIO_gets(wbio, line, sizeof(line));
  3594. strcpy(line1,
  3595. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3596. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3597. /* skip to the end of prime element*/
  3598. for (i = 0; i < 17 ;i++) {
  3599. BIO_gets(wbio, line, sizeof(line));
  3600. }
  3601. BIO_gets(wbio, line, sizeof(line));
  3602. strcpy(line1, "generator: 2 (0x02)\n");
  3603. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3604. /* should reach EOF */
  3605. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3606. EVP_PKEY_free(pkey);
  3607. pkey = NULL;
  3608. BIO_free(rbio);
  3609. BIO_free(wbio);
  3610. rbio = NULL;
  3611. wbio = NULL;
  3612. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3613. /* to prevent "unused variable" warning */
  3614. (void)pkey;
  3615. (void)wbio;
  3616. (void)rbio;
  3617. (void)line;
  3618. (void)line1;
  3619. (void)i;
  3620. res = TEST_RES_CHECK(1);
  3621. #endif /* OPENSSL_EXTRA */
  3622. return res;
  3623. }
  3624. /* Test functions for base64 encode/decode */
  3625. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3626. {
  3627. int res = TEST_SKIPPED;
  3628. #if defined(OPENSSL_EXTRA) && \
  3629. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3630. EVP_ENCODE_CTX* ctx = NULL;
  3631. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3632. AssertIntEQ( ctx->remaining,0);
  3633. AssertIntEQ( ctx->data[0],0);
  3634. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3635. EVP_ENCODE_CTX_free(ctx);
  3636. res = TEST_RES_CHECK(1);
  3637. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3638. return res;
  3639. }
  3640. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3641. {
  3642. int res = TEST_SKIPPED;
  3643. #if defined(OPENSSL_EXTRA) && \
  3644. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3645. EVP_ENCODE_CTX* ctx = NULL;
  3646. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3647. EVP_ENCODE_CTX_free(ctx);
  3648. res = TEST_RES_CHECK(1);
  3649. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3650. return res;
  3651. }
  3652. static int test_wolfSSL_EVP_EncodeInit(void)
  3653. {
  3654. int res = TEST_SKIPPED;
  3655. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3656. EVP_ENCODE_CTX* ctx = NULL;
  3657. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3658. AssertIntEQ( ctx->remaining,0);
  3659. AssertIntEQ( ctx->data[0],0);
  3660. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3661. /* make ctx dirty */
  3662. ctx->remaining = 10;
  3663. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3664. EVP_EncodeInit(ctx);
  3665. AssertIntEQ( ctx->remaining,0);
  3666. AssertIntEQ( ctx->data[0],0);
  3667. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3668. EVP_ENCODE_CTX_free(ctx);
  3669. res = TEST_RES_CHECK(1);
  3670. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3671. return res;
  3672. }
  3673. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3674. {
  3675. int res = TEST_SKIPPED;
  3676. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3677. int outl;
  3678. int total;
  3679. const unsigned char plain0[] = {"Th"};
  3680. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3681. const unsigned char plain2[] = {"This is additional data."};
  3682. const unsigned char enc0[] = {"VGg=\n"};
  3683. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3684. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3685. const unsigned char enc2[] =
  3686. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3687. unsigned char encOutBuff[300];
  3688. EVP_ENCODE_CTX* ctx = NULL;
  3689. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3690. EVP_EncodeInit(ctx);
  3691. /* illegal parameter test */
  3692. AssertIntEQ(
  3693. EVP_EncodeUpdate(
  3694. NULL, /* pass NULL as ctx */
  3695. encOutBuff,
  3696. &outl,
  3697. plain1,
  3698. sizeof(plain1)-1),
  3699. 0 /* expected result code 0: fail */
  3700. );
  3701. AssertIntEQ(
  3702. EVP_EncodeUpdate(
  3703. ctx,
  3704. NULL, /* pass NULL as out buff */
  3705. &outl,
  3706. plain1,
  3707. sizeof(plain1)-1),
  3708. 0 /* expected result code 0: fail */
  3709. );
  3710. AssertIntEQ(
  3711. EVP_EncodeUpdate(
  3712. ctx,
  3713. encOutBuff,
  3714. NULL, /* pass NULL as outl */
  3715. plain1,
  3716. sizeof(plain1)-1),
  3717. 0 /* expected result code 0: fail */
  3718. );
  3719. AssertIntEQ(
  3720. EVP_EncodeUpdate(
  3721. ctx,
  3722. encOutBuff,
  3723. &outl,
  3724. NULL, /* pass NULL as in */
  3725. sizeof(plain1)-1),
  3726. 0 /* expected result code 0: fail */
  3727. );
  3728. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3729. /* meaningless parameter test */
  3730. AssertIntEQ(
  3731. EVP_EncodeUpdate(
  3732. ctx,
  3733. encOutBuff,
  3734. &outl,
  3735. plain1,
  3736. 0), /* pass zero input */
  3737. 1 /* expected result code 1: success */
  3738. );
  3739. /* very small data encoding test */
  3740. EVP_EncodeInit(ctx);
  3741. AssertIntEQ(
  3742. EVP_EncodeUpdate(
  3743. ctx,
  3744. encOutBuff,
  3745. &outl,
  3746. plain0,
  3747. sizeof(plain0)-1),
  3748. 1 /* expected result code 1: success */
  3749. );
  3750. AssertIntEQ(outl,0);
  3751. EVP_EncodeFinal(
  3752. ctx,
  3753. encOutBuff + outl,
  3754. &outl);
  3755. AssertIntEQ( outl, sizeof(enc0)-1);
  3756. AssertIntEQ(
  3757. XSTRNCMP(
  3758. (const char*)encOutBuff,
  3759. (const char*)enc0,sizeof(enc0) ),
  3760. 0);
  3761. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3762. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3763. sizeof(enc0)-1);
  3764. AssertIntEQ(
  3765. XSTRNCMP(
  3766. (const char*)encOutBuff,
  3767. (const char*)enc0,sizeof(enc0) ),
  3768. 0);
  3769. /* pass small size( < 48bytes ) input, then make sure they are not
  3770. * encoded and just stored in ctx
  3771. */
  3772. EVP_EncodeInit(ctx);
  3773. total = 0;
  3774. outl = 0;
  3775. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3776. AssertIntEQ(
  3777. EVP_EncodeUpdate(
  3778. ctx,
  3779. encOutBuff, /* buffer for output */
  3780. &outl, /* size of output */
  3781. plain1, /* input */
  3782. sizeof(plain1)-1), /* size of input */
  3783. 1); /* expected result code 1:success */
  3784. total += outl;
  3785. AssertIntEQ(outl, 0); /* no output expected */
  3786. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3787. AssertTrue(
  3788. XSTRNCMP((const char*)(ctx->data),
  3789. (const char*)plain1,
  3790. ctx->remaining) ==0 );
  3791. AssertTrue(encOutBuff[0] == 0);
  3792. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3793. * the stored data and the new input together
  3794. */
  3795. AssertIntEQ(
  3796. EVP_EncodeUpdate(
  3797. ctx,
  3798. encOutBuff + outl, /* buffer for output */
  3799. &outl, /* size of output */
  3800. plain2, /* additional input */
  3801. sizeof(plain2) -1), /* size of additional input */
  3802. 1); /* expected result code 1:success */
  3803. total += outl;
  3804. AssertIntNE(outl, 0); /* some output is expected this time*/
  3805. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3806. AssertIntEQ(
  3807. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3808. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3809. EVP_EncodeFinal(
  3810. ctx,
  3811. encOutBuff + outl,
  3812. &outl);
  3813. total += outl;
  3814. AssertIntNE(total,0);
  3815. AssertIntNE(outl,0);
  3816. AssertIntEQ(XSTRNCMP(
  3817. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3818. /* test with illeagal parameters */
  3819. outl = 1;
  3820. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3821. AssertIntEQ(outl, 0);
  3822. outl = 1;
  3823. EVP_EncodeFinal(ctx, NULL, &outl);
  3824. AssertIntEQ(outl, 0);
  3825. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3826. EVP_EncodeFinal(NULL, NULL, NULL);
  3827. EVP_ENCODE_CTX_free(ctx);
  3828. res = TEST_RES_CHECK(1);
  3829. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3830. return res;
  3831. }
  3832. static int test_wolfSSL_EVP_EncodeFinal(void)
  3833. {
  3834. int res = TEST_SKIPPED;
  3835. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3836. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3837. * test_wolfSSL_EVP_EncodeUpdate
  3838. */
  3839. res = TEST_RES_CHECK(1);
  3840. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3841. return res;
  3842. }
  3843. static int test_wolfSSL_EVP_DecodeInit(void)
  3844. {
  3845. int res = TEST_SKIPPED;
  3846. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3847. EVP_ENCODE_CTX* ctx = NULL;
  3848. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3849. AssertIntEQ( ctx->remaining,0);
  3850. AssertIntEQ( ctx->data[0],0);
  3851. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3852. /* make ctx dirty */
  3853. ctx->remaining = 10;
  3854. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3855. EVP_DecodeInit(ctx);
  3856. AssertIntEQ( ctx->remaining,0);
  3857. AssertIntEQ( ctx->data[0],0);
  3858. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3859. EVP_ENCODE_CTX_free(ctx);
  3860. res = TEST_RES_CHECK(1);
  3861. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3862. return res;
  3863. }
  3864. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3865. {
  3866. int res = TEST_SKIPPED;
  3867. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3868. int outl;
  3869. unsigned char decOutBuff[300];
  3870. EVP_ENCODE_CTX* ctx;
  3871. static const unsigned char enc1[] =
  3872. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3873. /* const unsigned char plain1[] =
  3874. {"This is a base64 decoding test."} */
  3875. ctx = EVP_ENCODE_CTX_new();
  3876. EVP_DecodeInit(ctx);
  3877. /* illegal parameter tests */
  3878. /* pass NULL as ctx */
  3879. AssertIntEQ(
  3880. EVP_DecodeUpdate(
  3881. NULL, /* pass NULL as ctx */
  3882. decOutBuff,
  3883. &outl,
  3884. enc1,
  3885. sizeof(enc1)-1),
  3886. -1 /* expected result code -1: fail */
  3887. );
  3888. AssertIntEQ( outl, 0);
  3889. /* pass NULL as output */
  3890. AssertIntEQ(
  3891. EVP_DecodeUpdate(
  3892. ctx,
  3893. NULL, /* pass NULL as out buff */
  3894. &outl,
  3895. enc1,
  3896. sizeof(enc1)-1),
  3897. -1 /* expected result code -1: fail */
  3898. );
  3899. AssertIntEQ( outl, 0);
  3900. /* pass NULL as outl */
  3901. AssertIntEQ(
  3902. EVP_DecodeUpdate(
  3903. ctx,
  3904. decOutBuff,
  3905. NULL, /* pass NULL as outl */
  3906. enc1,
  3907. sizeof(enc1)-1),
  3908. -1 /* expected result code -1: fail */
  3909. );
  3910. /* pass NULL as input */
  3911. AssertIntEQ(
  3912. EVP_DecodeUpdate(
  3913. ctx,
  3914. decOutBuff,
  3915. &outl,
  3916. NULL, /* pass NULL as in */
  3917. sizeof(enc1)-1),
  3918. -1 /* expected result code -1: fail */
  3919. );
  3920. AssertIntEQ( outl, 0);
  3921. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3922. /* pass zero length input */
  3923. AssertIntEQ(
  3924. EVP_DecodeUpdate(
  3925. ctx,
  3926. decOutBuff,
  3927. &outl,
  3928. enc1,
  3929. 0), /* pass zero as input len */
  3930. 1 /* expected result code 1: success */
  3931. );
  3932. /* decode correct base64 string */
  3933. {
  3934. static const unsigned char enc2[] =
  3935. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3936. static const unsigned char plain2[] =
  3937. {"This is a base64 decoding test."};
  3938. EVP_EncodeInit(ctx);
  3939. AssertIntEQ(
  3940. EVP_DecodeUpdate(
  3941. ctx,
  3942. decOutBuff,
  3943. &outl,
  3944. enc2,
  3945. sizeof(enc2)-1),
  3946. 0 /* expected result code 0: success */
  3947. );
  3948. AssertIntEQ(outl,sizeof(plain2) -1);
  3949. AssertIntEQ(
  3950. EVP_DecodeFinal(
  3951. ctx,
  3952. decOutBuff + outl,
  3953. &outl),
  3954. 1 /* expected result code 1: success */
  3955. );
  3956. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3957. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3958. sizeof(plain2) -1 ),0);
  3959. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3960. sizeof(plain2)-1);
  3961. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3962. sizeof(plain2) -1 ),0);
  3963. }
  3964. /* decode correct base64 string which does not have '\n' in its last*/
  3965. {
  3966. static const unsigned char enc3[] =
  3967. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3968. static const unsigned char plain3[] =
  3969. {"This is a base64 decoding test."}; /* 31 chars */
  3970. EVP_EncodeInit(ctx);
  3971. AssertIntEQ(
  3972. EVP_DecodeUpdate(
  3973. ctx,
  3974. decOutBuff,
  3975. &outl,
  3976. enc3,
  3977. sizeof(enc3)-1),
  3978. 0 /* expected result code 0: success */
  3979. );
  3980. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3981. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3982. sizeof(plain3) -1 ),0);
  3983. AssertIntEQ(
  3984. EVP_DecodeFinal(
  3985. ctx,
  3986. decOutBuff + outl,
  3987. &outl),
  3988. 1 /* expected result code 1: success */
  3989. );
  3990. AssertIntEQ(outl,0 );
  3991. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3992. sizeof(plain3)-1);
  3993. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3994. sizeof(plain3) -1 ),0);
  3995. }
  3996. /* decode string which has a padding char ('=') in the illegal position*/
  3997. {
  3998. static const unsigned char enc4[] =
  3999. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4000. EVP_EncodeInit(ctx);
  4001. AssertIntEQ(
  4002. EVP_DecodeUpdate(
  4003. ctx,
  4004. decOutBuff,
  4005. &outl,
  4006. enc4,
  4007. sizeof(enc4)-1),
  4008. -1 /* expected result code -1: error */
  4009. );
  4010. AssertIntEQ(outl,0);
  4011. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4012. }
  4013. /* small data decode test */
  4014. {
  4015. static const unsigned char enc00[] = {"VG"};
  4016. static const unsigned char enc01[] = {"g=\n"};
  4017. static const unsigned char plain4[] = {"Th"};
  4018. EVP_EncodeInit(ctx);
  4019. AssertIntEQ(
  4020. EVP_DecodeUpdate(
  4021. ctx,
  4022. decOutBuff,
  4023. &outl,
  4024. enc00,
  4025. sizeof(enc00)-1),
  4026. 1 /* expected result code 1: success */
  4027. );
  4028. AssertIntEQ(outl,0);
  4029. AssertIntEQ(
  4030. EVP_DecodeUpdate(
  4031. ctx,
  4032. decOutBuff + outl,
  4033. &outl,
  4034. enc01,
  4035. sizeof(enc01)-1),
  4036. 0 /* expected result code 0: success */
  4037. );
  4038. AssertIntEQ(outl,sizeof(plain4)-1);
  4039. /* test with illegal parameters */
  4040. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4041. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4042. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4043. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4044. EVP_DecodeFinal(
  4045. ctx,
  4046. decOutBuff + outl,
  4047. &outl);
  4048. AssertIntEQ( outl, 0);
  4049. AssertIntEQ(
  4050. XSTRNCMP(
  4051. (const char*)decOutBuff,
  4052. (const char*)plain4,sizeof(plain4)-1 ),
  4053. 0);
  4054. }
  4055. EVP_ENCODE_CTX_free(ctx);
  4056. res = TEST_RES_CHECK(1);
  4057. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4058. return res;
  4059. }
  4060. static int test_wolfSSL_EVP_DecodeFinal(void)
  4061. {
  4062. int res = TEST_SKIPPED;
  4063. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4064. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4065. * test_wolfSSL_EVP_DecodeUpdate
  4066. */
  4067. res = TEST_RES_CHECK(1);
  4068. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4069. return res;
  4070. }
  4071. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4072. */
  4073. #ifdef OPENSSL_EXTRA
  4074. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4075. {
  4076. #ifndef NO_AES
  4077. const WOLFSSL_EVP_CIPHER* c;
  4078. c = wolfSSL_EVP_get_cipherbynid(419);
  4079. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4080. defined(WOLFSSL_AES_128)
  4081. AssertNotNull(c);
  4082. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4083. #else
  4084. AssertNull(c);
  4085. #endif
  4086. c = wolfSSL_EVP_get_cipherbynid(423);
  4087. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4088. defined(WOLFSSL_AES_192)
  4089. AssertNotNull(c);
  4090. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4091. #else
  4092. AssertNull(c);
  4093. #endif
  4094. c = wolfSSL_EVP_get_cipherbynid(427);
  4095. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4096. defined(WOLFSSL_AES_256)
  4097. AssertNotNull(c);
  4098. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4099. #else
  4100. AssertNull(c);
  4101. #endif
  4102. c = wolfSSL_EVP_get_cipherbynid(904);
  4103. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4104. AssertNotNull(c);
  4105. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4106. #else
  4107. AssertNull(c);
  4108. #endif
  4109. c = wolfSSL_EVP_get_cipherbynid(905);
  4110. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4111. AssertNotNull(c);
  4112. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4113. #else
  4114. AssertNull(c);
  4115. #endif
  4116. c = wolfSSL_EVP_get_cipherbynid(906);
  4117. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4118. AssertNotNull(c);
  4119. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4120. #else
  4121. AssertNull(c);
  4122. #endif
  4123. c = wolfSSL_EVP_get_cipherbynid(418);
  4124. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4125. AssertNotNull(c);
  4126. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4127. #else
  4128. AssertNull(c);
  4129. #endif
  4130. c = wolfSSL_EVP_get_cipherbynid(422);
  4131. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4132. AssertNotNull(c);
  4133. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4134. #else
  4135. AssertNull(c);
  4136. #endif
  4137. c = wolfSSL_EVP_get_cipherbynid(426);
  4138. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4139. AssertNotNull(c);
  4140. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4141. #else
  4142. AssertNull(c);
  4143. #endif
  4144. #endif /* !NO_AES */
  4145. #ifndef NO_DES3
  4146. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4147. #ifdef WOLFSSL_DES_ECB
  4148. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4149. #endif
  4150. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4151. #ifdef WOLFSSL_DES_ECB
  4152. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4153. #endif
  4154. #endif /* !NO_DES3 */
  4155. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4156. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4157. #endif
  4158. /* test for nid is out of range */
  4159. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4160. return TEST_RES_CHECK(1);
  4161. }
  4162. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4163. {
  4164. int res = TEST_SKIPPED;
  4165. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4166. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4167. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4168. const EVP_CIPHER *test;
  4169. byte key[AES_BLOCK_SIZE] = {0};
  4170. byte iv[AES_BLOCK_SIZE] = {0};
  4171. AssertNotNull(ctx);
  4172. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4173. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4174. test = EVP_CIPHER_CTX_cipher(ctx);
  4175. AssertTrue(init == test);
  4176. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4177. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4178. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4179. EVP_CIPHER_CTX_free(ctx);
  4180. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4181. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4182. res = TEST_RES_CHECK(1);
  4183. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4184. return res;
  4185. }
  4186. #endif /* OPENSSL_EXTRA */
  4187. /*----------------------------------------------------------------------------*
  4188. | IO
  4189. *----------------------------------------------------------------------------*/
  4190. /* helper functions */
  4191. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4192. #ifdef WOLFSSL_SESSION_EXPORT
  4193. #ifdef WOLFSSL_DTLS
  4194. /* set up function for sending session information */
  4195. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4196. {
  4197. WOLFSSL_CTX* ctx = NULL;
  4198. WOLFSSL* ssl = NULL;
  4199. AssertNotNull(inSsl);
  4200. AssertNotNull(buf);
  4201. AssertIntNE(0, sz);
  4202. /* Set ctx to DTLS 1.2 */
  4203. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4204. AssertNotNull(ctx);
  4205. ssl = wolfSSL_new(ctx);
  4206. AssertNotNull(ssl);
  4207. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4208. wolfSSL_free(ssl);
  4209. wolfSSL_CTX_free(ctx);
  4210. (void)userCtx;
  4211. return 0;
  4212. }
  4213. #endif
  4214. /* returns negative value on fail and positive (including 0) on success */
  4215. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4216. {
  4217. int ret, err, loop_count, count, timeout = 10;
  4218. char msg[] = "I hear you fa shizzle!";
  4219. char input[1024];
  4220. loop_count = ((func_args*)args)->argc;
  4221. #ifdef WOLFSSL_ASYNC_CRYPT
  4222. err = 0; /* Reset error */
  4223. #endif
  4224. do {
  4225. #ifdef WOLFSSL_ASYNC_CRYPT
  4226. if (err == WC_PENDING_E) {
  4227. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4228. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4229. }
  4230. #endif
  4231. ret = wolfSSL_accept(ssl);
  4232. err = wolfSSL_get_error(ssl, 0);
  4233. if (err == WOLFSSL_ERROR_WANT_READ ||
  4234. err == WOLFSSL_ERROR_WANT_WRITE) {
  4235. int select_ret;
  4236. err = WC_PENDING_E;
  4237. select_ret = tcp_select(*sockfd, timeout);
  4238. if (select_ret == TEST_TIMEOUT) {
  4239. return WOLFSSL_FATAL_ERROR;
  4240. }
  4241. }
  4242. } while (err == WC_PENDING_E);
  4243. if (ret != WOLFSSL_SUCCESS) {
  4244. char buff[WOLFSSL_MAX_ERROR_SZ];
  4245. fprintf(stderr, "error = %d, %s\n", err,
  4246. wolfSSL_ERR_error_string(err, buff));
  4247. return ret;
  4248. }
  4249. for (count = 0; count < loop_count; count++) {
  4250. int select_ret;
  4251. select_ret = tcp_select(*sockfd, timeout);
  4252. if (select_ret == TEST_TIMEOUT) {
  4253. ret = WOLFSSL_FATAL_ERROR;
  4254. break;
  4255. }
  4256. do {
  4257. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4258. if (ret > 0) {
  4259. input[ret] = '\0';
  4260. fprintf(stderr, "Client message: %s\n", input);
  4261. }
  4262. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4263. do {
  4264. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4265. return WOLFSSL_FATAL_ERROR;
  4266. }
  4267. err = wolfSSL_get_error(ssl, ret);
  4268. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4269. }
  4270. return ret;
  4271. }
  4272. #endif /* WOLFSSL_SESSION_EXPORT */
  4273. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4274. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4275. defined(HAVE_AES_CBC)
  4276. typedef struct openssl_key_ctx {
  4277. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4278. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4279. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4280. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4281. } openssl_key_ctx;
  4282. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4283. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4284. static WC_INLINE int OpenSSLTicketInit(void)
  4285. {
  4286. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4287. if (ret != 0) return ret;
  4288. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4289. sizeof(myOpenSSLKey_ctx.name));
  4290. if (ret != 0) return ret;
  4291. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4292. sizeof(myOpenSSLKey_ctx.key));
  4293. if (ret != 0) return ret;
  4294. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4295. sizeof(myOpenSSLKey_ctx.hmacKey));
  4296. if (ret != 0) return ret;
  4297. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4298. sizeof(myOpenSSLKey_ctx.iv));
  4299. if (ret != 0) return ret;
  4300. return 0;
  4301. }
  4302. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4303. byte name[WOLFSSL_TICKET_NAME_SZ],
  4304. byte iv[WOLFSSL_TICKET_IV_SZ],
  4305. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4306. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4307. (void)ssl;
  4308. if (enc) {
  4309. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4310. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4311. }
  4312. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4313. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4314. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4315. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4316. return 0;
  4317. }
  4318. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4319. if (enc)
  4320. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4321. else
  4322. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4323. return 1;
  4324. }
  4325. static WC_INLINE void OpenSSLTicketCleanup(void)
  4326. {
  4327. wc_FreeRng(&myOpenSSLKey_rng);
  4328. }
  4329. #endif
  4330. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4331. #ifdef WC_SHA512_DIGEST_SIZE
  4332. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4333. #else
  4334. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4335. #endif
  4336. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4337. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4338. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4339. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4340. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4341. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4342. {
  4343. SOCKET_T sockfd = 0;
  4344. SOCKET_T clientfd = 0;
  4345. word16 port;
  4346. callback_functions* cbf;
  4347. WOLFSSL_CTX* ctx = 0;
  4348. WOLFSSL* ssl = 0;
  4349. func_args* opts = (func_args*)args;
  4350. char msg[] = "I hear you fa shizzle!";
  4351. char input[1024];
  4352. int idx;
  4353. int ret, err = 0;
  4354. int sharedCtx = 0;
  4355. int doUdp = 0;
  4356. SOCKADDR_IN_T cliAddr;
  4357. socklen_t cliLen;
  4358. const char* certFile = svrCertFile;
  4359. const char* keyFile = svrKeyFile;
  4360. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4361. size_t msg_len = 0;
  4362. #endif
  4363. #ifdef WOLFSSL_TIRTOS
  4364. fdOpenSession(Task_self());
  4365. #endif
  4366. opts->return_code = TEST_FAIL;
  4367. cbf = opts->callbacks;
  4368. if (cbf != NULL && cbf->ctx) {
  4369. ctx = cbf->ctx;
  4370. sharedCtx = 1;
  4371. }
  4372. else
  4373. {
  4374. WOLFSSL_METHOD* method = NULL;
  4375. if (cbf != NULL && cbf->method != NULL) {
  4376. method = cbf->method();
  4377. }
  4378. else {
  4379. method = wolfSSLv23_server_method();
  4380. }
  4381. ctx = wolfSSL_CTX_new(method);
  4382. }
  4383. if (ctx == NULL) {
  4384. goto done;
  4385. }
  4386. if (cbf == NULL || !cbf->ticNoInit) {
  4387. #if defined(HAVE_SESSION_TICKET) && \
  4388. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4389. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4390. OpenSSLTicketInit();
  4391. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4392. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4393. TicketInit();
  4394. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4395. #endif
  4396. #endif
  4397. }
  4398. #if defined(USE_WINDOWS_API)
  4399. port = opts->signal->port;
  4400. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4401. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4402. /* Let tcp_listen assign port */
  4403. port = 0;
  4404. #else
  4405. /* Use default port */
  4406. port = wolfSSLPort;
  4407. #endif
  4408. if (cbf != NULL)
  4409. doUdp = cbf->doUdp;
  4410. /* do it here to detect failure */
  4411. tcp_accept(
  4412. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4413. if (doUdp) {
  4414. cliLen = sizeof(cliAddr);
  4415. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4416. (struct sockaddr*)&cliAddr, &cliLen);
  4417. AssertIntGT(idx, 0);
  4418. }
  4419. else {
  4420. CloseSocket(sockfd);
  4421. }
  4422. wolfSSL_CTX_set_verify(ctx,
  4423. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4424. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4425. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4426. #endif
  4427. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4428. != WOLFSSL_SUCCESS) {
  4429. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4430. goto done;
  4431. }
  4432. if (cbf != NULL && cbf->certPemFile != NULL)
  4433. certFile = cbf->certPemFile;
  4434. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4435. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4436. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4437. #else
  4438. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  4439. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4440. #endif
  4441. /*err_sys("can't load server cert chain file, "
  4442. "Please run from wolfSSL home dir");*/
  4443. goto done;
  4444. }
  4445. if (cbf != NULL && cbf->keyPemFile != NULL)
  4446. keyFile = cbf->keyPemFile;
  4447. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4448. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4449. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4450. #else
  4451. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  4452. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4453. #endif
  4454. /*err_sys("can't load server key file, "
  4455. "Please run from wolfSSL home dir");*/
  4456. goto done;
  4457. }
  4458. #ifdef HAVE_CRL
  4459. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4460. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4461. goto done;
  4462. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4463. != WOLFSSL_SUCCESS)
  4464. goto done;
  4465. }
  4466. #endif
  4467. /* call ctx setup callback */
  4468. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4469. cbf->ctx_ready(ctx);
  4470. }
  4471. ssl = wolfSSL_new(ctx);
  4472. if (ssl == NULL) {
  4473. goto done;
  4474. }
  4475. if (doUdp) {
  4476. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4477. if (err != WOLFSSL_SUCCESS)
  4478. goto done;
  4479. }
  4480. #ifdef WOLFSSL_SESSION_EXPORT
  4481. /* only add in more complex nonblocking case with session export tests */
  4482. if (args && opts->argc > 0) {
  4483. /* set as nonblock and time out for waiting on read/write */
  4484. tcp_set_nonblocking(&clientfd);
  4485. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4486. }
  4487. #endif
  4488. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4489. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  4490. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4491. #else
  4492. if (wolfSSL_use_certificate_file(ssl, certFile,
  4493. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4494. #endif
  4495. /*err_sys("can't load server cert chain file, "
  4496. "Please run from wolfSSL home dir");*/
  4497. goto done;
  4498. }
  4499. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4500. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4501. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4502. #else
  4503. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  4504. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4505. #endif
  4506. /*err_sys("can't load server key file, "
  4507. "Please run from wolfSSL home dir");*/
  4508. goto done;
  4509. }
  4510. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4511. /*err_sys("SSL_set_fd failed");*/
  4512. goto done;
  4513. }
  4514. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4515. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4516. #elif !defined(NO_DH)
  4517. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4518. #endif
  4519. /* call ssl setup callback */
  4520. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4521. cbf->ssl_ready(ssl);
  4522. }
  4523. #ifdef WOLFSSL_SESSION_EXPORT
  4524. /* only add in more complex nonblocking case with session export tests */
  4525. if (opts->argc > 0) {
  4526. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4527. if (ret >= 0) {
  4528. opts->return_code = TEST_SUCCESS;
  4529. }
  4530. #ifdef WOLFSSL_TIRTOS
  4531. Task_yield();
  4532. #endif
  4533. goto done;
  4534. }
  4535. #endif
  4536. #ifdef WOLFSSL_ASYNC_CRYPT
  4537. err = 0; /* Reset error */
  4538. #endif
  4539. do {
  4540. #ifdef WOLFSSL_ASYNC_CRYPT
  4541. if (err == WC_PENDING_E) {
  4542. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4543. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4544. }
  4545. #endif
  4546. ret = wolfSSL_accept(ssl);
  4547. err = wolfSSL_get_error(ssl, 0);
  4548. } while (err == WC_PENDING_E);
  4549. if (ret != WOLFSSL_SUCCESS) {
  4550. char buff[WOLFSSL_MAX_ERROR_SZ];
  4551. fprintf(stderr, "error = %d, %s\n", err,
  4552. wolfSSL_ERR_error_string(err, buff));
  4553. /*err_sys("SSL_accept failed");*/
  4554. goto done;
  4555. }
  4556. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4557. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4558. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4559. AssertIntGE(msg_len, 0);
  4560. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4561. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4562. AssertIntGE(msg_len, 0);
  4563. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4564. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4565. if (idx > 0) {
  4566. input[idx] = '\0';
  4567. fprintf(stderr, "Client message: %s\n", input);
  4568. }
  4569. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4570. /*err_sys("SSL_write failed");*/
  4571. #ifdef WOLFSSL_TIRTOS
  4572. return;
  4573. #else
  4574. return 0;
  4575. #endif
  4576. }
  4577. if (cbf != NULL && cbf->on_result != NULL)
  4578. cbf->on_result(ssl);
  4579. #ifdef WOLFSSL_TIRTOS
  4580. Task_yield();
  4581. #endif
  4582. opts->return_code = TEST_SUCCESS;
  4583. done:
  4584. if (cbf != NULL)
  4585. cbf->last_err = err;
  4586. if (cbf != NULL && cbf->on_cleanup != NULL)
  4587. cbf->on_cleanup(ssl);
  4588. wolfSSL_shutdown(ssl);
  4589. wolfSSL_free(ssl);
  4590. if (!sharedCtx)
  4591. wolfSSL_CTX_free(ctx);
  4592. CloseSocket(clientfd);
  4593. #ifdef WOLFSSL_TIRTOS
  4594. fdCloseSession(Task_self());
  4595. #endif
  4596. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4597. && defined(HAVE_THREAD_LS)
  4598. wc_ecc_fp_free(); /* free per thread cache */
  4599. #endif
  4600. if (cbf == NULL || !cbf->ticNoInit) {
  4601. #if defined(HAVE_SESSION_TICKET) && \
  4602. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4603. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4604. OpenSSLTicketCleanup();
  4605. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4606. TicketCleanup();
  4607. #endif
  4608. #endif
  4609. }
  4610. #ifndef WOLFSSL_TIRTOS
  4611. return 0;
  4612. #endif
  4613. }
  4614. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4615. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4616. {
  4617. SOCKET_T sockfd = 0;
  4618. SOCKET_T clientfd = 0;
  4619. word16 port;
  4620. callback_functions* cbf;
  4621. WOLFSSL_CTX* ctx = 0;
  4622. WOLFSSL* ssl = 0;
  4623. char msg[] = "I hear you fa shizzle!";
  4624. char input[1024];
  4625. int idx;
  4626. int ret, err = 0;
  4627. int sharedCtx = 0;
  4628. int loop_count = ((func_args*)args)->argc;
  4629. int count = 0;
  4630. #ifdef WOLFSSL_TIRTOS
  4631. fdOpenSession(Task_self());
  4632. #endif
  4633. ((func_args*)args)->return_code = TEST_FAIL;
  4634. cbf = ((func_args*)args)->callbacks;
  4635. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4636. if (cbf != NULL && cbf->ctx) {
  4637. ctx = cbf->ctx;
  4638. sharedCtx = 1;
  4639. }
  4640. else
  4641. #endif
  4642. {
  4643. WOLFSSL_METHOD* method = NULL;
  4644. if (cbf != NULL && cbf->method != NULL) {
  4645. method = cbf->method();
  4646. }
  4647. else {
  4648. method = wolfSSLv23_server_method();
  4649. }
  4650. ctx = wolfSSL_CTX_new(method);
  4651. }
  4652. #if defined(USE_WINDOWS_API)
  4653. port = ((func_args*)args)->signal->port;
  4654. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4655. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4656. /* Let tcp_listen assign port */
  4657. port = 0;
  4658. #else
  4659. /* Use default port */
  4660. port = wolfSSLPort;
  4661. #endif
  4662. wolfSSL_CTX_set_verify(ctx,
  4663. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4664. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4665. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4666. #endif
  4667. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4668. != WOLFSSL_SUCCESS) {
  4669. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4670. goto done;
  4671. }
  4672. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4673. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4674. /*err_sys("can't load server cert chain file, "
  4675. "Please run from wolfSSL home dir");*/
  4676. goto done;
  4677. }
  4678. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4679. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4680. /*err_sys("can't load server key file, "
  4681. "Please run from wolfSSL home dir");*/
  4682. goto done;
  4683. }
  4684. /* call ctx setup callback */
  4685. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4686. cbf->ctx_ready(ctx);
  4687. }
  4688. while (count != loop_count) {
  4689. ssl = wolfSSL_new(ctx);
  4690. if (ssl == NULL) {
  4691. goto done;
  4692. }
  4693. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4694. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4695. /*err_sys("can't load server cert chain file, "
  4696. "Please run from wolfSSL home dir");*/
  4697. goto done;
  4698. }
  4699. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4700. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4701. /*err_sys("can't load server key file, "
  4702. "Please run from wolfSSL home dir");*/
  4703. goto done;
  4704. }
  4705. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4706. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4707. #elif !defined(NO_DH)
  4708. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4709. #endif
  4710. /* call ssl setup callback */
  4711. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4712. cbf->ssl_ready(ssl);
  4713. }
  4714. /* do it here to detect failure */
  4715. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4716. CloseSocket(sockfd);
  4717. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4718. /*err_sys("SSL_set_fd failed");*/
  4719. goto done;
  4720. }
  4721. #ifdef WOLFSSL_ASYNC_CRYPT
  4722. err = 0; /* Reset error */
  4723. #endif
  4724. do {
  4725. #ifdef WOLFSSL_ASYNC_CRYPT
  4726. if (err == WC_PENDING_E) {
  4727. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4728. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4729. }
  4730. #endif
  4731. ret = wolfSSL_accept(ssl);
  4732. err = wolfSSL_get_error(ssl, 0);
  4733. } while (err == WC_PENDING_E);
  4734. if (ret != WOLFSSL_SUCCESS) {
  4735. char buff[WOLFSSL_MAX_ERROR_SZ];
  4736. fprintf(stderr, "error = %d, %s\n", err,
  4737. wolfSSL_ERR_error_string(err, buff));
  4738. /*err_sys("SSL_accept failed");*/
  4739. goto done;
  4740. }
  4741. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4742. if (idx > 0) {
  4743. input[idx] = '\0';
  4744. fprintf(stderr, "Client message: %s\n", input);
  4745. }
  4746. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4747. /*err_sys("SSL_write failed");*/
  4748. #ifdef WOLFSSL_TIRTOS
  4749. return;
  4750. #else
  4751. return 0;
  4752. #endif
  4753. }
  4754. /* free ssl for this connection */
  4755. wolfSSL_shutdown(ssl);
  4756. wolfSSL_free(ssl); ssl = NULL;
  4757. CloseSocket(clientfd);
  4758. count++;
  4759. }
  4760. #ifdef WOLFSSL_TIRTOS
  4761. Task_yield();
  4762. #endif
  4763. ((func_args*)args)->return_code = TEST_SUCCESS;
  4764. done:
  4765. if (ssl != NULL) {
  4766. wolfSSL_shutdown(ssl);
  4767. wolfSSL_free(ssl);
  4768. }
  4769. if (!sharedCtx)
  4770. wolfSSL_CTX_free(ctx);
  4771. CloseSocket(clientfd);
  4772. #ifdef WOLFSSL_TIRTOS
  4773. fdCloseSession(Task_self());
  4774. #endif
  4775. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4776. && defined(HAVE_THREAD_LS)
  4777. wc_ecc_fp_free(); /* free per thread cache */
  4778. #endif
  4779. #ifndef WOLFSSL_TIRTOS
  4780. return 0;
  4781. #endif
  4782. }
  4783. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4784. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4785. static int test_client_nofail(void* args, cbType cb)
  4786. {
  4787. #if !defined(NO_WOLFSSL_CLIENT)
  4788. SOCKET_T sockfd = 0;
  4789. callback_functions* cbf;
  4790. WOLFSSL_CTX* ctx = 0;
  4791. WOLFSSL* ssl = 0;
  4792. WOLFSSL_CIPHER* cipher;
  4793. char msg[64] = "hello wolfssl!";
  4794. char reply[1024];
  4795. int input;
  4796. int msgSz = (int)XSTRLEN(msg);
  4797. int ret, err = 0;
  4798. int cipherSuite;
  4799. int sharedCtx = 0;
  4800. int doUdp = 0;
  4801. const char* cipherName1, *cipherName2;
  4802. #ifdef WOLFSSL_TIRTOS
  4803. fdOpenSession(Task_self());
  4804. #endif
  4805. ((func_args*)args)->return_code = TEST_FAIL;
  4806. cbf = ((func_args*)args)->callbacks;
  4807. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4808. if (cbf != NULL && cbf->ctx) {
  4809. ctx = cbf->ctx;
  4810. sharedCtx = cbf->isSharedCtx;
  4811. }
  4812. else
  4813. #endif
  4814. {
  4815. WOLFSSL_METHOD* method = NULL;
  4816. if (cbf != NULL && cbf->method != NULL) {
  4817. method = cbf->method();
  4818. }
  4819. else {
  4820. method = wolfSSLv23_client_method();
  4821. }
  4822. ctx = wolfSSL_CTX_new(method);
  4823. }
  4824. if (cbf != NULL)
  4825. doUdp = cbf->doUdp;
  4826. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4827. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4828. #endif
  4829. /* Do connect here so server detects failures */
  4830. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4831. doUdp, 0, NULL);
  4832. /* Connect the socket so that we don't have to set the peer later on */
  4833. if (doUdp)
  4834. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4835. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4836. {
  4837. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4838. goto done;
  4839. }
  4840. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4841. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4842. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4843. #else
  4844. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4845. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4846. #endif
  4847. /*err_sys("can't load client cert file, "
  4848. "Please run from wolfSSL home dir");*/
  4849. goto done;
  4850. }
  4851. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4852. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4853. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4854. #else
  4855. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4856. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4857. #endif
  4858. /*err_sys("can't load client key file, "
  4859. "Please run from wolfSSL home dir");*/
  4860. goto done;
  4861. }
  4862. #ifdef HAVE_CRL
  4863. if (cbf != NULL && cbf->crlPemFile != NULL) {
  4864. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  4865. goto done;
  4866. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  4867. != WOLFSSL_SUCCESS)
  4868. goto done;
  4869. }
  4870. #endif
  4871. /* call ctx setup callback */
  4872. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4873. cbf->ctx_ready(ctx);
  4874. }
  4875. ssl = wolfSSL_new(ctx);
  4876. if (ssl == NULL) {
  4877. goto done;
  4878. }
  4879. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4880. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4881. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4882. #else
  4883. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4884. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4885. #endif
  4886. /*err_sys("can't load client cert file, "
  4887. "Please run from wolfSSL home dir");*/
  4888. goto done;
  4889. }
  4890. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4891. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4892. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4893. #else
  4894. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4895. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4896. #endif
  4897. /*err_sys("can't load client key file, "
  4898. "Please run from wolfSSL home dir");*/
  4899. goto done;
  4900. }
  4901. if (!doUdp) {
  4902. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4903. /*err_sys("SSL_set_fd failed");*/
  4904. goto done;
  4905. }
  4906. }
  4907. else {
  4908. #ifdef WOLFSSL_DTLS
  4909. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4910. /*err_sys("SSL_set_fd failed");*/
  4911. goto done;
  4912. }
  4913. #else
  4914. goto done;
  4915. #endif
  4916. }
  4917. /* call ssl setup callback */
  4918. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4919. cbf->ssl_ready(ssl);
  4920. }
  4921. #ifdef WOLFSSL_ASYNC_CRYPT
  4922. err = 0; /* Reset error */
  4923. #endif
  4924. do {
  4925. #ifdef WOLFSSL_ASYNC_CRYPT
  4926. if (err == WC_PENDING_E) {
  4927. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4928. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4929. }
  4930. #endif
  4931. ret = wolfSSL_connect(ssl);
  4932. err = wolfSSL_get_error(ssl, 0);
  4933. } while (err == WC_PENDING_E);
  4934. if (ret != WOLFSSL_SUCCESS) {
  4935. char buff[WOLFSSL_MAX_ERROR_SZ];
  4936. fprintf(stderr, "error = %d, %s\n", err,
  4937. wolfSSL_ERR_error_string(err, buff));
  4938. /*err_sys("SSL_connect failed");*/
  4939. goto done;
  4940. }
  4941. /* test the various get cipher methods */
  4942. /* Internal cipher suite names */
  4943. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4944. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4945. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4946. (cipherSuite >> 8), cipherSuite & 0xFF);
  4947. AssertStrEQ(cipherName1, cipherName2);
  4948. /* IANA Cipher Suites Names */
  4949. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4950. then it's the internal cipher suite name */
  4951. cipher = wolfSSL_get_current_cipher(ssl);
  4952. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4953. cipherName2 = wolfSSL_get_cipher(ssl);
  4954. AssertStrEQ(cipherName1, cipherName2);
  4955. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4956. !defined(WOLFSSL_QT)
  4957. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4958. (cipherSuite >> 8), cipherSuite & 0xFF);
  4959. AssertStrEQ(cipherName1, cipherName2);
  4960. #endif
  4961. if (cb != NULL)
  4962. (cb)(ctx, ssl);
  4963. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4964. /*err_sys("SSL_write failed");*/
  4965. goto done;
  4966. }
  4967. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4968. if (input > 0) {
  4969. reply[input] = '\0';
  4970. fprintf(stderr, "Server response: %s\n", reply);
  4971. }
  4972. if (cbf != NULL && cbf->on_result != NULL)
  4973. cbf->on_result(ssl);
  4974. ((func_args*)args)->return_code = TEST_SUCCESS;
  4975. done:
  4976. if (cbf != NULL)
  4977. cbf->last_err = err;
  4978. if (cbf != NULL && cbf->on_cleanup != NULL)
  4979. cbf->on_cleanup(ssl);
  4980. wolfSSL_free(ssl);
  4981. if (!sharedCtx)
  4982. wolfSSL_CTX_free(ctx);
  4983. CloseSocket(sockfd);
  4984. #ifdef WOLFSSL_TIRTOS
  4985. fdCloseSession(Task_self());
  4986. #endif
  4987. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4988. && defined(HAVE_THREAD_LS)
  4989. wc_ecc_fp_free(); /* free per thread cache */
  4990. #endif
  4991. #else
  4992. (void)args;
  4993. (void)cb;
  4994. #endif /* !NO_WOLFSSL_CLIENT */
  4995. return 0;
  4996. }
  4997. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  4998. callback_functions* server_cb)
  4999. {
  5000. func_args client_args;
  5001. func_args server_args;
  5002. tcp_ready ready;
  5003. THREAD_TYPE serverThread;
  5004. XMEMSET(&client_args, 0, sizeof(func_args));
  5005. XMEMSET(&server_args, 0, sizeof(func_args));
  5006. #ifdef WOLFSSL_TIRTOS
  5007. fdOpenSession(Task_self());
  5008. #endif
  5009. StartTCP();
  5010. InitTcpReady(&ready);
  5011. #if defined(USE_WINDOWS_API)
  5012. /* use RNG to get random port if using windows */
  5013. ready.port = GetRandomPort();
  5014. #endif
  5015. server_args.signal = &ready;
  5016. server_args.callbacks = server_cb;
  5017. client_args.signal = &ready;
  5018. client_args.callbacks = client_cb;
  5019. start_thread(test_server_nofail, &server_args, &serverThread);
  5020. wait_tcp_ready(&server_args);
  5021. test_client_nofail(&client_args, NULL);
  5022. join_thread(serverThread);
  5023. client_cb->return_code = client_args.return_code;
  5024. server_cb->return_code = server_args.return_code;
  5025. FreeTcpReady(&ready);
  5026. #ifdef WOLFSSL_TIRTOS
  5027. fdOpenSession(Task_self());
  5028. #endif
  5029. }
  5030. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5031. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  5032. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  5033. {
  5034. SOCKET_T sockfd = 0;
  5035. callback_functions* cbf;
  5036. WOLFSSL_CTX* ctx = 0;
  5037. WOLFSSL* ssl = 0;
  5038. WOLFSSL_SESSION* session = NULL;
  5039. char msg[64] = "hello wolfssl!";
  5040. char reply[1024];
  5041. int input;
  5042. int msgSz = (int)XSTRLEN(msg);
  5043. int ret, err = 0;
  5044. int sharedCtx = 0;
  5045. #ifdef WOLFSSL_TIRTOS
  5046. fdOpenSession(Task_self());
  5047. #endif
  5048. ((func_args*)args)->return_code = TEST_FAIL;
  5049. cbf = ((func_args*)args)->callbacks;
  5050. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5051. if (cbf != NULL && cbf->ctx) {
  5052. ctx = cbf->ctx;
  5053. sharedCtx = 1;
  5054. }
  5055. else
  5056. #endif
  5057. {
  5058. WOLFSSL_METHOD* method = NULL;
  5059. if (cbf != NULL && cbf->method != NULL) {
  5060. method = cbf->method();
  5061. }
  5062. else {
  5063. method = wolfSSLv23_client_method();
  5064. }
  5065. ctx = wolfSSL_CTX_new(method);
  5066. }
  5067. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5068. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5069. #endif
  5070. /* Do connect here so server detects failures */
  5071. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5072. 0, 0, NULL);
  5073. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5074. {
  5075. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5076. goto done;
  5077. }
  5078. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5079. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5080. /*err_sys("can't load client cert file, "
  5081. "Please run from wolfSSL home dir");*/
  5082. goto done;
  5083. }
  5084. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5085. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5086. /*err_sys("can't load client key file, "
  5087. "Please run from wolfSSL home dir");*/
  5088. goto done;
  5089. }
  5090. /* call ctx setup callback */
  5091. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5092. cbf->ctx_ready(ctx);
  5093. }
  5094. ssl = wolfSSL_new(ctx);
  5095. if (ssl == NULL) {
  5096. goto done;
  5097. }
  5098. /* keep handshakre resources for re-using WOLFSSL obj */
  5099. wolfSSL_KeepArrays(ssl);
  5100. if (wolfSSL_KeepHandshakeResources(ssl)) {
  5101. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5102. goto done;
  5103. }
  5104. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5105. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5106. /*err_sys("can't load client cert file, "
  5107. "Please run from wolfSSL home dir");*/
  5108. goto done;
  5109. }
  5110. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5111. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5112. /*err_sys("can't load client key file, "
  5113. "Please run from wolfSSL home dir");*/
  5114. goto done;
  5115. }
  5116. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5117. /*err_sys("SSL_set_fd failed");*/
  5118. goto done;
  5119. }
  5120. /* call ssl setup callback */
  5121. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5122. cbf->ssl_ready(ssl);
  5123. }
  5124. #ifdef WOLFSSL_ASYNC_CRYPT
  5125. err = 0; /* Reset error */
  5126. #endif
  5127. do {
  5128. #ifdef WOLFSSL_ASYNC_CRYPT
  5129. if (err == WC_PENDING_E) {
  5130. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5131. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5132. }
  5133. #endif
  5134. ret = wolfSSL_connect(ssl);
  5135. err = wolfSSL_get_error(ssl, 0);
  5136. } while (err == WC_PENDING_E);
  5137. if (ret != WOLFSSL_SUCCESS) {
  5138. char buff[WOLFSSL_MAX_ERROR_SZ];
  5139. fprintf(stderr, "error = %d, %s\n", err,
  5140. wolfSSL_ERR_error_string(err, buff));
  5141. /*err_sys("SSL_connect failed");*/
  5142. goto done;
  5143. }
  5144. /* Build first session */
  5145. if (cb != NULL)
  5146. ((cbType)cb)(ctx, ssl);
  5147. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5148. /*err_sys("SSL_write failed");*/
  5149. goto done;
  5150. }
  5151. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5152. if (input > 0) {
  5153. reply[input] = '\0';
  5154. fprintf(stderr, "Server response: %s\n", reply);
  5155. }
  5156. /* Session Resumption by re-using WOLFSSL object */
  5157. wolfSSL_set_quiet_shutdown(ssl, 1);
  5158. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5159. /* err_sys ("SSL shutdown failed"); */
  5160. goto done;
  5161. }
  5162. session = wolfSSL_get1_session(ssl);
  5163. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5164. /* err_sys ("SSL_clear failed"); */
  5165. goto done;
  5166. }
  5167. wolfSSL_set_session(ssl, session);
  5168. wolfSSL_SESSION_free(session);
  5169. session = NULL;
  5170. /* close socket once */
  5171. CloseSocket(sockfd);
  5172. sockfd = 0;
  5173. /* wait until server ready */
  5174. wait_tcp_ready((func_args*)server_args);
  5175. fprintf(stderr, "session resumption\n");
  5176. /* Do re-connect */
  5177. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5178. 0, 0, NULL);
  5179. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5180. /*err_sys("SSL_set_fd failed");*/
  5181. goto done;
  5182. }
  5183. #ifdef WOLFSSL_ASYNC_CRYPT
  5184. err = 0; /* Reset error */
  5185. #endif
  5186. do {
  5187. #ifdef WOLFSSL_ASYNC_CRYPT
  5188. if (err == WC_PENDING_E) {
  5189. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5190. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5191. }
  5192. #endif
  5193. ret = wolfSSL_connect(ssl);
  5194. err = wolfSSL_get_error(ssl, 0);
  5195. } while (err == WC_PENDING_E);
  5196. if (ret != WOLFSSL_SUCCESS) {
  5197. char buff[WOLFSSL_MAX_ERROR_SZ];
  5198. fprintf(stderr, "error = %d, %s\n", err,
  5199. wolfSSL_ERR_error_string(err, buff));
  5200. /*err_sys("SSL_connect failed");*/
  5201. goto done;
  5202. }
  5203. /* Build first session */
  5204. if (cb != NULL)
  5205. ((cbType)cb)(ctx, ssl);
  5206. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5207. /*err_sys("SSL_write failed");*/
  5208. goto done;
  5209. }
  5210. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5211. if (input > 0) {
  5212. reply[input] = '\0';
  5213. fprintf(stderr, "Server response: %s\n", reply);
  5214. }
  5215. ((func_args*)args)->return_code = TEST_SUCCESS;
  5216. done:
  5217. wolfSSL_free(ssl);
  5218. if (!sharedCtx)
  5219. wolfSSL_CTX_free(ctx);
  5220. CloseSocket(sockfd);
  5221. #ifdef WOLFSSL_TIRTOS
  5222. fdCloseSession(Task_self());
  5223. #endif
  5224. return;
  5225. }
  5226. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5227. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5228. static int test_client_verifyDepth(void* args)
  5229. {
  5230. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5231. SOCKET_T sockfd = 0;
  5232. callback_functions* cbf;
  5233. WOLFSSL_CTX* ctx = 0;
  5234. WOLFSSL* ssl = 0;
  5235. char msg[64] = "hello wolfssl!";
  5236. char reply[1024];
  5237. int input;
  5238. int msgSz = (int)XSTRLEN(msg);
  5239. int ret, err = 0;
  5240. int verify_depth = ((func_args*)args)->argc;
  5241. ((func_args*)args)->return_code = TEST_FAIL;
  5242. cbf = ((func_args*)args)->callbacks;
  5243. {
  5244. WOLFSSL_METHOD* method = NULL;
  5245. if (cbf != NULL && cbf->method != NULL) {
  5246. method = cbf->method();
  5247. }
  5248. else {
  5249. method = wolfSSLv23_client_method();
  5250. }
  5251. ctx = wolfSSL_CTX_new(method);
  5252. }
  5253. /* Do connect here so server detects failures */
  5254. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5255. 0, 0, NULL);
  5256. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5257. != WOLFSSL_SUCCESS)
  5258. {
  5259. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5260. goto done;
  5261. }
  5262. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5263. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5264. /*err_sys("can't load client cert file, "
  5265. "Please run from wolfSSL home dir");*/
  5266. goto done;
  5267. }
  5268. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5269. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5270. /*err_sys("can't load client key file, "
  5271. "Please run from wolfSSL home dir");*/
  5272. goto done;
  5273. }
  5274. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5275. /* set verify depth */
  5276. if (verify_depth == 0) {
  5277. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5278. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5279. }
  5280. else if (verify_depth == -1) {
  5281. myVerifyAction = VERIFY_USE_PREVERFIY;
  5282. SSL_CTX_set_verify_depth(ctx, 0);
  5283. }
  5284. else if (verify_depth > 0) {
  5285. myVerifyAction = VERIFY_USE_PREVERFIY;
  5286. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5287. }
  5288. ssl = wolfSSL_new(ctx);
  5289. if (ssl == NULL) {
  5290. goto done;
  5291. }
  5292. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5293. /*err_sys("SSL_set_fd failed");*/
  5294. goto done;
  5295. }
  5296. #ifdef WOLFSSL_ASYNC_CRYPT
  5297. err = 0; /* Reset error */
  5298. #endif
  5299. do {
  5300. #ifdef WOLFSSL_ASYNC_CRYPT
  5301. if (err == WC_PENDING_E) {
  5302. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5303. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5304. }
  5305. #endif
  5306. ret = wolfSSL_connect(ssl);
  5307. err = wolfSSL_get_error(ssl, 0);
  5308. } while (err == WC_PENDING_E);
  5309. if (ret != WOLFSSL_SUCCESS) {
  5310. char buff[WOLFSSL_MAX_ERROR_SZ];
  5311. fprintf(stderr, "error = %d, %s\n", err,
  5312. wolfSSL_ERR_error_string(err, buff));
  5313. goto done;
  5314. }
  5315. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5316. goto done;
  5317. }
  5318. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5319. if (input > 0) {
  5320. reply[input] = '\0';
  5321. fprintf(stderr, "Server response: %s\n", reply);
  5322. }
  5323. ((func_args*)args)->return_code = TEST_SUCCESS;
  5324. done:
  5325. wolfSSL_free(ssl);
  5326. wolfSSL_CTX_free(ctx);
  5327. CloseSocket(sockfd);
  5328. #else
  5329. (void)args;
  5330. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5331. return 0;
  5332. }
  5333. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5334. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5335. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5336. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5337. #define HAVE_ALPN_PROTOS_SUPPORT
  5338. #endif
  5339. /* Generic TLS client / server with callbacks for API unit tests
  5340. * Used by SNI / ALPN / crypto callback helper functions */
  5341. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5342. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5343. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5344. #define ENABLE_TLS_CALLBACK_TEST
  5345. #endif
  5346. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5347. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5348. /* TLS server for API unit testing - generic */
  5349. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5350. {
  5351. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5352. WOLFSSL_CTX* ctx = NULL;
  5353. WOLFSSL* ssl = NULL;
  5354. SOCKET_T sfd = 0;
  5355. SOCKET_T cfd = 0;
  5356. word16 port;
  5357. char msg[] = "I hear you fa shizzle!";
  5358. int len = (int) XSTRLEN(msg);
  5359. char input[1024];
  5360. int idx;
  5361. int ret, err = 0;
  5362. ((func_args*)args)->return_code = TEST_FAIL;
  5363. #ifdef WOLFSSL_STATIC_MEMORY
  5364. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5365. callbacks->memSz > 0) {
  5366. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5367. callbacks->mem, callbacks->memSz, 0, 1);
  5368. if (ret != WOLFSSL_SUCCESS) {
  5369. fprintf(stderr, "CTX static new failed %d\n", ret);
  5370. return 0;
  5371. }
  5372. }
  5373. #else
  5374. if (ctx == NULL) {
  5375. ctx = wolfSSL_CTX_new(callbacks->method());
  5376. }
  5377. if (ctx == NULL) {
  5378. fprintf(stderr, "CTX new failed\n");
  5379. return 0;
  5380. }
  5381. #endif
  5382. /* set defaults */
  5383. if (callbacks->caPemFile == NULL)
  5384. callbacks->caPemFile = cliCertFile;
  5385. if (callbacks->certPemFile == NULL)
  5386. callbacks->certPemFile = svrCertFile;
  5387. if (callbacks->keyPemFile == NULL)
  5388. callbacks->keyPemFile = svrKeyFile;
  5389. #ifdef WOLFSSL_TIRTOS
  5390. fdOpenSession(Task_self());
  5391. #endif
  5392. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5393. #if defined(USE_WINDOWS_API)
  5394. port = ((func_args*)args)->signal->port;
  5395. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5396. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5397. /* Let tcp_listen assign port */
  5398. port = 0;
  5399. #else
  5400. /* Use default port */
  5401. port = wolfSSLPort;
  5402. #endif
  5403. wolfSSL_CTX_set_verify(ctx,
  5404. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5405. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5406. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5407. #endif
  5408. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5409. if (callbacks->method == wolfDTLSv1_2_server_method) {
  5410. AssertIntEQ(WOLFSSL_SUCCESS,
  5411. wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5412. }
  5413. #endif
  5414. AssertIntEQ(WOLFSSL_SUCCESS,
  5415. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5416. AssertIntEQ(WOLFSSL_SUCCESS,
  5417. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5418. WOLFSSL_FILETYPE_PEM));
  5419. AssertIntEQ(WOLFSSL_SUCCESS,
  5420. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5421. WOLFSSL_FILETYPE_PEM));
  5422. #ifdef HAVE_CRL
  5423. if (callbacks->crlPemFile != NULL) {
  5424. AssertIntEQ(WOLFSSL_SUCCESS,
  5425. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5426. WOLFSSL_FILETYPE_PEM));
  5427. }
  5428. #endif
  5429. if (callbacks->ctx_ready)
  5430. callbacks->ctx_ready(ctx);
  5431. ssl = wolfSSL_new(ctx);
  5432. if (ssl == NULL) {
  5433. fprintf(stderr, "SSL new failed\n");
  5434. wolfSSL_CTX_free(ctx);
  5435. return 0;
  5436. }
  5437. if (wolfSSL_dtls(ssl)) {
  5438. SOCKADDR_IN_T cliAddr;
  5439. socklen_t cliLen;
  5440. cliLen = sizeof(cliAddr);
  5441. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5442. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5443. (struct sockaddr*)&cliAddr, &cliLen);
  5444. AssertIntGT(idx, 0);
  5445. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5446. }
  5447. else {
  5448. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5449. CloseSocket(sfd);
  5450. }
  5451. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5452. if (callbacks->loadToSSL) {
  5453. wolfSSL_SetDevId(ssl, callbacks->devId);
  5454. AssertIntEQ(WOLFSSL_SUCCESS,
  5455. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5456. WOLFSSL_FILETYPE_PEM));
  5457. AssertIntEQ(WOLFSSL_SUCCESS,
  5458. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5459. WOLFSSL_FILETYPE_PEM));
  5460. }
  5461. #ifdef NO_PSK
  5462. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5463. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5464. #elif !defined(NO_DH)
  5465. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5466. #endif
  5467. #endif
  5468. if (callbacks->ssl_ready)
  5469. callbacks->ssl_ready(ssl);
  5470. #ifdef WOLFSSL_ASYNC_CRYPT
  5471. err = 0; /* Reset error */
  5472. #endif
  5473. do {
  5474. #ifdef WOLFSSL_ASYNC_CRYPT
  5475. if (err == WC_PENDING_E) {
  5476. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5477. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5478. }
  5479. #endif
  5480. ret = wolfSSL_accept(ssl);
  5481. err = wolfSSL_get_error(ssl, ret);
  5482. } while (err == WC_PENDING_E);
  5483. if (ret != WOLFSSL_SUCCESS) {
  5484. char buff[WOLFSSL_MAX_ERROR_SZ];
  5485. fprintf(stderr, "accept error = %d, %s\n", err,
  5486. wolfSSL_ERR_error_string(err, buff));
  5487. /*err_sys("SSL_accept failed");*/
  5488. }
  5489. else {
  5490. #ifdef WOLFSSL_ASYNC_CRYPT
  5491. err = 0; /* Reset error */
  5492. #endif
  5493. do {
  5494. #ifdef WOLFSSL_ASYNC_CRYPT
  5495. if (err == WC_PENDING_E) {
  5496. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5497. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5498. }
  5499. #endif
  5500. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5501. err = wolfSSL_get_error(ssl, idx);
  5502. } while (err == WC_PENDING_E);
  5503. if (idx > 0) {
  5504. input[idx] = 0;
  5505. fprintf(stderr, "Client message: %s\n", input);
  5506. }
  5507. #ifdef WOLFSSL_ASYNC_CRYPT
  5508. err = 0; /* Reset error */
  5509. #endif
  5510. do {
  5511. #ifdef WOLFSSL_ASYNC_CRYPT
  5512. if (err == WC_PENDING_E) {
  5513. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5514. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5515. }
  5516. #endif
  5517. ret = wolfSSL_write(ssl, msg, len);
  5518. err = wolfSSL_get_error(ssl, ret);
  5519. } while (err == WC_PENDING_E);
  5520. AssertIntEQ(len, ret);
  5521. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5522. defined(WOLFSSL_DTLS)
  5523. if (wolfSSL_dtls(ssl)) {
  5524. byte* import;
  5525. word32 sz;
  5526. wolfSSL_dtls_export(ssl, NULL, &sz);
  5527. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5528. AssertNotNull(import);
  5529. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5530. AssertIntGE(idx, 0);
  5531. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5532. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5533. }
  5534. #endif
  5535. #ifdef WOLFSSL_TIRTOS
  5536. Task_yield();
  5537. #endif
  5538. ((func_args*)args)->return_code = TEST_SUCCESS;
  5539. }
  5540. if (callbacks->on_result)
  5541. callbacks->on_result(ssl);
  5542. wolfSSL_shutdown(ssl);
  5543. wolfSSL_free(ssl);
  5544. wolfSSL_CTX_free(ctx);
  5545. CloseSocket(cfd);
  5546. #ifdef WOLFSSL_TIRTOS
  5547. fdCloseSession(Task_self());
  5548. #endif
  5549. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5550. && defined(HAVE_THREAD_LS)
  5551. wc_ecc_fp_free(); /* free per thread cache */
  5552. #endif
  5553. #ifndef WOLFSSL_TIRTOS
  5554. return 0;
  5555. #endif
  5556. }
  5557. /* TLS Client for API unit testing - generic */
  5558. static void run_wolfssl_client(void* args)
  5559. {
  5560. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5561. WOLFSSL_CTX* ctx = NULL;
  5562. WOLFSSL* ssl = NULL;
  5563. SOCKET_T sfd = 0;
  5564. char msg[] = "hello wolfssl server!";
  5565. int len = (int) XSTRLEN(msg);
  5566. char input[1024];
  5567. int ret, err = 0;
  5568. ((func_args*)args)->return_code = TEST_FAIL;
  5569. /* set defaults */
  5570. if (callbacks->caPemFile == NULL)
  5571. callbacks->caPemFile = caCertFile;
  5572. if (callbacks->certPemFile == NULL)
  5573. callbacks->certPemFile = cliCertFile;
  5574. if (callbacks->keyPemFile == NULL)
  5575. callbacks->keyPemFile = cliKeyFile;
  5576. #ifdef WOLFSSL_STATIC_MEMORY
  5577. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5578. callbacks->memSz > 0) {
  5579. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5580. callbacks->mem, callbacks->memSz, 0, 1);
  5581. if (ret != WOLFSSL_SUCCESS) {
  5582. fprintf(stderr, "CTX static new failed %d\n", ret);
  5583. return;
  5584. }
  5585. }
  5586. #else
  5587. if (ctx == NULL) {
  5588. ctx = wolfSSL_CTX_new(callbacks->method());
  5589. }
  5590. if (ctx == NULL) {
  5591. fprintf(stderr, "CTX new failed\n");
  5592. return;
  5593. }
  5594. #endif
  5595. #ifdef WOLFSSL_TIRTOS
  5596. fdOpenSession(Task_self());
  5597. #endif
  5598. if (!callbacks->loadToSSL) {
  5599. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5600. }
  5601. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5602. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5603. #endif
  5604. AssertIntEQ(WOLFSSL_SUCCESS,
  5605. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5606. if (!callbacks->loadToSSL) {
  5607. AssertIntEQ(WOLFSSL_SUCCESS,
  5608. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5609. WOLFSSL_FILETYPE_PEM));
  5610. AssertIntEQ(WOLFSSL_SUCCESS,
  5611. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5612. WOLFSSL_FILETYPE_PEM));
  5613. }
  5614. #ifdef HAVE_CRL
  5615. if (callbacks->crlPemFile != NULL) {
  5616. AssertIntEQ(WOLFSSL_SUCCESS,
  5617. wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  5618. WOLFSSL_FILETYPE_PEM));
  5619. }
  5620. #endif
  5621. if (callbacks->ctx_ready)
  5622. callbacks->ctx_ready(ctx);
  5623. ssl = wolfSSL_new(ctx);
  5624. if (wolfSSL_dtls(ssl)) {
  5625. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5626. 1, 0, ssl);
  5627. }
  5628. else {
  5629. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5630. 0, 0, ssl);
  5631. }
  5632. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5633. if (callbacks->loadToSSL) {
  5634. wolfSSL_SetDevId(ssl, callbacks->devId);
  5635. AssertIntEQ(WOLFSSL_SUCCESS,
  5636. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5637. WOLFSSL_FILETYPE_PEM));
  5638. AssertIntEQ(WOLFSSL_SUCCESS,
  5639. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5640. WOLFSSL_FILETYPE_PEM));
  5641. }
  5642. if (callbacks->ssl_ready)
  5643. callbacks->ssl_ready(ssl);
  5644. #ifdef WOLFSSL_ASYNC_CRYPT
  5645. err = 0; /* Reset error */
  5646. #endif
  5647. do {
  5648. #ifdef WOLFSSL_ASYNC_CRYPT
  5649. if (err == WC_PENDING_E) {
  5650. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5651. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5652. }
  5653. #endif
  5654. ret = wolfSSL_connect(ssl);
  5655. err = wolfSSL_get_error(ssl, ret);
  5656. } while (err == WC_PENDING_E);
  5657. if (ret != WOLFSSL_SUCCESS) {
  5658. char buff[WOLFSSL_MAX_ERROR_SZ];
  5659. fprintf(stderr, "error = %d, %s\n", err,
  5660. wolfSSL_ERR_error_string(err, buff));
  5661. /*err_sys("SSL_connect failed");*/
  5662. }
  5663. else {
  5664. #ifdef WOLFSSL_ASYNC_CRYPT
  5665. err = 0; /* Reset error */
  5666. #endif
  5667. do {
  5668. #ifdef WOLFSSL_ASYNC_CRYPT
  5669. if (err == WC_PENDING_E) {
  5670. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5671. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5672. }
  5673. #endif
  5674. ret = wolfSSL_write(ssl, msg, len);
  5675. err = wolfSSL_get_error(ssl, ret);
  5676. } while (err == WC_PENDING_E);
  5677. AssertIntEQ(len, ret);
  5678. #ifdef WOLFSSL_ASYNC_CRYPT
  5679. err = 0; /* Reset error */
  5680. #endif
  5681. do {
  5682. #ifdef WOLFSSL_ASYNC_CRYPT
  5683. if (err == WC_PENDING_E) {
  5684. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5685. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5686. }
  5687. #endif
  5688. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5689. err = wolfSSL_get_error(ssl, ret);
  5690. } while (err == WC_PENDING_E);
  5691. if (ret > 0) {
  5692. input[ret] = '\0'; /* null term */
  5693. fprintf(stderr, "Server response: %s\n", input);
  5694. }
  5695. ((func_args*)args)->return_code = TEST_SUCCESS;
  5696. }
  5697. if (callbacks->on_result)
  5698. callbacks->on_result(ssl);
  5699. wolfSSL_free(ssl);
  5700. wolfSSL_CTX_free(ctx);
  5701. CloseSocket(sfd);
  5702. #ifdef WOLFSSL_TIRTOS
  5703. fdCloseSession(Task_self());
  5704. #endif
  5705. }
  5706. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5707. static int test_wolfSSL_read_write(void)
  5708. {
  5709. /* The unit testing for read and write shall happen simultaneously, since
  5710. * one can't do anything with one without the other. (Except for a failure
  5711. * test case.) This function will call all the others that will set up,
  5712. * execute, and report their test findings.
  5713. *
  5714. * Set up the success case first. This function will become the template
  5715. * for the other tests. This should eventually be renamed
  5716. *
  5717. * The success case isn't interesting, how can this fail?
  5718. * - Do not give the client context a CA certificate. The connect should
  5719. * fail. Do not need server for this?
  5720. * - Using NULL for the ssl object on server. Do not need client for this.
  5721. * - Using NULL for the ssl object on client. Do not need server for this.
  5722. * - Good ssl objects for client and server. Client write() without server
  5723. * read().
  5724. * - Good ssl objects for client and server. Server write() without client
  5725. * read().
  5726. * - Forgetting the password callback?
  5727. */
  5728. tcp_ready ready;
  5729. func_args client_args;
  5730. func_args server_args;
  5731. THREAD_TYPE serverThread;
  5732. XMEMSET(&client_args, 0, sizeof(func_args));
  5733. XMEMSET(&server_args, 0, sizeof(func_args));
  5734. #ifdef WOLFSSL_TIRTOS
  5735. fdOpenSession(Task_self());
  5736. #endif
  5737. StartTCP();
  5738. InitTcpReady(&ready);
  5739. #if defined(USE_WINDOWS_API)
  5740. /* use RNG to get random port if using windows */
  5741. ready.port = GetRandomPort();
  5742. #endif
  5743. server_args.signal = &ready;
  5744. client_args.signal = &ready;
  5745. start_thread(test_server_nofail, &server_args, &serverThread);
  5746. wait_tcp_ready(&server_args);
  5747. test_client_nofail(&client_args, NULL);
  5748. join_thread(serverThread);
  5749. AssertTrue(client_args.return_code);
  5750. AssertTrue(server_args.return_code);
  5751. FreeTcpReady(&ready);
  5752. #ifdef WOLFSSL_TIRTOS
  5753. fdOpenSession(Task_self());
  5754. #endif
  5755. return TEST_RES_CHECK(1);
  5756. }
  5757. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5758. {
  5759. int res = TEST_SKIPPED;
  5760. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5761. !defined(WOLFSSL_TLS13)
  5762. /* The unit test for session resumption by re-using WOLFSSL object.
  5763. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5764. * for second connection.
  5765. */
  5766. tcp_ready ready;
  5767. func_args client_args;
  5768. func_args server_args;
  5769. THREAD_TYPE serverThread;
  5770. XMEMSET(&client_args, 0, sizeof(func_args));
  5771. XMEMSET(&server_args, 0, sizeof(func_args));
  5772. #ifdef WOLFSSL_TIRTOS
  5773. fdOpenSession(Task_self());
  5774. #endif
  5775. StartTCP();
  5776. InitTcpReady(&ready);
  5777. #if defined(USE_WINDOWS_API)
  5778. /* use RNG to get random port if using windows */
  5779. ready.port = GetRandomPort();
  5780. #endif
  5781. server_args.signal = &ready;
  5782. client_args.signal = &ready;
  5783. /* the var is used for loop number */
  5784. server_args.argc = 2;
  5785. start_thread(test_server_loop, &server_args, &serverThread);
  5786. wait_tcp_ready(&server_args);
  5787. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5788. join_thread(serverThread);
  5789. AssertTrue(client_args.return_code);
  5790. AssertTrue(server_args.return_code);
  5791. FreeTcpReady(&ready);
  5792. #ifdef WOLFSSL_TIRTOS
  5793. fdOpenSession(Task_self());
  5794. #endif
  5795. res = TEST_RES_CHECK(1);
  5796. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5797. return res;
  5798. }
  5799. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5800. {
  5801. int res = TEST_SKIPPED;
  5802. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5803. /* This unit test is to check set verify Depth */
  5804. tcp_ready ready;
  5805. func_args client_args;
  5806. func_args server_args;
  5807. THREAD_TYPE serverThread;
  5808. callback_functions client_cbf;
  5809. XMEMSET(&client_args, 0, sizeof(func_args));
  5810. XMEMSET(&server_args, 0, sizeof(func_args));
  5811. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5812. #ifdef WOLFSSL_TLS13
  5813. client_cbf.method = wolfTLSv1_3_client_method;
  5814. #endif /* WOLFSSL_TLS13 */
  5815. client_args.callbacks = &client_cbf;
  5816. StartTCP();
  5817. InitTcpReady(&ready);
  5818. #if defined(USE_WINDOWS_API)
  5819. /* use RNG to get random port if using windows */
  5820. ready.port = GetRandomPort();
  5821. #endif
  5822. server_args.signal = &ready;
  5823. client_args.signal = &ready;
  5824. /* the var is used for loop number */
  5825. server_args.argc = 1;
  5826. /* test case 1 verify depth is equal to peer chain */
  5827. {
  5828. start_thread(test_server_nofail, &server_args, &serverThread);
  5829. wait_tcp_ready(&server_args);
  5830. /* the var is used for verify depth */
  5831. client_args.argc = 2;
  5832. test_client_verifyDepth(&client_args);
  5833. join_thread(serverThread);
  5834. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5835. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5836. }
  5837. /* test case 2
  5838. * verify depth is zero, number of peer's chain is 2.
  5839. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5840. * callback.
  5841. */
  5842. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5843. {
  5844. start_thread(test_server_nofail, &server_args, &serverThread);
  5845. wait_tcp_ready(&server_args);
  5846. client_args.argc = 0;
  5847. test_client_verifyDepth(&client_args);
  5848. join_thread(serverThread);
  5849. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5850. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5851. }
  5852. /* test case 3
  5853. * verify depth is zero, number of peer's chain is 2
  5854. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5855. * therefore, handshake becomes failure.
  5856. */
  5857. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5858. {
  5859. start_thread(test_server_nofail, &server_args, &serverThread);
  5860. wait_tcp_ready(&server_args);
  5861. client_args.argc = -1;
  5862. test_client_verifyDepth(&client_args);
  5863. join_thread(serverThread);
  5864. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5865. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5866. }
  5867. FreeTcpReady(&ready);
  5868. res = TEST_RES_CHECK(1);
  5869. #else
  5870. (void)test_client_verifyDepth;
  5871. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5872. return res;
  5873. }
  5874. static int test_wolfSSL_CTX_set_cipher_list(void)
  5875. {
  5876. int res = TEST_SKIPPED;
  5877. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5878. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  5879. && !defined(NO_SHA256) && defined(HAVE_ECC)
  5880. WOLFSSL_CTX* ctx;
  5881. WOLFSSL_CTX* ctxClient;
  5882. WOLFSSL* sslClient;
  5883. tcp_ready ready;
  5884. func_args client_args;
  5885. func_args server_args;
  5886. callback_functions client_cb;
  5887. callback_functions server_cb;
  5888. THREAD_TYPE serverThread;
  5889. XMEMSET(&client_args, 0, sizeof(func_args));
  5890. XMEMSET(&server_args, 0, sizeof(func_args));
  5891. StartTCP();
  5892. InitTcpReady(&ready);
  5893. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5894. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5895. AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
  5896. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  5897. AssertIntEQ(WOLFSSL_SUCCESS,
  5898. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5899. AssertIntEQ(WOLFSSL_SUCCESS,
  5900. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5901. AssertIntEQ(WOLFSSL_SUCCESS,
  5902. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5903. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5904. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
  5905. client_cb.ctx = ctxClient;
  5906. server_cb.ctx = ctx;
  5907. /* we are responsible for free'ing WOLFSSL_CTX */
  5908. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  5909. server_args.signal = &ready;
  5910. server_args.callbacks = &server_cb;
  5911. client_args.signal = &ready;
  5912. client_args.callbacks = &client_cb;
  5913. client_args.return_code = TEST_FAIL;
  5914. start_thread(test_server_nofail, &server_args, &serverThread);
  5915. wait_tcp_ready(&server_args);
  5916. test_client_nofail(&client_args, NULL);
  5917. join_thread(serverThread);
  5918. wolfSSL_CTX_free(client_cb.ctx);
  5919. wolfSSL_CTX_free(server_cb.ctx);
  5920. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5921. FreeTcpReady(&ready);
  5922. /* check with cipher string that has '+' */
  5923. AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  5924. AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  5925. AssertNotNull((sslClient = wolfSSL_new(ctxClient)));
  5926. /* check for the existance of an ECDHE ECDSA cipher suite */
  5927. {
  5928. int i = 0;
  5929. int found = 0;
  5930. const char* suite;
  5931. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk;
  5932. WOLFSSL_CIPHER* current;
  5933. AssertNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  5934. do {
  5935. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  5936. if (current) {
  5937. suite = wolfSSL_CIPHER_get_name(current);
  5938. if (suite && XSTRSTR(suite, "ECDSA")) {
  5939. found = 1;
  5940. break;
  5941. }
  5942. }
  5943. } while (current);
  5944. AssertIntEQ(found, 1);
  5945. }
  5946. wolfSSL_free(sslClient);
  5947. wolfSSL_CTX_free(ctxClient);
  5948. res = TEST_RES_CHECK(1);
  5949. #endif
  5950. return res;
  5951. }
  5952. static int test_client_get_finished(void* args, cbType cb)
  5953. {
  5954. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5955. SOCKET_T sockfd = 0;
  5956. callback_functions* cbf;
  5957. WOLFSSL_CTX* ctx = 0;
  5958. WOLFSSL* ssl = 0;
  5959. char msg[64] = "hello wolfssl!";
  5960. char reply[1024];
  5961. int msgSz = (int)XSTRLEN(msg);
  5962. int ret, err = 0;
  5963. WOLFSSL_METHOD* method = NULL;
  5964. size_t msg_len = 0;
  5965. (void) args;
  5966. (void) cb;
  5967. ((func_args*)args)->return_code = TEST_FAIL;
  5968. cbf = ((func_args*)args)->callbacks;
  5969. if (cbf != NULL && cbf->method != NULL) {
  5970. method = cbf->method();
  5971. }
  5972. else {
  5973. method = wolfSSLv23_client_method();
  5974. }
  5975. ctx = wolfSSL_CTX_new(method);
  5976. /* Do connect here so server detects failures */
  5977. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5978. 0, 0, NULL);
  5979. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5980. {
  5981. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5982. goto done;
  5983. }
  5984. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5985. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5986. goto done;
  5987. }
  5988. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5989. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5990. goto done;
  5991. }
  5992. /* call ctx setup callback */
  5993. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5994. cbf->ctx_ready(ctx);
  5995. }
  5996. ssl = wolfSSL_new(ctx);
  5997. if (ssl == NULL) {
  5998. goto done;
  5999. }
  6000. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6001. goto done;
  6002. }
  6003. /* call ssl setup callback */
  6004. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6005. cbf->ssl_ready(ssl);
  6006. }
  6007. #ifdef WOLFSSL_ASYNC_CRYPT
  6008. err = 0; /* Reset error */
  6009. #endif
  6010. do {
  6011. #ifdef WOLFSSL_ASYNC_CRYPT
  6012. if (err == WC_PENDING_E) {
  6013. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6014. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6015. }
  6016. #endif
  6017. ret = wolfSSL_connect(ssl);
  6018. err = wolfSSL_get_error(ssl, 0);
  6019. } while (err == WC_PENDING_E);
  6020. if (ret != WOLFSSL_SUCCESS) {
  6021. char buff[WOLFSSL_MAX_ERROR_SZ];
  6022. fprintf(stderr, "error = %d, %s\n", err,
  6023. wolfSSL_ERR_error_string(err, buff));
  6024. goto done;
  6025. }
  6026. /* get_finished test */
  6027. /* 1. get own sent message */
  6028. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6029. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6030. AssertIntGE(msg_len, 0);
  6031. /* 2. get peer message */
  6032. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6033. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6034. AssertIntGE(msg_len, 0);
  6035. if (cb != NULL)
  6036. (cb)(ctx, ssl);
  6037. #ifdef WOLFSSL_ASYNC_CRYPT
  6038. err = 0; /* Reset error */
  6039. #endif
  6040. do {
  6041. #ifdef WOLFSSL_ASYNC_CRYPT
  6042. if (err == WC_PENDING_E) {
  6043. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6044. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6045. }
  6046. #endif
  6047. ret = wolfSSL_write(ssl, msg, msgSz);
  6048. err = wolfSSL_get_error(ssl, 0);
  6049. } while (err == WC_PENDING_E);
  6050. if (ret != msgSz) {
  6051. /*err_sys("SSL_write failed");*/
  6052. goto done;
  6053. }
  6054. #ifdef WOLFSSL_ASYNC_CRYPT
  6055. err = 0; /* Reset error */
  6056. #endif
  6057. do {
  6058. #ifdef WOLFSSL_ASYNC_CRYPT
  6059. if (err == WC_PENDING_E) {
  6060. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6061. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6062. }
  6063. #endif
  6064. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6065. err = wolfSSL_get_error(ssl, 0);
  6066. } while (err == WC_PENDING_E);
  6067. if (ret > 0) {
  6068. reply[ret] = '\0';
  6069. fprintf(stderr, "Server response: %s\n", reply);
  6070. }
  6071. ((func_args*)args)->return_code = TEST_SUCCESS;
  6072. done:
  6073. wolfSSL_free(ssl);
  6074. wolfSSL_CTX_free(ctx);
  6075. CloseSocket(sockfd);
  6076. #else
  6077. (void)args;
  6078. (void)cb;
  6079. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  6080. return 0;
  6081. }
  6082. static int test_wolfSSL_get_finished(void)
  6083. {
  6084. int res = TEST_SKIPPED;
  6085. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6086. tcp_ready ready;
  6087. func_args client_args;
  6088. func_args server_args;
  6089. THREAD_TYPE serverThread;
  6090. XMEMSET(&client_args, 0, sizeof(func_args));
  6091. XMEMSET(&server_args, 0, sizeof(func_args));
  6092. StartTCP();
  6093. InitTcpReady(&ready);
  6094. #if defined(USE_WINDOWS_API)
  6095. /* use RNG to get random port if using windows */
  6096. ready.port = GetRandomPort();
  6097. #endif
  6098. server_args.signal = &ready;
  6099. client_args.signal = &ready;
  6100. start_thread(test_server_nofail, &server_args, &serverThread);
  6101. wait_tcp_ready(&server_args);
  6102. test_client_get_finished(&client_args, NULL);
  6103. join_thread(serverThread);
  6104. AssertTrue(client_args.return_code);
  6105. AssertTrue(server_args.return_code);
  6106. /* test received msg vs sent msg */
  6107. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6108. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6109. FreeTcpReady(&ready);
  6110. res = TEST_RES_CHECK(1);
  6111. #else
  6112. (void)test_client_get_finished;
  6113. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6114. return res;
  6115. }
  6116. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6117. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6118. !defined(NO_SESSION_CACHE)
  6119. /* Sessions to restore/store */
  6120. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6121. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6122. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6123. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6124. {
  6125. /* Don't store sessions. Lookup is still enabled. */
  6126. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6127. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6128. #ifdef OPENSSL_EXTRA
  6129. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6130. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6131. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6132. #endif
  6133. /* Require both peers to provide certs */
  6134. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6135. }
  6136. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6137. {
  6138. WOLFSSL_SESSION** sess;
  6139. if (wolfSSL_is_server(ssl))
  6140. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6141. else
  6142. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6143. if (*sess == NULL) {
  6144. #ifdef NO_SESSION_CACHE_REF
  6145. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6146. #else
  6147. /* Test for backwards compatibility */
  6148. if (wolfSSL_is_server(ssl)) {
  6149. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6150. }
  6151. else {
  6152. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6153. }
  6154. #endif
  6155. /* Now save the session in the internal store to make it available
  6156. * for lookup. For TLS 1.3, we can't save the session without
  6157. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6158. * session from cache. */
  6159. if (wolfSSL_is_server(ssl)
  6160. #ifndef WOLFSSL_TICKET_HAVE_ID
  6161. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6162. #endif
  6163. )
  6164. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6165. *sess), WOLFSSL_SUCCESS);
  6166. }
  6167. else {
  6168. /* If we have a session retrieved then remaining connections should be
  6169. * resuming on that session */
  6170. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6171. }
  6172. /* Save CTX to be able to decrypt tickets */
  6173. if (wolfSSL_is_server(ssl) &&
  6174. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6175. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6176. = wolfSSL_get_SSL_CTX(ssl));
  6177. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6178. WOLFSSL_SUCCESS);
  6179. }
  6180. #ifdef SESSION_CERTS
  6181. #ifndef WOLFSSL_TICKET_HAVE_ID
  6182. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6183. wolfSSL_session_reused(ssl))
  6184. #endif
  6185. {
  6186. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6187. * for all connections. TLS 1.3 only has tickets so if we don't
  6188. * include the session id in the ticket then the certificates
  6189. * will not be available on resumption. */
  6190. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6191. AssertNotNull(peer);
  6192. wolfSSL_X509_free(peer);
  6193. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6194. #ifdef OPENSSL_EXTRA
  6195. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6196. #endif
  6197. }
  6198. #endif /* SESSION_CERTS */
  6199. }
  6200. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6201. {
  6202. /* Set the session to reuse for the client */
  6203. AssertIntEQ(wolfSSL_set_session(ssl,
  6204. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6205. }
  6206. #endif
  6207. static int test_wolfSSL_CTX_add_session(void)
  6208. {
  6209. int res = TEST_SKIPPED;
  6210. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6211. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6212. !defined(NO_SESSION_CACHE)
  6213. tcp_ready ready;
  6214. func_args client_args;
  6215. func_args server_args;
  6216. THREAD_TYPE serverThread;
  6217. callback_functions client_cb;
  6218. callback_functions server_cb;
  6219. method_provider methods[][2] = {
  6220. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6221. !defined(NO_DES3))
  6222. /* Without AES there are almost no ciphersuites available. This leads
  6223. * to no ciphersuites being available and an error. */
  6224. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6225. #endif
  6226. #ifndef WOLFSSL_NO_TLS12
  6227. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6228. #endif
  6229. /* Needs the default ticket callback since it is tied to the
  6230. * connection context and this makes it easy to carry over the ticket
  6231. * crypto context between connections */
  6232. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6233. defined(HAVE_SESSION_TICKET)
  6234. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6235. #endif
  6236. };
  6237. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6238. size_t i, j;
  6239. for (i = 0; i < methodsLen; i++) {
  6240. /* First run creates a connection while the second+ run will attempt
  6241. * to resume the connection. The trick is that the internal cache
  6242. * is turned off. wolfSSL_CTX_add_session should put the session in
  6243. * the cache anyway. */
  6244. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6245. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6246. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6247. #ifdef NO_SESSION_CACHE_REF
  6248. for (j = 0; j < 5; j++) {
  6249. #else
  6250. /* The session may be overwritten in this case. Do only one resumption
  6251. * to stop this test from failing intermittently. */
  6252. for (j = 0; j < 2; j++) {
  6253. #endif
  6254. #ifdef WOLFSSL_TIRTOS
  6255. fdOpenSession(Task_self());
  6256. #endif
  6257. StartTCP();
  6258. InitTcpReady(&ready);
  6259. XMEMSET(&client_args, 0, sizeof(func_args));
  6260. XMEMSET(&server_args, 0, sizeof(func_args));
  6261. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6262. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6263. client_cb.method = methods[i][0];
  6264. server_cb.method = methods[i][1];
  6265. server_args.signal = &ready;
  6266. server_args.callbacks = &server_cb;
  6267. client_args.signal = &ready;
  6268. client_args.callbacks = &client_cb;
  6269. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6270. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6271. server_cb.isSharedCtx = 1;
  6272. }
  6273. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6274. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6275. if (j != 0)
  6276. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6277. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6278. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6279. server_cb.ticNoInit = 1; /* Use default builtin */
  6280. start_thread(test_server_nofail, &server_args, &serverThread);
  6281. wait_tcp_ready(&server_args);
  6282. test_client_nofail(&client_args, NULL);
  6283. join_thread(serverThread);
  6284. AssertTrue(client_args.return_code);
  6285. AssertTrue(server_args.return_code);
  6286. FreeTcpReady(&ready);
  6287. }
  6288. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6289. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6290. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6291. }
  6292. res = TEST_RES_CHECK(1);
  6293. #endif
  6294. return res;
  6295. }
  6296. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6297. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6298. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  6299. defined(SESSION_CERTS) && defined(HAVE_SESSION_TICKET) && \
  6300. !defined(TITAN_SESSION_CACHE) && \
  6301. !defined(HUGE_SESSION_CACHE) && \
  6302. !defined(BIG_SESSION_CACHE) && \
  6303. !defined(MEDIUM_SESSION_CACHE)
  6304. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  6305. /* Sessions to restore/store */
  6306. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  6307. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  6308. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  6309. static int twcase_new_session_called = 0;
  6310. static int twcase_remove_session_called = 0;
  6311. static int twcase_get_session_called = 0;
  6312. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  6313. #define SESSION_CACHE_SIZE 33
  6314. typedef struct {
  6315. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  6316. WOLFSSL_SESSION* value;
  6317. } hashTable_entry;
  6318. typedef struct {
  6319. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  6320. size_t capacity; /* size of entries */
  6321. size_t length; /* number of items in the hash table */
  6322. wolfSSL_Mutex htLock; /* lock */
  6323. }hashTable;
  6324. static hashTable server_sessionCache;
  6325. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  6326. {
  6327. int i;
  6328. (void)ssl;
  6329. /*
  6330. * This example uses a hash table.
  6331. * Steps you should take for a non-demo code:
  6332. * - acquire a lock for the file named according to the session id
  6333. * - open the file
  6334. * - encrypt and write the SSL_SESSION object to the file
  6335. * - release the lock
  6336. *
  6337. * Return:
  6338. * 0: The callback does not wish to hold a reference of the sess
  6339. * 1: The callback wants to hold a reference of the sess. The callback is
  6340. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  6341. */
  6342. if (sess == NULL)
  6343. return 0;
  6344. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6345. return 0;
  6346. }
  6347. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6348. if (server_sessionCache.entries[i].value == NULL) {
  6349. if (sess->haveAltSessionID == 1)
  6350. server_sessionCache.entries[i].key = sess->altSessionID;
  6351. else
  6352. server_sessionCache.entries[i].key = sess->sessionID;
  6353. server_sessionCache.entries[i].value = sess;
  6354. server_sessionCache.length++;
  6355. break;
  6356. }
  6357. }
  6358. ++twcase_new_session_called;
  6359. wc_UnLockMutex(&server_sessionCache.htLock);
  6360. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  6361. twcase_new_session_called);
  6362. return 1;
  6363. }
  6364. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  6365. {
  6366. int i;
  6367. (void)ctx;
  6368. (void)sess;
  6369. if (sess == NULL)
  6370. return;
  6371. /*
  6372. * This example uses a hash table.
  6373. * Steps you should take for a non-demo code:
  6374. * - acquire a lock for the file named according to the session id
  6375. * - remove the file
  6376. * - release the lock
  6377. */
  6378. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  6379. return;
  6380. }
  6381. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6382. if (server_sessionCache.entries[i].key != NULL &&
  6383. XMEMCMP(server_sessionCache.entries[i].key,
  6384. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6385. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6386. server_sessionCache.entries[i].value = NULL;
  6387. server_sessionCache.entries[i].key = NULL;
  6388. server_sessionCache.length--;
  6389. break;
  6390. }
  6391. }
  6392. ++twcase_remove_session_called;
  6393. wc_UnLockMutex(&server_sessionCache.htLock);
  6394. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  6395. twcase_remove_session_called);
  6396. }
  6397. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  6398. const unsigned char *id, int len, int *ref)
  6399. {
  6400. int i;
  6401. (void)ssl;
  6402. (void)id;
  6403. (void)len;
  6404. /*
  6405. * This example uses a hash table.
  6406. * Steps you should take for a non-demo code:
  6407. * - acquire a lock for the file named according to the session id in the
  6408. * 2nd arg
  6409. * - read and decrypt contents of file and create a new SSL_SESSION
  6410. * - object release the lock
  6411. * - return the new session object
  6412. */
  6413. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  6414. ++twcase_get_session_called);
  6415. /* This callback want to retain a copy of the object. If we want wolfSSL to
  6416. * be responsible for the pointer then set to 0. */
  6417. *ref = 1;
  6418. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6419. if (server_sessionCache.entries[i].key != NULL &&
  6420. XMEMCMP(server_sessionCache.entries[i].key, id,
  6421. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  6422. return server_sessionCache.entries[i].value;
  6423. }
  6424. }
  6425. return NULL;
  6426. }
  6427. static void twcase_get_sessionCb_cleanup(void)
  6428. {
  6429. int i;
  6430. int cnt = 0;
  6431. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  6432. * for freeing sessions */
  6433. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  6434. if (server_sessionCache.entries[i].value != NULL) {
  6435. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  6436. cnt++;
  6437. }
  6438. }
  6439. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  6440. cnt);
  6441. }
  6442. static void twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  6443. {
  6444. /* off - Disable internal cache */
  6445. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6446. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6447. #ifdef OPENSSL_EXTRA
  6448. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6449. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6450. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6451. #endif
  6452. /* off - Donot setup external cache */
  6453. /* Require both peers to provide certs */
  6454. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6455. }
  6456. static void twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  6457. {
  6458. /* on - internal cache is on by default*/
  6459. /* off - Donot setup external cache */
  6460. /* Require both peers to provide certs */
  6461. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6462. }
  6463. static void twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  6464. {
  6465. /* off - Disable internal cache */
  6466. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6467. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6468. #ifdef OPENSSL_EXTRA
  6469. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6470. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6471. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6472. #endif
  6473. /* on - Enable external cache */
  6474. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6475. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6476. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6477. /* Require both peers to provide certs */
  6478. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6479. }
  6480. static void twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  6481. {
  6482. /* on - internal cache is on by default */
  6483. /* on - Enable external cache */
  6484. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6485. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6486. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6487. /* Require both peers to provide certs */
  6488. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6489. }
  6490. static void twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  6491. {
  6492. /* on - internal cache is on by default */
  6493. /* on - Enable external cache */
  6494. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  6495. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  6496. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  6497. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  6498. /* Require both peers to provide certs */
  6499. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6500. }
  6501. static void twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  6502. {
  6503. WOLFSSL_SESSION** sess;
  6504. if (wolfSSL_is_server(ssl))
  6505. sess = &twcase_server_first_session_ptr;
  6506. else
  6507. return;
  6508. if (*sess == NULL) {
  6509. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6510. /* Now save the session in the internal store to make it available
  6511. * for lookup. For TLS 1.3, we can't save the session without
  6512. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6513. * session from cache. */
  6514. if (wolfSSL_is_server(ssl)
  6515. #ifndef WOLFSSL_TICKET_HAVE_ID
  6516. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6517. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  6518. #endif
  6519. ) {
  6520. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6521. *sess), WOLFSSL_SUCCESS);
  6522. }
  6523. }
  6524. /* Save CTX to be able to decrypt tickets */
  6525. if (twcase_server_current_ctx_ptr == NULL) {
  6526. AssertNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  6527. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6528. WOLFSSL_SUCCESS);
  6529. }
  6530. #ifdef SESSION_CERTS
  6531. #ifndef WOLFSSL_TICKET_HAVE_ID
  6532. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6533. wolfSSL_session_reused(ssl))
  6534. #endif
  6535. {
  6536. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6537. * for all connections. TLS 1.3 only has tickets so if we don't
  6538. * include the session id in the ticket then the certificates
  6539. * will not be available on resumption. */
  6540. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6541. AssertNotNull(peer);
  6542. wolfSSL_X509_free(peer);
  6543. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6544. }
  6545. #endif
  6546. }
  6547. static void twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  6548. {
  6549. WOLFSSL_SESSION** sess;
  6550. sess = &twcase_client_first_session_ptr;
  6551. if (*sess == NULL) {
  6552. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6553. }
  6554. else {
  6555. /* If we have a session retrieved then remaining connections should be
  6556. * resuming on that session */
  6557. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6558. }
  6559. #ifdef SESSION_CERTS
  6560. #ifndef WOLFSSL_TICKET_HAVE_ID
  6561. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6562. wolfSSL_session_reused(ssl))
  6563. #endif
  6564. {
  6565. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6566. AssertNotNull(peer);
  6567. wolfSSL_X509_free(peer);
  6568. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6569. #ifdef OPENSSL_EXTRA
  6570. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  6571. #endif
  6572. }
  6573. #endif
  6574. }
  6575. static void twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  6576. {
  6577. /* Set the session to reuse for the client */
  6578. AssertNotNull(ssl);
  6579. AssertNotNull(twcase_client_first_session_ptr);
  6580. AssertIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  6581. WOLFSSL_SUCCESS);
  6582. }
  6583. #endif
  6584. static int test_wolfSSL_CTX_add_session_ext(void)
  6585. {
  6586. int res = TEST_SKIPPED;
  6587. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6588. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6589. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  6590. defined(SESSION_CERTS) && defined(HAVE_SESSION_TICKET) && \
  6591. !defined(TITAN_SESSION_CACHE) && \
  6592. !defined(HUGE_SESSION_CACHE) && \
  6593. !defined(BIG_SESSION_CACHE) && \
  6594. !defined(MEDIUM_SESSION_CACHE)
  6595. /* Test the default 33 sessions */
  6596. struct test_params {
  6597. method_provider client_meth;
  6598. method_provider server_meth;
  6599. const char* tls_version;
  6600. } params[] = {
  6601. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6602. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  6603. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  6604. #ifdef WOLFSSL_DTLS13
  6605. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  6606. #endif
  6607. #endif
  6608. #ifndef WOLFSSL_NO_TLS12
  6609. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  6610. #ifdef WOLFSSL_DTLS
  6611. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  6612. #endif
  6613. #endif
  6614. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6615. !defined(NO_DES3))
  6616. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  6617. #ifdef WOLFSSL_DTLS
  6618. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  6619. #endif
  6620. #endif
  6621. };
  6622. const int paramsLen = sizeof(params)/sizeof(*params);
  6623. int i, j;
  6624. /* Clear cache before starting */
  6625. wolfSSL_CTX_flush_sessions(NULL, -1);
  6626. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  6627. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  6628. return BAD_MUTEX_E;
  6629. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  6630. for (i = 0; i < paramsLen; i++) {
  6631. fprintf(stderr, "\tBegin %s\n", params[i].tls_version);
  6632. for (j = 0; j < 5; j++) {
  6633. int tls13 = XSTRSTR(params[i].tls_version, "TLSv1_3") != NULL;
  6634. int dtls = XSTRSTR(params[i].tls_version, "DTLS") != NULL;
  6635. callback_functions client_cb;
  6636. callback_functions server_cb;
  6637. (void)dtls;
  6638. /* Test five cache configurations */
  6639. twcase_client_first_session_ptr = NULL;
  6640. twcase_server_first_session_ptr = NULL;
  6641. twcase_server_current_ctx_ptr = NULL;
  6642. twcase_new_session_called = 0;
  6643. twcase_remove_session_called = 0;
  6644. twcase_get_session_called = 0;
  6645. /* connection 1 - first connection */
  6646. fprintf(stderr, "\tconnect: %s: j=%d, methodsLen=%d\n",
  6647. params[i].tls_version, j, paramsLen);
  6648. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6649. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6650. client_cb.method = params[i].client_meth;
  6651. server_cb.method = params[i].server_meth;
  6652. if (dtls)
  6653. client_cb.doUdp = server_cb.doUdp = 1;
  6654. /* Setup internal and external cache */
  6655. switch (j) {
  6656. case 0:
  6657. /* SSL_OP_NO_TICKET stateful ticket case */
  6658. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  6659. break;
  6660. case 1:
  6661. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  6662. break;
  6663. case 2:
  6664. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  6665. break;
  6666. case 3:
  6667. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  6668. break;
  6669. case 4:
  6670. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  6671. break;
  6672. }
  6673. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  6674. /* Add session to internal cache and save SSL session for testing */
  6675. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  6676. /* Save client SSL session for testing */
  6677. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  6678. server_cb.ticNoInit = 1; /* Use default builtin */
  6679. /* Don't free/release ctx */
  6680. server_cb.ctx = twcase_server_current_ctx_ptr;
  6681. server_cb.isSharedCtx = 1;
  6682. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  6683. AssertTrue(client_cb.return_code);
  6684. AssertTrue(server_cb.return_code);
  6685. AssertIntEQ(twcase_get_session_called, 0);
  6686. switch (j) {
  6687. case 0:
  6688. case 1:
  6689. case 2:
  6690. /* cache cannot be searched with out a connection */
  6691. /* Add a new session */
  6692. AssertIntEQ(twcase_new_session_called, 1);
  6693. /* In twcase_server_sess_ctx_pre_shutdown
  6694. * wolfSSL_CTX_add_session which evicts the existing session
  6695. * in cache and adds it back in */
  6696. AssertIntLE(twcase_remove_session_called, 1);
  6697. break;
  6698. case 3:
  6699. case 4:
  6700. /* no external cache */
  6701. AssertIntEQ(twcase_new_session_called, 0);
  6702. AssertIntEQ(twcase_remove_session_called, 0);
  6703. break;
  6704. }
  6705. /* connection 2 - session resume */
  6706. fprintf(stderr, "\tresume: %s: j=%d, methodsLen=%d\n",
  6707. params[i].tls_version, j, paramsLen);
  6708. twcase_new_session_called = 0;
  6709. twcase_remove_session_called = 0;
  6710. twcase_get_session_called = 0;
  6711. server_cb.on_result = 0;
  6712. client_cb.on_result = 0;
  6713. server_cb.ticNoInit = 1; /* Use default builtin */
  6714. server_cb.ctx = twcase_server_current_ctx_ptr;
  6715. /* try session resumption */
  6716. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  6717. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  6718. AssertTrue(client_cb.return_code);
  6719. AssertTrue(server_cb.return_code);
  6720. /* Clear cache before checking */
  6721. wolfSSL_CTX_flush_sessions(NULL, -1);
  6722. switch (j) {
  6723. case 0:
  6724. if (tls13) {
  6725. /* (D)TLSv1.3 stateful case */
  6726. /* cache hit */
  6727. /* DTLS accesses cache once for stateless parsing and
  6728. * once for stateful parsing */
  6729. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6730. /* (D)TLSv1.3 creates a new ticket,
  6731. * updates both internal and external cache */
  6732. AssertIntEQ(twcase_new_session_called, 1);
  6733. AssertIntEQ(twcase_remove_session_called, 1);
  6734. }
  6735. else {
  6736. /* non (D)TLSv1.3 case, no update */
  6737. /* DTLS accesses cache once for stateless parsing and
  6738. * once for stateful parsing */
  6739. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6740. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6741. #else
  6742. AssertIntEQ(twcase_get_session_called, 1);
  6743. #endif
  6744. AssertIntEQ(twcase_new_session_called, 0);
  6745. /* Called on session added in
  6746. * twcase_server_sess_ctx_pre_shutdown */
  6747. AssertIntEQ(twcase_remove_session_called, 1);
  6748. }
  6749. break;
  6750. case 1:
  6751. if (tls13) {
  6752. /* (D)TLSv1.3 case */
  6753. /* cache hit */
  6754. AssertIntEQ(twcase_get_session_called, 1);
  6755. /* (D)TLSv1.3 creates a new ticket,
  6756. * updates both internal and external cache */
  6757. AssertIntEQ(twcase_new_session_called, 1);
  6758. /* Called on session added in
  6759. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  6760. AssertIntEQ(twcase_remove_session_called, 1);
  6761. }
  6762. else {
  6763. /* non (D)TLSv1.3 case */
  6764. /* cache hit */
  6765. /* DTLS accesses cache once for stateless parsing and
  6766. * once for stateful parsing */
  6767. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6768. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6769. #else
  6770. AssertIntEQ(twcase_get_session_called, 1);
  6771. #endif
  6772. AssertIntEQ(twcase_new_session_called, 0);
  6773. /* Called on session added in
  6774. * twcase_server_sess_ctx_pre_shutdown */
  6775. AssertIntEQ(twcase_remove_session_called, 1);
  6776. }
  6777. break;
  6778. case 2:
  6779. if (tls13) {
  6780. /* (D)TLSv1.3 case */
  6781. /* cache hit */
  6782. AssertIntEQ(twcase_get_session_called, 1);
  6783. /* (D)TLSv1.3 creates a new ticket,
  6784. * updates both internal and external cache */
  6785. AssertIntEQ(twcase_new_session_called, 1);
  6786. /* Called on session added in
  6787. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  6788. AssertIntEQ(twcase_remove_session_called, 1);
  6789. }
  6790. else {
  6791. /* non (D)TLSv1.3 case */
  6792. /* cache hit */
  6793. /* DTLS accesses cache once for stateless parsing and
  6794. * once for stateful parsing */
  6795. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  6796. AssertIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  6797. #else
  6798. AssertIntEQ(twcase_get_session_called, 1);
  6799. #endif
  6800. AssertIntEQ(twcase_new_session_called, 0);
  6801. /* Called on session added in
  6802. * twcase_server_sess_ctx_pre_shutdown */
  6803. AssertIntEQ(twcase_remove_session_called, 1);
  6804. }
  6805. break;
  6806. case 3:
  6807. case 4:
  6808. /* no external cache */
  6809. AssertIntEQ(twcase_get_session_called, 0);
  6810. AssertIntEQ(twcase_new_session_called, 0);
  6811. AssertIntEQ(twcase_remove_session_called, 0);
  6812. break;
  6813. }
  6814. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  6815. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  6816. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  6817. }
  6818. twcase_get_sessionCb_cleanup();
  6819. XMEMSET(&server_sessionCache.entries, 0,
  6820. sizeof(server_sessionCache.entries));
  6821. fprintf(stderr, "\tEnd %s\n", params[i].tls_version);
  6822. }
  6823. wc_FreeMutex(&server_sessionCache.htLock);
  6824. res = TEST_RES_CHECK(1);
  6825. #endif
  6826. return res;
  6827. }
  6828. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6829. /* canned export of a session using older version 3 */
  6830. static unsigned char version_3[] = {
  6831. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6832. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6833. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6834. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6835. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6836. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6837. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6838. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6839. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6840. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6841. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6842. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6843. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6844. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6845. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6846. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6847. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6848. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6849. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6850. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6851. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6852. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6853. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6854. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6855. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6856. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6857. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6858. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6859. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6860. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6861. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6862. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  6863. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6864. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6865. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6866. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6867. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6868. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6869. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6870. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6871. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6872. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6873. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6874. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6875. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6876. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6877. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6878. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6879. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6880. 0x2E, 0x31, 0xED, 0x4F
  6881. };
  6882. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6883. static int test_wolfSSL_dtls_export(void)
  6884. {
  6885. int res = TEST_SKIPPED;
  6886. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6887. tcp_ready ready;
  6888. func_args client_args;
  6889. func_args server_args;
  6890. THREAD_TYPE serverThread;
  6891. callback_functions server_cbf;
  6892. callback_functions client_cbf;
  6893. #ifdef WOLFSSL_TIRTOS
  6894. fdOpenSession(Task_self());
  6895. #endif
  6896. InitTcpReady(&ready);
  6897. #if defined(USE_WINDOWS_API)
  6898. /* use RNG to get random port if using windows */
  6899. ready.port = GetRandomPort();
  6900. #endif
  6901. /* set using dtls */
  6902. XMEMSET(&client_args, 0, sizeof(func_args));
  6903. XMEMSET(&server_args, 0, sizeof(func_args));
  6904. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6905. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6906. server_cbf.method = wolfDTLSv1_2_server_method;
  6907. client_cbf.method = wolfDTLSv1_2_client_method;
  6908. server_args.callbacks = &server_cbf;
  6909. client_args.callbacks = &client_cbf;
  6910. server_args.signal = &ready;
  6911. client_args.signal = &ready;
  6912. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6913. wait_tcp_ready(&server_args);
  6914. run_wolfssl_client(&client_args);
  6915. join_thread(serverThread);
  6916. AssertTrue(client_args.return_code);
  6917. AssertTrue(server_args.return_code);
  6918. FreeTcpReady(&ready);
  6919. #ifdef WOLFSSL_TIRTOS
  6920. fdOpenSession(Task_self());
  6921. #endif
  6922. {
  6923. SOCKET_T sockfd = 0;
  6924. WOLFSSL_CTX* ctx;
  6925. WOLFSSL* ssl;
  6926. char msg[64] = "hello wolfssl!";
  6927. char reply[1024];
  6928. int msgSz = (int)XSTRLEN(msg);
  6929. byte *session, *window;
  6930. unsigned int sessionSz, windowSz;
  6931. #ifndef TEST_IPV6
  6932. struct sockaddr_in peerAddr;
  6933. #else
  6934. struct sockaddr_in6 peerAddr;
  6935. #endif /* TEST_IPV6 */
  6936. int i;
  6937. /* Set ctx to DTLS 1.2 */
  6938. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6939. AssertNotNull(ssl = wolfSSL_new(ctx));
  6940. /* test importing version 3 */
  6941. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6942. /* test importing bad length and bad version */
  6943. version_3[2] += 1;
  6944. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6945. version_3[2] -= 1; version_3[1] = 0XA0;
  6946. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6947. wolfSSL_free(ssl);
  6948. wolfSSL_CTX_free(ctx);
  6949. /* check storing client state after connection and storing window only */
  6950. #ifdef WOLFSSL_TIRTOS
  6951. fdOpenSession(Task_self());
  6952. #endif
  6953. InitTcpReady(&ready);
  6954. #if defined(USE_WINDOWS_API)
  6955. /* use RNG to get random port if using windows */
  6956. ready.port = GetRandomPort();
  6957. #endif
  6958. /* set using dtls */
  6959. XMEMSET(&server_args, 0, sizeof(func_args));
  6960. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6961. server_cbf.method = wolfDTLSv1_2_server_method;
  6962. server_cbf.doUdp = 1;
  6963. server_args.callbacks = &server_cbf;
  6964. server_args.argc = 3; /* set loop_count to 3 */
  6965. server_args.signal = &ready;
  6966. start_thread(test_server_nofail, &server_args, &serverThread);
  6967. wait_tcp_ready(&server_args);
  6968. /* create and connect with client */
  6969. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6970. AssertIntEQ(WOLFSSL_SUCCESS,
  6971. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6972. AssertIntEQ(WOLFSSL_SUCCESS,
  6973. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6974. AssertIntEQ(WOLFSSL_SUCCESS,
  6975. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6976. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6977. AssertNotNull(ssl = wolfSSL_new(ctx));
  6978. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6979. /* store server information connected too */
  6980. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6981. #ifndef TEST_IPV6
  6982. peerAddr.sin_family = AF_INET;
  6983. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6984. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6985. #else
  6986. peerAddr.sin6_family = AF_INET6;
  6987. AssertIntEQ(
  6988. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6989. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6990. #endif
  6991. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6992. WOLFSSL_SUCCESS);
  6993. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6994. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6995. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6996. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6997. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6998. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6999. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  7000. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7001. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7002. wolfSSL_free(ssl);
  7003. for (i = 1; i < server_args.argc; i++) {
  7004. /* restore state */
  7005. AssertNotNull(ssl = wolfSSL_new(ctx));
  7006. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  7007. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  7008. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7009. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7010. WOLFSSL_SUCCESS);
  7011. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7012. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7013. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7014. wolfSSL_free(ssl);
  7015. }
  7016. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7017. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7018. wolfSSL_CTX_free(ctx);
  7019. fprintf(stderr, "done and waiting for server\n");
  7020. join_thread(serverThread);
  7021. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7022. FreeTcpReady(&ready);
  7023. #ifdef WOLFSSL_TIRTOS
  7024. fdOpenSession(Task_self());
  7025. #endif
  7026. }
  7027. res = TEST_RES_CHECK(1);
  7028. #endif
  7029. return res;
  7030. }
  7031. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7032. #ifdef WOLFSSL_TLS13
  7033. static const byte canned_client_tls13_session[] = {
  7034. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  7035. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7036. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  7037. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7038. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7039. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7040. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7041. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  7042. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7043. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7044. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7045. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  7046. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7047. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7048. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7049. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7050. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7051. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7052. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7053. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7054. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7055. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7056. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7057. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7058. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7059. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7060. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7061. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7062. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7063. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  7064. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  7065. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  7066. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7067. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7068. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7069. 0x00, 0x03
  7070. };
  7071. static const byte canned_server_tls13_session[] = {
  7072. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  7073. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7074. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  7075. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7076. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7077. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7078. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7079. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  7080. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7081. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7082. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7083. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  7084. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7085. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7086. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7087. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7088. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7089. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7090. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7091. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7092. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7093. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7094. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7095. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7096. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7097. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7098. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7099. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7101. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  7102. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  7103. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  7104. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7105. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7106. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7107. 0x00, 0x04
  7108. };
  7109. #endif /* WOLFSSL_TLS13 */
  7110. static const byte canned_client_session[] = {
  7111. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7112. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7113. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  7114. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7115. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7116. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7118. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  7119. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7120. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7121. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7122. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  7123. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7124. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7125. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7126. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7127. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7128. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7129. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7130. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7131. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7132. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7133. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7134. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7135. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7136. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7137. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7138. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7139. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7144. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7145. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7146. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7147. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7148. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7149. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7150. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7151. 0x00, 0x03
  7152. };
  7153. static const byte canned_server_session[] = {
  7154. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  7155. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  7156. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7157. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7158. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  7161. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  7162. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  7163. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7164. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7165. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  7166. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  7167. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  7168. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  7169. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  7170. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  7171. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  7172. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  7173. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  7174. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  7175. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  7176. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  7177. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  7178. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  7179. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  7180. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  7181. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  7182. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  7183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7187. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  7188. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  7189. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  7190. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  7191. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  7192. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  7193. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7194. 0x00, 0x04
  7195. };
  7196. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  7197. {
  7198. SOCKET_T sockfd = 0;
  7199. SOCKET_T clientfd = 0;
  7200. word16 port;
  7201. callback_functions* cbf;
  7202. WOLFSSL_CTX* ctx = 0;
  7203. WOLFSSL* ssl = 0;
  7204. char msg[] = "I hear you fa shizzle!";
  7205. char input[1024];
  7206. int idx;
  7207. #ifdef WOLFSSL_TIRTOS
  7208. fdOpenSession(Task_self());
  7209. #endif
  7210. ((func_args*)args)->return_code = TEST_FAIL;
  7211. cbf = ((func_args*)args)->callbacks;
  7212. {
  7213. WOLFSSL_METHOD* method = NULL;
  7214. if (cbf != NULL && cbf->method != NULL) {
  7215. method = cbf->method();
  7216. }
  7217. else {
  7218. method = wolfTLSv1_2_server_method();
  7219. }
  7220. ctx = wolfSSL_CTX_new(method);
  7221. }
  7222. if (ctx == NULL) {
  7223. goto done;
  7224. }
  7225. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7226. #if defined(USE_WINDOWS_API)
  7227. port = ((func_args*)args)->signal->port;
  7228. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7229. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7230. /* Let tcp_listen assign port */
  7231. port = 0;
  7232. #else
  7233. /* Use default port */
  7234. port = wolfSSLPort;
  7235. #endif
  7236. /* do it here to detect failure */
  7237. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7238. CloseSocket(sockfd);
  7239. /* call ctx setup callback */
  7240. if (cbf != NULL && cbf->ctx_ready != NULL) {
  7241. cbf->ctx_ready(ctx);
  7242. }
  7243. ssl = wolfSSL_new(ctx);
  7244. if (ssl == NULL) {
  7245. goto done;
  7246. }
  7247. wolfSSL_set_fd(ssl, clientfd);
  7248. /* call ssl setup callback */
  7249. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7250. cbf->ssl_ready(ssl);
  7251. }
  7252. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7253. if (idx > 0) {
  7254. input[idx] = '\0';
  7255. fprintf(stderr, "Client message export/import: %s\n", input);
  7256. }
  7257. else {
  7258. fprintf(stderr, "ret = %d error = %d\n", idx,
  7259. wolfSSL_get_error(ssl, idx));
  7260. goto done;
  7261. }
  7262. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  7263. /*err_sys("SSL_write failed");*/
  7264. #ifdef WOLFSSL_TIRTOS
  7265. return;
  7266. #else
  7267. return 0;
  7268. #endif
  7269. }
  7270. #ifdef WOLFSSL_TIRTOS
  7271. Task_yield();
  7272. #endif
  7273. ((func_args*)args)->return_code = TEST_SUCCESS;
  7274. done:
  7275. wolfSSL_shutdown(ssl);
  7276. wolfSSL_free(ssl);
  7277. wolfSSL_CTX_free(ctx);
  7278. CloseSocket(clientfd);
  7279. #ifdef WOLFSSL_TIRTOS
  7280. fdCloseSession(Task_self());
  7281. #endif
  7282. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7283. && defined(HAVE_THREAD_LS)
  7284. wc_ecc_fp_free(); /* free per thread cache */
  7285. #endif
  7286. #if defined(HAVE_SESSION_TICKET) && \
  7287. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  7288. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  7289. OpenSSLTicketCleanup();
  7290. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  7291. TicketCleanup();
  7292. #endif
  7293. #endif
  7294. #ifndef WOLFSSL_TIRTOS
  7295. return 0;
  7296. #endif
  7297. }
  7298. static void load_tls12_canned_server(WOLFSSL* ssl)
  7299. {
  7300. int clientfd = wolfSSL_get_fd(ssl);
  7301. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  7302. sizeof(canned_server_session)), sizeof(canned_server_session));
  7303. wolfSSL_set_fd(ssl, clientfd);
  7304. }
  7305. #ifdef WOLFSSL_TLS13
  7306. static void load_tls13_canned_server(WOLFSSL* ssl)
  7307. {
  7308. int clientfd = wolfSSL_get_fd(ssl);
  7309. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  7310. sizeof(canned_server_tls13_session)),
  7311. sizeof(canned_server_tls13_session));
  7312. wolfSSL_set_fd(ssl, clientfd);
  7313. }
  7314. #endif
  7315. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  7316. static int test_wolfSSL_tls_export_run(int v)
  7317. {
  7318. SOCKET_T sockfd = 0;
  7319. WOLFSSL_CTX* ctx = 0;
  7320. WOLFSSL* ssl = 0;
  7321. char msg[64] = "hello wolfssl!";
  7322. char reply[1024];
  7323. word32 replySz;
  7324. int msgSz = (int)XSTRLEN(msg);
  7325. const byte* clientSession = NULL;
  7326. int clientSessionSz = 0;
  7327. tcp_ready ready;
  7328. func_args server_args;
  7329. THREAD_TYPE serverThread;
  7330. callback_functions server_cbf;
  7331. #ifdef WOLFSSL_TIRTOS
  7332. fdOpenSession(Task_self());
  7333. #endif
  7334. InitTcpReady(&ready);
  7335. #if defined(USE_WINDOWS_API)
  7336. /* use RNG to get random port if using windows */
  7337. ready.port = GetRandomPort();
  7338. #endif
  7339. XMEMSET(&server_args, 0, sizeof(func_args));
  7340. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7341. switch (v) {
  7342. case WOLFSSL_TLSV1_2:
  7343. server_cbf.method = wolfTLSv1_2_server_method;
  7344. server_cbf.ssl_ready = load_tls12_canned_server;
  7345. /* setup the client side */
  7346. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7347. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  7348. clientSession = canned_client_session;
  7349. clientSessionSz = sizeof(canned_client_session);
  7350. break;
  7351. #ifdef WOLFSSL_TLS13
  7352. case WOLFSSL_TLSV1_3:
  7353. server_cbf.method = wolfTLSv1_3_server_method;
  7354. server_cbf.ssl_ready = load_tls13_canned_server;
  7355. /* setup the client side */
  7356. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7357. clientSession = canned_client_tls13_session;
  7358. clientSessionSz = sizeof(canned_client_tls13_session);
  7359. break;
  7360. #endif
  7361. }
  7362. server_args.callbacks = &server_cbf;
  7363. server_args.signal = &ready;
  7364. start_thread(tls_export_server, &server_args, &serverThread);
  7365. wait_tcp_ready(&server_args);
  7366. #ifdef WOLFSSL_TIRTOS
  7367. fdOpenSession(Task_self());
  7368. #endif
  7369. AssertNotNull(ssl = wolfSSL_new(ctx));
  7370. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  7371. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  7372. clientSessionSz);
  7373. replySz = sizeof(reply);
  7374. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  7375. #if !defined(NO_PSK) && defined(HAVE_ANON)
  7376. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  7377. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  7378. #endif
  7379. wolfSSL_set_fd(ssl, sockfd);
  7380. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7381. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  7382. wolfSSL_free(ssl);
  7383. wolfSSL_CTX_free(ctx);
  7384. CloseSocket(sockfd);
  7385. #ifdef WOLFSSL_TIRTOS
  7386. fdCloseSession(Task_self());
  7387. #endif
  7388. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7389. && defined(HAVE_THREAD_LS)
  7390. wc_ecc_fp_free(); /* free per thread cache */
  7391. #endif
  7392. join_thread(serverThread);
  7393. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7394. FreeTcpReady(&ready);
  7395. #ifdef WOLFSSL_TIRTOS
  7396. fdOpenSession(Task_self());
  7397. #endif
  7398. return TEST_RES_CHECK(1);
  7399. }
  7400. #endif
  7401. static int test_wolfSSL_tls_export(void)
  7402. {
  7403. int res = TEST_SKIPPED;
  7404. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7405. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  7406. #ifdef WOLFSSL_TLS13
  7407. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  7408. #endif
  7409. res = TEST_RES_CHECK(1);
  7410. #endif
  7411. return res;
  7412. }
  7413. /*----------------------------------------------------------------------------*
  7414. | TLS extensions tests
  7415. *----------------------------------------------------------------------------*/
  7416. #ifdef ENABLE_TLS_CALLBACK_TEST
  7417. /* Connection test runner - generic */
  7418. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  7419. callback_functions* server_callbacks)
  7420. {
  7421. tcp_ready ready;
  7422. func_args client_args;
  7423. func_args server_args;
  7424. THREAD_TYPE serverThread;
  7425. XMEMSET(&client_args, 0, sizeof(func_args));
  7426. XMEMSET(&server_args, 0, sizeof(func_args));
  7427. StartTCP();
  7428. client_args.callbacks = client_callbacks;
  7429. server_args.callbacks = server_callbacks;
  7430. #ifdef WOLFSSL_TIRTOS
  7431. fdOpenSession(Task_self());
  7432. #endif
  7433. /* RUN Server side */
  7434. InitTcpReady(&ready);
  7435. #if defined(USE_WINDOWS_API)
  7436. /* use RNG to get random port if using windows */
  7437. ready.port = GetRandomPort();
  7438. #endif
  7439. server_args.signal = &ready;
  7440. client_args.signal = &ready;
  7441. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7442. wait_tcp_ready(&server_args);
  7443. /* RUN Client side */
  7444. run_wolfssl_client(&client_args);
  7445. join_thread(serverThread);
  7446. FreeTcpReady(&ready);
  7447. #ifdef WOLFSSL_TIRTOS
  7448. fdCloseSession(Task_self());
  7449. #endif
  7450. client_callbacks->return_code = client_args.return_code;
  7451. server_callbacks->return_code = server_args.return_code;
  7452. }
  7453. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7454. #ifdef HAVE_SNI
  7455. static int test_wolfSSL_UseSNI_params(void)
  7456. {
  7457. int res = TEST_SKIPPED;
  7458. #if !defined(NO_WOLFSSL_CLIENT)
  7459. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7460. WOLFSSL *ssl = wolfSSL_new(ctx);
  7461. AssertNotNull(ctx);
  7462. AssertNotNull(ssl);
  7463. /* invalid [ctx|ssl] */
  7464. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  7465. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  7466. /* invalid type */
  7467. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  7468. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  7469. /* invalid data */
  7470. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  7471. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  7472. /* success case */
  7473. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  7474. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  7475. wolfSSL_free(ssl);
  7476. wolfSSL_CTX_free(ctx);
  7477. res = TEST_RES_CHECK(1);
  7478. #endif /* !NO_WOLFSSL_CLIENT */
  7479. return res;
  7480. }
  7481. /* BEGIN of connection tests callbacks */
  7482. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7483. {
  7484. AssertIntEQ(WOLFSSL_SUCCESS,
  7485. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7486. }
  7487. static void use_SNI_at_ssl(WOLFSSL* ssl)
  7488. {
  7489. AssertIntEQ(WOLFSSL_SUCCESS,
  7490. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  7491. }
  7492. static void different_SNI_at_ssl(WOLFSSL* ssl)
  7493. {
  7494. AssertIntEQ(WOLFSSL_SUCCESS,
  7495. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  7496. }
  7497. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  7498. {
  7499. use_SNI_at_ssl(ssl);
  7500. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7501. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  7502. }
  7503. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  7504. {
  7505. use_SNI_at_ssl(ssl);
  7506. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7507. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  7508. }
  7509. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7510. {
  7511. use_SNI_at_ctx(ctx);
  7512. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7513. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7514. }
  7515. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  7516. {
  7517. use_SNI_at_ssl(ssl);
  7518. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  7519. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7520. }
  7521. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  7522. {
  7523. use_SNI_at_ctx(ctx);
  7524. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  7525. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  7526. }
  7527. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  7528. {
  7529. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  7530. }
  7531. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  7532. {
  7533. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  7534. }
  7535. static void verify_SNI_no_matching(WOLFSSL* ssl)
  7536. {
  7537. byte type = WOLFSSL_SNI_HOST_NAME;
  7538. void* request = (void*) &type; /* to be overwritten */
  7539. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  7540. AssertNotNull(request);
  7541. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7542. AssertNull(request);
  7543. }
  7544. static void verify_SNI_real_matching(WOLFSSL* ssl)
  7545. {
  7546. byte type = WOLFSSL_SNI_HOST_NAME;
  7547. void* request = NULL;
  7548. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  7549. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7550. AssertNotNull(request);
  7551. AssertStrEQ("www.wolfssl.com", (char*)request);
  7552. }
  7553. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  7554. {
  7555. byte type = WOLFSSL_SNI_HOST_NAME;
  7556. void* request = NULL;
  7557. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  7558. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  7559. AssertNotNull(request);
  7560. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  7561. }
  7562. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7563. {
  7564. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  7565. }
  7566. /* END of connection tests callbacks */
  7567. static int test_wolfSSL_UseSNI_connection(void)
  7568. {
  7569. int res = TEST_SKIPPED;
  7570. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7571. callback_functions client_cb;
  7572. callback_functions server_cb;
  7573. size_t i;
  7574. struct {
  7575. method_provider client_meth;
  7576. method_provider server_meth;
  7577. } methods[] = {
  7578. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  7579. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  7580. #endif
  7581. #ifndef WOLFSSL_NO_TLS12
  7582. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  7583. #endif
  7584. #ifdef WOLFSSL_TLS13
  7585. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  7586. #endif
  7587. };
  7588. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  7589. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7590. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7591. client_cb.method = methods[i].client_meth;
  7592. server_cb.method = methods[i].server_meth;
  7593. client_cb.devId = testDevId;
  7594. server_cb.devId = testDevId;
  7595. /* success case at ctx */
  7596. printf("success case at ctx\n");
  7597. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7598. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7599. test_wolfSSL_client_server(&client_cb, &server_cb);
  7600. /* success case at ssl */
  7601. printf("success case at ssl\n");
  7602. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  7603. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  7604. test_wolfSSL_client_server(&client_cb, &server_cb);
  7605. /* default mismatch behavior */
  7606. printf("default mismatch behavior\n");
  7607. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7608. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  7609. test_wolfSSL_client_server(&client_cb, &server_cb);
  7610. /* continue on mismatch */
  7611. printf("continue on mismatch\n");
  7612. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7613. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  7614. test_wolfSSL_client_server(&client_cb, &server_cb);
  7615. /* fake answer on mismatch */
  7616. printf("fake answer on mismatch\n");
  7617. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7618. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  7619. test_wolfSSL_client_server(&client_cb, &server_cb);
  7620. /* sni abort - success */
  7621. printf("sni abort - success\n");
  7622. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7623. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  7624. test_wolfSSL_client_server(&client_cb, &server_cb);
  7625. /* sni abort - abort when absent (ctx) */
  7626. printf("sni abort - abort when absent (ctx)\n");
  7627. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7628. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7629. test_wolfSSL_client_server(&client_cb, &server_cb);
  7630. /* sni abort - abort when absent (ssl) */
  7631. printf("sni abort - abort when absent (ssl)\n");
  7632. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  7633. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  7634. test_wolfSSL_client_server(&client_cb, &server_cb);
  7635. /* sni abort - success when overwritten */
  7636. printf("sni abort - success when overwritten\n");
  7637. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7638. 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;
  7639. test_wolfSSL_client_server(&client_cb, &server_cb);
  7640. /* sni abort - success when allowing mismatches */
  7641. printf("sni abort - success when allowing mismatches\n");
  7642. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  7643. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  7644. test_wolfSSL_client_server(&client_cb, &server_cb);
  7645. }
  7646. res = TEST_RES_CHECK(1);
  7647. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7648. return res;
  7649. }
  7650. static int test_wolfSSL_SNI_GetFromBuffer(void)
  7651. {
  7652. byte buff[] = { /* www.paypal.com */
  7653. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  7654. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  7655. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  7656. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7657. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7658. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  7659. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  7660. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  7661. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7662. };
  7663. byte buff2[] = { /* api.textmate.org */
  7664. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  7665. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7666. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7667. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7668. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7669. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7670. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7671. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7672. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7673. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7674. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7675. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7676. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7677. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7678. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7679. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7680. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7681. };
  7682. byte buff3[] = { /* no sni extension */
  7683. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7684. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7685. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7686. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7687. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7688. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7689. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7690. };
  7691. byte buff4[] = { /* last extension has zero size */
  7692. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7693. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7694. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7695. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7696. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7697. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7698. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7699. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7700. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7701. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7702. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7703. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7704. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7705. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7706. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7707. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7708. 0x12, 0x00, 0x00
  7709. };
  7710. byte buff5[] = { /* SSL v2.0 client hello */
  7711. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7712. /* dummy bytes bellow, just to pass size check */
  7713. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7714. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7715. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7716. };
  7717. byte result[32] = {0};
  7718. word32 length = 32;
  7719. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7720. 0, result, &length));
  7721. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7722. 0, result, &length));
  7723. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7724. 1, result, &length));
  7725. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7726. 0, result, &length));
  7727. buff[0] = 0x16;
  7728. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7729. 0, result, &length));
  7730. buff[1] = 0x03;
  7731. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7732. sizeof(buff), 0, result, &length));
  7733. buff[2] = 0x03;
  7734. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7735. sizeof(buff), 0, result, &length));
  7736. buff[4] = 0x64;
  7737. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7738. 0, result, &length));
  7739. result[length] = 0;
  7740. AssertStrEQ("www.paypal.com", (const char*) result);
  7741. length = 32;
  7742. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7743. 0, result, &length));
  7744. result[length] = 0;
  7745. AssertStrEQ("api.textmate.org", (const char*) result);
  7746. /* SSL v2.0 tests */
  7747. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7748. sizeof(buff5), 0, result, &length));
  7749. buff5[2] = 0x02;
  7750. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7751. sizeof(buff5), 0, result, &length));
  7752. buff5[2] = 0x01; buff5[6] = 0x08;
  7753. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7754. sizeof(buff5), 0, result, &length));
  7755. buff5[6] = 0x09; buff5[8] = 0x01;
  7756. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7757. sizeof(buff5), 0, result, &length));
  7758. return TEST_RES_CHECK(1);
  7759. }
  7760. #endif /* HAVE_SNI */
  7761. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7762. static int test_wolfSSL_UseTrustedCA(void)
  7763. {
  7764. int res = TEST_SKIPPED;
  7765. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7766. && !defined(NO_RSA)
  7767. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7768. WOLFSSL_CTX *ctx;
  7769. WOLFSSL *ssl;
  7770. byte id[20];
  7771. #ifndef NO_WOLFSSL_SERVER
  7772. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7773. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7774. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7775. #else
  7776. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7777. #endif
  7778. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7779. XMEMSET(id, 0, sizeof(id));
  7780. /* error cases */
  7781. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7782. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7783. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7784. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7785. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7786. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7787. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7788. #ifdef NO_SHA
  7789. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7790. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7791. #endif
  7792. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7793. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7794. /* success cases */
  7795. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7796. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7797. #ifndef NO_SHA
  7798. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7799. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7800. #endif
  7801. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7802. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7803. wolfSSL_free(ssl);
  7804. wolfSSL_CTX_free(ctx);
  7805. res = TEST_RES_CHECK(1);
  7806. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7807. #endif /* HAVE_TRUSTED_CA */
  7808. return res;
  7809. }
  7810. static int test_wolfSSL_UseMaxFragment(void)
  7811. {
  7812. int res = TEST_SKIPPED;
  7813. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7814. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7815. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7816. #ifndef NO_WOLFSSL_SERVER
  7817. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7818. #else
  7819. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7820. #endif
  7821. WOLFSSL *ssl;
  7822. #ifdef OPENSSL_EXTRA
  7823. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7824. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7825. #else
  7826. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7827. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7828. #endif
  7829. #ifndef NO_WOLFSSL_SERVER
  7830. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7831. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7832. #endif
  7833. AssertNotNull(ctx);
  7834. ssl = wolfSSL_new(ctx);
  7835. AssertNotNull(ssl);
  7836. #ifdef OPENSSL_EXTRA
  7837. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7838. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7839. #else
  7840. UseMaxFragment = wolfSSL_UseMaxFragment;
  7841. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7842. #endif
  7843. /* error cases */
  7844. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7845. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7846. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7847. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7848. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7849. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7850. /* success case */
  7851. #ifdef OPENSSL_EXTRA
  7852. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7853. #else
  7854. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7855. #endif
  7856. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7857. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7858. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7859. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7860. #ifdef OPENSSL_EXTRA
  7861. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7862. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7863. #else
  7864. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7865. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7866. #endif
  7867. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7868. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7869. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7870. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7871. #ifdef OPENSSL_EXTRA
  7872. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7873. #else
  7874. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7875. #endif
  7876. wolfSSL_free(ssl);
  7877. wolfSSL_CTX_free(ctx);
  7878. res = TEST_RES_CHECK(1);
  7879. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7880. #endif
  7881. return res;
  7882. }
  7883. static int test_wolfSSL_UseTruncatedHMAC(void)
  7884. {
  7885. int res = TEST_SKIPPED;
  7886. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7887. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7888. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7889. #ifndef NO_WOLFSSL_SERVER
  7890. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7891. #else
  7892. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7893. #endif
  7894. WOLFSSL *ssl;
  7895. AssertNotNull(ctx);
  7896. #ifndef NO_WOLFSSL_SERVER
  7897. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7898. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7899. #endif
  7900. ssl = wolfSSL_new(ctx);
  7901. AssertNotNull(ssl);
  7902. /* error cases */
  7903. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7904. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7905. /* success case */
  7906. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7907. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7908. wolfSSL_free(ssl);
  7909. wolfSSL_CTX_free(ctx);
  7910. res = TEST_RES_CHECK(1);
  7911. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7912. #endif
  7913. return res;
  7914. }
  7915. static int test_wolfSSL_UseSupportedCurve(void)
  7916. {
  7917. int res = TEST_SKIPPED;
  7918. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7919. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7920. WOLFSSL *ssl = wolfSSL_new(ctx);
  7921. AssertNotNull(ctx);
  7922. AssertNotNull(ssl);
  7923. /* error cases */
  7924. AssertIntNE(WOLFSSL_SUCCESS,
  7925. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7926. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7927. AssertIntNE(WOLFSSL_SUCCESS,
  7928. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7929. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7930. /* success case */
  7931. AssertIntEQ(WOLFSSL_SUCCESS,
  7932. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7933. AssertIntEQ(WOLFSSL_SUCCESS,
  7934. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7935. wolfSSL_free(ssl);
  7936. wolfSSL_CTX_free(ctx);
  7937. res = TEST_RES_CHECK(1);
  7938. #endif
  7939. return res;
  7940. }
  7941. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7942. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7943. {
  7944. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7945. }
  7946. static void use_ALPN_all(WOLFSSL* ssl)
  7947. {
  7948. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7949. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7950. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7951. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7952. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7953. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7954. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7955. }
  7956. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7957. {
  7958. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7959. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7960. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7961. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7962. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7963. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7964. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7965. }
  7966. static void use_ALPN_one(WOLFSSL* ssl)
  7967. {
  7968. /* spdy/2 */
  7969. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7970. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7971. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7972. }
  7973. static void use_ALPN_unknown(WOLFSSL* ssl)
  7974. {
  7975. /* http/2.0 */
  7976. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7977. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7978. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7979. }
  7980. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7981. {
  7982. /* http/2.0 */
  7983. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7984. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7985. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7986. }
  7987. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7988. {
  7989. /* spdy/3 */
  7990. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7991. char *proto = NULL;
  7992. word16 protoSz = 0;
  7993. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7994. /* check value */
  7995. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7996. if (proto) {
  7997. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7998. }
  7999. }
  8000. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  8001. {
  8002. char *proto = NULL;
  8003. word16 protoSz = 0;
  8004. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  8005. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8006. /* check value */
  8007. AssertIntEQ(1, (0 == protoSz));
  8008. AssertIntEQ(1, (NULL == proto));
  8009. }
  8010. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  8011. {
  8012. /* http/1.1 */
  8013. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8014. char *proto;
  8015. word16 protoSz = 0;
  8016. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8017. /* check value */
  8018. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8019. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8020. }
  8021. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  8022. {
  8023. /* spdy/2 */
  8024. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8025. char *proto;
  8026. word16 protoSz = 0;
  8027. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8028. /* check value */
  8029. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8030. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8031. }
  8032. static void verify_ALPN_client_list(WOLFSSL* ssl)
  8033. {
  8034. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8035. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8036. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8037. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8038. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8039. char *clist = NULL;
  8040. word16 clistSz = 0;
  8041. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  8042. &clistSz));
  8043. /* check value */
  8044. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  8045. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  8046. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  8047. }
  8048. static int test_wolfSSL_UseALPN_connection(void)
  8049. {
  8050. int res = TEST_SKIPPED;
  8051. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8052. callback_functions client_cb;
  8053. callback_functions server_cb;
  8054. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8055. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8056. client_cb.method = wolfSSLv23_client_method;
  8057. server_cb.method = wolfSSLv23_server_method;
  8058. client_cb.devId = testDevId;
  8059. server_cb.devId = testDevId;
  8060. /* success case same list */
  8061. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8062. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  8063. test_wolfSSL_client_server(&client_cb, &server_cb);
  8064. /* success case only one for server */
  8065. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8066. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  8067. test_wolfSSL_client_server(&client_cb, &server_cb);
  8068. /* success case only one for client */
  8069. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  8070. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  8071. test_wolfSSL_client_server(&client_cb, &server_cb);
  8072. /* success case none for client */
  8073. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8074. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  8075. test_wolfSSL_client_server(&client_cb, &server_cb);
  8076. /* success case mismatch behavior but option 'continue' set */
  8077. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  8078. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  8079. test_wolfSSL_client_server(&client_cb, &server_cb);
  8080. /* success case read protocol send by client */
  8081. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8082. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  8083. test_wolfSSL_client_server(&client_cb, &server_cb);
  8084. /* mismatch behavior with same list
  8085. * the first and only this one must be taken */
  8086. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8087. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  8088. test_wolfSSL_client_server(&client_cb, &server_cb);
  8089. /* default mismatch behavior */
  8090. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8091. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  8092. test_wolfSSL_client_server(&client_cb, &server_cb);
  8093. res = TEST_RES_CHECK(1);
  8094. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8095. return res;
  8096. }
  8097. static int test_wolfSSL_UseALPN_params(void)
  8098. {
  8099. int res = TEST_SKIPPED;
  8100. #ifndef NO_WOLFSSL_CLIENT
  8101. /* "http/1.1" */
  8102. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8103. /* "spdy/1" */
  8104. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  8105. /* "spdy/2" */
  8106. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8107. /* "spdy/3" */
  8108. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8109. char buff[256];
  8110. word32 idx;
  8111. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8112. WOLFSSL *ssl = wolfSSL_new(ctx);
  8113. AssertNotNull(ctx);
  8114. AssertNotNull(ssl);
  8115. /* error cases */
  8116. AssertIntNE(WOLFSSL_SUCCESS,
  8117. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  8118. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8119. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  8120. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8121. /* success case */
  8122. /* http1 only */
  8123. AssertIntEQ(WOLFSSL_SUCCESS,
  8124. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  8125. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8126. /* http1, spdy1 */
  8127. XMEMCPY(buff, http1, sizeof(http1));
  8128. idx = sizeof(http1);
  8129. buff[idx++] = ',';
  8130. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8131. idx += sizeof(spdy1);
  8132. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8133. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8134. /* http1, spdy2, spdy1 */
  8135. XMEMCPY(buff, http1, sizeof(http1));
  8136. idx = sizeof(http1);
  8137. buff[idx++] = ',';
  8138. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8139. idx += sizeof(spdy2);
  8140. buff[idx++] = ',';
  8141. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8142. idx += sizeof(spdy1);
  8143. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8144. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8145. /* spdy3, http1, spdy2, spdy1 */
  8146. XMEMCPY(buff, spdy3, sizeof(spdy3));
  8147. idx = sizeof(spdy3);
  8148. buff[idx++] = ',';
  8149. XMEMCPY(buff+idx, http1, sizeof(http1));
  8150. idx += sizeof(http1);
  8151. buff[idx++] = ',';
  8152. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  8153. idx += sizeof(spdy2);
  8154. buff[idx++] = ',';
  8155. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  8156. idx += sizeof(spdy1);
  8157. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  8158. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8159. wolfSSL_free(ssl);
  8160. wolfSSL_CTX_free(ctx);
  8161. res = TEST_RES_CHECK(1);
  8162. #endif
  8163. return res;
  8164. }
  8165. #endif /* HAVE_ALPN */
  8166. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  8167. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  8168. {
  8169. unsigned char p[] = {
  8170. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8171. 6, 's', 'p', 'd', 'y', '/', '2',
  8172. 6, 's', 'p', 'd', 'y', '/', '1',
  8173. };
  8174. unsigned char p_len = sizeof(p);
  8175. int ret;
  8176. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  8177. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8178. AssertIntEQ(ret, 0);
  8179. #else
  8180. AssertIntEQ(ret, SSL_SUCCESS);
  8181. #endif
  8182. }
  8183. static void set_alpn_protos(SSL* ssl)
  8184. {
  8185. unsigned char p[] = {
  8186. 6, 's', 'p', 'd', 'y', '/', '3',
  8187. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  8188. 6, 's', 'p', 'd', 'y', '/', '2',
  8189. 6, 's', 'p', 'd', 'y', '/', '1',
  8190. };
  8191. unsigned char p_len = sizeof(p);
  8192. int ret;
  8193. ret = SSL_set_alpn_protos(ssl, p, p_len);
  8194. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  8195. AssertIntEQ(ret, 0);
  8196. #else
  8197. AssertIntEQ(ret, SSL_SUCCESS);
  8198. #endif
  8199. }
  8200. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  8201. {
  8202. /* "spdy/3" */
  8203. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8204. const unsigned char *proto;
  8205. unsigned int protoSz = 0;
  8206. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8207. /* check value */
  8208. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8209. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8210. }
  8211. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  8212. {
  8213. /* "http/1.1" */
  8214. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8215. const unsigned char *proto;
  8216. unsigned int protoSz = 0;
  8217. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  8218. /* check value */
  8219. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8220. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8221. }
  8222. static int test_wolfSSL_set_alpn_protos(void)
  8223. {
  8224. int res = TEST_SKIPPED;
  8225. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8226. callback_functions client_cb;
  8227. callback_functions server_cb;
  8228. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8229. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8230. client_cb.method = wolfSSLv23_client_method;
  8231. server_cb.method = wolfSSLv23_server_method;
  8232. client_cb.devId = testDevId;
  8233. server_cb.devId = testDevId;
  8234. /* use CTX_alpn_protos */
  8235. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8236. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  8237. test_wolfSSL_client_server(&client_cb, &server_cb);
  8238. /* use set_alpn_protos */
  8239. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  8240. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  8241. test_wolfSSL_client_server(&client_cb, &server_cb);
  8242. res = TEST_RES_CHECK(1);
  8243. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8244. return res;
  8245. }
  8246. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  8247. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  8248. {
  8249. int res = TEST_SKIPPED;
  8250. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  8251. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8252. WOLFSSL *ssl = wolfSSL_new(ctx);
  8253. AssertNotNull(ctx);
  8254. AssertNotNull(ssl);
  8255. /* error cases */
  8256. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  8257. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  8258. /* success cases */
  8259. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  8260. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  8261. wolfSSL_free(ssl);
  8262. wolfSSL_CTX_free(ctx);
  8263. res = TEST_RES_CHECK(1);
  8264. #endif
  8265. return res;
  8266. }
  8267. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  8268. {
  8269. int res = TEST_SKIPPED;
  8270. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  8271. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8272. WOLFSSL *ssl = wolfSSL_new(ctx);
  8273. AssertNotNull(ctx);
  8274. AssertNotNull(ssl);
  8275. /* error cases */
  8276. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  8277. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  8278. /* success cases */
  8279. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  8280. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  8281. wolfSSL_free(ssl);
  8282. wolfSSL_CTX_free(ctx);
  8283. res = TEST_RES_CHECK(1);
  8284. #endif
  8285. return res;
  8286. }
  8287. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  8288. static int test_wolfSSL_SCR_Reconnect(void)
  8289. {
  8290. int res = TEST_SKIPPED;
  8291. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  8292. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  8293. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
  8294. struct test_memio_ctx test_ctx;
  8295. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  8296. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  8297. byte data;
  8298. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  8299. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  8300. test_ctx.s_ciphers =
  8301. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  8302. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8303. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8304. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  8305. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  8306. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  8307. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  8308. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8309. /* WOLFSSL_FATAL_ERROR since it will block */
  8310. AssertIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  8311. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8312. WOLFSSL_ERROR_WANT_READ);
  8313. AssertIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  8314. AssertIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  8315. WOLFSSL_ERROR_WANT_READ);
  8316. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8317. wolfSSL_free(ssl_c);
  8318. ssl_c = NULL;
  8319. wolfSSL_free(ssl_s);
  8320. ssl_s = NULL;
  8321. wolfSSL_CTX_free(ctx_c);
  8322. ctx_c = NULL;
  8323. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  8324. AssertIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  8325. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  8326. AssertIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  8327. wolfSSL_free(ssl_s);
  8328. wolfSSL_free(ssl_c);
  8329. wolfSSL_CTX_free(ctx_s);
  8330. wolfSSL_CTX_free(ctx_c);
  8331. res = TEST_RES_CHECK(1);
  8332. #endif
  8333. return res;
  8334. }
  8335. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  8336. (!defined(NO_RSA) || defined(HAVE_ECC))
  8337. /* Called when writing. */
  8338. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8339. {
  8340. (void)ssl;
  8341. (void)buf;
  8342. (void)sz;
  8343. (void)ctx;
  8344. /* Force error return from wolfSSL_accept_TLSv13(). */
  8345. return WANT_WRITE;
  8346. }
  8347. /* Called when reading. */
  8348. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  8349. {
  8350. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  8351. int len = (int)msg->length;
  8352. (void)ssl;
  8353. (void)sz;
  8354. /* Pass back as much of message as will fit in buffer. */
  8355. if (len > sz)
  8356. len = sz;
  8357. XMEMCPY(buf, msg->buffer, len);
  8358. /* Move over returned data. */
  8359. msg->buffer += len;
  8360. msg->length -= len;
  8361. /* Amount actually copied. */
  8362. return len;
  8363. }
  8364. #endif
  8365. /* Test the detection of duplicate known TLS extensions.
  8366. * Specifically in a ClientHello.
  8367. */
  8368. static int test_tls_ext_duplicate(void)
  8369. {
  8370. int res = TEST_SKIPPED;
  8371. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  8372. !defined(NO_FILESYSTEM)
  8373. const unsigned char clientHelloDupTlsExt[] = {
  8374. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  8375. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  8376. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  8377. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  8378. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  8379. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  8380. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  8381. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  8382. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8383. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  8384. 0x00, 0x9e, 0x01, 0x00,
  8385. /* Extensions - duplicate signature algorithms. */
  8386. 0x00, 0x19, 0x00, 0x0d,
  8387. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  8388. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  8389. /* Supported Versions extension for TLS 1.3. */
  8390. 0x00, 0x2b,
  8391. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  8392. };
  8393. WOLFSSL_BUFFER_INFO msg;
  8394. const char* testCertFile;
  8395. const char* testKeyFile;
  8396. WOLFSSL_CTX *ctx;
  8397. WOLFSSL *ssl;
  8398. #ifndef NO_RSA
  8399. testCertFile = svrCertFile;
  8400. testKeyFile = svrKeyFile;
  8401. #elif defined(HAVE_ECC)
  8402. testCertFile = eccCertFile;
  8403. testKeyFile = eccKeyFile;
  8404. #endif
  8405. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8406. AssertNotNull(ctx);
  8407. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  8408. WOLFSSL_FILETYPE_PEM));
  8409. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  8410. WOLFSSL_FILETYPE_PEM));
  8411. /* Read from 'msg'. */
  8412. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  8413. /* No where to send to - dummy sender. */
  8414. wolfSSL_SetIOSend(ctx, DummySend);
  8415. ssl = wolfSSL_new(ctx);
  8416. AssertNotNull(ssl);
  8417. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  8418. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  8419. wolfSSL_SetIOReadCtx(ssl, &msg);
  8420. AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  8421. AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  8422. wolfSSL_free(ssl);
  8423. wolfSSL_CTX_free(ctx);
  8424. res = TEST_RES_CHECK(1);
  8425. #endif
  8426. return res;
  8427. }
  8428. /*----------------------------------------------------------------------------*
  8429. | X509 Tests
  8430. *----------------------------------------------------------------------------*/
  8431. static int test_wolfSSL_X509_NAME_get_entry(void)
  8432. {
  8433. int res = TEST_SKIPPED;
  8434. #if !defined(NO_CERTS) && !defined(NO_RSA)
  8435. #if defined(OPENSSL_ALL) || \
  8436. (defined(OPENSSL_EXTRA) && \
  8437. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  8438. /* use openssl like name to test mapping */
  8439. X509_NAME_ENTRY* ne;
  8440. X509_NAME* name;
  8441. X509* x509;
  8442. #ifndef NO_FILESYSTEM
  8443. ASN1_STRING* asn;
  8444. char* subCN = NULL;
  8445. #endif
  8446. int idx;
  8447. ASN1_OBJECT *object = NULL;
  8448. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8449. defined(WOLFSSL_NGINX)
  8450. #ifndef NO_BIO
  8451. BIO* bio;
  8452. #endif
  8453. #endif
  8454. #ifndef NO_FILESYSTEM
  8455. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8456. WOLFSSL_FILETYPE_PEM);
  8457. AssertNotNull(x509);
  8458. name = X509_get_subject_name(x509);
  8459. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  8460. AssertIntGE(idx, 0);
  8461. ne = X509_NAME_get_entry(name, idx);
  8462. AssertNotNull(ne);
  8463. asn = X509_NAME_ENTRY_get_data(ne);
  8464. AssertNotNull(asn);
  8465. subCN = (char*)ASN1_STRING_data(asn);
  8466. AssertNotNull(subCN);
  8467. wolfSSL_FreeX509(x509);
  8468. #endif
  8469. x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8470. WOLFSSL_FILETYPE_PEM);
  8471. AssertNotNull(x509);
  8472. name = X509_get_subject_name(x509);
  8473. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  8474. AssertIntGE(idx, 0);
  8475. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  8476. defined(WOLFSSL_NGINX)
  8477. #ifndef NO_BIO
  8478. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8479. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  8480. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  8481. AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  8482. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  8483. BIO_free(bio);
  8484. #endif
  8485. #endif
  8486. ne = X509_NAME_get_entry(name, idx);
  8487. AssertNotNull(ne);
  8488. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  8489. wolfSSL_FreeX509(x509);
  8490. res = TEST_RES_CHECK(1);
  8491. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  8492. #endif /* !NO_CERTS && !NO_RSA */
  8493. return res;
  8494. }
  8495. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  8496. static int test_wolfSSL_PKCS12(void)
  8497. {
  8498. int res = TEST_SKIPPED;
  8499. /* .p12 file is encrypted with DES3 */
  8500. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  8501. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  8502. * Password Key
  8503. */
  8504. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  8505. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  8506. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  8507. byte buf[6000];
  8508. char file[] = "./certs/test-servercert.p12";
  8509. char order[] = "./certs/ecc-rsa-server.p12";
  8510. #ifdef WC_RC2
  8511. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  8512. #endif
  8513. char pass[] = "a password";
  8514. const char goodPsw[] = "wolfSSL test";
  8515. const char badPsw[] = "bad";
  8516. #ifdef HAVE_ECC
  8517. WOLFSSL_X509_NAME* subject;
  8518. WOLFSSL_X509 *x509;
  8519. #endif
  8520. XFILE f;
  8521. int bytes, ret, goodPswLen, badPswLen;
  8522. WOLFSSL_BIO *bio;
  8523. WOLFSSL_EVP_PKEY *pkey;
  8524. WC_PKCS12 *pkcs12;
  8525. WC_PKCS12 *pkcs12_2;
  8526. WOLFSSL_X509 *cert;
  8527. WOLFSSL_X509 *tmp;
  8528. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  8529. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8530. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8531. WOLFSSL_CTX *ctx;
  8532. WOLFSSL *ssl;
  8533. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  8534. #endif
  8535. f = XFOPEN(file, "rb");
  8536. AssertTrue((f != XBADFILE));
  8537. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8538. XFCLOSE(f);
  8539. goodPswLen = (int)XSTRLEN(goodPsw);
  8540. badPswLen = (int)XSTRLEN(badPsw);
  8541. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  8542. AssertNotNull(bio);
  8543. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  8544. d2i_PKCS12_bio(bio, &pkcs12);
  8545. AssertNotNull(pkcs12);
  8546. BIO_free(bio);
  8547. /* check verify MAC directly */
  8548. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  8549. AssertIntEQ(ret, 1);
  8550. /* check verify MAC fail case directly */
  8551. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  8552. AssertIntEQ(ret, 0);
  8553. /* check verify MAC fail case */
  8554. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8555. AssertIntEQ(ret, 0);
  8556. AssertNull(pkey);
  8557. AssertNull(cert);
  8558. /* check parse with no extra certs kept */
  8559. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8560. AssertIntEQ(ret, 1);
  8561. AssertNotNull(pkey);
  8562. AssertNotNull(cert);
  8563. wolfSSL_EVP_PKEY_free(pkey);
  8564. wolfSSL_X509_free(cert);
  8565. /* check parse with extra certs kept */
  8566. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8567. AssertIntEQ(ret, 1);
  8568. AssertNotNull(pkey);
  8569. AssertNotNull(cert);
  8570. AssertNotNull(ca);
  8571. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8572. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8573. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  8574. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8575. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8576. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8577. #else
  8578. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8579. #endif
  8580. /* Copy stack structure */
  8581. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  8582. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  8583. /* CTX now owns the tmp_ca stack structure */
  8584. tmp_ca = NULL;
  8585. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  8586. AssertNotNull(tmp_ca);
  8587. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  8588. /* Check that the main cert is also set */
  8589. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  8590. AssertNotNull(ssl = SSL_new(ctx));
  8591. AssertNotNull(SSL_get_certificate(ssl));
  8592. SSL_free(ssl);
  8593. SSL_CTX_free(ctx);
  8594. #endif
  8595. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8596. /* should be 2 other certs on stack */
  8597. tmp = sk_X509_pop(ca);
  8598. AssertNotNull(tmp);
  8599. X509_free(tmp);
  8600. tmp = sk_X509_pop(ca);
  8601. AssertNotNull(tmp);
  8602. X509_free(tmp);
  8603. AssertNull(sk_X509_pop(ca));
  8604. EVP_PKEY_free(pkey);
  8605. X509_free(cert);
  8606. sk_X509_pop_free(ca, X509_free);
  8607. /* check PKCS12_create */
  8608. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  8609. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  8610. SSL_SUCCESS);
  8611. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8612. -1, -1, 100, -1, 0)));
  8613. EVP_PKEY_free(pkey);
  8614. X509_free(cert);
  8615. sk_X509_pop_free(ca, NULL);
  8616. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8617. SSL_SUCCESS);
  8618. PKCS12_free(pkcs12_2);
  8619. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  8620. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8621. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  8622. 2000, 1, 0)));
  8623. EVP_PKEY_free(pkey);
  8624. X509_free(cert);
  8625. sk_X509_pop_free(ca, NULL);
  8626. /* convert to DER then back and parse */
  8627. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  8628. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  8629. PKCS12_free(pkcs12_2);
  8630. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  8631. BIO_free(bio);
  8632. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8633. SSL_SUCCESS);
  8634. /* should be 2 other certs on stack */
  8635. tmp = sk_X509_pop(ca);
  8636. AssertNotNull(tmp);
  8637. X509_free(tmp);
  8638. tmp = sk_X509_pop(ca);
  8639. AssertNotNull(tmp);
  8640. X509_free(tmp);
  8641. AssertNull(sk_X509_pop(ca));
  8642. #ifndef NO_RC4
  8643. PKCS12_free(pkcs12_2);
  8644. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  8645. NID_pbe_WithSHA1And128BitRC4,
  8646. NID_pbe_WithSHA1And128BitRC4,
  8647. 2000, 1, 0)));
  8648. EVP_PKEY_free(pkey);
  8649. X509_free(cert);
  8650. sk_X509_pop_free(ca, NULL);
  8651. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  8652. SSL_SUCCESS);
  8653. #endif /* NO_RC4 */
  8654. EVP_PKEY_free(pkey);
  8655. X509_free(cert);
  8656. PKCS12_free(pkcs12);
  8657. PKCS12_free(pkcs12_2);
  8658. sk_X509_pop_free(ca, NULL);
  8659. #ifdef HAVE_ECC
  8660. /* test order of parsing */
  8661. f = XFOPEN(order, "rb");
  8662. AssertTrue(f != XBADFILE);
  8663. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8664. XFCLOSE(f);
  8665. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8666. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8667. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  8668. WOLFSSL_SUCCESS);
  8669. /* check use of pkey after parse */
  8670. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  8671. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  8672. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8673. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  8674. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8675. #else
  8676. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8677. #endif
  8678. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  8679. SSL_CTX_free(ctx);
  8680. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8681. #endif
  8682. AssertNotNull(pkey);
  8683. AssertNotNull(cert);
  8684. AssertNotNull(ca);
  8685. /* compare subject lines of certificates */
  8686. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8687. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  8688. SSL_FILETYPE_PEM));
  8689. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8690. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8691. X509_free(x509);
  8692. /* test expected fail case */
  8693. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8694. SSL_FILETYPE_PEM));
  8695. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8696. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8697. X509_free(x509);
  8698. X509_free(cert);
  8699. /* get subject line from ca stack */
  8700. AssertNotNull(cert = sk_X509_pop(ca));
  8701. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  8702. /* compare subject from certificate in ca to expected */
  8703. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  8704. SSL_FILETYPE_PEM));
  8705. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  8706. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  8707. EVP_PKEY_free(pkey);
  8708. X509_free(x509);
  8709. X509_free(cert);
  8710. BIO_free(bio);
  8711. PKCS12_free(pkcs12);
  8712. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  8713. /* test order of parsing */
  8714. f = XFOPEN(file, "rb");
  8715. AssertTrue(f != XBADFILE);
  8716. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8717. XFCLOSE(f);
  8718. /* check verify MAC fail case */
  8719. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8720. AssertIntEQ(ret, 0);
  8721. AssertNull(pkey);
  8722. AssertNull(cert);
  8723. /* check parse with no extra certs kept */
  8724. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8725. AssertIntEQ(ret, 1);
  8726. AssertNotNull(pkey);
  8727. AssertNotNull(cert);
  8728. wolfSSL_EVP_PKEY_free(pkey);
  8729. wolfSSL_X509_free(cert);
  8730. /* check parse with extra certs kept */
  8731. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8732. AssertIntEQ(ret, 1);
  8733. AssertNotNull(pkey);
  8734. AssertNotNull(cert);
  8735. AssertNotNull(ca);
  8736. wolfSSL_EVP_PKEY_free(pkey);
  8737. wolfSSL_X509_free(cert);
  8738. sk_X509_pop_free(ca, NULL);
  8739. PKCS12_free(pkcs12);
  8740. #endif /* HAVE_ECC */
  8741. #ifdef WC_RC2
  8742. /* test PKCS#12 with RC2 encryption */
  8743. f = XFOPEN(rc2p12, "rb");
  8744. AssertTrue(f != XBADFILE);
  8745. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  8746. XFCLOSE(f);
  8747. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  8748. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  8749. /* check verify MAC fail case */
  8750. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  8751. AssertIntEQ(ret, 0);
  8752. AssertNull(pkey);
  8753. AssertNull(cert);
  8754. /* check parse iwth not extra certs kept */
  8755. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  8756. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8757. AssertNotNull(pkey);
  8758. AssertNotNull(cert);
  8759. wolfSSL_EVP_PKEY_free(pkey);
  8760. wolfSSL_X509_free(cert);
  8761. /* check parse with extra certs kept */
  8762. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  8763. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  8764. AssertNotNull(pkey);
  8765. AssertNotNull(cert);
  8766. AssertNotNull(ca);
  8767. wolfSSL_EVP_PKEY_free(pkey);
  8768. wolfSSL_X509_free(cert);
  8769. sk_X509_pop_free(ca, NULL);
  8770. BIO_free(bio);
  8771. PKCS12_free(pkcs12);
  8772. #endif /* WC_RC2 */
  8773. /* Test i2d_PKCS12_bio */
  8774. f = XFOPEN(file, "rb");
  8775. AssertTrue((f != XBADFILE));
  8776. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  8777. XFCLOSE(f);
  8778. bio = BIO_new(BIO_s_mem());
  8779. AssertNotNull(bio);
  8780. ret = i2d_PKCS12_bio(bio, pkcs12);
  8781. AssertIntEQ(ret, 1);
  8782. ret = i2d_PKCS12_bio(NULL, pkcs12);
  8783. AssertIntEQ(ret, 0);
  8784. ret = i2d_PKCS12_bio(bio, NULL);
  8785. AssertIntEQ(ret, 0);
  8786. PKCS12_free(pkcs12);
  8787. BIO_free(bio);
  8788. (void)order;
  8789. res = TEST_RES_CHECK(1);
  8790. #endif /* OPENSSL_EXTRA */
  8791. #endif /* HAVE_FIPS */
  8792. return res;
  8793. }
  8794. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8795. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  8796. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  8797. #define TEST_PKCS8_ENC
  8798. #endif
  8799. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8800. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8801. /* used to keep track if FailTestCallback was called */
  8802. static int failTestCallbackCalled = 0;
  8803. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  8804. {
  8805. (void)passwd;
  8806. (void)sz;
  8807. (void)rw;
  8808. (void)userdata;
  8809. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8810. failTestCallbackCalled = 1;
  8811. return -1;
  8812. }
  8813. #endif
  8814. static int test_wolfSSL_no_password_cb(void)
  8815. {
  8816. int res = TEST_SKIPPED;
  8817. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8818. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8819. WOLFSSL_CTX* ctx;
  8820. byte buff[FOURK_BUF];
  8821. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8822. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8823. XFILE f;
  8824. int bytes;
  8825. #ifndef NO_WOLFSSL_CLIENT
  8826. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8827. #else
  8828. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8829. #endif
  8830. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8831. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8832. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8833. XFCLOSE(f);
  8834. AssertIntLE(bytes, sizeof(buff));
  8835. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8836. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8837. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8838. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8839. XFCLOSE(f);
  8840. AssertIntLE(bytes, sizeof(buff));
  8841. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8842. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8843. wolfSSL_CTX_free(ctx);
  8844. if (failTestCallbackCalled != 0) {
  8845. Fail(("Password callback should not be called by default"),
  8846. ("Password callback was called without attempting "
  8847. "to first decipher private key without password."));
  8848. }
  8849. res = TEST_RES_CHECK(1);
  8850. #endif
  8851. return res;
  8852. }
  8853. #ifdef TEST_PKCS8_ENC
  8854. /* for PKCS8 test case */
  8855. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8856. {
  8857. int flag = 0;
  8858. (void)rw;
  8859. if (userdata != NULL) {
  8860. flag = *((int*)userdata); /* user set data */
  8861. }
  8862. switch (flag) {
  8863. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8864. * can be anything the user wishes to be passed to the callback
  8865. * associated with the WOLFSSL_CTX */
  8866. XSTRNCPY(passwd, "yassl123", sz);
  8867. return 8;
  8868. default:
  8869. return BAD_FUNC_ARG;
  8870. }
  8871. }
  8872. #endif /* TEST_PKCS8_ENC */
  8873. /* Testing functions dealing with PKCS8 */
  8874. static int test_wolfSSL_PKCS8(void)
  8875. {
  8876. int res = TEST_SKIPPED;
  8877. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  8878. !defined(WOLFCRYPT_ONLY)
  8879. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8880. byte buff[FOURK_BUF];
  8881. byte der[FOURK_BUF];
  8882. #ifndef NO_RSA
  8883. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8884. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8885. #endif
  8886. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8887. #ifdef HAVE_ECC
  8888. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8889. #endif
  8890. XFILE f;
  8891. int bytes;
  8892. WOLFSSL_CTX* ctx;
  8893. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8894. int ret;
  8895. ecc_key key;
  8896. word32 x = 0;
  8897. #endif
  8898. #ifdef TEST_PKCS8_ENC
  8899. #if !defined(NO_RSA) && !defined(NO_SHA)
  8900. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8901. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8902. #endif
  8903. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8904. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8905. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8906. #endif
  8907. int flag;
  8908. #endif
  8909. (void)der;
  8910. #ifndef NO_WOLFSSL_CLIENT
  8911. #ifndef WOLFSSL_NO_TLS12
  8912. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8913. #else
  8914. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8915. #endif
  8916. #else
  8917. #ifndef WOLFSSL_NO_TLS12
  8918. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8919. #else
  8920. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8921. #endif
  8922. #endif
  8923. #ifdef TEST_PKCS8_ENC
  8924. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8925. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8926. flag = 1; /* used by password callback as return code */
  8927. #if !defined(NO_RSA) && !defined(NO_SHA)
  8928. /* test loading PEM PKCS8 encrypted file */
  8929. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8930. AssertTrue((f != XBADFILE));
  8931. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8932. XFCLOSE(f);
  8933. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8934. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8935. /* this next case should fail because of password callback return code */
  8936. flag = 0; /* used by password callback as return code */
  8937. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8938. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8939. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8940. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8941. "yassl123"), 0);
  8942. /* test that error value is returned with a bad password */
  8943. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8944. "bad"), 0);
  8945. /* test loading PEM PKCS8 encrypted file */
  8946. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8947. AssertTrue((f != XBADFILE));
  8948. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8949. XFCLOSE(f);
  8950. flag = 1; /* used by password callback as return code */
  8951. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8952. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8953. /* this next case should fail because of password callback return code */
  8954. flag = 0; /* used by password callback as return code */
  8955. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8956. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8957. #endif /* !NO_RSA && !NO_SHA */
  8958. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8959. /* test loading PEM PKCS8 encrypted ECC Key file */
  8960. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8961. AssertTrue((f != XBADFILE));
  8962. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8963. XFCLOSE(f);
  8964. flag = 1; /* used by password callback as return code */
  8965. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8966. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8967. /* this next case should fail because of password callback return code */
  8968. flag = 0; /* used by password callback as return code */
  8969. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8970. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8971. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8972. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8973. "yassl123"), 0);
  8974. /* test that error value is returned with a bad password */
  8975. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8976. "bad"), 0);
  8977. /* test loading DER PKCS8 encrypted ECC Key file */
  8978. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8979. AssertTrue((f != XBADFILE));
  8980. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8981. XFCLOSE(f);
  8982. flag = 1; /* used by password callback as return code */
  8983. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8984. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8985. /* this next case should fail because of password callback return code */
  8986. flag = 0; /* used by password callback as return code */
  8987. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8988. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8989. /* leave flag as "okay" */
  8990. flag = 1;
  8991. #endif /* HAVE_ECC && !NO_SHA */
  8992. #endif /* TEST_PKCS8_ENC */
  8993. #ifndef NO_RSA
  8994. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8995. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8996. AssertTrue((f != XBADFILE));
  8997. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8998. XFCLOSE(f);
  8999. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9000. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9001. /* test loading PEM PKCS8 private key file (not encrypted) */
  9002. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  9003. AssertTrue((f != XBADFILE));
  9004. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9005. XFCLOSE(f);
  9006. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9007. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9008. #endif /* !NO_RSA */
  9009. /* Test PKCS8 PEM ECC key no crypt */
  9010. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  9011. AssertTrue((f != XBADFILE));
  9012. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9013. XFCLOSE(f);
  9014. #ifdef HAVE_ECC
  9015. /* Test PKCS8 PEM ECC key no crypt */
  9016. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9017. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9018. #ifndef NO_CODING
  9019. /* decrypt PKCS8 PEM to key in DER format */
  9020. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  9021. (word32)sizeof(der), NULL)), 0);
  9022. ret = wc_ecc_init(&key);
  9023. if (ret == 0) {
  9024. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  9025. wc_ecc_free(&key);
  9026. }
  9027. AssertIntEQ(ret, 0);
  9028. #endif
  9029. /* Test PKCS8 DER ECC key no crypt */
  9030. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  9031. AssertTrue((f != XBADFILE));
  9032. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  9033. XFCLOSE(f);
  9034. /* Test using a PKCS8 ECC PEM */
  9035. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9036. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9037. #else
  9038. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  9039. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  9040. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  9041. #endif /* HAVE_ECC */
  9042. wolfSSL_CTX_free(ctx);
  9043. res = TEST_RES_CHECK(1);
  9044. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9045. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  9046. return res;
  9047. }
  9048. static int test_wolfSSL_PKCS8_ED25519(void)
  9049. {
  9050. int res = TEST_SKIPPED;
  9051. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9052. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  9053. defined(HAVE_ED25519_KEY_IMPORT)
  9054. const byte encPrivKey[] = \
  9055. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9056. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  9057. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  9058. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  9059. "2L9W4v7swXkV+X+a1ww=\n"
  9060. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9061. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9062. byte der[FOURK_BUF];
  9063. WOLFSSL_CTX* ctx;
  9064. int bytes;
  9065. XMEMSET(der, 0, sizeof(der));
  9066. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9067. (word32)sizeof(der), password)), 0);
  9068. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9069. #ifndef NO_WOLFSSL_SERVER
  9070. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9071. #else
  9072. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9073. #endif
  9074. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9075. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9076. wolfSSL_CTX_free(ctx);
  9077. res = TEST_RES_CHECK(1);
  9078. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9079. #endif
  9080. return res;
  9081. }
  9082. static int test_wolfSSL_PKCS8_ED448(void)
  9083. {
  9084. int res = TEST_SKIPPED;
  9085. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  9086. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  9087. defined(HAVE_ED448_KEY_IMPORT)
  9088. const byte encPrivKey[] = \
  9089. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  9090. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  9091. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  9092. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  9093. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  9094. "-----END ENCRYPTED PRIVATE KEY-----\n";
  9095. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  9096. byte der[FOURK_BUF];
  9097. WOLFSSL_CTX* ctx;
  9098. int bytes;
  9099. XMEMSET(der, 0, sizeof(der));
  9100. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  9101. (word32)sizeof(der), password)), 0);
  9102. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9103. #ifndef NO_WOLFSSL_SERVER
  9104. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9105. #else
  9106. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9107. #endif
  9108. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  9109. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9110. wolfSSL_CTX_free(ctx);
  9111. res = TEST_RES_CHECK(1);
  9112. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9113. #endif
  9114. return res;
  9115. }
  9116. /* Testing functions dealing with PKCS5 */
  9117. static int test_wolfSSL_PKCS5(void)
  9118. {
  9119. int res = TEST_SKIPPED;
  9120. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  9121. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  9122. const char* passwd = "myfipsPa$$W0rd";
  9123. #else
  9124. const char *passwd = "pass1234";
  9125. #endif
  9126. const unsigned char *salt = (unsigned char *)"salt1234";
  9127. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  9128. DYNAMIC_TYPE_TMP_BUFFER);
  9129. int ret = 0;
  9130. AssertNotNull(out);
  9131. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  9132. (int)XSTRLEN((const char *) salt), 10,
  9133. WC_SHA_DIGEST_SIZE,out);
  9134. AssertIntEQ(ret, SSL_SUCCESS);
  9135. #ifdef WOLFSSL_SHA512
  9136. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  9137. (int)XSTRLEN((const char *) salt), 10,
  9138. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  9139. AssertIntEQ(ret, SSL_SUCCESS);
  9140. #endif
  9141. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9142. res = TEST_RES_CHECK(1);
  9143. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  9144. return res;
  9145. }
  9146. /* test parsing URI from certificate */
  9147. static int test_wolfSSL_URI(void)
  9148. {
  9149. int res = TEST_SKIPPED;
  9150. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9151. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  9152. defined(OPENSSL_EXTRA))
  9153. WOLFSSL_X509* x509;
  9154. const char uri[] = "./certs/client-uri-cert.pem";
  9155. const char urn[] = "./certs/client-absolute-urn.pem";
  9156. const char badUri[] = "./certs/client-relative-uri.pem";
  9157. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  9158. AssertNotNull(x509);
  9159. wolfSSL_FreeX509(x509);
  9160. x509 = wolfSSL_X509_load_certificate_file(urn, WOLFSSL_FILETYPE_PEM);
  9161. AssertNotNull(x509);
  9162. wolfSSL_FreeX509(x509);
  9163. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  9164. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  9165. && !defined(WOLFSSL_FPKI)
  9166. AssertNull(x509);
  9167. #else
  9168. AssertNotNull(x509);
  9169. wolfSSL_FreeX509(x509);
  9170. #endif
  9171. res = TEST_RES_CHECK(1);
  9172. #endif
  9173. return res;
  9174. }
  9175. static int test_wolfSSL_TBS(void)
  9176. {
  9177. int res = TEST_SKIPPED;
  9178. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9179. && defined(OPENSSL_EXTRA)
  9180. WOLFSSL_X509* x509;
  9181. const unsigned char* tbs;
  9182. int tbsSz;
  9183. AssertNotNull(x509 =
  9184. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  9185. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  9186. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  9187. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  9188. AssertIntEQ(tbsSz, 1003);
  9189. wolfSSL_FreeX509(x509);
  9190. res = TEST_RES_CHECK(1);
  9191. #endif
  9192. return res;
  9193. }
  9194. static int test_wolfSSL_X509_verify(void)
  9195. {
  9196. int res = TEST_SKIPPED;
  9197. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  9198. && defined(OPENSSL_EXTRA)
  9199. WOLFSSL_X509* ca;
  9200. WOLFSSL_X509* serv;
  9201. WOLFSSL_EVP_PKEY* pkey;
  9202. unsigned char buf[2048];
  9203. const unsigned char* pt = NULL;
  9204. int bufSz;
  9205. AssertNotNull(ca =
  9206. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  9207. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  9208. WOLFSSL_SUCCESS);
  9209. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  9210. WOLFSSL_SUCCESS);
  9211. AssertIntEQ(bufSz, 294);
  9212. bufSz = 2048;
  9213. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  9214. WOLFSSL_SUCCESS);
  9215. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  9216. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  9217. AssertNotNull(serv =
  9218. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  9219. /* success case */
  9220. pt = buf;
  9221. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9222. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  9223. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  9224. wolfSSL_EVP_PKEY_free(pkey);
  9225. /* fail case */
  9226. bufSz = 2048;
  9227. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  9228. WOLFSSL_SUCCESS);
  9229. pt = buf;
  9230. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  9231. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  9232. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  9233. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  9234. wolfSSL_EVP_PKEY_free(pkey);
  9235. wolfSSL_FreeX509(ca);
  9236. wolfSSL_FreeX509(serv);
  9237. res = TEST_RES_CHECK(1);
  9238. #endif
  9239. return res;
  9240. }
  9241. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  9242. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  9243. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  9244. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  9245. && !defined(NO_ASN_TIME)
  9246. /* create certificate with version 2 */
  9247. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  9248. {
  9249. WOLFSSL_X509 *x509, *x509v2;
  9250. WOLFSSL_EVP_PKEY *priv, *pub;
  9251. unsigned char *der = NULL, *key = NULL, *pt;
  9252. char *header, *name;
  9253. int derSz;
  9254. long keySz;
  9255. XFILE fp;
  9256. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  9257. time_t t;
  9258. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9259. WOLFSSL_FILETYPE_PEM));
  9260. fp = XFOPEN(cliKeyFile, "rb");
  9261. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  9262. WOLFSSL_SUCCESS);
  9263. XFCLOSE(fp);
  9264. pt = key;
  9265. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  9266. (const unsigned char**)&pt, keySz));
  9267. /* create the version 2 certificate */
  9268. AssertNotNull(x509v2 = X509_new());
  9269. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  9270. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  9271. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  9272. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  9273. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  9274. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  9275. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  9276. t = time(NULL);
  9277. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  9278. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  9279. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  9280. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  9281. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  9282. derSz = wolfSSL_i2d_X509(x509v2, &der);
  9283. AssertIntGT(derSz, 0);
  9284. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  9285. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9286. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  9287. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9288. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9289. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9290. wolfSSL_X509_free(x509);
  9291. wolfSSL_X509_free(x509v2);
  9292. wolfSSL_EVP_PKEY_free(priv);
  9293. wolfSSL_EVP_PKEY_free(pub);
  9294. wolfSSL_ASN1_TIME_free(notBefore);
  9295. wolfSSL_ASN1_TIME_free(notAfter);
  9296. }
  9297. /* override certificate version error */
  9298. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  9299. {
  9300. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9301. AssertIntEQ(store->error, ASN_VERSION_E);
  9302. #else
  9303. AssertIntEQ(store->error, 0);
  9304. #endif
  9305. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  9306. (void)preverify;
  9307. return 1;
  9308. }
  9309. /* set verify callback that will override bad certificate version */
  9310. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  9311. {
  9312. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  9313. }
  9314. #endif
  9315. static int test_wolfSSL_X509_TLS_version(void)
  9316. {
  9317. int res = TEST_SKIPPED;
  9318. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  9319. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  9320. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  9321. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) \
  9322. && !defined(NO_ASN_TIME)
  9323. tcp_ready ready;
  9324. func_args server_args;
  9325. func_args client_args;
  9326. THREAD_TYPE serverThread;
  9327. callback_functions func_cb_client;
  9328. callback_functions func_cb_server;
  9329. /* test server rejects a client certificate that is not version 3 */
  9330. #ifdef WOLFSSL_TIRTOS
  9331. fdOpenSession(Task_self());
  9332. #endif
  9333. XMEMSET(&server_args, 0, sizeof(func_args));
  9334. XMEMSET(&client_args, 0, sizeof(func_args));
  9335. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  9336. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  9337. StartTCP();
  9338. InitTcpReady(&ready);
  9339. #if defined(USE_WINDOWS_API)
  9340. /* use RNG to get random port if using windows */
  9341. ready.port = GetRandomPort();
  9342. #endif
  9343. server_args.signal = &ready;
  9344. client_args.signal = &ready;
  9345. server_args.return_code = TEST_FAIL;
  9346. client_args.return_code = TEST_FAIL;
  9347. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9348. #ifndef WOLFSSL_NO_TLS12
  9349. func_cb_client.method = wolfTLSv1_2_client_method;
  9350. #else
  9351. func_cb_client.method = wolfTLSv1_3_client_method;
  9352. #endif
  9353. client_args.callbacks = &func_cb_client;
  9354. #ifndef WOLFSSL_NO_TLS12
  9355. func_cb_server.method = wolfTLSv1_2_server_method;
  9356. #else
  9357. func_cb_server.method = wolfTLSv1_3_server_method;
  9358. #endif
  9359. server_args.callbacks = &func_cb_server;
  9360. start_thread(test_server_nofail, &server_args, &serverThread);
  9361. wait_tcp_ready(&server_args);
  9362. test_client_nofail(&client_args, NULL);
  9363. join_thread(serverThread);
  9364. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  9365. AssertIntEQ(client_args.return_code, TEST_FAIL);
  9366. AssertIntEQ(server_args.return_code, TEST_FAIL);
  9367. #else
  9368. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  9369. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  9370. #endif
  9371. FreeTcpReady(&ready);
  9372. #ifdef WOLFSSL_TIRTOS
  9373. fdCloseSession(Task_self());
  9374. #endif
  9375. /* Now re run but override the bad X509 version */
  9376. #ifdef WOLFSSL_TIRTOS
  9377. fdOpenSession(Task_self());
  9378. #endif
  9379. XMEMSET(&server_args, 0, sizeof(func_args));
  9380. XMEMSET(&client_args, 0, sizeof(func_args));
  9381. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  9382. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  9383. StartTCP();
  9384. InitTcpReady(&ready);
  9385. #if defined(USE_WINDOWS_API)
  9386. /* use RNG to get random port if using windows */
  9387. ready.port = GetRandomPort();
  9388. #endif
  9389. server_args.signal = &ready;
  9390. client_args.signal = &ready;
  9391. server_args.return_code = TEST_FAIL;
  9392. client_args.return_code = TEST_FAIL;
  9393. func_cb_client.ctx_ready = &test_set_x509_badversion;
  9394. func_cb_server.ctx_ready = &test_set_override_x509;
  9395. #ifndef WOLFSSL_NO_TLS12
  9396. func_cb_client.method = wolfTLSv1_2_client_method;
  9397. #else
  9398. func_cb_client.method = wolfTLSv1_3_client_method;
  9399. #endif
  9400. client_args.callbacks = &func_cb_client;
  9401. #ifndef WOLFSSL_NO_TLS12
  9402. func_cb_server.method = wolfTLSv1_2_server_method;
  9403. #else
  9404. func_cb_server.method = wolfTLSv1_3_server_method;
  9405. #endif
  9406. server_args.callbacks = &func_cb_server;
  9407. start_thread(test_server_nofail, &server_args, &serverThread);
  9408. wait_tcp_ready(&server_args);
  9409. test_client_nofail(&client_args, NULL);
  9410. join_thread(serverThread);
  9411. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  9412. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  9413. FreeTcpReady(&ready);
  9414. #ifdef WOLFSSL_TIRTOS
  9415. fdCloseSession(Task_self());
  9416. #endif
  9417. res = TEST_RES_CHECK(1);
  9418. #endif
  9419. return res;
  9420. }
  9421. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  9422. * version allowed.
  9423. * POST: 1 on success.
  9424. */
  9425. static int test_wolfSSL_CTX_SetMinVersion(void)
  9426. {
  9427. int res = TEST_SKIPPED;
  9428. #ifndef NO_WOLFSSL_CLIENT
  9429. int failFlag = WOLFSSL_SUCCESS;
  9430. WOLFSSL_CTX* ctx;
  9431. int itr;
  9432. #ifndef NO_OLD_TLS
  9433. const int versions[] = {
  9434. #ifdef WOLFSSL_ALLOW_TLSV10
  9435. WOLFSSL_TLSV1,
  9436. #endif
  9437. WOLFSSL_TLSV1_1,
  9438. WOLFSSL_TLSV1_2 };
  9439. #elif !defined(WOLFSSL_NO_TLS12)
  9440. const int versions[] = { WOLFSSL_TLSV1_2 };
  9441. #elif defined(WOLFSSL_TLS13)
  9442. const int versions[] = { WOLFSSL_TLSV1_3 };
  9443. #else
  9444. const int versions[0];
  9445. #endif
  9446. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9447. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  9448. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  9449. != WOLFSSL_SUCCESS) {
  9450. failFlag = WOLFSSL_FAILURE;
  9451. }
  9452. }
  9453. wolfSSL_CTX_free(ctx);
  9454. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  9455. #endif
  9456. return res;
  9457. } /* END test_wolfSSL_CTX_SetMinVersion */
  9458. /*----------------------------------------------------------------------------*
  9459. | OCSP Stapling
  9460. *----------------------------------------------------------------------------*/
  9461. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  9462. * need to contact the CA, lowering the cost of cert revocation checking.
  9463. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  9464. * POST: 1 returned for success.
  9465. */
  9466. static int test_wolfSSL_UseOCSPStapling(void)
  9467. {
  9468. int res = TEST_SKIPPED;
  9469. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  9470. !defined(NO_WOLFSSL_CLIENT)
  9471. int ret;
  9472. WOLFSSL_CTX* ctx;
  9473. WOLFSSL* ssl;
  9474. #ifndef NO_WOLFSSL_CLIENT
  9475. #ifndef WOLFSSL_NO_TLS12
  9476. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  9477. #else
  9478. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  9479. #endif
  9480. #else
  9481. #ifndef WOLFSSL_NO_TLS12
  9482. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  9483. #else
  9484. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  9485. #endif
  9486. #endif
  9487. ssl = wolfSSL_new(ctx);
  9488. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  9489. WOLFSSL_CSR2_OCSP_USE_NONCE);
  9490. wolfSSL_free(ssl);
  9491. wolfSSL_CTX_free(ctx);
  9492. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  9493. #endif
  9494. return res;
  9495. } /*END test_wolfSSL_UseOCSPStapling */
  9496. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  9497. * stapling eliminates the need to contact the CA and lowers cert revocation
  9498. * check.
  9499. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  9500. */
  9501. static int test_wolfSSL_UseOCSPStaplingV2(void)
  9502. {
  9503. int res = TEST_SKIPPED;
  9504. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  9505. !defined(NO_WOLFSSL_CLIENT)
  9506. int ret;
  9507. WOLFSSL_CTX* ctx;
  9508. WOLFSSL* ssl;
  9509. #ifndef NO_WOLFSSL_CLIENT
  9510. #ifndef WOLFSSL_NO_TLS12
  9511. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  9512. #else
  9513. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  9514. #endif
  9515. #else
  9516. #ifndef WOLFSSL_NO_TLS12
  9517. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  9518. #else
  9519. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  9520. #endif
  9521. #endif
  9522. ssl = wolfSSL_new(ctx);
  9523. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  9524. WOLFSSL_CSR2_OCSP_USE_NONCE );
  9525. wolfSSL_free(ssl);
  9526. wolfSSL_CTX_free(ctx);
  9527. res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  9528. #endif
  9529. return res;
  9530. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  9531. /*----------------------------------------------------------------------------*
  9532. | Multicast Tests
  9533. *----------------------------------------------------------------------------*/
  9534. static int test_wolfSSL_mcast(void)
  9535. {
  9536. int res = TEST_SKIPPED;
  9537. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  9538. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  9539. WOLFSSL_CTX* ctx;
  9540. WOLFSSL* ssl;
  9541. int result;
  9542. byte preMasterSecret[512];
  9543. byte clientRandom[32];
  9544. byte serverRandom[32];
  9545. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  9546. byte buf[256];
  9547. word16 newId;
  9548. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  9549. AssertNotNull(ctx);
  9550. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  9551. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9552. ssl = wolfSSL_new(ctx);
  9553. AssertNotNull(ssl);
  9554. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  9555. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  9556. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  9557. result = wolfSSL_set_secret(ssl, 23,
  9558. preMasterSecret, sizeof(preMasterSecret),
  9559. clientRandom, serverRandom, suite);
  9560. AssertIntEQ(result, WOLFSSL_SUCCESS);
  9561. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  9562. AssertIntLE(result, 0);
  9563. AssertIntLE(newId, 100);
  9564. wolfSSL_free(ssl);
  9565. wolfSSL_CTX_free(ctx);
  9566. res = TEST_RES_CHECK(1);
  9567. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  9568. * WOLFSSL_SNIFFER) */
  9569. return res;
  9570. }
  9571. /*----------------------------------------------------------------------------*
  9572. | Wolfcrypt
  9573. *----------------------------------------------------------------------------*/
  9574. /*
  9575. * Unit test for the wc_InitBlake2b()
  9576. */
  9577. static int test_wc_InitBlake2b(void)
  9578. {
  9579. int res = TEST_SKIPPED;
  9580. #ifdef HAVE_BLAKE2
  9581. Blake2b blake;
  9582. int ret = 0;
  9583. /* Test good arg. */
  9584. ret = wc_InitBlake2b(&blake, 64);
  9585. if (ret != 0) {
  9586. ret = WOLFSSL_FATAL_ERROR;
  9587. }
  9588. /* Test bad arg. */
  9589. if (!ret) {
  9590. ret = wc_InitBlake2b(NULL, 64);
  9591. if (ret == 0) {
  9592. ret = WOLFSSL_FATAL_ERROR;
  9593. }
  9594. else {
  9595. ret = 0;
  9596. }
  9597. }
  9598. if (!ret) {
  9599. ret = wc_InitBlake2b(NULL, 128);
  9600. if (ret == 0) {
  9601. ret = WOLFSSL_FATAL_ERROR;
  9602. }
  9603. else {
  9604. ret = 0;
  9605. }
  9606. }
  9607. if (!ret) {
  9608. ret = wc_InitBlake2b(&blake, 128);
  9609. if (ret == 0) {
  9610. ret = WOLFSSL_FATAL_ERROR;
  9611. }
  9612. else {
  9613. ret = 0;
  9614. }
  9615. }
  9616. if (!ret) {
  9617. ret = wc_InitBlake2b(NULL, 0);
  9618. if (ret == 0) {
  9619. ret = WOLFSSL_FATAL_ERROR;
  9620. }
  9621. else {
  9622. ret = 0;
  9623. }
  9624. }
  9625. if (!ret) {
  9626. ret = wc_InitBlake2b(&blake, 0);
  9627. if (ret == 0) {
  9628. ret = WOLFSSL_FATAL_ERROR;
  9629. }
  9630. else {
  9631. ret = 0;
  9632. }
  9633. }
  9634. res = TEST_RES_CHECK(ret == 0);
  9635. #endif
  9636. return res;
  9637. } /*END test_wc_InitBlake2b*/
  9638. /*
  9639. * Unit test for the wc_InitBlake2b_WithKey()
  9640. */
  9641. static int test_wc_InitBlake2b_WithKey(void)
  9642. {
  9643. int res = TEST_SKIPPED;
  9644. #ifdef HAVE_BLAKE2
  9645. Blake2b blake;
  9646. word32 digestSz = BLAKE2B_KEYBYTES;
  9647. byte key[BLAKE2B_KEYBYTES];
  9648. word32 keylen = BLAKE2B_KEYBYTES;
  9649. int ret = 0;
  9650. XMEMSET(key, 0, sizeof(key));
  9651. /* Test good arg. */
  9652. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  9653. if (ret != 0) {
  9654. ret = WOLFSSL_FATAL_ERROR;
  9655. }
  9656. /* Test bad args. */
  9657. if (ret == 0) {
  9658. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  9659. if (ret == BAD_FUNC_ARG) {
  9660. ret = 0;
  9661. }
  9662. }
  9663. if (ret == 0) {
  9664. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  9665. if (ret == BAD_FUNC_ARG) {
  9666. ret = 0;
  9667. }
  9668. }
  9669. if (ret == 0) {
  9670. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  9671. }
  9672. res = TEST_RES_CHECK(ret == 0);
  9673. #endif
  9674. return res;
  9675. } /*END wc_InitBlake2b_WithKey*/
  9676. /*
  9677. * Unit test for the wc_InitBlake2s_WithKey()
  9678. */
  9679. static int test_wc_InitBlake2s_WithKey(void)
  9680. {
  9681. int res = TEST_SKIPPED;
  9682. #ifdef HAVE_BLAKE2S
  9683. Blake2s blake;
  9684. word32 digestSz = BLAKE2S_KEYBYTES;
  9685. byte *key = (byte*)"01234567890123456789012345678901";
  9686. word32 keylen = BLAKE2S_KEYBYTES;
  9687. int ret = 0;
  9688. /* Test good arg. */
  9689. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  9690. if (ret != 0) {
  9691. ret = WOLFSSL_FATAL_ERROR;
  9692. }
  9693. /* Test bad args. */
  9694. if (ret == 0) {
  9695. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  9696. if (ret == BAD_FUNC_ARG) {
  9697. ret = 0;
  9698. }
  9699. }
  9700. if (ret == 0) {
  9701. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  9702. if (ret == BAD_FUNC_ARG) {
  9703. ret = 0;
  9704. }
  9705. }
  9706. if (ret == 0) {
  9707. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  9708. }
  9709. res = TEST_RES_CHECK(ret == 0);
  9710. #endif
  9711. return res;
  9712. } /*END wc_InitBlake2s_WithKey*/
  9713. /*
  9714. * Unit test for the wc_InitMd5()
  9715. */
  9716. static int test_wc_InitMd5(void)
  9717. {
  9718. int res = TEST_SKIPPED;
  9719. #ifndef NO_MD5
  9720. wc_Md5 md5;
  9721. int ret;
  9722. int flag = 0;
  9723. /* Test good arg. */
  9724. ret = wc_InitMd5(&md5);
  9725. if (ret != 0) {
  9726. flag = WOLFSSL_FATAL_ERROR;
  9727. }
  9728. /* Test bad arg. */
  9729. if (!flag) {
  9730. ret = wc_InitMd5(NULL);
  9731. if (ret != BAD_FUNC_ARG) {
  9732. flag = WOLFSSL_FATAL_ERROR;
  9733. }
  9734. }
  9735. wc_Md5Free(&md5);
  9736. res = TEST_RES_CHECK(flag == 0);
  9737. #endif
  9738. return res;
  9739. } /* END test_wc_InitMd5 */
  9740. /*
  9741. * Testing wc_UpdateMd5()
  9742. */
  9743. static int test_wc_Md5Update(void)
  9744. {
  9745. int res = TEST_SKIPPED;
  9746. #ifndef NO_MD5
  9747. wc_Md5 md5;
  9748. byte hash[WC_MD5_DIGEST_SIZE];
  9749. testVector a, b, c;
  9750. int ret;
  9751. int flag = 0;
  9752. ret = wc_InitMd5(&md5);
  9753. if (ret != 0) {
  9754. flag = ret;
  9755. }
  9756. /* Input */
  9757. if (!flag) {
  9758. a.input = "a";
  9759. a.inLen = XSTRLEN(a.input);
  9760. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  9761. if (ret != 0) {
  9762. flag = ret;
  9763. }
  9764. }
  9765. if (!flag) {
  9766. ret = wc_Md5Final(&md5, hash);
  9767. if (ret != 0) {
  9768. flag = ret;
  9769. }
  9770. }
  9771. /* Update input. */
  9772. if (!flag) {
  9773. a.input = "abc";
  9774. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  9775. "\x72";
  9776. a.inLen = XSTRLEN(a.input);
  9777. a.outLen = XSTRLEN(a.output);
  9778. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  9779. if (ret != 0) {
  9780. flag = ret;
  9781. }
  9782. }
  9783. if (!flag) {
  9784. ret = wc_Md5Final(&md5, hash);
  9785. if (ret != 0) {
  9786. flag = ret;
  9787. }
  9788. }
  9789. if (!flag) {
  9790. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  9791. flag = WOLFSSL_FATAL_ERROR;
  9792. }
  9793. }
  9794. /*Pass in bad values. */
  9795. if (!flag) {
  9796. b.input = NULL;
  9797. b.inLen = 0;
  9798. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  9799. if (ret != 0) {
  9800. flag = ret;
  9801. }
  9802. }
  9803. if (!flag) {
  9804. c.input = NULL;
  9805. c.inLen = WC_MD5_DIGEST_SIZE;
  9806. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  9807. if (ret != BAD_FUNC_ARG) {
  9808. flag = WOLFSSL_FATAL_ERROR;
  9809. }
  9810. }
  9811. if (!flag) {
  9812. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  9813. if (ret != BAD_FUNC_ARG) {
  9814. flag = WOLFSSL_FATAL_ERROR;
  9815. }
  9816. }
  9817. wc_Md5Free(&md5);
  9818. res = TEST_RES_CHECK(flag == 0);
  9819. #endif
  9820. return res;
  9821. } /* END test_wc_Md5Update() */
  9822. /*
  9823. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9824. */
  9825. static int test_wc_Md5Final(void)
  9826. {
  9827. int res = TEST_SKIPPED;
  9828. #ifndef NO_MD5
  9829. /* Instantiate */
  9830. wc_Md5 md5;
  9831. byte* hash_test[3];
  9832. byte hash1[WC_MD5_DIGEST_SIZE];
  9833. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9834. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9835. int times, i, ret;
  9836. int flag = 0;
  9837. /* Initialize */
  9838. ret = wc_InitMd5(&md5);
  9839. if (ret != 0) {
  9840. flag = ret;
  9841. }
  9842. if (!flag) {
  9843. hash_test[0] = hash1;
  9844. hash_test[1] = hash2;
  9845. hash_test[2] = hash3;
  9846. }
  9847. times = sizeof(hash_test)/sizeof(byte*);
  9848. for (i = 0; i < times; i++) {
  9849. if (!flag) {
  9850. ret = wc_Md5Final(&md5, hash_test[i]);
  9851. if (ret != 0) {
  9852. flag = WOLFSSL_FATAL_ERROR;
  9853. }
  9854. }
  9855. }
  9856. /* Test bad args. */
  9857. if (!flag) {
  9858. ret = wc_Md5Final(NULL, NULL);
  9859. if (ret != BAD_FUNC_ARG) {
  9860. flag = WOLFSSL_FATAL_ERROR;
  9861. }
  9862. }
  9863. if (!flag) {
  9864. ret = wc_Md5Final(NULL, hash1);
  9865. if (ret != BAD_FUNC_ARG) {
  9866. flag = WOLFSSL_FATAL_ERROR;
  9867. }
  9868. }
  9869. if (!flag) {
  9870. ret = wc_Md5Final(&md5, NULL);
  9871. if (ret != BAD_FUNC_ARG) {
  9872. flag = WOLFSSL_FATAL_ERROR;
  9873. }
  9874. }
  9875. wc_Md5Free(&md5);
  9876. res = TEST_RES_CHECK(flag == 0);
  9877. #endif
  9878. return res;
  9879. }
  9880. /*
  9881. * Unit test for the wc_InitSha()
  9882. */
  9883. static int test_wc_InitSha(void)
  9884. {
  9885. int res = TEST_SKIPPED;
  9886. #ifndef NO_SHA
  9887. wc_Sha sha;
  9888. int ret;
  9889. int flag = 0;
  9890. /* Test good arg. */
  9891. ret = wc_InitSha(&sha);
  9892. if (ret != 0) {
  9893. flag = WOLFSSL_FATAL_ERROR;
  9894. }
  9895. /* Test bad arg. */
  9896. if (!flag) {
  9897. ret = wc_InitSha(NULL);
  9898. if (ret != BAD_FUNC_ARG) {
  9899. flag = WOLFSSL_FATAL_ERROR;
  9900. }
  9901. }
  9902. wc_ShaFree(&sha);
  9903. res = TEST_RES_CHECK(flag == 0);
  9904. #endif
  9905. return res;
  9906. } /* END test_wc_InitSha */
  9907. /*
  9908. * Tesing wc_ShaUpdate()
  9909. */
  9910. static int test_wc_ShaUpdate(void)
  9911. {
  9912. int res = TEST_SKIPPED;
  9913. #ifndef NO_SHA
  9914. wc_Sha sha;
  9915. byte hash[WC_SHA_DIGEST_SIZE];
  9916. testVector a, b, c;
  9917. int flag = 0;
  9918. int ret;
  9919. ret = wc_InitSha(&sha);
  9920. if (ret != 0) {
  9921. flag = ret;
  9922. }
  9923. /* Input. */
  9924. if (!flag) {
  9925. a.input = "a";
  9926. a.inLen = XSTRLEN(a.input);
  9927. ret = wc_ShaUpdate(&sha, NULL, 0);
  9928. if (ret != 0) {
  9929. flag = ret;
  9930. }
  9931. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9932. if (ret != 0) {
  9933. flag = ret;
  9934. }
  9935. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9936. if (ret != 0) {
  9937. flag = ret;
  9938. }
  9939. }
  9940. if (!flag) {
  9941. ret = wc_ShaFinal(&sha, hash);
  9942. if (ret != 0) {
  9943. flag = ret;
  9944. }
  9945. }
  9946. /* Update input. */
  9947. if (!flag) {
  9948. a.input = "abc";
  9949. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9950. "\x6C\x9C\xD0\xD8\x9D";
  9951. a.inLen = XSTRLEN(a.input);
  9952. a.outLen = XSTRLEN(a.output);
  9953. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9954. if (ret != 0) {
  9955. flag = ret;
  9956. }
  9957. }
  9958. if (!flag) {
  9959. ret = wc_ShaFinal(&sha, hash);
  9960. if (ret !=0) {
  9961. flag = ret;
  9962. }
  9963. }
  9964. if (!flag) {
  9965. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9966. flag = WOLFSSL_FATAL_ERROR;
  9967. }
  9968. }
  9969. /* Try passing in bad values. */
  9970. if (!flag) {
  9971. b.input = NULL;
  9972. b.inLen = 0;
  9973. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9974. if (ret != 0) {
  9975. flag = ret;
  9976. }
  9977. }
  9978. if (!flag) {
  9979. c.input = NULL;
  9980. c.inLen = WC_SHA_DIGEST_SIZE;
  9981. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9982. if (ret != BAD_FUNC_ARG) {
  9983. flag = WOLFSSL_FATAL_ERROR;
  9984. }
  9985. }
  9986. if (!flag) {
  9987. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9988. if (ret != BAD_FUNC_ARG) {
  9989. flag = WOLFSSL_FATAL_ERROR;
  9990. }
  9991. }
  9992. wc_ShaFree(&sha);
  9993. res = TEST_RES_CHECK(flag == 0);
  9994. #endif
  9995. return res;
  9996. } /* END test_wc_ShaUpdate() */
  9997. /*
  9998. * Unit test on wc_ShaFinal
  9999. */
  10000. static int test_wc_ShaFinal(void)
  10001. {
  10002. int res = TEST_SKIPPED;
  10003. #ifndef NO_SHA
  10004. wc_Sha sha;
  10005. byte* hash_test[3];
  10006. byte hash1[WC_SHA_DIGEST_SIZE];
  10007. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10008. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10009. int times, i, ret;
  10010. int flag = 0;
  10011. /*Initialize*/
  10012. ret = wc_InitSha(&sha);
  10013. if (ret) {
  10014. flag = ret;
  10015. }
  10016. if (!flag) {
  10017. hash_test[0] = hash1;
  10018. hash_test[1] = hash2;
  10019. hash_test[2] = hash3;
  10020. }
  10021. times = sizeof(hash_test)/sizeof(byte*);
  10022. for (i = 0; i < times; i++) {
  10023. if (!flag) {
  10024. ret = wc_ShaFinal(&sha, hash_test[i]);
  10025. if (ret != 0) {
  10026. flag = WOLFSSL_FATAL_ERROR;
  10027. }
  10028. }
  10029. }
  10030. /* Test bad args. */
  10031. if (!flag) {
  10032. ret = wc_ShaFinal(NULL, NULL);
  10033. if (ret != BAD_FUNC_ARG) {
  10034. flag = WOLFSSL_FATAL_ERROR;
  10035. }
  10036. }
  10037. if (!flag) {
  10038. ret = wc_ShaFinal(NULL, hash1);
  10039. if (ret != BAD_FUNC_ARG) {
  10040. flag = WOLFSSL_FATAL_ERROR;
  10041. }
  10042. }
  10043. if (!flag) {
  10044. ret = wc_ShaFinal(&sha, NULL);
  10045. if (ret != BAD_FUNC_ARG) {
  10046. flag = WOLFSSL_FATAL_ERROR;
  10047. }
  10048. }
  10049. wc_ShaFree(&sha);
  10050. res = TEST_RES_CHECK(flag == 0);
  10051. #endif
  10052. return res;
  10053. } /* END test_wc_ShaFinal */
  10054. /*
  10055. * Unit test for wc_InitSha256()
  10056. */
  10057. static int test_wc_InitSha256(void)
  10058. {
  10059. int res = TEST_SKIPPED;
  10060. #ifndef NO_SHA256
  10061. wc_Sha256 sha256;
  10062. int ret;
  10063. int flag = 0;
  10064. /* Test good arg. */
  10065. ret = wc_InitSha256(&sha256);
  10066. if (ret != 0) {
  10067. flag = WOLFSSL_FATAL_ERROR;
  10068. }
  10069. /* Test bad arg. */
  10070. if (!flag) {
  10071. ret = wc_InitSha256(NULL);
  10072. if (ret != BAD_FUNC_ARG) {
  10073. flag = WOLFSSL_FATAL_ERROR;
  10074. }
  10075. }
  10076. wc_Sha256Free(&sha256);
  10077. res = TEST_RES_CHECK(flag == 0);
  10078. #endif
  10079. return res;
  10080. } /* END test_wc_InitSha256 */
  10081. /*
  10082. * Unit test for wc_Sha256Update()
  10083. */
  10084. static int test_wc_Sha256Update(void)
  10085. {
  10086. int res = TEST_SKIPPED;
  10087. #ifndef NO_SHA256
  10088. wc_Sha256 sha256;
  10089. byte hash[WC_SHA256_DIGEST_SIZE];
  10090. testVector a, b, c;
  10091. int ret;
  10092. int flag = 0;
  10093. ret = wc_InitSha256(&sha256);
  10094. if (ret != 0) {
  10095. flag = ret;
  10096. }
  10097. /* Input. */
  10098. if (!flag) {
  10099. a.input = "a";
  10100. a.inLen = XSTRLEN(a.input);
  10101. ret = wc_Sha256Update(&sha256, NULL, 0);
  10102. if (ret != 0) {
  10103. flag = ret;
  10104. }
  10105. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  10106. if (ret != 0) {
  10107. flag = ret;
  10108. }
  10109. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10110. if (ret != 0) {
  10111. flag = ret;
  10112. }
  10113. }
  10114. if (!flag) {
  10115. ret = wc_Sha256Final(&sha256, hash);
  10116. if (ret != 0) {
  10117. flag = ret;
  10118. }
  10119. }
  10120. /* Update input. */
  10121. if (!flag) {
  10122. a.input = "abc";
  10123. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10124. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10125. "\x15\xAD";
  10126. a.inLen = XSTRLEN(a.input);
  10127. a.outLen = XSTRLEN(a.output);
  10128. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  10129. if (ret != 0) {
  10130. flag = ret;
  10131. }
  10132. }
  10133. if (!flag) {
  10134. ret = wc_Sha256Final(&sha256, hash);
  10135. if (ret != 0) {
  10136. flag = ret;
  10137. }
  10138. }
  10139. if (!flag) {
  10140. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  10141. flag = WOLFSSL_FATAL_ERROR;
  10142. }
  10143. }
  10144. /* Try passing in bad values */
  10145. if (!flag) {
  10146. b.input = NULL;
  10147. b.inLen = 0;
  10148. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  10149. if (ret != 0) {
  10150. flag = ret;
  10151. }
  10152. }
  10153. if (!flag) {
  10154. c.input = NULL;
  10155. c.inLen = WC_SHA256_DIGEST_SIZE;
  10156. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  10157. if (ret != BAD_FUNC_ARG) {
  10158. flag = WOLFSSL_FATAL_ERROR;
  10159. }
  10160. }
  10161. if (!flag) {
  10162. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10163. if (ret != BAD_FUNC_ARG) {
  10164. flag = WOLFSSL_FATAL_ERROR;
  10165. }
  10166. }
  10167. wc_Sha256Free(&sha256);
  10168. res = TEST_RES_CHECK(flag == 0);
  10169. #endif
  10170. return res;
  10171. } /* END test_wc_Sha256Update */
  10172. /*
  10173. * Unit test function for wc_Sha256Final()
  10174. */
  10175. static int test_wc_Sha256Final(void)
  10176. {
  10177. int res = TEST_SKIPPED;
  10178. #ifndef NO_SHA256
  10179. wc_Sha256 sha256;
  10180. byte* hash_test[3];
  10181. byte hash1[WC_SHA256_DIGEST_SIZE];
  10182. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10183. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10184. int times, i, ret;
  10185. int flag = 0;
  10186. /* Initialize */
  10187. ret = wc_InitSha256(&sha256);
  10188. if (ret != 0) {
  10189. flag = ret;
  10190. }
  10191. if (!flag) {
  10192. hash_test[0] = hash1;
  10193. hash_test[1] = hash2;
  10194. hash_test[2] = hash3;
  10195. }
  10196. times = sizeof(hash_test) / sizeof(byte*);
  10197. for (i = 0; i < times; i++) {
  10198. if (!flag) {
  10199. ret = wc_Sha256Final(&sha256, hash_test[i]);
  10200. if (ret != 0) {
  10201. flag = WOLFSSL_FATAL_ERROR;
  10202. }
  10203. }
  10204. }
  10205. /* Test bad args. */
  10206. if (!flag ) {
  10207. ret = wc_Sha256Final(NULL, NULL);
  10208. if (ret != BAD_FUNC_ARG) {
  10209. flag = WOLFSSL_FATAL_ERROR;
  10210. }
  10211. }
  10212. if (!flag) {
  10213. ret = wc_Sha256Final(NULL, hash1);
  10214. if (ret != BAD_FUNC_ARG) {
  10215. flag = WOLFSSL_FATAL_ERROR;
  10216. }
  10217. }
  10218. if (!flag) {
  10219. ret = wc_Sha256Final(&sha256, NULL);
  10220. if (ret != BAD_FUNC_ARG) {
  10221. flag = WOLFSSL_FATAL_ERROR;
  10222. }
  10223. }
  10224. wc_Sha256Free(&sha256);
  10225. res = TEST_RES_CHECK(flag == 0);
  10226. #endif
  10227. return res;
  10228. } /* END test_wc_Sha256Final */
  10229. /*
  10230. * Unit test function for wc_Sha256FinalRaw()
  10231. */
  10232. static int test_wc_Sha256FinalRaw(void)
  10233. {
  10234. int res = TEST_SKIPPED;
  10235. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10236. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10237. !defined(WOLFSSL_NO_HASH_RAW)
  10238. wc_Sha256 sha256;
  10239. byte* hash_test[3];
  10240. byte hash1[WC_SHA256_DIGEST_SIZE];
  10241. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10242. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10243. int times, i, ret;
  10244. int flag = 0;
  10245. /* Initialize */
  10246. ret = wc_InitSha256(&sha256);
  10247. if (ret != 0) {
  10248. flag = ret;
  10249. }
  10250. if (!flag) {
  10251. hash_test[0] = hash1;
  10252. hash_test[1] = hash2;
  10253. hash_test[2] = hash3;
  10254. }
  10255. times = sizeof(hash_test) / sizeof(byte*);
  10256. for (i = 0; i < times; i++) {
  10257. if (!flag) {
  10258. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  10259. if (ret != 0) {
  10260. flag = WOLFSSL_FATAL_ERROR;
  10261. }
  10262. }
  10263. }
  10264. /* Test bad args. */
  10265. if (!flag) {
  10266. ret = wc_Sha256FinalRaw(NULL, NULL);
  10267. if (ret != BAD_FUNC_ARG) {
  10268. flag = WOLFSSL_FATAL_ERROR;
  10269. }
  10270. }
  10271. if (!flag) {
  10272. ret = wc_Sha256FinalRaw(NULL, hash1);
  10273. if (ret != BAD_FUNC_ARG) {
  10274. flag = WOLFSSL_FATAL_ERROR;
  10275. }
  10276. }
  10277. if (!flag) {
  10278. ret = wc_Sha256FinalRaw(&sha256, NULL);
  10279. if (ret != BAD_FUNC_ARG) {
  10280. flag = WOLFSSL_FATAL_ERROR;
  10281. }
  10282. }
  10283. wc_Sha256Free(&sha256);
  10284. res = TEST_RES_CHECK(flag == 0);
  10285. #endif
  10286. return res;
  10287. } /* END test_wc_Sha256FinalRaw */
  10288. /*
  10289. * Unit test function for wc_Sha256GetFlags()
  10290. */
  10291. static int test_wc_Sha256GetFlags(void)
  10292. {
  10293. int res = TEST_SKIPPED;
  10294. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10295. wc_Sha256 sha256;
  10296. word32 flags = 0;
  10297. int flag = 0;
  10298. /* Initialize */
  10299. flag = wc_InitSha256(&sha256);
  10300. if (flag == 0) {
  10301. flag = wc_Sha256GetFlags(&sha256, &flags);
  10302. }
  10303. if (flag == 0) {
  10304. if (flags & WC_HASH_FLAG_ISCOPY) {
  10305. flag = 0;
  10306. }
  10307. }
  10308. wc_Sha256Free(&sha256);
  10309. res = TEST_RES_CHECK(flag == 0);
  10310. #endif
  10311. return res;
  10312. } /* END test_wc_Sha256GetFlags */
  10313. /*
  10314. * Unit test function for wc_Sha256Free()
  10315. */
  10316. static int test_wc_Sha256Free(void)
  10317. {
  10318. int res = TEST_SKIPPED;
  10319. #ifndef NO_SHA256
  10320. wc_Sha256Free(NULL);
  10321. res = TEST_RES_CHECK(1);
  10322. #endif
  10323. return res;
  10324. } /* END test_wc_Sha256Free */
  10325. /*
  10326. * Unit test function for wc_Sha256GetHash()
  10327. */
  10328. static int test_wc_Sha256GetHash(void)
  10329. {
  10330. int res = TEST_SKIPPED;
  10331. #ifndef NO_SHA256
  10332. wc_Sha256 sha256;
  10333. byte hash1[WC_SHA256_DIGEST_SIZE];
  10334. int flag = 0;
  10335. /* Initialize */
  10336. flag = wc_InitSha256(&sha256);
  10337. if (flag == 0) {
  10338. flag = wc_Sha256GetHash(&sha256, hash1);
  10339. }
  10340. /*test bad arguments*/
  10341. if (flag == 0) {
  10342. flag = wc_Sha256GetHash(NULL, NULL);
  10343. if (flag == BAD_FUNC_ARG) {
  10344. flag = 0;
  10345. }
  10346. }
  10347. if (flag == 0) {
  10348. flag = wc_Sha256GetHash(NULL, hash1);
  10349. if (flag == BAD_FUNC_ARG) {
  10350. flag = 0;
  10351. }
  10352. }
  10353. if (flag == 0) {
  10354. flag = wc_Sha256GetHash(&sha256, NULL);
  10355. if (flag == BAD_FUNC_ARG) {
  10356. flag = 0;
  10357. }
  10358. }
  10359. wc_Sha256Free(&sha256);
  10360. res = TEST_RES_CHECK(flag == 0);
  10361. #endif
  10362. return res;
  10363. } /* END test_wc_Sha256GetHash */
  10364. /*
  10365. * Unit test function for wc_Sha256Copy()
  10366. */
  10367. static int test_wc_Sha256Copy(void)
  10368. {
  10369. int res = TEST_SKIPPED;
  10370. #ifndef NO_SHA256
  10371. wc_Sha256 sha256;
  10372. wc_Sha256 temp;
  10373. int flag = 0;
  10374. /* Initialize */
  10375. flag = wc_InitSha256(&sha256);
  10376. if (flag == 0) {
  10377. flag = wc_InitSha256(&temp);
  10378. }
  10379. if (flag == 0) {
  10380. flag = wc_Sha256Copy(&sha256, &temp);
  10381. }
  10382. /*test bad arguments*/
  10383. if (flag == 0) {
  10384. flag = wc_Sha256Copy(NULL, NULL);
  10385. if (flag == BAD_FUNC_ARG) {
  10386. flag = 0;
  10387. }
  10388. }
  10389. if (flag == 0) {
  10390. flag = wc_Sha256Copy(NULL, &temp);
  10391. if (flag == BAD_FUNC_ARG) {
  10392. flag = 0;
  10393. }
  10394. }
  10395. if (flag == 0) {
  10396. flag = wc_Sha256Copy(&sha256, NULL);
  10397. if (flag == BAD_FUNC_ARG) {
  10398. flag = 0;
  10399. }
  10400. }
  10401. wc_Sha256Free(&sha256);
  10402. wc_Sha256Free(&temp);
  10403. res = TEST_RES_CHECK(flag == 0);
  10404. #endif
  10405. return res;
  10406. } /* END test_wc_Sha256Copy */
  10407. /*
  10408. * Testing wc_InitSha512()
  10409. */
  10410. static int test_wc_InitSha512(void)
  10411. {
  10412. int res = TEST_SKIPPED;
  10413. #ifdef WOLFSSL_SHA512
  10414. wc_Sha512 sha512;
  10415. int ret;
  10416. int flag = 0;
  10417. /* Test good arg. */
  10418. ret = wc_InitSha512(&sha512);
  10419. if (ret != 0) {
  10420. flag = WOLFSSL_FATAL_ERROR;
  10421. }
  10422. /* Test bad arg. */
  10423. if (!flag) {
  10424. ret = wc_InitSha512(NULL);
  10425. if (ret != BAD_FUNC_ARG) {
  10426. flag = WOLFSSL_FATAL_ERROR;
  10427. }
  10428. }
  10429. wc_Sha512Free(&sha512);
  10430. res = TEST_RES_CHECK(flag == 0);
  10431. #endif
  10432. return res;
  10433. } /* END test_wc_InitSha512 */
  10434. /*
  10435. * wc_Sha512Update() test.
  10436. */
  10437. static int test_wc_Sha512Update(void)
  10438. {
  10439. int res = TEST_SKIPPED;
  10440. #ifdef WOLFSSL_SHA512
  10441. wc_Sha512 sha512;
  10442. byte hash[WC_SHA512_DIGEST_SIZE];
  10443. testVector a, b, c;
  10444. int ret;
  10445. int flag = 0;
  10446. ret = wc_InitSha512(&sha512);
  10447. if (ret != 0) {
  10448. flag = ret;
  10449. }
  10450. /* Input. */
  10451. if (!flag) {
  10452. a.input = "a";
  10453. a.inLen = XSTRLEN(a.input);
  10454. ret = wc_Sha512Update(&sha512, NULL, 0);
  10455. if (ret != 0) {
  10456. flag = ret;
  10457. }
  10458. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  10459. if (ret != 0) {
  10460. flag = ret;
  10461. }
  10462. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10463. if (ret != 0) {
  10464. flag = ret;
  10465. }
  10466. ret = wc_Sha512Final(&sha512, hash);
  10467. if (ret != 0) {
  10468. flag = ret;
  10469. }
  10470. }
  10471. /* Update input. */
  10472. if (!flag) {
  10473. a.input = "abc";
  10474. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  10475. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  10476. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  10477. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  10478. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  10479. a.inLen = XSTRLEN(a.input);
  10480. a.outLen = XSTRLEN(a.output);
  10481. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10482. if (ret != 0) {
  10483. flag = ret;
  10484. }
  10485. }
  10486. if (!flag) {
  10487. ret = wc_Sha512Final(&sha512, hash);
  10488. if (ret != 0) {
  10489. flag = ret;
  10490. }
  10491. }
  10492. if (!flag) {
  10493. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  10494. flag = WOLFSSL_FATAL_ERROR;
  10495. }
  10496. }
  10497. /* Try passing in bad values */
  10498. if (!flag) {
  10499. b.input = NULL;
  10500. b.inLen = 0;
  10501. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  10502. if (ret != 0) {
  10503. flag = ret;
  10504. }
  10505. }
  10506. if (!flag) {
  10507. c.input = NULL;
  10508. c.inLen = WC_SHA512_DIGEST_SIZE;
  10509. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10510. if (ret != BAD_FUNC_ARG) {
  10511. flag = WOLFSSL_FATAL_ERROR;
  10512. }
  10513. }
  10514. if (!flag) {
  10515. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  10516. if (ret != BAD_FUNC_ARG) {
  10517. flag = WOLFSSL_FATAL_ERROR;
  10518. }
  10519. }
  10520. wc_Sha512Free(&sha512);
  10521. res = TEST_RES_CHECK(flag == 0);
  10522. #endif
  10523. return res;
  10524. } /* END test_wc_Sha512Update */
  10525. #ifdef WOLFSSL_SHA512
  10526. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10527. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10528. /* Perfoms test for
  10529. * - wc_Sha512Final/wc_Sha512FinalRaw
  10530. * - wc_Sha512_224Final/wc_Sha512_224Final
  10531. * - wc_Sha512_256Final/wc_Sha512_256Final
  10532. * parameter:
  10533. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  10534. * WC_HASH_TYPE_SHA512_256
  10535. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  10536. *return 0 on success
  10537. */
  10538. static int test_Sha512_Family_Final(int type, int isRaw)
  10539. {
  10540. wc_Sha512 sha512;
  10541. byte* hash_test[3];
  10542. byte hash1[WC_SHA512_DIGEST_SIZE];
  10543. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10544. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10545. int times, i, ret;
  10546. int(*initFp)(wc_Sha512*);
  10547. int(*finalFp)(wc_Sha512*, byte*);
  10548. void(*freeFp)(wc_Sha512*);
  10549. if (type == WC_HASH_TYPE_SHA512) {
  10550. initFp = wc_InitSha512;
  10551. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10552. !defined(WOLFSSL_NO_HASH_RAW)
  10553. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  10554. #else
  10555. finalFp = (isRaw)? NULL : wc_Sha512Final;
  10556. #endif
  10557. freeFp = wc_Sha512Free;
  10558. }
  10559. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10560. #if !defined(WOLFSSL_NOSHA512_224)
  10561. else if (type == WC_HASH_TYPE_SHA512_224) {
  10562. initFp = wc_InitSha512_224;
  10563. #if !defined(WOLFSSL_NO_HASH_RAW)
  10564. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  10565. #else
  10566. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  10567. #endif
  10568. freeFp = wc_Sha512_224Free;
  10569. }
  10570. #endif
  10571. #if !defined(WOLFSSL_NOSHA512_256)
  10572. else if (type == WC_HASH_TYPE_SHA512_256) {
  10573. initFp = wc_InitSha512_256;
  10574. #if !defined(WOLFSSL_NO_HASH_RAW)
  10575. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  10576. #else
  10577. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  10578. #endif
  10579. freeFp = wc_Sha512_256Free;
  10580. }
  10581. #endif
  10582. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10583. else
  10584. return TEST_FAIL;
  10585. /* Initialize */
  10586. ret = initFp(&sha512);
  10587. if (!ret) {
  10588. hash_test[0] = hash1;
  10589. hash_test[1] = hash2;
  10590. hash_test[2] = hash3;
  10591. }
  10592. times = sizeof(hash_test) / sizeof(byte *);
  10593. /* Good test args. */
  10594. for (i = 0; i < times && ret == 0; i++) {
  10595. ret = finalFp(&sha512, hash_test[i]);
  10596. }
  10597. /* Test bad args. */
  10598. if (!ret) {
  10599. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  10600. ret = WOLFSSL_FATAL_ERROR;
  10601. }
  10602. }
  10603. if (!ret) {
  10604. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  10605. ret = WOLFSSL_FATAL_ERROR;
  10606. }
  10607. }
  10608. if (!ret) {
  10609. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  10610. ret = WOLFSSL_FATAL_ERROR;
  10611. }
  10612. }
  10613. freeFp(&sha512);
  10614. return ret;
  10615. }
  10616. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10617. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10618. #endif /* WOLFSSL_SHA512 */
  10619. /*
  10620. * Unit test function for wc_Sha512Final()
  10621. */
  10622. static int test_wc_Sha512Final(void)
  10623. {
  10624. int res = TEST_SKIPPED;
  10625. #ifdef WOLFSSL_SHA512
  10626. wc_Sha512 sha512;
  10627. byte* hash_test[3];
  10628. byte hash1[WC_SHA512_DIGEST_SIZE];
  10629. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10630. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10631. int times, i, ret;
  10632. int flag = 0;
  10633. /* Initialize */
  10634. ret = wc_InitSha512(&sha512);
  10635. if (ret != 0) {
  10636. flag = ret;
  10637. }
  10638. if (!flag) {
  10639. hash_test[0] = hash1;
  10640. hash_test[1] = hash2;
  10641. hash_test[2] = hash3;
  10642. }
  10643. times = sizeof(hash_test) / sizeof(byte *);
  10644. for (i = 0; i < times; i++) {
  10645. if (!flag) {
  10646. ret = wc_Sha512Final(&sha512, hash_test[i]);
  10647. if (ret != 0) {
  10648. flag = WOLFSSL_FATAL_ERROR;
  10649. }
  10650. }
  10651. }
  10652. /* Test bad args. */
  10653. if (!flag) {
  10654. ret = wc_Sha512Final(NULL, NULL);
  10655. if (ret != BAD_FUNC_ARG) {
  10656. flag = WOLFSSL_FATAL_ERROR;
  10657. }
  10658. }
  10659. if (!flag) {
  10660. ret = wc_Sha512Final(NULL, hash1);
  10661. if (ret != BAD_FUNC_ARG) {
  10662. flag = WOLFSSL_FATAL_ERROR;
  10663. }
  10664. }
  10665. if (!flag) {
  10666. ret = wc_Sha512Final(&sha512, NULL);
  10667. if (ret != BAD_FUNC_ARG) {
  10668. flag = WOLFSSL_FATAL_ERROR;
  10669. }
  10670. }
  10671. wc_Sha512Free(&sha512);
  10672. res = TEST_RES_CHECK(flag == 0);
  10673. #endif
  10674. return res;
  10675. } /* END test_wc_Sha512Final */
  10676. /*
  10677. * Unit test function for wc_Sha512GetFlags()
  10678. */
  10679. static int test_wc_Sha512GetFlags(void)
  10680. {
  10681. int res = TEST_SKIPPED;
  10682. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  10683. wc_Sha512 sha512;
  10684. word32 flags = 0;
  10685. int flag = 0;
  10686. /* Initialize */
  10687. flag = wc_InitSha512(&sha512);
  10688. if (flag == 0) {
  10689. flag = wc_Sha512GetFlags(&sha512, &flags);
  10690. }
  10691. if (flag == 0) {
  10692. if (flags & WC_HASH_FLAG_ISCOPY) {
  10693. flag = 0;
  10694. }
  10695. }
  10696. wc_Sha512Free(&sha512);
  10697. res = TEST_RES_CHECK(flag == 0);
  10698. #endif
  10699. return res;
  10700. } /* END test_wc_Sha512GetFlags */
  10701. /*
  10702. * Unit test function for wc_Sha512FinalRaw()
  10703. */
  10704. static int test_wc_Sha512FinalRaw(void)
  10705. {
  10706. int res = TEST_SKIPPED;
  10707. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10708. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10709. !defined(WOLFSSL_NO_HASH_RAW)
  10710. wc_Sha512 sha512;
  10711. byte* hash_test[3];
  10712. byte hash1[WC_SHA512_DIGEST_SIZE];
  10713. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  10714. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  10715. int times, i, ret;
  10716. int flag = 0;
  10717. /* Initialize */
  10718. ret = wc_InitSha512(&sha512);
  10719. if (ret != 0) {
  10720. flag = ret;
  10721. }
  10722. if (!flag) {
  10723. hash_test[0] = hash1;
  10724. hash_test[1] = hash2;
  10725. hash_test[2] = hash3;
  10726. }
  10727. times = sizeof(hash_test) / sizeof(byte*);
  10728. /* Good test args. */
  10729. for (i = 0; i < times; i++) {
  10730. if (!flag) {
  10731. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  10732. if (ret != 0) {
  10733. flag = WOLFSSL_FATAL_ERROR;
  10734. }
  10735. }
  10736. }
  10737. /* Test bad args. */
  10738. if (!flag ) {
  10739. ret = wc_Sha512FinalRaw(NULL, NULL);
  10740. if (ret != BAD_FUNC_ARG) {
  10741. flag = WOLFSSL_FATAL_ERROR;
  10742. }
  10743. }
  10744. if (!flag) {
  10745. ret = wc_Sha512FinalRaw(NULL, hash1);
  10746. if (ret != BAD_FUNC_ARG) {
  10747. flag = WOLFSSL_FATAL_ERROR;
  10748. }
  10749. }
  10750. if (!flag) {
  10751. ret = wc_Sha512FinalRaw(&sha512, NULL);
  10752. if (ret != BAD_FUNC_ARG) {
  10753. flag = WOLFSSL_FATAL_ERROR;
  10754. }
  10755. }
  10756. wc_Sha512Free(&sha512);
  10757. res = TEST_RES_CHECK(flag == 0);
  10758. #endif
  10759. return res;
  10760. } /* END test_wc_Sha512FinalRaw */
  10761. /*
  10762. * Unit test function for wc_Sha512Free()
  10763. */
  10764. static int test_wc_Sha512Free(void)
  10765. {
  10766. int res = TEST_SKIPPED;
  10767. #ifdef WOLFSSL_SHA512
  10768. wc_Sha512Free(NULL);
  10769. res = TEST_RES_CHECK(1);
  10770. #endif
  10771. return res;
  10772. } /* END test_wc_Sha512Free */
  10773. #ifdef WOLFSSL_SHA512
  10774. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10775. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10776. static int test_Sha512_Family_GetHash(int type )
  10777. {
  10778. int flag = 0;
  10779. int(*initFp)(wc_Sha512*);
  10780. int(*ghashFp)(wc_Sha512*, byte*);
  10781. wc_Sha512 sha512;
  10782. byte hash1[WC_SHA512_DIGEST_SIZE];
  10783. if (type == WC_HASH_TYPE_SHA512) {
  10784. initFp = wc_InitSha512;
  10785. ghashFp = wc_Sha512GetHash;
  10786. }
  10787. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10788. #if !defined(WOLFSSL_NOSHA512_224)
  10789. else if (type == WC_HASH_TYPE_SHA512_224) {
  10790. initFp = wc_InitSha512_224;
  10791. ghashFp = wc_Sha512_224GetHash;
  10792. }
  10793. #endif
  10794. #if !defined(WOLFSSL_NOSHA512_256)
  10795. else if (type == WC_HASH_TYPE_SHA512_256) {
  10796. initFp = wc_InitSha512_256;
  10797. ghashFp = wc_Sha512_256GetHash;
  10798. }
  10799. #endif
  10800. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10801. else {
  10802. initFp = NULL;
  10803. ghashFp = NULL;
  10804. }
  10805. if (initFp == NULL || ghashFp == NULL)
  10806. return TEST_FAIL;
  10807. if (!flag) {
  10808. flag = initFp(&sha512);
  10809. }
  10810. if (!flag) {
  10811. flag = ghashFp(&sha512, hash1);
  10812. }
  10813. /*test bad arguments*/
  10814. if (!flag) {
  10815. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10816. flag = WOLFSSL_FATAL_ERROR;
  10817. }
  10818. if (!flag) {
  10819. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10820. flag = WOLFSSL_FATAL_ERROR;
  10821. }
  10822. if (!flag) {
  10823. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10824. flag = WOLFSSL_FATAL_ERROR;
  10825. }
  10826. wc_Sha512Free(&sha512);
  10827. return flag;
  10828. }
  10829. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10830. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10831. #endif /* WOLFSSL_SHA512 */
  10832. /*
  10833. * Unit test function for wc_Sha512GetHash()
  10834. */
  10835. static int test_wc_Sha512GetHash(void)
  10836. {
  10837. int res = TEST_SKIPPED;
  10838. #ifdef WOLFSSL_SHA512
  10839. wc_Sha512 sha512;
  10840. byte hash1[WC_SHA512_DIGEST_SIZE];
  10841. int flag = 0;
  10842. /* Initialize */
  10843. flag = wc_InitSha512(&sha512);
  10844. if (flag == 0) {
  10845. flag = wc_Sha512GetHash(&sha512, hash1);
  10846. }
  10847. /*test bad arguments*/
  10848. if (flag == 0) {
  10849. flag = wc_Sha512GetHash(NULL, NULL);
  10850. if (flag == BAD_FUNC_ARG) {
  10851. flag = 0;
  10852. }
  10853. }
  10854. if (flag == 0) {
  10855. flag = wc_Sha512GetHash(NULL, hash1);
  10856. if (flag == BAD_FUNC_ARG) {
  10857. flag = 0;
  10858. }
  10859. }
  10860. if (flag == 0) {
  10861. flag = wc_Sha512GetHash(&sha512, NULL);
  10862. if (flag == BAD_FUNC_ARG) {
  10863. flag = 0;
  10864. }
  10865. }
  10866. wc_Sha512Free(&sha512);
  10867. res = TEST_RES_CHECK(flag == 0);
  10868. #endif
  10869. return res;
  10870. } /* END test_wc_Sha512GetHash */
  10871. /*
  10872. * Unit test function for wc_Sha512Copy()
  10873. */
  10874. static int test_wc_Sha512Copy(void)
  10875. {
  10876. int res = TEST_SKIPPED;
  10877. #ifdef WOLFSSL_SHA512
  10878. wc_Sha512 sha512;
  10879. wc_Sha512 temp;
  10880. int flag;
  10881. /* Initialize */
  10882. flag = wc_InitSha512(&sha512);
  10883. if (flag == 0) {
  10884. flag = wc_InitSha512(&temp);
  10885. }
  10886. if (flag == 0) {
  10887. flag = wc_Sha512Copy(&sha512, &temp);
  10888. }
  10889. /*test bad arguments*/
  10890. if (flag == 0) {
  10891. flag = wc_Sha512Copy(NULL, NULL);
  10892. if (flag == BAD_FUNC_ARG) {
  10893. flag = 0;
  10894. }
  10895. }
  10896. if (flag == 0) {
  10897. flag = wc_Sha512Copy(NULL, &temp);
  10898. if (flag == BAD_FUNC_ARG) {
  10899. flag = 0;
  10900. }
  10901. }
  10902. if (flag == 0) {
  10903. flag = wc_Sha512Copy(&sha512, NULL);
  10904. if (flag == BAD_FUNC_ARG) {
  10905. flag = 0;
  10906. }
  10907. }
  10908. wc_Sha512Free(&sha512);
  10909. wc_Sha512Free(&temp);
  10910. res = TEST_RES_CHECK(flag == 0);
  10911. #endif
  10912. return res;
  10913. } /* END test_wc_Sha512Copy */
  10914. static int test_wc_InitSha512_224(void)
  10915. {
  10916. int res = TEST_SKIPPED;
  10917. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10918. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10919. wc_Sha512 sha512;
  10920. int ret;
  10921. int flag = 0;
  10922. /* Test good arg. */
  10923. ret = wc_InitSha512_224(&sha512);
  10924. if (ret != 0) {
  10925. flag = WOLFSSL_FATAL_ERROR;
  10926. }
  10927. /* Test bad arg. */
  10928. if (!flag) {
  10929. ret = wc_InitSha512_224(NULL);
  10930. if (ret != BAD_FUNC_ARG) {
  10931. flag = WOLFSSL_FATAL_ERROR;
  10932. }
  10933. }
  10934. wc_Sha512_224Free(&sha512);
  10935. res = TEST_RES_CHECK(flag == 0);
  10936. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10937. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10938. return res;
  10939. }
  10940. static int test_wc_Sha512_224Update(void)
  10941. {
  10942. int res = TEST_SKIPPED;
  10943. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10944. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10945. wc_Sha512 sha512;
  10946. byte hash[WC_SHA512_DIGEST_SIZE];
  10947. testVector a, c;
  10948. int ret;
  10949. int flag = 0;
  10950. ret = wc_InitSha512_224(&sha512);
  10951. if (ret != 0) {
  10952. flag = ret;
  10953. }
  10954. /* Input. */
  10955. if (!flag) {
  10956. a.input = "a";
  10957. a.inLen = XSTRLEN(a.input);
  10958. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10959. if (ret != 0) {
  10960. flag = ret;
  10961. }
  10962. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10963. if (ret != 0) {
  10964. flag = ret;
  10965. }
  10966. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10967. if (ret != 0) {
  10968. flag = ret;
  10969. }
  10970. ret = wc_Sha512_224Final(&sha512, hash);
  10971. if (ret != 0) {
  10972. flag = ret;
  10973. }
  10974. }
  10975. /* Update input. */
  10976. if (!flag) {
  10977. a.input = "abc";
  10978. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10979. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10980. a.inLen = XSTRLEN(a.input);
  10981. a.outLen = XSTRLEN(a.output);
  10982. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10983. if (ret != 0) {
  10984. flag = ret;
  10985. }
  10986. }
  10987. if (!flag) {
  10988. ret = wc_Sha512_224Final(&sha512, hash);
  10989. if (ret != 0) {
  10990. flag = ret;
  10991. }
  10992. }
  10993. if (!flag) {
  10994. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10995. flag = WOLFSSL_FATAL_ERROR;
  10996. }
  10997. }
  10998. if (!flag) {
  10999. c.input = NULL;
  11000. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11001. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11002. if (ret != BAD_FUNC_ARG) {
  11003. flag = WOLFSSL_FATAL_ERROR;
  11004. }
  11005. }
  11006. if (!flag) {
  11007. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11008. if (ret != BAD_FUNC_ARG) {
  11009. flag = WOLFSSL_FATAL_ERROR;
  11010. }
  11011. }
  11012. wc_Sha512_224Free(&sha512);
  11013. res = TEST_RES_CHECK(flag == 0);
  11014. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11015. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11016. return res;
  11017. }
  11018. static int test_wc_Sha512_224Final(void)
  11019. {
  11020. int res = TEST_SKIPPED;
  11021. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11022. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11023. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  11024. res = TEST_RES_CHECK(ret == 0);
  11025. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11026. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11027. return res;
  11028. }
  11029. static int test_wc_Sha512_224GetFlags(void)
  11030. {
  11031. int res = TEST_SKIPPED;
  11032. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11033. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11034. wc_Sha512 sha512, copy;
  11035. word32 flags = 0;
  11036. int flag = 0;
  11037. /* Initialize */
  11038. flag = wc_InitSha512_224(&sha512);
  11039. if (!flag) {
  11040. flag = wc_InitSha512_224(&copy);
  11041. }
  11042. if (!flag) {
  11043. flag = wc_Sha512_224Copy(&sha512, &copy);
  11044. }
  11045. if (!flag) {
  11046. flag = wc_Sha512_224GetFlags(&copy, &flags);
  11047. }
  11048. if (!flag) {
  11049. if (flags & WC_HASH_FLAG_ISCOPY)
  11050. flag = 0;
  11051. else
  11052. flag = WOLFSSL_FATAL_ERROR;
  11053. }
  11054. wc_Sha512_224Free(&copy);
  11055. wc_Sha512_224Free(&sha512);
  11056. res = TEST_RES_CHECK(flag == 0);
  11057. #endif
  11058. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11059. return res;
  11060. }
  11061. static int test_wc_Sha512_224FinalRaw(void)
  11062. {
  11063. int res = TEST_SKIPPED;
  11064. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11065. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11066. !defined(WOLFSSL_NO_HASH_RAW)
  11067. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  11068. res = TEST_RES_CHECK(ret == 0);
  11069. #endif
  11070. return res;
  11071. }
  11072. static int test_wc_Sha512_224Free(void)
  11073. {
  11074. int res = TEST_SKIPPED;
  11075. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11076. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11077. wc_Sha512_224Free(NULL);
  11078. res = TEST_RES_CHECK(1);
  11079. #endif
  11080. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11081. return res;
  11082. }
  11083. static int test_wc_Sha512_224GetHash(void)
  11084. {
  11085. int res = TEST_SKIPPED;
  11086. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11087. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11088. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  11089. res = TEST_RES_CHECK(ret == 0);
  11090. #endif
  11091. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11092. return res;
  11093. }
  11094. static int test_wc_Sha512_224Copy(void)
  11095. {
  11096. int res = TEST_SKIPPED;
  11097. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11098. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11099. wc_Sha512 sha512;
  11100. wc_Sha512 temp;
  11101. int flag = 0;
  11102. /* Initialize */
  11103. flag = wc_InitSha512_224(&sha512);
  11104. if (flag == 0) {
  11105. flag = wc_InitSha512_224(&temp);
  11106. }
  11107. if (flag == 0) {
  11108. flag = wc_Sha512_224Copy(&sha512, &temp);
  11109. }
  11110. /*test bad arguments*/
  11111. if (flag == 0) {
  11112. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  11113. flag = WOLFSSL_FATAL_ERROR;
  11114. }
  11115. if (flag == 0) {
  11116. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  11117. flag = WOLFSSL_FATAL_ERROR;
  11118. }
  11119. if (flag == 0) {
  11120. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11121. flag = WOLFSSL_FATAL_ERROR;
  11122. }
  11123. wc_Sha512_224Free(&sha512);
  11124. wc_Sha512_224Free(&temp);
  11125. res = TEST_RES_CHECK(flag == 0);
  11126. #endif
  11127. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11128. return res;
  11129. }
  11130. static int test_wc_InitSha512_256(void)
  11131. {
  11132. int res = TEST_SKIPPED;
  11133. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11134. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11135. wc_Sha512 sha512;
  11136. int ret;
  11137. int flag = 0;
  11138. /* Test good arg. */
  11139. ret = wc_InitSha512_256(&sha512);
  11140. if (ret != 0) {
  11141. flag = WOLFSSL_FATAL_ERROR;
  11142. }
  11143. /* Test bad arg. */
  11144. if (!flag) {
  11145. ret = wc_InitSha512_256(NULL);
  11146. if (ret != BAD_FUNC_ARG) {
  11147. flag = WOLFSSL_FATAL_ERROR;
  11148. }
  11149. }
  11150. wc_Sha512_256Free(&sha512);
  11151. res = TEST_RES_CHECK(flag == 0);
  11152. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11153. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11154. return res;
  11155. }
  11156. static int test_wc_Sha512_256Update(void)
  11157. {
  11158. int res = TEST_SKIPPED;
  11159. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11160. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11161. wc_Sha512 sha512;
  11162. byte hash[WC_SHA512_DIGEST_SIZE];
  11163. testVector a, c;
  11164. int ret;
  11165. int flag = 0;
  11166. ret = wc_InitSha512_256(&sha512);
  11167. if (ret != 0) {
  11168. flag = ret;
  11169. }
  11170. /* Input. */
  11171. if (!flag) {
  11172. a.input = "a";
  11173. a.inLen = XSTRLEN(a.input);
  11174. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  11175. if (ret != 0) {
  11176. flag = ret;
  11177. }
  11178. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  11179. if (ret != 0) {
  11180. flag = ret;
  11181. }
  11182. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  11183. if (ret != 0) {
  11184. flag = ret;
  11185. }
  11186. ret = wc_Sha512_256Final(&sha512, hash);
  11187. if (ret != 0) {
  11188. flag = ret;
  11189. }
  11190. }
  11191. /* Update input. */
  11192. if (!flag) {
  11193. a.input = "abc";
  11194. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11195. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11196. "\x07\xe7\xaf\x23";
  11197. a.inLen = XSTRLEN(a.input);
  11198. a.outLen = XSTRLEN(a.output);
  11199. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  11200. if (ret != 0) {
  11201. flag = ret;
  11202. }
  11203. }
  11204. if (!flag) {
  11205. ret = wc_Sha512_256Final(&sha512, hash);
  11206. if (ret != 0) {
  11207. flag = ret;
  11208. }
  11209. }
  11210. if (!flag) {
  11211. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  11212. flag = WOLFSSL_FATAL_ERROR;
  11213. }
  11214. }
  11215. if (!flag) {
  11216. c.input = NULL;
  11217. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11218. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  11219. if (ret != BAD_FUNC_ARG) {
  11220. flag = WOLFSSL_FATAL_ERROR;
  11221. }
  11222. }
  11223. if (!flag) {
  11224. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  11225. if (ret != BAD_FUNC_ARG) {
  11226. flag = WOLFSSL_FATAL_ERROR;
  11227. }
  11228. }
  11229. wc_Sha512_256Free(&sha512);
  11230. res = TEST_RES_CHECK(flag == 0);
  11231. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11232. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11233. return res;
  11234. }
  11235. static int test_wc_Sha512_256Final(void)
  11236. {
  11237. int res = TEST_SKIPPED;
  11238. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11239. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11240. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  11241. res = TEST_RES_CHECK(ret == 0);
  11242. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11243. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11244. return res;
  11245. }
  11246. static int test_wc_Sha512_256GetFlags(void)
  11247. {
  11248. int res = TEST_SKIPPED;
  11249. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11250. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11251. wc_Sha512 sha512, copy;
  11252. word32 flags = 0;
  11253. int flag = 0;
  11254. /* Initialize */
  11255. flag = wc_InitSha512_256(&sha512);
  11256. if (!flag ) {
  11257. flag = wc_InitSha512_256(&copy);
  11258. }
  11259. if (!flag ) {
  11260. flag = wc_Sha512_256Copy(&sha512, &copy);
  11261. }
  11262. if (!flag ) {
  11263. flag = wc_Sha512_256GetFlags(&copy, &flags);
  11264. }
  11265. if (!flag) {
  11266. if (flags & WC_HASH_FLAG_ISCOPY)
  11267. flag = 0;
  11268. else
  11269. flag = WOLFSSL_FATAL_ERROR;
  11270. }
  11271. wc_Sha512_256Free(&sha512);
  11272. res = TEST_RES_CHECK(flag == 0);
  11273. #endif
  11274. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11275. return res;
  11276. }
  11277. static int test_wc_Sha512_256FinalRaw(void)
  11278. {
  11279. int res = TEST_SKIPPED;
  11280. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11281. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11282. !defined(WOLFSSL_NO_HASH_RAW)
  11283. int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  11284. res = TEST_RES_CHECK(ret == 0);
  11285. #endif
  11286. return res;
  11287. }
  11288. static int test_wc_Sha512_256Free(void)
  11289. {
  11290. int res = TEST_SKIPPED;
  11291. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11292. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11293. wc_Sha512_256Free(NULL);
  11294. res = TEST_RES_CHECK(1);
  11295. #endif
  11296. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11297. return res;
  11298. }
  11299. static int test_wc_Sha512_256GetHash(void)
  11300. {
  11301. int res = TEST_SKIPPED;
  11302. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11303. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11304. int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  11305. res = TEST_RES_CHECK(ret == 0);
  11306. #endif
  11307. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11308. return res;
  11309. }
  11310. static int test_wc_Sha512_256Copy(void)
  11311. {
  11312. int res = TEST_SKIPPED;
  11313. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11314. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11315. wc_Sha512 sha512;
  11316. wc_Sha512 temp;
  11317. int flag = 0;
  11318. /* Initialize */
  11319. flag = wc_InitSha512_256(&sha512);
  11320. if (flag == 0) {
  11321. flag = wc_InitSha512_256(&temp);
  11322. }
  11323. if (flag == 0) {
  11324. flag = wc_Sha512_256Copy(&sha512, &temp);
  11325. }
  11326. /*test bad arguments*/
  11327. if (flag == 0) {
  11328. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  11329. flag = WOLFSSL_FATAL_ERROR;
  11330. }
  11331. if (flag == 0) {
  11332. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  11333. flag = WOLFSSL_FATAL_ERROR;
  11334. }
  11335. if (flag == 0) {
  11336. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  11337. flag = WOLFSSL_FATAL_ERROR;
  11338. }
  11339. wc_Sha512_256Free(&sha512);
  11340. wc_Sha512_256Free(&temp);
  11341. res = TEST_RES_CHECK(flag == 0);
  11342. #endif
  11343. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11344. return res;
  11345. }
  11346. /*
  11347. * Testing wc_InitSha384()
  11348. */
  11349. static int test_wc_InitSha384(void)
  11350. {
  11351. int res = TEST_SKIPPED;
  11352. #ifdef WOLFSSL_SHA384
  11353. wc_Sha384 sha384;
  11354. int ret;
  11355. int flag = 0;
  11356. /* Test good arg. */
  11357. ret = wc_InitSha384(&sha384);
  11358. if (ret != 0) {
  11359. flag = WOLFSSL_FATAL_ERROR;
  11360. }
  11361. /* Test bad arg. */
  11362. if (!flag) {
  11363. ret = wc_InitSha384(NULL);
  11364. if (ret != BAD_FUNC_ARG) {
  11365. flag = WOLFSSL_FATAL_ERROR;
  11366. }
  11367. }
  11368. wc_Sha384Free(&sha384);
  11369. res = TEST_RES_CHECK(flag == 0);
  11370. #endif
  11371. return res;
  11372. } /* END test_wc_InitSha384 */
  11373. /*
  11374. * test wc_Sha384Update()
  11375. */
  11376. static int test_wc_Sha384Update(void)
  11377. {
  11378. int res = TEST_SKIPPED;
  11379. #ifdef WOLFSSL_SHA384
  11380. wc_Sha384 sha384;
  11381. byte hash[WC_SHA384_DIGEST_SIZE];
  11382. testVector a, b, c;
  11383. int ret;
  11384. int flag = 0;
  11385. ret = wc_InitSha384(&sha384);
  11386. if (ret != 0) {
  11387. flag = ret;
  11388. }
  11389. /* Input */
  11390. if (!flag) {
  11391. a.input = "a";
  11392. a.inLen = XSTRLEN(a.input);
  11393. ret = wc_Sha384Update(&sha384, NULL, 0);
  11394. if (ret != 0) {
  11395. flag = ret;
  11396. }
  11397. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  11398. if (ret != 0) {
  11399. flag = ret;
  11400. }
  11401. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11402. if (ret != 0) {
  11403. flag = ret;
  11404. }
  11405. }
  11406. if (!flag) {
  11407. ret = wc_Sha384Final(&sha384, hash);
  11408. if (ret != 0) {
  11409. flag = ret;
  11410. }
  11411. }
  11412. /* Update input. */
  11413. if (!flag) {
  11414. a.input = "abc";
  11415. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11416. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11417. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11418. "\xc8\x25\xa7";
  11419. a.inLen = XSTRLEN(a.input);
  11420. a.outLen = XSTRLEN(a.output);
  11421. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  11422. if (ret != 0) {
  11423. flag = ret;
  11424. }
  11425. }
  11426. if (!flag) {
  11427. ret = wc_Sha384Final(&sha384, hash);
  11428. if (ret != 0) {
  11429. flag = ret;
  11430. }
  11431. }
  11432. if (!flag) {
  11433. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  11434. flag = WOLFSSL_FATAL_ERROR;
  11435. }
  11436. }
  11437. /* Pass in bad values. */
  11438. if (!flag) {
  11439. b.input = NULL;
  11440. b.inLen = 0;
  11441. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  11442. if (ret != 0) {
  11443. flag = ret;
  11444. }
  11445. }
  11446. if (!flag) {
  11447. c.input = NULL;
  11448. c.inLen = WC_SHA384_DIGEST_SIZE;
  11449. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  11450. if (ret != BAD_FUNC_ARG) {
  11451. flag = WOLFSSL_FATAL_ERROR;
  11452. }
  11453. }
  11454. if (!flag) {
  11455. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  11456. if (ret != BAD_FUNC_ARG) {
  11457. flag = WOLFSSL_FATAL_ERROR;
  11458. }
  11459. }
  11460. wc_Sha384Free(&sha384);
  11461. res = TEST_RES_CHECK(flag == 0);
  11462. #endif
  11463. return res;
  11464. } /* END test_wc_Sha384Update */
  11465. /*
  11466. * Unit test function for wc_Sha384Final();
  11467. */
  11468. static int test_wc_Sha384Final(void)
  11469. {
  11470. int res = TEST_SKIPPED;
  11471. #ifdef WOLFSSL_SHA384
  11472. wc_Sha384 sha384;
  11473. byte* hash_test[3];
  11474. byte hash1[WC_SHA384_DIGEST_SIZE];
  11475. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11476. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11477. int times, i, ret;
  11478. int flag = 0;
  11479. /* Initialize */
  11480. ret = wc_InitSha384(&sha384);
  11481. if (ret) {
  11482. flag = ret;
  11483. }
  11484. if (!flag) {
  11485. hash_test[0] = hash1;
  11486. hash_test[1] = hash2;
  11487. hash_test[2] = hash3;
  11488. }
  11489. times = sizeof(hash_test) / sizeof(byte*);
  11490. /* Good test args. */
  11491. for (i = 0; i < times; i++) {
  11492. if (!flag) {
  11493. ret = wc_Sha384Final(&sha384, hash_test[i]);
  11494. if (ret != 0) {
  11495. flag = WOLFSSL_FATAL_ERROR;
  11496. }
  11497. }
  11498. }
  11499. /* Test bad args. */
  11500. if (!flag) {
  11501. ret = wc_Sha384Final(NULL, NULL);
  11502. if (ret != BAD_FUNC_ARG) {
  11503. flag = WOLFSSL_FATAL_ERROR;
  11504. }
  11505. }
  11506. if (!flag) {
  11507. ret = wc_Sha384Final(NULL, hash1);
  11508. if (ret != BAD_FUNC_ARG) {
  11509. flag = WOLFSSL_FATAL_ERROR;
  11510. }
  11511. }
  11512. if (!flag) {
  11513. ret = wc_Sha384Final(&sha384, NULL);
  11514. if (ret != BAD_FUNC_ARG) {
  11515. flag = WOLFSSL_FATAL_ERROR;
  11516. }
  11517. }
  11518. wc_Sha384Free(&sha384);
  11519. res = TEST_RES_CHECK(flag == 0);
  11520. #endif
  11521. return res;
  11522. } /* END test_wc_Sha384Final */
  11523. /*
  11524. * Unit test function for wc_Sha384GetFlags()
  11525. */
  11526. static int test_wc_Sha384GetFlags(void)
  11527. {
  11528. int res = TEST_SKIPPED;
  11529. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11530. wc_Sha384 sha384;
  11531. word32 flags = 0;
  11532. int flag = 0;
  11533. /* Initialize */
  11534. flag = wc_InitSha384(&sha384);
  11535. if (flag == 0) {
  11536. flag = wc_Sha384GetFlags(&sha384, &flags);
  11537. }
  11538. if (flag == 0) {
  11539. if (flags & WC_HASH_FLAG_ISCOPY) {
  11540. flag = 0;
  11541. }
  11542. }
  11543. wc_Sha384Free(&sha384);
  11544. res = TEST_RES_CHECK(flag == 0);
  11545. #endif
  11546. return res;
  11547. } /* END test_wc_Sha384GetFlags */
  11548. /*
  11549. * Unit test function for wc_Sha384FinalRaw()
  11550. */
  11551. static int test_wc_Sha384FinalRaw(void)
  11552. {
  11553. int res = TEST_SKIPPED;
  11554. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11555. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11556. !defined(WOLFSSL_NO_HASH_RAW)
  11557. wc_Sha384 sha384;
  11558. byte* hash_test[3];
  11559. byte hash1[WC_SHA384_DIGEST_SIZE];
  11560. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11561. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11562. int times, i, ret;
  11563. int flag = 0;
  11564. /* Initialize */
  11565. ret = wc_InitSha384(&sha384);
  11566. if (ret != 0) {
  11567. flag = ret;
  11568. }
  11569. if (!flag) {
  11570. hash_test[0] = hash1;
  11571. hash_test[1] = hash2;
  11572. hash_test[2] = hash3;
  11573. }
  11574. times = sizeof(hash_test) / sizeof(byte*);
  11575. /* Good test args. */
  11576. for (i = 0; i < times; i++) {
  11577. if (!flag) {
  11578. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  11579. if (ret != 0) {
  11580. flag = WOLFSSL_FATAL_ERROR;
  11581. }
  11582. }
  11583. }
  11584. /* Test bad args. */
  11585. if (!flag ) {
  11586. ret = wc_Sha384FinalRaw(NULL, NULL);
  11587. if (ret != BAD_FUNC_ARG) {
  11588. flag = WOLFSSL_FATAL_ERROR;
  11589. }
  11590. }
  11591. if (!flag) {
  11592. ret = wc_Sha384FinalRaw(NULL, hash1);
  11593. if (ret != BAD_FUNC_ARG) {
  11594. flag = WOLFSSL_FATAL_ERROR;
  11595. }
  11596. }
  11597. if (!flag) {
  11598. ret = wc_Sha384FinalRaw(&sha384, NULL);
  11599. if (ret != BAD_FUNC_ARG) {
  11600. flag = WOLFSSL_FATAL_ERROR;
  11601. }
  11602. }
  11603. wc_Sha384Free(&sha384);
  11604. res = TEST_RES_CHECK(flag == 0);
  11605. #endif
  11606. return res;
  11607. } /* END test_wc_Sha384FinalRaw */
  11608. /*
  11609. * Unit test function for wc_Sha384Free()
  11610. */
  11611. static int test_wc_Sha384Free(void)
  11612. {
  11613. int res = TEST_SKIPPED;
  11614. #ifdef WOLFSSL_SHA384
  11615. wc_Sha384Free(NULL);
  11616. res = TEST_RES_CHECK(1);
  11617. #endif
  11618. return res;
  11619. } /* END test_wc_Sha384Free */
  11620. /*
  11621. * Unit test function for wc_Sha384GetHash()
  11622. */
  11623. static int test_wc_Sha384GetHash(void)
  11624. {
  11625. int res = TEST_SKIPPED;
  11626. #ifdef WOLFSSL_SHA384
  11627. wc_Sha384 sha384;
  11628. byte hash1[WC_SHA384_DIGEST_SIZE];
  11629. int flag = 0;
  11630. /* Initialize */
  11631. flag = wc_InitSha384(&sha384);
  11632. if (flag == 0) {
  11633. flag = wc_Sha384GetHash(&sha384, hash1);
  11634. }
  11635. /*test bad arguments*/
  11636. if (flag == 0) {
  11637. flag = wc_Sha384GetHash(NULL, NULL);
  11638. if (flag == BAD_FUNC_ARG) {
  11639. flag = 0;
  11640. }
  11641. }
  11642. if (flag == 0) {
  11643. flag = wc_Sha384GetHash(NULL, hash1);
  11644. if (flag == BAD_FUNC_ARG) {
  11645. flag = 0;
  11646. }
  11647. }
  11648. if (flag == 0) {
  11649. flag = wc_Sha384GetHash(&sha384, NULL);
  11650. if (flag == BAD_FUNC_ARG) {
  11651. flag = 0;
  11652. }
  11653. }
  11654. wc_Sha384Free(&sha384);
  11655. res = TEST_RES_CHECK(flag == 0);
  11656. #endif
  11657. return res;
  11658. } /* END test_wc_Sha384GetHash */
  11659. /*
  11660. * Unit test function for wc_Sha384Copy()
  11661. */
  11662. static int test_wc_Sha384Copy(void)
  11663. {
  11664. int res = TEST_SKIPPED;
  11665. #ifdef WOLFSSL_SHA384
  11666. wc_Sha384 sha384;
  11667. wc_Sha384 temp;
  11668. int flag = 0;
  11669. /* Initialize */
  11670. flag = wc_InitSha384(&sha384);
  11671. if (flag == 0) {
  11672. flag = wc_InitSha384(&temp);
  11673. }
  11674. if (flag == 0) {
  11675. flag = wc_Sha384Copy(&sha384, &temp);
  11676. }
  11677. /*test bad arguments*/
  11678. if (flag == 0) {
  11679. flag = wc_Sha384Copy(NULL, NULL);
  11680. if (flag == BAD_FUNC_ARG) {
  11681. flag = 0;
  11682. }
  11683. }
  11684. if (flag == 0) {
  11685. flag = wc_Sha384Copy(NULL, &temp);
  11686. if (flag == BAD_FUNC_ARG) {
  11687. flag = 0;
  11688. }
  11689. }
  11690. if (flag == 0) {
  11691. flag = wc_Sha384Copy(&sha384, NULL);
  11692. if (flag == BAD_FUNC_ARG) {
  11693. flag = 0;
  11694. }
  11695. }
  11696. wc_Sha384Free(&sha384);
  11697. wc_Sha384Free(&temp);
  11698. res = TEST_RES_CHECK(flag == 0);
  11699. #endif
  11700. return res;
  11701. } /* END test_wc_Sha384Copy */
  11702. /*
  11703. * Testing wc_InitSha224();
  11704. */
  11705. static int test_wc_InitSha224(void)
  11706. {
  11707. int res = TEST_SKIPPED;
  11708. #ifdef WOLFSSL_SHA224
  11709. wc_Sha224 sha224;
  11710. int ret;
  11711. int flag = 0;
  11712. /* Test good arg. */
  11713. ret = wc_InitSha224(&sha224);
  11714. if (ret != 0) {
  11715. flag = WOLFSSL_FATAL_ERROR;
  11716. }
  11717. /* Test bad arg. */
  11718. if (!flag) {
  11719. ret = wc_InitSha224(NULL);
  11720. if (ret != BAD_FUNC_ARG) {
  11721. flag = WOLFSSL_FATAL_ERROR;
  11722. }
  11723. }
  11724. wc_Sha224Free(&sha224);
  11725. res = TEST_RES_CHECK(flag == 0);
  11726. #endif
  11727. return res;
  11728. } /* END test_wc_InitSha224 */
  11729. /*
  11730. * Unit test on wc_Sha224Update
  11731. */
  11732. static int test_wc_Sha224Update(void)
  11733. {
  11734. int res = TEST_SKIPPED;
  11735. #ifdef WOLFSSL_SHA224
  11736. wc_Sha224 sha224;
  11737. byte hash[WC_SHA224_DIGEST_SIZE];
  11738. testVector a, b, c;
  11739. int ret;
  11740. int flag = 0;
  11741. ret = wc_InitSha224(&sha224);
  11742. if (ret != 0) {
  11743. flag = ret;
  11744. }
  11745. /* Input. */
  11746. if (!flag) {
  11747. a.input = "a";
  11748. a.inLen = XSTRLEN(a.input);
  11749. ret = wc_Sha224Update(&sha224, NULL, 0);
  11750. if (ret != 0) {
  11751. flag = ret;
  11752. }
  11753. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  11754. if (ret != 0) {
  11755. flag = ret;
  11756. }
  11757. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11758. if (ret != 0) {
  11759. flag = ret;
  11760. }
  11761. }
  11762. if (!flag) {
  11763. ret = wc_Sha224Final(&sha224, hash);
  11764. if (ret != 0) {
  11765. flag = ret;
  11766. }
  11767. }
  11768. /* Update input. */
  11769. if (!flag) {
  11770. a.input = "abc";
  11771. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11772. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11773. a.inLen = XSTRLEN(a.input);
  11774. a.outLen = XSTRLEN(a.output);
  11775. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  11776. if (ret != 0) {
  11777. flag = ret;
  11778. }
  11779. }
  11780. if (!flag) {
  11781. ret = wc_Sha224Final(&sha224, hash);
  11782. if (ret != 0) {
  11783. flag = ret;
  11784. }
  11785. }
  11786. if (!flag) {
  11787. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11788. flag = WOLFSSL_FATAL_ERROR;
  11789. }
  11790. }
  11791. /* Pass in bad values. */
  11792. if (!flag) {
  11793. b.input = NULL;
  11794. b.inLen = 0;
  11795. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11796. if (ret != 0) {
  11797. flag = ret;
  11798. }
  11799. }
  11800. if (!flag) {
  11801. c.input = NULL;
  11802. c.inLen = WC_SHA224_DIGEST_SIZE;
  11803. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  11804. if (ret != BAD_FUNC_ARG) {
  11805. flag = WOLFSSL_FATAL_ERROR;
  11806. }
  11807. }
  11808. if (!flag) {
  11809. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11810. if (ret != BAD_FUNC_ARG) {
  11811. flag = WOLFSSL_FATAL_ERROR;
  11812. }
  11813. }
  11814. wc_Sha224Free(&sha224);
  11815. res = TEST_RES_CHECK(flag == 0);
  11816. #endif
  11817. return res;
  11818. } /* END test_wc_Sha224Update */
  11819. /*
  11820. * Unit test for wc_Sha224Final();
  11821. */
  11822. static int test_wc_Sha224Final(void)
  11823. {
  11824. int res = TEST_SKIPPED;
  11825. #ifdef WOLFSSL_SHA224
  11826. wc_Sha224 sha224;
  11827. byte* hash_test[3];
  11828. byte hash1[WC_SHA224_DIGEST_SIZE];
  11829. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11830. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11831. int times, i, ret;
  11832. int flag = 0;
  11833. /* Initialize */
  11834. ret = wc_InitSha224(&sha224);
  11835. if (ret) {
  11836. flag = ret;
  11837. }
  11838. if (!flag) {
  11839. hash_test[0] = hash1;
  11840. hash_test[1] = hash2;
  11841. hash_test[2] = hash3;
  11842. }
  11843. times = sizeof(hash_test) / sizeof(byte*);
  11844. /* Good test args. */
  11845. /* Testing oversized buffers. */
  11846. for (i = 0; i < times; i++) {
  11847. if (!flag) {
  11848. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11849. if (ret != 0) {
  11850. flag = WOLFSSL_FATAL_ERROR;
  11851. }
  11852. }
  11853. }
  11854. /* Test bad args. */
  11855. if (!flag) {
  11856. ret = wc_Sha224Final(NULL, NULL);
  11857. if (ret != BAD_FUNC_ARG) {
  11858. flag = WOLFSSL_FATAL_ERROR;
  11859. }
  11860. }
  11861. if (!flag) {
  11862. ret = wc_Sha224Final(NULL, hash1);
  11863. if (ret != BAD_FUNC_ARG) {
  11864. flag = WOLFSSL_FATAL_ERROR;
  11865. }
  11866. }
  11867. if (!flag) {
  11868. ret = wc_Sha224Final(&sha224, NULL);
  11869. if (ret != BAD_FUNC_ARG) {
  11870. flag = WOLFSSL_FATAL_ERROR;
  11871. }
  11872. }
  11873. wc_Sha224Free(&sha224);
  11874. res = TEST_RES_CHECK(flag == 0);
  11875. #endif
  11876. return res;
  11877. } /* END test_wc_Sha224Final */
  11878. /*
  11879. * Unit test function for wc_Sha224SetFlags()
  11880. */
  11881. static int test_wc_Sha224SetFlags(void)
  11882. {
  11883. int res = TEST_SKIPPED;
  11884. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11885. wc_Sha224 sha224;
  11886. word32 flags = 0;
  11887. int flag = 0;
  11888. /* Initialize */
  11889. flag = wc_InitSha224(&sha224);
  11890. if (flag == 0) {
  11891. flag = wc_Sha224SetFlags(&sha224, flags);
  11892. }
  11893. if (flag == 0) {
  11894. if (flags & WC_HASH_FLAG_ISCOPY) {
  11895. flag = 0;
  11896. }
  11897. }
  11898. wc_Sha224Free(&sha224);
  11899. res = TEST_RES_CHECK(flag == 0);
  11900. #endif
  11901. return res;
  11902. } /* END test_wc_Sha224SetFlags */
  11903. /*
  11904. * Unit test function for wc_Sha224GetFlags()
  11905. */
  11906. static int test_wc_Sha224GetFlags(void)
  11907. {
  11908. int res = TEST_SKIPPED;
  11909. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11910. wc_Sha224 sha224;
  11911. word32 flags = 0;
  11912. int flag = 0;
  11913. /* Initialize */
  11914. flag = wc_InitSha224(&sha224);
  11915. if (flag == 0) {
  11916. flag = wc_Sha224GetFlags(&sha224, &flags);
  11917. }
  11918. if (flag == 0) {
  11919. if (flags & WC_HASH_FLAG_ISCOPY) {
  11920. flag = 0;
  11921. }
  11922. }
  11923. wc_Sha224Free(&sha224);
  11924. res = TEST_RES_CHECK(flag == 0);
  11925. #endif
  11926. return res;
  11927. } /* END test_wc_Sha224GetFlags */
  11928. /*
  11929. * Unit test function for wc_Sha224Free()
  11930. */
  11931. static int test_wc_Sha224Free(void)
  11932. {
  11933. int res = TEST_SKIPPED;
  11934. #ifdef WOLFSSL_SHA224
  11935. wc_Sha224Free(NULL);
  11936. res = TEST_RES_CHECK(1);
  11937. #endif
  11938. return res;
  11939. } /* END test_wc_Sha224Free */
  11940. /*
  11941. * Unit test function for wc_Sha224GetHash()
  11942. */
  11943. static int test_wc_Sha224GetHash(void)
  11944. {
  11945. int res = TEST_SKIPPED;
  11946. #ifdef WOLFSSL_SHA224
  11947. wc_Sha224 sha224;
  11948. byte hash1[WC_SHA224_DIGEST_SIZE];
  11949. int flag = 0;
  11950. /* Initialize */
  11951. flag = wc_InitSha224(&sha224);
  11952. if (flag == 0) {
  11953. flag = wc_Sha224GetHash(&sha224, hash1);
  11954. }
  11955. /*test bad arguments*/
  11956. if (flag == 0) {
  11957. flag = wc_Sha224GetHash(NULL, NULL);
  11958. if (flag == BAD_FUNC_ARG) {
  11959. flag = 0;
  11960. }
  11961. }
  11962. if (flag == 0) {
  11963. flag = wc_Sha224GetHash(NULL, hash1);
  11964. if (flag == BAD_FUNC_ARG) {
  11965. flag = 0;
  11966. }
  11967. }
  11968. if (flag == 0) {
  11969. flag = wc_Sha224GetHash(&sha224, NULL);
  11970. if (flag == BAD_FUNC_ARG) {
  11971. flag = 0;
  11972. }
  11973. }
  11974. wc_Sha224Free(&sha224);
  11975. res = TEST_RES_CHECK(flag == 0);
  11976. #endif
  11977. return res;
  11978. } /* END test_wc_Sha224GetHash */
  11979. /*
  11980. * Unit test function for wc_Sha224Copy()
  11981. */
  11982. static int test_wc_Sha224Copy(void)
  11983. {
  11984. int res = TEST_SKIPPED;
  11985. #ifdef WOLFSSL_SHA224
  11986. wc_Sha224 sha224;
  11987. wc_Sha224 temp;
  11988. int flag = 0;
  11989. /* Initialize */
  11990. flag = wc_InitSha224(&sha224);
  11991. if (flag == 0) {
  11992. flag = wc_InitSha224(&temp);
  11993. }
  11994. if (flag == 0) {
  11995. flag = wc_Sha224Copy(&sha224, &temp);
  11996. }
  11997. /*test bad arguments*/
  11998. if (flag == 0) {
  11999. flag = wc_Sha224Copy(NULL, NULL);
  12000. if (flag == BAD_FUNC_ARG) {
  12001. flag = 0;
  12002. }
  12003. }
  12004. if (flag == 0) {
  12005. flag = wc_Sha224Copy(NULL, &temp);
  12006. if (flag == BAD_FUNC_ARG) {
  12007. flag = 0;
  12008. }
  12009. }
  12010. if (flag == 0) {
  12011. flag = wc_Sha224Copy(&sha224, NULL);
  12012. if (flag == BAD_FUNC_ARG) {
  12013. flag = 0;
  12014. }
  12015. }
  12016. wc_Sha224Free(&sha224);
  12017. wc_Sha224Free(&temp);
  12018. res = TEST_RES_CHECK(flag == 0);
  12019. #endif
  12020. return res;
  12021. } /* END test_wc_Sha224Copy */
  12022. /*
  12023. * Testing wc_InitRipeMd()
  12024. */
  12025. static int test_wc_InitRipeMd(void)
  12026. {
  12027. int res = TEST_SKIPPED;
  12028. #ifdef WOLFSSL_RIPEMD
  12029. RipeMd ripemd;
  12030. int ret;
  12031. int flag = 0;
  12032. /* Test good arg. */
  12033. ret = wc_InitRipeMd(&ripemd);
  12034. if (ret != 0) {
  12035. flag = WOLFSSL_FATAL_ERROR;
  12036. }
  12037. /* Test bad arg. */
  12038. if (!flag) {
  12039. ret = wc_InitRipeMd(NULL);
  12040. if (ret != BAD_FUNC_ARG) {
  12041. flag = WOLFSSL_FATAL_ERROR;
  12042. }
  12043. }
  12044. res = TEST_RES_CHECK(flag == 0);
  12045. #endif
  12046. return res;
  12047. } /* END test_wc_InitRipeMd */
  12048. /*
  12049. * Testing wc_RipeMdUpdate()
  12050. */
  12051. static int test_wc_RipeMdUpdate(void)
  12052. {
  12053. int res = TEST_SKIPPED;
  12054. #ifdef WOLFSSL_RIPEMD
  12055. RipeMd ripemd;
  12056. byte hash[RIPEMD_DIGEST_SIZE];
  12057. testVector a, b, c;
  12058. int ret;
  12059. int flag = 0;
  12060. ret = wc_InitRipeMd(&ripemd);
  12061. if (ret != 0) {
  12062. flag = ret;
  12063. }
  12064. /* Input */
  12065. if (!flag) {
  12066. a.input = "a";
  12067. a.inLen = XSTRLEN(a.input);
  12068. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12069. if (ret != 0) {
  12070. flag = ret;
  12071. }
  12072. }
  12073. if (!flag) {
  12074. ret = wc_RipeMdFinal(&ripemd, hash);
  12075. if (ret != 0) {
  12076. flag = ret;
  12077. }
  12078. }
  12079. /* Update input. */
  12080. if (!flag) {
  12081. a.input = "abc";
  12082. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12083. "\xb0\x87\xf1\x5a\x0b\xfc";
  12084. a.inLen = XSTRLEN(a.input);
  12085. a.outLen = XSTRLEN(a.output);
  12086. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  12087. if (ret != 0) {
  12088. flag = ret;
  12089. }
  12090. }
  12091. if (!flag) {
  12092. ret = wc_RipeMdFinal(&ripemd, hash);
  12093. if (ret != 0) {
  12094. flag = ret;
  12095. }
  12096. }
  12097. if (!flag) {
  12098. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  12099. flag = WOLFSSL_FATAL_ERROR;
  12100. }
  12101. }
  12102. /* Pass in bad values. */
  12103. if (!flag) {
  12104. b.input = NULL;
  12105. b.inLen = 0;
  12106. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  12107. if (ret != 0) {
  12108. flag = ret;
  12109. }
  12110. }
  12111. if (!flag) {
  12112. c.input = NULL;
  12113. c.inLen = RIPEMD_DIGEST_SIZE;
  12114. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  12115. if (ret != BAD_FUNC_ARG) {
  12116. flag = WOLFSSL_FATAL_ERROR;
  12117. }
  12118. }
  12119. if (!flag) {
  12120. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12121. if (ret != BAD_FUNC_ARG) {
  12122. flag = WOLFSSL_FATAL_ERROR;
  12123. }
  12124. }
  12125. res = TEST_RES_CHECK(flag == 0);
  12126. #endif
  12127. return res;
  12128. } /* END test_wc_RipeMdUdpate */
  12129. /*
  12130. * Unit test function for wc_RipeMdFinal()
  12131. */
  12132. static int test_wc_RipeMdFinal(void)
  12133. {
  12134. int res = TEST_SKIPPED;
  12135. #ifdef WOLFSSL_RIPEMD
  12136. RipeMd ripemd;
  12137. byte* hash_test[3];
  12138. byte hash1[RIPEMD_DIGEST_SIZE];
  12139. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12140. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12141. int times, i, ret;
  12142. int flag = 0;
  12143. /* Initialize */
  12144. ret = wc_InitRipeMd(&ripemd);
  12145. if (ret != 0) {
  12146. flag = ret;
  12147. }
  12148. if (!flag) {
  12149. hash_test[0] = hash1;
  12150. hash_test[1] = hash2;
  12151. hash_test[2] = hash3;
  12152. }
  12153. times = sizeof(hash_test) / sizeof(byte*);
  12154. /* Testing oversized buffers. */
  12155. for (i = 0; i < times; i++) {
  12156. if (!flag) {
  12157. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  12158. if (ret != 0) {
  12159. flag = WOLFSSL_FATAL_ERROR;
  12160. }
  12161. }
  12162. }
  12163. /* Test bad args. */
  12164. if (!flag) {
  12165. ret = wc_RipeMdFinal(NULL, NULL);
  12166. if (ret != BAD_FUNC_ARG) {
  12167. flag = WOLFSSL_FATAL_ERROR;
  12168. }
  12169. }
  12170. if (!flag) {
  12171. ret = wc_RipeMdFinal(NULL, hash1);
  12172. if (ret != BAD_FUNC_ARG) {
  12173. flag = WOLFSSL_FATAL_ERROR;
  12174. }
  12175. }
  12176. if (!flag) {
  12177. ret = wc_RipeMdFinal(&ripemd, NULL);
  12178. if (ret != BAD_FUNC_ARG) {
  12179. flag = WOLFSSL_FATAL_ERROR;
  12180. }
  12181. }
  12182. res = TEST_RES_CHECK(flag == 0);
  12183. #endif
  12184. return res;
  12185. } /* END test_wc_RipeMdFinal */
  12186. /*
  12187. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12188. * wc_InitSha3_512
  12189. */
  12190. static int test_wc_InitSha3(void)
  12191. {
  12192. int res = TEST_SKIPPED;
  12193. #if defined(WOLFSSL_SHA3)
  12194. wc_Sha3 sha3;
  12195. int ret = 0;
  12196. (void)sha3;
  12197. #if !defined(WOLFSSL_NOSHA3_224)
  12198. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12199. /* Test bad args. */
  12200. if (ret == 0) {
  12201. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  12202. if (ret == BAD_FUNC_ARG) {
  12203. ret = 0;
  12204. }
  12205. else if (ret == 0) {
  12206. ret = WOLFSSL_FATAL_ERROR;
  12207. }
  12208. }
  12209. wc_Sha3_224_Free(&sha3);
  12210. #endif /* NOSHA3_224 */
  12211. #if !defined(WOLFSSL_NOSHA3_256)
  12212. if (ret == 0) {
  12213. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12214. /* Test bad args. */
  12215. if (ret == 0) {
  12216. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  12217. if (ret == BAD_FUNC_ARG) {
  12218. ret = 0;
  12219. }
  12220. else if (ret == 0) {
  12221. ret = WOLFSSL_FATAL_ERROR;
  12222. }
  12223. }
  12224. wc_Sha3_256_Free(&sha3);
  12225. } /* END sha3_256 */
  12226. #endif /* NOSHA3_256 */
  12227. #if !defined(WOLFSSL_NOSHA3_384)
  12228. if (ret == 0) {
  12229. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12230. /* Test bad args. */
  12231. if (ret == 0) {
  12232. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  12233. if (ret == BAD_FUNC_ARG) {
  12234. ret = 0;
  12235. }
  12236. else if (ret == 0) {
  12237. ret = WOLFSSL_FATAL_ERROR;
  12238. }
  12239. }
  12240. wc_Sha3_384_Free(&sha3);
  12241. } /* END sha3_384 */
  12242. #endif /* NOSHA3_384 */
  12243. #if !defined(WOLFSSL_NOSHA3_512)
  12244. if (ret == 0) {
  12245. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12246. /* Test bad args. */
  12247. if (ret == 0) {
  12248. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  12249. if (ret == BAD_FUNC_ARG) {
  12250. ret = 0;
  12251. }
  12252. else if (ret == 0) {
  12253. ret = WOLFSSL_FATAL_ERROR;
  12254. }
  12255. }
  12256. wc_Sha3_512_Free(&sha3);
  12257. } /* END sha3_512 */
  12258. #endif /* NOSHA3_512 */
  12259. res = TEST_RES_CHECK(ret == 0);
  12260. #endif
  12261. return res;
  12262. } /* END test_wc_InitSha3 */
  12263. /*
  12264. * Testing wc_Sha3_Update()
  12265. */
  12266. static int testing_wc_Sha3_Update(void)
  12267. {
  12268. int res = TEST_SKIPPED;
  12269. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12270. !defined(WOLFSSL_AFALG_XILINX)
  12271. wc_Sha3 sha3;
  12272. byte msg[] = "Everybody's working for the weekend.";
  12273. byte msg2[] = "Everybody gets Friday off.";
  12274. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12275. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12276. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12277. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12278. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12279. word32 msglen = sizeof(msg) - 1;
  12280. word32 msg2len = sizeof(msg2);
  12281. word32 msgCmplen = sizeof(msgCmp);
  12282. int ret = 0;
  12283. #if !defined(WOLFSSL_NOSHA3_224)
  12284. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12285. if (ret != 0) {
  12286. return TEST_FAIL;
  12287. }
  12288. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  12289. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12290. ret = WOLFSSL_FATAL_ERROR;
  12291. }
  12292. if (ret == 0) {
  12293. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12294. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12295. ret = WOLFSSL_FATAL_ERROR;
  12296. }
  12297. }
  12298. /* Pass bad args. */
  12299. if (ret == 0) {
  12300. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  12301. if (ret == BAD_FUNC_ARG) {
  12302. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  12303. }
  12304. if (ret == BAD_FUNC_ARG) {
  12305. wc_Sha3_224_Free(&sha3);
  12306. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  12307. return TEST_FAIL;
  12308. }
  12309. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  12310. if (ret == 0) {
  12311. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  12312. }
  12313. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12314. ret = WOLFSSL_FATAL_ERROR;
  12315. }
  12316. }
  12317. }
  12318. wc_Sha3_224_Free(&sha3);
  12319. #endif /* SHA3_224 */
  12320. #if !defined(WOLFSSL_NOSHA3_256)
  12321. if (ret == 0) {
  12322. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12323. if (ret != 0) {
  12324. return TEST_FAIL;
  12325. }
  12326. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  12327. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12328. ret = WOLFSSL_FATAL_ERROR;
  12329. }
  12330. if (ret == 0) {
  12331. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12332. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12333. ret = WOLFSSL_FATAL_ERROR;
  12334. }
  12335. }
  12336. /* Pass bad args. */
  12337. if (ret == 0) {
  12338. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  12339. if (ret == BAD_FUNC_ARG) {
  12340. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  12341. }
  12342. if (ret == BAD_FUNC_ARG) {
  12343. wc_Sha3_256_Free(&sha3);
  12344. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  12345. return TEST_FAIL;
  12346. }
  12347. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  12348. if (ret == 0) {
  12349. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  12350. }
  12351. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12352. ret = WOLFSSL_FATAL_ERROR;
  12353. }
  12354. }
  12355. }
  12356. wc_Sha3_256_Free(&sha3);
  12357. }
  12358. #endif /* SHA3_256 */
  12359. #if !defined(WOLFSSL_NOSHA3_384)
  12360. if (ret == 0) {
  12361. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12362. if (ret != 0) {
  12363. return TEST_FAIL;
  12364. }
  12365. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  12366. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12367. ret = WOLFSSL_FATAL_ERROR;
  12368. }
  12369. if (ret == 0) {
  12370. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12371. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12372. ret = WOLFSSL_FATAL_ERROR;
  12373. }
  12374. }
  12375. /* Pass bad args. */
  12376. if (ret == 0) {
  12377. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  12378. if (ret == BAD_FUNC_ARG) {
  12379. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  12380. }
  12381. if (ret == BAD_FUNC_ARG) {
  12382. wc_Sha3_384_Free(&sha3);
  12383. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  12384. return TEST_FAIL;
  12385. }
  12386. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  12387. if (ret == 0) {
  12388. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  12389. }
  12390. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12391. ret = WOLFSSL_FATAL_ERROR;
  12392. }
  12393. }
  12394. }
  12395. wc_Sha3_384_Free(&sha3);
  12396. }
  12397. #endif /* SHA3_384 */
  12398. #if !defined(WOLFSSL_NOSHA3_512)
  12399. if (ret == 0) {
  12400. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12401. if (ret != 0) {
  12402. return TEST_FAIL;
  12403. }
  12404. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  12405. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  12406. ret = WOLFSSL_FATAL_ERROR;
  12407. }
  12408. if (ret == 0) {
  12409. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12410. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  12411. ret = WOLFSSL_FATAL_ERROR;
  12412. }
  12413. }
  12414. /* Pass bad args. */
  12415. if (ret == 0) {
  12416. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  12417. if (ret == BAD_FUNC_ARG) {
  12418. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  12419. }
  12420. if (ret == BAD_FUNC_ARG) {
  12421. wc_Sha3_512_Free(&sha3);
  12422. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  12423. return TEST_FAIL;
  12424. }
  12425. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  12426. if (ret == 0) {
  12427. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  12428. }
  12429. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  12430. ret = WOLFSSL_FATAL_ERROR;
  12431. }
  12432. }
  12433. }
  12434. wc_Sha3_512_Free(&sha3);
  12435. }
  12436. #endif /* SHA3_512 */
  12437. res = TEST_RES_CHECK(ret == 0);
  12438. #endif /* WOLFSSL_SHA3 */
  12439. return res;
  12440. } /* END testing_wc_Sha3_Update */
  12441. /*
  12442. * Testing wc_Sha3_224_Final()
  12443. */
  12444. static int test_wc_Sha3_224_Final(void)
  12445. {
  12446. int res = TEST_SKIPPED;
  12447. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12448. wc_Sha3 sha3;
  12449. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12450. "nopnopq";
  12451. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12452. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12453. "\x64\xea\xd0\xfc\xce\x33";
  12454. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12455. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12456. int ret = 0;
  12457. /* Init stack variables. */
  12458. XMEMSET(hash, 0, sizeof(hash));
  12459. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12460. if (ret != 0) {
  12461. return TEST_FAIL;
  12462. }
  12463. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12464. if (ret == 0) {
  12465. ret = wc_Sha3_224_Final(&sha3, hash);
  12466. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  12467. ret = WOLFSSL_FATAL_ERROR;
  12468. }
  12469. }
  12470. /* Test bad args. */
  12471. if (ret == 0) {
  12472. ret = wc_Sha3_224_Final(NULL, hash);
  12473. if (ret == 0) {
  12474. ret = wc_Sha3_224_Final(&sha3, NULL);
  12475. }
  12476. if (ret == BAD_FUNC_ARG) {
  12477. ret = 0;
  12478. }
  12479. else if (ret == 0) {
  12480. ret = WOLFSSL_FATAL_ERROR;
  12481. }
  12482. }
  12483. wc_Sha3_224_Free(&sha3);
  12484. if (ret == 0) {
  12485. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12486. if (ret != 0) {
  12487. return TEST_FAIL;
  12488. }
  12489. /* Init stack variables. */
  12490. XMEMSET(hash, 0, sizeof(hash));
  12491. XMEMSET(hashRet, 0, sizeof(hashRet));
  12492. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12493. if (ret == 0) {
  12494. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  12495. }
  12496. if (ret == 0) {
  12497. ret = wc_Sha3_224_Final(&sha3, hash);
  12498. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  12499. ret = WOLFSSL_FATAL_ERROR;
  12500. }
  12501. }
  12502. if (ret == 0) {
  12503. /* Test bad args. */
  12504. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  12505. if (ret == BAD_FUNC_ARG) {
  12506. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  12507. }
  12508. if (ret == BAD_FUNC_ARG) {
  12509. ret = 0;
  12510. }
  12511. else if (ret == 0) {
  12512. ret = WOLFSSL_FATAL_ERROR;
  12513. }
  12514. }
  12515. }
  12516. wc_Sha3_224_Free(&sha3);
  12517. res = TEST_RES_CHECK(ret == 0);
  12518. #endif
  12519. return res;
  12520. } /* END test_wc_Sha3_224_Final */
  12521. /*
  12522. * Testing wc_Sha3_256_Final()
  12523. */
  12524. static int test_wc_Sha3_256_Final(void)
  12525. {
  12526. int res = TEST_SKIPPED;
  12527. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12528. wc_Sha3 sha3;
  12529. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12530. "nopnopq";
  12531. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12532. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12533. "\xdd\x97\x49\x6d\x33\x76";
  12534. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12535. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12536. int ret = 0;
  12537. /* Init stack variables. */
  12538. XMEMSET(hash, 0, sizeof(hash));
  12539. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12540. if (ret != 0) {
  12541. return TEST_FAIL;
  12542. }
  12543. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12544. if (ret == 0) {
  12545. ret = wc_Sha3_256_Final(&sha3, hash);
  12546. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12547. ret = WOLFSSL_FATAL_ERROR;
  12548. }
  12549. }
  12550. /* Test bad args. */
  12551. if (ret == 0) {
  12552. ret = wc_Sha3_256_Final(NULL, hash);
  12553. if (ret == 0) {
  12554. ret = wc_Sha3_256_Final(&sha3, NULL);
  12555. }
  12556. if (ret == BAD_FUNC_ARG) {
  12557. ret = 0;
  12558. }
  12559. else if (ret == 0) {
  12560. ret = WOLFSSL_FATAL_ERROR;
  12561. }
  12562. }
  12563. wc_Sha3_256_Free(&sha3);
  12564. if (ret == 0) {
  12565. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12566. if (ret != 0) {
  12567. return TEST_FAIL;
  12568. }
  12569. /* Init stack variables. */
  12570. XMEMSET(hash, 0, sizeof(hash));
  12571. XMEMSET(hashRet, 0, sizeof(hashRet));
  12572. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12573. if (ret == 0) {
  12574. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  12575. }
  12576. if (ret == 0) {
  12577. ret = wc_Sha3_256_Final(&sha3, hash);
  12578. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  12579. ret = WOLFSSL_FATAL_ERROR;
  12580. }
  12581. }
  12582. if (ret == 0) {
  12583. /* Test bad args. */
  12584. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  12585. if (ret == BAD_FUNC_ARG) {
  12586. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  12587. }
  12588. if (ret == BAD_FUNC_ARG) {
  12589. ret = 0;
  12590. }
  12591. else if (ret == 0) {
  12592. ret = WOLFSSL_FATAL_ERROR;
  12593. }
  12594. }
  12595. }
  12596. wc_Sha3_256_Free(&sha3);
  12597. res = TEST_RES_CHECK(ret == 0);
  12598. #endif
  12599. return res;
  12600. } /* END test_wc_Sha3_256_Final */
  12601. /*
  12602. * Testing wc_Sha3_384_Final()
  12603. */
  12604. static int test_wc_Sha3_384_Final(void)
  12605. {
  12606. int res = TEST_SKIPPED;
  12607. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12608. wc_Sha3 sha3;
  12609. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12610. "nopnopq";
  12611. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12612. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12613. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12614. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12615. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12616. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12617. int ret = 0;
  12618. /* Init stack variables. */
  12619. XMEMSET(hash, 0, sizeof(hash));
  12620. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12621. if (ret != 0) {
  12622. return TEST_FAIL;
  12623. }
  12624. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12625. if (ret == 0) {
  12626. ret = wc_Sha3_384_Final(&sha3, hash);
  12627. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12628. ret = WOLFSSL_FATAL_ERROR;
  12629. }
  12630. }
  12631. /* Test bad args. */
  12632. if (ret == 0) {
  12633. ret = wc_Sha3_384_Final(NULL, hash);
  12634. if (ret == 0) {
  12635. ret = wc_Sha3_384_Final(&sha3, NULL);
  12636. }
  12637. if (ret == BAD_FUNC_ARG) {
  12638. ret = 0;
  12639. }
  12640. else if (ret == 0) {
  12641. ret = WOLFSSL_FATAL_ERROR;
  12642. }
  12643. }
  12644. wc_Sha3_384_Free(&sha3);
  12645. if (ret == 0) {
  12646. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12647. if (ret != 0) {
  12648. return TEST_FAIL;
  12649. }
  12650. /* Init stack variables. */
  12651. XMEMSET(hash, 0, sizeof(hash));
  12652. XMEMSET(hashRet, 0, sizeof(hashRet));
  12653. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12654. if (ret == 0) {
  12655. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  12656. }
  12657. if (ret == 0) {
  12658. ret = wc_Sha3_384_Final(&sha3, hash);
  12659. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  12660. ret = WOLFSSL_FATAL_ERROR;
  12661. }
  12662. }
  12663. if (ret == 0) {
  12664. /* Test bad args. */
  12665. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  12666. if (ret == BAD_FUNC_ARG) {
  12667. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  12668. }
  12669. if (ret == BAD_FUNC_ARG) {
  12670. ret = 0;
  12671. }
  12672. else if (ret == 0) {
  12673. ret = WOLFSSL_FATAL_ERROR;
  12674. }
  12675. }
  12676. }
  12677. wc_Sha3_384_Free(&sha3);
  12678. res = TEST_RES_CHECK(ret == 0);
  12679. #endif
  12680. return res;
  12681. } /* END test_wc_Sha3_384_Final */
  12682. /*
  12683. * Testing wc_Sha3_512_Final()
  12684. */
  12685. static int test_wc_Sha3_512_Final(void)
  12686. {
  12687. int res = TEST_SKIPPED;
  12688. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12689. !defined(WOLFSSL_NOSHA3_384)
  12690. wc_Sha3 sha3;
  12691. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12692. "nopnopq";
  12693. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12694. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12695. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12696. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12697. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12698. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12699. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12700. int ret = 0;
  12701. /* Init stack variables. */
  12702. XMEMSET(hash, 0, sizeof(hash));
  12703. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12704. if (ret != 0) {
  12705. return TEST_FAIL;
  12706. }
  12707. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12708. if (ret == 0) {
  12709. ret = wc_Sha3_512_Final(&sha3, hash);
  12710. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12711. ret = WOLFSSL_FATAL_ERROR;
  12712. }
  12713. }
  12714. /* Test bad args. */
  12715. if (ret == 0) {
  12716. ret = wc_Sha3_512_Final(NULL, hash);
  12717. if (ret == 0) {
  12718. ret = wc_Sha3_384_Final(&sha3, NULL);
  12719. }
  12720. if (ret == BAD_FUNC_ARG) {
  12721. ret = 0;
  12722. }
  12723. else if (ret == 0) {
  12724. ret = WOLFSSL_FATAL_ERROR;
  12725. }
  12726. }
  12727. wc_Sha3_512_Free(&sha3);
  12728. if (ret == 0) {
  12729. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12730. if (ret != 0) {
  12731. return TEST_FAIL;
  12732. }
  12733. /* Init stack variables. */
  12734. XMEMSET(hash, 0, sizeof(hash));
  12735. XMEMSET(hashRet, 0, sizeof(hashRet));
  12736. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  12737. if (ret == 0) {
  12738. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  12739. }
  12740. if (ret == 0) {
  12741. ret = wc_Sha3_512_Final(&sha3, hash);
  12742. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  12743. ret = WOLFSSL_FATAL_ERROR;
  12744. }
  12745. }
  12746. if (ret == 0) {
  12747. /* Test bad args. */
  12748. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  12749. if (ret == BAD_FUNC_ARG) {
  12750. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  12751. }
  12752. if (ret == BAD_FUNC_ARG) {
  12753. ret = 0;
  12754. }
  12755. else if (ret == 0) {
  12756. ret = WOLFSSL_FATAL_ERROR;
  12757. }
  12758. }
  12759. }
  12760. wc_Sha3_512_Free(&sha3);
  12761. res = TEST_RES_CHECK(ret == 0);
  12762. #endif
  12763. return res;
  12764. } /* END test_wc_Sha3_512_Final */
  12765. /*
  12766. * Testing wc_Sha3_224_Copy()
  12767. */
  12768. static int test_wc_Sha3_224_Copy(void)
  12769. {
  12770. int res = TEST_SKIPPED;
  12771. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12772. wc_Sha3 sha3, sha3Cpy;
  12773. const char* msg = TEST_STRING;
  12774. word32 msglen = (word32)TEST_STRING_SZ;
  12775. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12776. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12777. int ret = 0;
  12778. XMEMSET(hash, 0, sizeof(hash));
  12779. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12780. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12781. if (ret != 0) {
  12782. return TEST_FAIL;
  12783. }
  12784. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12785. if (ret != 0) {
  12786. wc_Sha3_224_Free(&sha3);
  12787. return TEST_FAIL;
  12788. }
  12789. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12790. if (ret == 0) {
  12791. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12792. if (ret == 0) {
  12793. ret = wc_Sha3_224_Final(&sha3, hash);
  12794. if (ret == 0) {
  12795. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12796. }
  12797. }
  12798. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12799. ret = WOLFSSL_FATAL_ERROR;
  12800. }
  12801. }
  12802. /* Test bad args. */
  12803. if (ret == 0) {
  12804. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12805. if (ret == BAD_FUNC_ARG) {
  12806. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12807. }
  12808. if (ret == BAD_FUNC_ARG) {
  12809. ret = 0;
  12810. }
  12811. else if (ret == 0) {
  12812. ret = WOLFSSL_FATAL_ERROR;
  12813. }
  12814. }
  12815. res = TEST_RES_CHECK(ret == 0);
  12816. #endif
  12817. return res;
  12818. } /* END test_wc_Sha3_224_Copy */
  12819. /*
  12820. * Testing wc_Sha3_256_Copy()
  12821. */
  12822. static int test_wc_Sha3_256_Copy(void)
  12823. {
  12824. int res = TEST_SKIPPED;
  12825. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12826. wc_Sha3 sha3, sha3Cpy;
  12827. const char* msg = TEST_STRING;
  12828. word32 msglen = (word32)TEST_STRING_SZ;
  12829. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12830. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12831. int ret = 0;
  12832. XMEMSET(hash, 0, sizeof(hash));
  12833. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12834. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12835. if (ret != 0) {
  12836. return TEST_FAIL;
  12837. }
  12838. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12839. if (ret != 0) {
  12840. wc_Sha3_256_Free(&sha3);
  12841. return TEST_FAIL;
  12842. }
  12843. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12844. if (ret == 0) {
  12845. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12846. if (ret == 0) {
  12847. ret = wc_Sha3_256_Final(&sha3, hash);
  12848. if (ret == 0) {
  12849. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12850. }
  12851. }
  12852. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12853. ret = WOLFSSL_FATAL_ERROR;
  12854. }
  12855. }
  12856. /* Test bad args. */
  12857. if (ret == 0) {
  12858. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12859. if (ret == BAD_FUNC_ARG) {
  12860. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12861. }
  12862. if (ret == BAD_FUNC_ARG) {
  12863. ret = 0;
  12864. }
  12865. else if (ret == 0) {
  12866. ret = WOLFSSL_FATAL_ERROR;
  12867. }
  12868. }
  12869. res = TEST_RES_CHECK(ret == 0);
  12870. #endif
  12871. return res;
  12872. } /* END test_wc_Sha3_256_Copy */
  12873. /*
  12874. * Testing wc_Sha3_384_Copy()
  12875. */
  12876. static int test_wc_Sha3_384_Copy(void)
  12877. {
  12878. int res = TEST_SKIPPED;
  12879. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12880. wc_Sha3 sha3, sha3Cpy;
  12881. const char* msg = TEST_STRING;
  12882. word32 msglen = (word32)TEST_STRING_SZ;
  12883. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12884. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12885. int ret = 0;
  12886. XMEMSET(hash, 0, sizeof(hash));
  12887. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12888. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12889. if (ret != 0) {
  12890. return TEST_FAIL;
  12891. }
  12892. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12893. if (ret != 0) {
  12894. wc_Sha3_384_Free(&sha3);
  12895. return TEST_FAIL;
  12896. }
  12897. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12898. if (ret == 0) {
  12899. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12900. if (ret == 0) {
  12901. ret = wc_Sha3_384_Final(&sha3, hash);
  12902. if (ret == 0) {
  12903. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12904. }
  12905. }
  12906. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12907. ret = WOLFSSL_FATAL_ERROR;
  12908. }
  12909. }
  12910. /* Test bad args. */
  12911. if (ret == 0) {
  12912. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12913. if (ret == BAD_FUNC_ARG) {
  12914. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12915. }
  12916. if (ret == BAD_FUNC_ARG) {
  12917. ret = 0;
  12918. }
  12919. else if (ret == 0) {
  12920. ret = WOLFSSL_FATAL_ERROR;
  12921. }
  12922. }
  12923. res = TEST_RES_CHECK(ret == 0);
  12924. #endif
  12925. return res;
  12926. } /* END test_wc_Sha3_384_Copy */
  12927. /*
  12928. * Testing wc_Sha3_512_Copy()
  12929. */
  12930. static int test_wc_Sha3_512_Copy(void)
  12931. {
  12932. int res = TEST_SKIPPED;
  12933. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12934. wc_Sha3 sha3, sha3Cpy;
  12935. const char* msg = TEST_STRING;
  12936. word32 msglen = (word32)TEST_STRING_SZ;
  12937. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12938. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12939. int ret = 0;
  12940. XMEMSET(hash, 0, sizeof(hash));
  12941. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12942. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12943. if (ret != 0) {
  12944. return TEST_FAIL;
  12945. }
  12946. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12947. if (ret != 0) {
  12948. wc_Sha3_512_Free(&sha3);
  12949. return TEST_FAIL;
  12950. }
  12951. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12952. if (ret == 0) {
  12953. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12954. if (ret == 0) {
  12955. ret = wc_Sha3_512_Final(&sha3, hash);
  12956. if (ret == 0) {
  12957. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12958. }
  12959. }
  12960. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12961. ret = WOLFSSL_FATAL_ERROR;
  12962. }
  12963. }
  12964. /* Test bad args. */
  12965. if (ret == 0) {
  12966. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12967. if (ret == BAD_FUNC_ARG) {
  12968. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12969. }
  12970. if (ret == BAD_FUNC_ARG) {
  12971. ret = 0;
  12972. }
  12973. else if (ret == 0) {
  12974. ret = WOLFSSL_FATAL_ERROR;
  12975. }
  12976. }
  12977. res = TEST_RES_CHECK(ret == 0);
  12978. #endif
  12979. return res;
  12980. } /* END test_wc_Sha3_512_Copy */
  12981. /*
  12982. * Unit test function for wc_Sha3_GetFlags()
  12983. */
  12984. static int test_wc_Sha3_GetFlags(void)
  12985. {
  12986. int res = TEST_SKIPPED;
  12987. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12988. wc_Sha3 sha3;
  12989. word32 flags = 0;
  12990. int ret = 0;
  12991. /* Initialize */
  12992. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12993. if (ret != 0) {
  12994. return TEST_FAIL;
  12995. }
  12996. if (ret == 0) {
  12997. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12998. }
  12999. if (ret == 0) {
  13000. if (flags & WC_HASH_FLAG_ISCOPY) {
  13001. ret = 0;
  13002. }
  13003. }
  13004. wc_Sha3_224_Free(&sha3);
  13005. res = TEST_RES_CHECK(ret == 0);
  13006. #endif
  13007. return res;
  13008. } /* END test_wc_Sha3_GetFlags */
  13009. static int test_wc_InitShake256(void)
  13010. {
  13011. int res = TEST_SKIPPED;
  13012. #ifdef WOLFSSL_SHAKE256
  13013. wc_Shake shake;
  13014. int ret = 0;
  13015. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13016. /* Test bad args. */
  13017. if (ret == 0) {
  13018. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  13019. if (ret == BAD_FUNC_ARG) {
  13020. ret = 0;
  13021. }
  13022. else if (ret == 0) {
  13023. ret = WOLFSSL_FATAL_ERROR;
  13024. }
  13025. }
  13026. wc_Shake256_Free(&shake);
  13027. res = TEST_RES_CHECK(ret == 0);
  13028. #endif
  13029. return res;
  13030. } /* END test_wc_InitSha3 */
  13031. static int testing_wc_Shake256_Update(void)
  13032. {
  13033. int res = TEST_SKIPPED;
  13034. #ifdef WOLFSSL_SHAKE256
  13035. wc_Shake shake;
  13036. byte msg[] = "Everybody's working for the weekend.";
  13037. byte msg2[] = "Everybody gets Friday off.";
  13038. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13039. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13040. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13041. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13042. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13043. word32 msglen = sizeof(msg) - 1;
  13044. word32 msg2len = sizeof(msg2);
  13045. word32 msgCmplen = sizeof(msgCmp);
  13046. int ret = 0;
  13047. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13048. if (ret != 0) {
  13049. return TEST_FAIL;
  13050. }
  13051. ret = wc_Shake256_Update(&shake, msg, msglen);
  13052. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  13053. ret = WOLFSSL_FATAL_ERROR;
  13054. }
  13055. if (ret == 0) {
  13056. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13057. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  13058. ret = WOLFSSL_FATAL_ERROR;
  13059. }
  13060. }
  13061. /* Pass bad args. */
  13062. if (ret == 0) {
  13063. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  13064. if (ret == BAD_FUNC_ARG) {
  13065. ret = wc_Shake256_Update(&shake, NULL, 5);
  13066. }
  13067. if (ret == BAD_FUNC_ARG) {
  13068. wc_Shake256_Free(&shake);
  13069. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  13070. return TEST_FAIL;
  13071. }
  13072. ret = wc_Shake256_Update(&shake, NULL, 0);
  13073. if (ret == 0) {
  13074. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  13075. }
  13076. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  13077. ret = WOLFSSL_FATAL_ERROR;
  13078. }
  13079. }
  13080. }
  13081. wc_Shake256_Free(&shake);
  13082. res = TEST_RES_CHECK(ret == 0);
  13083. #endif /* WOLFSSL_SHAKE256 */
  13084. return res;
  13085. }
  13086. static int test_wc_Shake256_Final(void)
  13087. {
  13088. int res = TEST_SKIPPED;
  13089. #ifdef WOLFSSL_SHAKE256
  13090. wc_Shake shake;
  13091. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13092. "nopnopq";
  13093. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13094. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13095. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13096. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13097. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13098. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13099. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13100. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13101. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13102. byte hash[114];
  13103. int ret = 0;
  13104. /* Init stack variables. */
  13105. XMEMSET(hash, 0, sizeof(hash));
  13106. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13107. if (ret != 0) {
  13108. return TEST_FAIL;
  13109. }
  13110. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  13111. if (ret == 0) {
  13112. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  13113. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  13114. ret = WOLFSSL_FATAL_ERROR;
  13115. }
  13116. }
  13117. /* Test bad args. */
  13118. if (ret == 0) {
  13119. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  13120. if (ret == 0) {
  13121. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  13122. }
  13123. if (ret == BAD_FUNC_ARG) {
  13124. ret = 0;
  13125. }
  13126. else if (ret == 0) {
  13127. ret = WOLFSSL_FATAL_ERROR;
  13128. }
  13129. }
  13130. wc_Shake256_Free(&shake);
  13131. res = TEST_RES_CHECK(ret == 0);
  13132. #endif
  13133. return res;
  13134. }
  13135. /*
  13136. * Testing wc_Shake256_Copy()
  13137. */
  13138. static int test_wc_Shake256_Copy(void)
  13139. {
  13140. int res = TEST_SKIPPED;
  13141. #ifdef WOLFSSL_SHAKE256
  13142. wc_Shake shake, shakeCpy;
  13143. const char* msg = TEST_STRING;
  13144. word32 msglen = (word32)TEST_STRING_SZ;
  13145. byte hash[144];
  13146. byte hashCpy[144];
  13147. word32 hashLen = sizeof(hash);
  13148. word32 hashLenCpy = sizeof(hashCpy);
  13149. int ret;
  13150. XMEMSET(hash, 0, sizeof(hash));
  13151. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13152. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  13153. if (ret != 0) {
  13154. return TEST_FAIL;
  13155. }
  13156. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  13157. if (ret != 0) {
  13158. wc_Shake256_Free(&shake);
  13159. return TEST_FAIL;
  13160. }
  13161. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  13162. if (ret == 0) {
  13163. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  13164. if (ret == 0) {
  13165. ret = wc_Shake256_Final(&shake, hash, hashLen);
  13166. if (ret == 0) {
  13167. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  13168. }
  13169. }
  13170. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  13171. ret = WOLFSSL_FATAL_ERROR;
  13172. }
  13173. }
  13174. /* Test bad args. */
  13175. if (ret == 0) {
  13176. ret = wc_Shake256_Copy(NULL, &shake);
  13177. if (ret == BAD_FUNC_ARG) {
  13178. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  13179. }
  13180. if (ret == BAD_FUNC_ARG) {
  13181. ret = 0;
  13182. }
  13183. else if (ret == 0) {
  13184. ret = WOLFSSL_FATAL_ERROR;
  13185. }
  13186. }
  13187. wc_Shake256_Free(&shake);
  13188. res = TEST_RES_CHECK(ret == 0);
  13189. #endif
  13190. return res;
  13191. } /* END test_wc_Shake256_Copy */
  13192. /*
  13193. * Unit test function for wc_Shake256Hash()
  13194. */
  13195. static int test_wc_Shake256Hash(void)
  13196. {
  13197. int res = TEST_SKIPPED;
  13198. #ifdef WOLFSSL_SHAKE256
  13199. const byte data[] = { /* Hello World */
  13200. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13201. 0x72,0x6c,0x64
  13202. };
  13203. word32 len = sizeof(data);
  13204. byte hash[144];
  13205. word32 hashLen = sizeof(hash);
  13206. int ret;
  13207. ret = wc_Shake256Hash(data, len, hash, hashLen);
  13208. res = TEST_RES_CHECK(ret == 0);
  13209. #endif
  13210. return res;
  13211. } /* END test_wc_Shake256Hash */
  13212. /*
  13213. * Test function for wc_HmacSetKey
  13214. */
  13215. static int test_wc_Md5HmacSetKey(void)
  13216. {
  13217. int res = TEST_SKIPPED;
  13218. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13219. Hmac hmac;
  13220. int ret, times, itr;
  13221. int flag = 0;
  13222. const char* keys[]=
  13223. {
  13224. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13225. #ifndef HAVE_FIPS
  13226. "Jefe", /* smaller than minimum FIPS key size */
  13227. #endif
  13228. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13229. };
  13230. times = sizeof(keys) / sizeof(char*);
  13231. flag = 0;
  13232. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13233. if (ret != 0)
  13234. return TEST_FAIL;
  13235. for (itr = 0; itr < times; itr++) {
  13236. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13237. (word32)XSTRLEN(keys[itr]));
  13238. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13239. wc_HmacFree(&hmac);
  13240. if (ret == BAD_FUNC_ARG) {
  13241. return TEST_SUCCESS;
  13242. }
  13243. else {
  13244. return TEST_FAIL;
  13245. }
  13246. #else
  13247. if (ret != 0) {
  13248. flag = ret;
  13249. }
  13250. #endif
  13251. }
  13252. /* Bad args. */
  13253. if (!flag) {
  13254. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13255. (word32)XSTRLEN(keys[0]));
  13256. if (ret != BAD_FUNC_ARG) {
  13257. flag = WOLFSSL_FATAL_ERROR;
  13258. }
  13259. }
  13260. if (!flag) {
  13261. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  13262. if (ret != BAD_FUNC_ARG) {
  13263. flag = WOLFSSL_FATAL_ERROR;
  13264. }
  13265. }
  13266. if (!flag) {
  13267. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13268. (word32)XSTRLEN(keys[0]));
  13269. if (ret != BAD_FUNC_ARG) {
  13270. flag = WOLFSSL_FATAL_ERROR;
  13271. }
  13272. }
  13273. if (!flag) {
  13274. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13275. #ifdef HAVE_FIPS
  13276. if (ret != HMAC_MIN_KEYLEN_E) {
  13277. flag = WOLFSSL_FATAL_ERROR;
  13278. }
  13279. #else
  13280. if (ret != 0) {
  13281. flag = WOLFSSL_FATAL_ERROR;
  13282. }
  13283. #endif
  13284. }
  13285. wc_HmacFree(&hmac);
  13286. res = TEST_RES_CHECK(flag == 0);
  13287. #endif
  13288. return res;
  13289. } /* END test_wc_Md5HmacSetKey */
  13290. /*
  13291. * testing wc_HmacSetKey() on wc_Sha hash.
  13292. */
  13293. static int test_wc_ShaHmacSetKey(void)
  13294. {
  13295. int res = TEST_SKIPPED;
  13296. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13297. Hmac hmac;
  13298. int ret, times, itr;
  13299. int flag = 0;
  13300. const char* keys[]=
  13301. {
  13302. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13303. "\x0b\x0b\x0b",
  13304. #ifndef HAVE_FIPS
  13305. "Jefe", /* smaller than minimum FIPS key size */
  13306. #endif
  13307. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13308. "\xAA\xAA\xAA"
  13309. };
  13310. times = sizeof(keys) / sizeof(char*);
  13311. flag = 0;
  13312. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13313. if (ret != 0)
  13314. return ret;
  13315. for (itr = 0; itr < times; itr++) {
  13316. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13317. (word32)XSTRLEN(keys[itr]));
  13318. if (ret != 0) {
  13319. flag = ret;
  13320. }
  13321. }
  13322. /* Bad args. */
  13323. if (!flag) {
  13324. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13325. (word32)XSTRLEN(keys[0]));
  13326. if (ret != BAD_FUNC_ARG) {
  13327. flag = WOLFSSL_FATAL_ERROR;
  13328. }
  13329. }
  13330. if (!flag) {
  13331. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  13332. if (ret != BAD_FUNC_ARG) {
  13333. flag = WOLFSSL_FATAL_ERROR;
  13334. }
  13335. }
  13336. if (!flag) {
  13337. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13338. (word32)XSTRLEN(keys[0]));
  13339. if (ret != BAD_FUNC_ARG) {
  13340. flag = WOLFSSL_FATAL_ERROR;
  13341. }
  13342. }
  13343. if (!flag) {
  13344. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13345. #ifdef HAVE_FIPS
  13346. if (ret != HMAC_MIN_KEYLEN_E) {
  13347. flag = WOLFSSL_FATAL_ERROR;
  13348. }
  13349. #else
  13350. if (ret != 0) {
  13351. flag = WOLFSSL_FATAL_ERROR;
  13352. }
  13353. #endif
  13354. }
  13355. wc_HmacFree(&hmac);
  13356. res = TEST_RES_CHECK(flag == 0);
  13357. #endif
  13358. return res;
  13359. } /* END test_wc_ShaHmacSetKey() */
  13360. /*
  13361. * testing wc_HmacSetKey() on Sha224 hash.
  13362. */
  13363. static int test_wc_Sha224HmacSetKey(void)
  13364. {
  13365. int res = TEST_SKIPPED;
  13366. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13367. Hmac hmac;
  13368. int ret, times, itr;
  13369. int flag = 0;
  13370. const char* keys[]=
  13371. {
  13372. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13373. "\x0b\x0b\x0b",
  13374. #ifndef HAVE_FIPS
  13375. "Jefe", /* smaller than minimum FIPS key size */
  13376. #endif
  13377. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13378. "\xAA\xAA\xAA"
  13379. };
  13380. times = sizeof(keys) / sizeof(char*);
  13381. flag = 0;
  13382. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13383. if (ret != 0)
  13384. return ret;
  13385. for (itr = 0; itr < times; itr++) {
  13386. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13387. (word32)XSTRLEN(keys[itr]));
  13388. if (ret != 0) {
  13389. flag = ret;
  13390. }
  13391. }
  13392. /* Bad args. */
  13393. if (!flag) {
  13394. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13395. (word32)XSTRLEN(keys[0]));
  13396. if (ret != BAD_FUNC_ARG) {
  13397. flag = WOLFSSL_FATAL_ERROR;
  13398. }
  13399. }
  13400. if (!flag) {
  13401. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  13402. if (ret != BAD_FUNC_ARG) {
  13403. flag = WOLFSSL_FATAL_ERROR;
  13404. }
  13405. }
  13406. if (!flag) {
  13407. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13408. (word32)XSTRLEN(keys[0]));
  13409. if (ret != BAD_FUNC_ARG) {
  13410. flag = WOLFSSL_FATAL_ERROR;
  13411. }
  13412. }
  13413. if (!flag) {
  13414. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13415. #ifdef HAVE_FIPS
  13416. if (ret != HMAC_MIN_KEYLEN_E) {
  13417. flag = WOLFSSL_FATAL_ERROR;
  13418. }
  13419. #else
  13420. if (ret != 0) {
  13421. flag = WOLFSSL_FATAL_ERROR;
  13422. }
  13423. #endif
  13424. }
  13425. wc_HmacFree(&hmac);
  13426. res = TEST_RES_CHECK(flag == 0);
  13427. #endif
  13428. return res;
  13429. } /* END test_wc_Sha224HmacSetKey() */
  13430. /*
  13431. * testing wc_HmacSetKey() on Sha256 hash
  13432. */
  13433. static int test_wc_Sha256HmacSetKey(void)
  13434. {
  13435. int res = TEST_SKIPPED;
  13436. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13437. Hmac hmac;
  13438. int ret, times, itr;
  13439. int flag = 0;
  13440. const char* keys[]=
  13441. {
  13442. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13443. "\x0b\x0b\x0b",
  13444. #ifndef HAVE_FIPS
  13445. "Jefe", /* smaller than minimum FIPS key size */
  13446. #endif
  13447. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13448. "\xAA\xAA\xAA"
  13449. };
  13450. times = sizeof(keys) / sizeof(char*);
  13451. flag = 0;
  13452. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13453. if (ret != 0)
  13454. return ret;
  13455. for (itr = 0; itr < times; itr++) {
  13456. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13457. (word32)XSTRLEN(keys[itr]));
  13458. if (ret != 0) {
  13459. flag = ret;
  13460. }
  13461. }
  13462. /* Bad args. */
  13463. if (!flag) {
  13464. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13465. (word32)XSTRLEN(keys[0]));
  13466. if (ret != BAD_FUNC_ARG) {
  13467. flag = WOLFSSL_FATAL_ERROR;
  13468. }
  13469. }
  13470. if (!flag) {
  13471. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  13472. if (ret != BAD_FUNC_ARG) {
  13473. flag = WOLFSSL_FATAL_ERROR;
  13474. }
  13475. }
  13476. if (!flag) {
  13477. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13478. (word32)XSTRLEN(keys[0]));
  13479. if (ret != BAD_FUNC_ARG) {
  13480. flag = WOLFSSL_FATAL_ERROR;
  13481. }
  13482. }
  13483. if (!flag) {
  13484. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13485. #ifdef HAVE_FIPS
  13486. if (ret != HMAC_MIN_KEYLEN_E) {
  13487. flag = WOLFSSL_FATAL_ERROR;
  13488. }
  13489. #else
  13490. if (ret != 0) {
  13491. flag = WOLFSSL_FATAL_ERROR;
  13492. }
  13493. #endif
  13494. }
  13495. wc_HmacFree(&hmac);
  13496. res = TEST_RES_CHECK(flag == 0);
  13497. #endif
  13498. return res;
  13499. } /* END test_wc_Sha256HmacSetKey() */
  13500. /*
  13501. * testing wc_HmacSetKey on Sha384 hash.
  13502. */
  13503. static int test_wc_Sha384HmacSetKey(void)
  13504. {
  13505. int res = TEST_SKIPPED;
  13506. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13507. Hmac hmac;
  13508. int ret, times, itr;
  13509. int flag = 0;
  13510. const char* keys[]=
  13511. {
  13512. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13513. "\x0b\x0b\x0b",
  13514. #ifndef HAVE_FIPS
  13515. "Jefe", /* smaller than minimum FIPS key size */
  13516. #endif
  13517. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13518. "\xAA\xAA\xAA"
  13519. };
  13520. times = sizeof(keys) / sizeof(char*);
  13521. flag = 0;
  13522. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13523. if (ret != 0)
  13524. return ret;
  13525. for (itr = 0; itr < times; itr++) {
  13526. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13527. (word32)XSTRLEN(keys[itr]));
  13528. if (ret != 0) {
  13529. flag = ret;
  13530. }
  13531. }
  13532. /* Bad args. */
  13533. if (!flag) {
  13534. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13535. (word32)XSTRLEN(keys[0]));
  13536. if (ret != BAD_FUNC_ARG) {
  13537. flag = WOLFSSL_FATAL_ERROR;
  13538. }
  13539. }
  13540. if (!flag) {
  13541. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  13542. if (ret != BAD_FUNC_ARG) {
  13543. flag = WOLFSSL_FATAL_ERROR;
  13544. }
  13545. }
  13546. if (!flag) {
  13547. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  13548. (word32)XSTRLEN(keys[0]));
  13549. if (ret != BAD_FUNC_ARG) {
  13550. flag = WOLFSSL_FATAL_ERROR;
  13551. }
  13552. }
  13553. if (!flag) {
  13554. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13555. #ifdef HAVE_FIPS
  13556. if (ret != HMAC_MIN_KEYLEN_E) {
  13557. flag = WOLFSSL_FATAL_ERROR;
  13558. }
  13559. #else
  13560. if (ret != 0) {
  13561. flag = WOLFSSL_FATAL_ERROR;
  13562. }
  13563. #endif
  13564. }
  13565. wc_HmacFree(&hmac);
  13566. res = TEST_RES_CHECK(flag == 0);
  13567. #endif
  13568. return res;
  13569. } /* END test_wc_Sha384HmacSetKey() */
  13570. /*
  13571. * testing wc_HmacUpdate on wc_Md5 hash.
  13572. */
  13573. static int test_wc_Md5HmacUpdate(void)
  13574. {
  13575. int res = TEST_SKIPPED;
  13576. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13577. Hmac hmac;
  13578. testVector a, b;
  13579. int ret;
  13580. int flag = 0;
  13581. #ifdef HAVE_FIPS
  13582. const char* keys =
  13583. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13584. #else
  13585. const char* keys = "Jefe";
  13586. #endif
  13587. a.input = "what do ya want for nothing?";
  13588. a.inLen = XSTRLEN(a.input);
  13589. b.input = "Hi There";
  13590. b.inLen = XSTRLEN(b.input);
  13591. flag = 0;
  13592. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13593. if (ret != 0)
  13594. return ret;
  13595. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  13596. if (ret != 0) {
  13597. flag = ret;
  13598. }
  13599. if (!flag) {
  13600. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13601. if (ret != 0) {
  13602. flag = ret;
  13603. }
  13604. }
  13605. /* Update Hmac. */
  13606. if (!flag) {
  13607. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13608. if (ret != 0) {
  13609. flag = ret;
  13610. }
  13611. }
  13612. /* Test bad args. */
  13613. if (!flag) {
  13614. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13615. if (ret != BAD_FUNC_ARG) {
  13616. flag = WOLFSSL_FATAL_ERROR;
  13617. }
  13618. }
  13619. if (!flag) {
  13620. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13621. if (ret != BAD_FUNC_ARG) {
  13622. flag = WOLFSSL_FATAL_ERROR;
  13623. }
  13624. }
  13625. if (!flag) {
  13626. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13627. if (ret != 0) {
  13628. flag = ret;
  13629. }
  13630. }
  13631. wc_HmacFree(&hmac);
  13632. res = TEST_RES_CHECK(flag == 0);
  13633. #endif
  13634. return res;
  13635. } /* END test_wc_Md5HmacUpdate */
  13636. /*
  13637. * testing wc_HmacUpdate on SHA hash.
  13638. */
  13639. static int test_wc_ShaHmacUpdate(void)
  13640. {
  13641. int res = TEST_SKIPPED;
  13642. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13643. Hmac hmac;
  13644. testVector a, b;
  13645. int ret;
  13646. int flag = 0;
  13647. #ifdef HAVE_FIPS
  13648. const char* keys =
  13649. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13650. #else
  13651. const char* keys = "Jefe";
  13652. #endif
  13653. a.input = "what do ya want for nothing?";
  13654. a.inLen = XSTRLEN(a.input);
  13655. b.input = "Hi There";
  13656. b.inLen = XSTRLEN(b.input);
  13657. flag = 0;
  13658. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13659. if (ret != 0)
  13660. return ret;
  13661. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  13662. if (ret != 0) {
  13663. flag = ret;
  13664. }
  13665. if (!flag) {
  13666. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13667. if (ret != 0) {
  13668. flag = ret;
  13669. }
  13670. }
  13671. /* Update Hmac. */
  13672. if (!flag) {
  13673. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13674. if (ret != 0) {
  13675. flag = ret;
  13676. }
  13677. }
  13678. /* Test bad args. */
  13679. if (!flag) {
  13680. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13681. if (ret != BAD_FUNC_ARG) {
  13682. flag = WOLFSSL_FATAL_ERROR;
  13683. }
  13684. }
  13685. if (!flag) {
  13686. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13687. if (ret != BAD_FUNC_ARG) {
  13688. flag = WOLFSSL_FATAL_ERROR;
  13689. }
  13690. }
  13691. if (!flag) {
  13692. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13693. if (ret != 0) {
  13694. flag = ret;
  13695. }
  13696. }
  13697. wc_HmacFree(&hmac);
  13698. res = TEST_RES_CHECK(flag == 0);
  13699. #endif
  13700. return res;
  13701. } /* END test_wc_ShaHmacUpdate */
  13702. /*
  13703. * testing wc_HmacUpdate on SHA224 hash.
  13704. */
  13705. static int test_wc_Sha224HmacUpdate(void)
  13706. {
  13707. int res = TEST_SKIPPED;
  13708. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13709. Hmac hmac;
  13710. testVector a, b;
  13711. int ret;
  13712. int flag = 0;
  13713. #ifdef HAVE_FIPS
  13714. const char* keys =
  13715. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13716. #else
  13717. const char* keys = "Jefe";
  13718. #endif
  13719. a.input = "what do ya want for nothing?";
  13720. a.inLen = XSTRLEN(a.input);
  13721. b.input = "Hi There";
  13722. b.inLen = XSTRLEN(b.input);
  13723. flag = 0;
  13724. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13725. if (ret != 0)
  13726. return ret;
  13727. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  13728. if (ret != 0) {
  13729. flag = ret;
  13730. }
  13731. if (!flag) {
  13732. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13733. if (ret != 0) {
  13734. flag = ret;
  13735. }
  13736. }
  13737. /* Update Hmac. */
  13738. if (!flag) {
  13739. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13740. if (ret != 0) {
  13741. flag = ret;
  13742. }
  13743. }
  13744. /* Test bad args. */
  13745. if (!flag) {
  13746. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13747. if (ret != BAD_FUNC_ARG) {
  13748. flag = WOLFSSL_FATAL_ERROR;
  13749. }
  13750. }
  13751. if (!flag) {
  13752. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13753. if (ret != BAD_FUNC_ARG) {
  13754. flag = WOLFSSL_FATAL_ERROR;
  13755. }
  13756. }
  13757. if (!flag) {
  13758. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13759. if (ret != 0) {
  13760. flag = ret;
  13761. }
  13762. }
  13763. wc_HmacFree(&hmac);
  13764. res = TEST_RES_CHECK(flag == 0);
  13765. #endif
  13766. return res;
  13767. } /* END test_wc_Sha224HmacUpdate */
  13768. /*
  13769. * testing wc_HmacUpdate on SHA256 hash.
  13770. */
  13771. static int test_wc_Sha256HmacUpdate(void)
  13772. {
  13773. int res = TEST_SKIPPED;
  13774. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13775. Hmac hmac;
  13776. testVector a, b;
  13777. int ret;
  13778. int flag = 0;
  13779. #ifdef HAVE_FIPS
  13780. const char* keys =
  13781. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13782. #else
  13783. const char* keys = "Jefe";
  13784. #endif
  13785. a.input = "what do ya want for nothing?";
  13786. a.inLen = XSTRLEN(a.input);
  13787. b.input = "Hi There";
  13788. b.inLen = XSTRLEN(b.input);
  13789. flag = 0;
  13790. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13791. if (ret != 0)
  13792. return ret;
  13793. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13794. if (ret != 0) {
  13795. flag = ret;
  13796. }
  13797. if (!flag) {
  13798. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13799. if (ret != 0) {
  13800. flag = ret;
  13801. }
  13802. }
  13803. /* Update Hmac. */
  13804. if (!flag) {
  13805. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13806. if (ret != 0) {
  13807. flag = ret;
  13808. }
  13809. }
  13810. /* Test bad args. */
  13811. if (!flag) {
  13812. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13813. if (ret != BAD_FUNC_ARG) {
  13814. flag = WOLFSSL_FATAL_ERROR;
  13815. }
  13816. }
  13817. if (!flag) {
  13818. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13819. if (ret != BAD_FUNC_ARG) {
  13820. flag = WOLFSSL_FATAL_ERROR;
  13821. }
  13822. }
  13823. if (!flag) {
  13824. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13825. if (ret != 0) {
  13826. flag = ret;
  13827. }
  13828. }
  13829. wc_HmacFree(&hmac);
  13830. res = TEST_RES_CHECK(flag == 0);
  13831. #endif
  13832. return res;
  13833. } /* END test_wc_Sha256HmacUpdate */
  13834. /*
  13835. * testing wc_HmacUpdate on SHA384 hash.
  13836. */
  13837. static int test_wc_Sha384HmacUpdate(void)
  13838. {
  13839. int res = TEST_SKIPPED;
  13840. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13841. Hmac hmac;
  13842. testVector a, b;
  13843. int ret;
  13844. int flag = 0;
  13845. #ifdef HAVE_FIPS
  13846. const char* keys =
  13847. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13848. #else
  13849. const char* keys = "Jefe";
  13850. #endif
  13851. a.input = "what do ya want for nothing?";
  13852. a.inLen = XSTRLEN(a.input);
  13853. b.input = "Hi There";
  13854. b.inLen = XSTRLEN(b.input);
  13855. flag = 0;
  13856. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13857. if (ret != 0)
  13858. return ret;
  13859. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13860. if (ret != 0) {
  13861. flag = ret;
  13862. }
  13863. if (!flag) {
  13864. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13865. if (ret != 0) {
  13866. flag = ret;
  13867. }
  13868. }
  13869. /* Update Hmac. */
  13870. if (!flag) {
  13871. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13872. if (ret != 0) {
  13873. flag = ret;
  13874. }
  13875. }
  13876. /* Test bad args. */
  13877. if (!flag) {
  13878. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13879. if (ret != BAD_FUNC_ARG) {
  13880. flag = WOLFSSL_FATAL_ERROR;
  13881. }
  13882. }
  13883. if (!flag) {
  13884. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13885. if (ret != BAD_FUNC_ARG) {
  13886. flag = WOLFSSL_FATAL_ERROR;
  13887. }
  13888. }
  13889. if (!flag) {
  13890. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13891. if (ret != 0) {
  13892. flag = ret;
  13893. }
  13894. }
  13895. wc_HmacFree(&hmac);
  13896. res = TEST_RES_CHECK(flag == 0);
  13897. #endif
  13898. return res;
  13899. } /* END test_wc_Sha384HmacUpdate */
  13900. /*
  13901. * Testing wc_HmacFinal() with MD5
  13902. */
  13903. static int test_wc_Md5HmacFinal(void)
  13904. {
  13905. int res = TEST_SKIPPED;
  13906. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13907. Hmac hmac;
  13908. byte hash[WC_MD5_DIGEST_SIZE];
  13909. testVector a;
  13910. int ret;
  13911. const char* key;
  13912. int flag = 0;
  13913. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13914. a.input = "Hi There";
  13915. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13916. "\x9d";
  13917. a.inLen = XSTRLEN(a.input);
  13918. a.outLen = XSTRLEN(a.output);
  13919. flag = 0;
  13920. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13921. if (ret != 0)
  13922. return ret;
  13923. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13924. if (ret != 0) {
  13925. flag = ret;
  13926. }
  13927. if (!flag) {
  13928. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13929. if (ret != 0) {
  13930. flag = ret;
  13931. }
  13932. }
  13933. if (!flag) {
  13934. ret = wc_HmacFinal(&hmac, hash);
  13935. if (ret != 0) {
  13936. flag = ret;
  13937. }
  13938. }
  13939. if (!flag) {
  13940. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13941. flag = WOLFSSL_FATAL_ERROR;
  13942. }
  13943. }
  13944. /* Try bad parameters. */
  13945. if (!flag) {
  13946. ret = wc_HmacFinal(NULL, hash);
  13947. if (ret != BAD_FUNC_ARG) {
  13948. flag = WOLFSSL_FATAL_ERROR;
  13949. }
  13950. }
  13951. #ifndef HAVE_FIPS
  13952. if (!flag) {
  13953. ret = wc_HmacFinal(&hmac, NULL);
  13954. if (ret != BAD_FUNC_ARG) {
  13955. flag = WOLFSSL_FATAL_ERROR;
  13956. }
  13957. }
  13958. #endif
  13959. wc_HmacFree(&hmac);
  13960. res = TEST_RES_CHECK(flag == 0);
  13961. #endif
  13962. return res;
  13963. } /* END test_wc_Md5HmacFinal */
  13964. /*
  13965. * Testing wc_HmacFinal() with SHA
  13966. */
  13967. static int test_wc_ShaHmacFinal(void)
  13968. {
  13969. int res = TEST_SKIPPED;
  13970. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13971. Hmac hmac;
  13972. byte hash[WC_SHA_DIGEST_SIZE];
  13973. testVector a;
  13974. int ret;
  13975. int flag = 0;
  13976. const char* key;
  13977. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13978. "\x0b\x0b\x0b";
  13979. a.input = "Hi There";
  13980. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13981. "\x8e\xf1\x46\xbe\x00";
  13982. a.inLen = XSTRLEN(a.input);
  13983. a.outLen = XSTRLEN(a.output);
  13984. flag = 0;
  13985. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13986. if (ret != 0)
  13987. return ret;
  13988. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13989. if (ret != 0) {
  13990. flag = ret;
  13991. }
  13992. if (!flag) {
  13993. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13994. if (ret != 0) {
  13995. flag = ret;
  13996. }
  13997. }
  13998. if (!flag) {
  13999. ret = wc_HmacFinal(&hmac, hash);
  14000. if (ret != 0) {
  14001. flag = ret;
  14002. }
  14003. }
  14004. if (!flag) {
  14005. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  14006. flag = WOLFSSL_FATAL_ERROR;
  14007. }
  14008. }
  14009. /* Try bad parameters. */
  14010. if (!flag) {
  14011. ret = wc_HmacFinal(NULL, hash);
  14012. if (ret != BAD_FUNC_ARG) {
  14013. flag = WOLFSSL_FATAL_ERROR;
  14014. }
  14015. }
  14016. #ifndef HAVE_FIPS
  14017. if (!flag) {
  14018. ret = wc_HmacFinal(&hmac, NULL);
  14019. if (ret != BAD_FUNC_ARG) {
  14020. flag = WOLFSSL_FATAL_ERROR;
  14021. }
  14022. }
  14023. #endif
  14024. wc_HmacFree(&hmac);
  14025. res = TEST_RES_CHECK(flag == 0);
  14026. #endif
  14027. return res;
  14028. } /* END test_wc_ShaHmacFinal */
  14029. /*
  14030. * Testing wc_HmacFinal() with SHA224
  14031. */
  14032. static int test_wc_Sha224HmacFinal(void)
  14033. {
  14034. int res = TEST_SKIPPED;
  14035. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14036. Hmac hmac;
  14037. byte hash[WC_SHA224_DIGEST_SIZE];
  14038. testVector a;
  14039. int ret;
  14040. int flag = 0;
  14041. const char* key;
  14042. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14043. "\x0b\x0b\x0b";
  14044. a.input = "Hi There";
  14045. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14046. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14047. a.inLen = XSTRLEN(a.input);
  14048. a.outLen = XSTRLEN(a.output);
  14049. flag = 0;
  14050. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14051. if (ret != 0)
  14052. return ret;
  14053. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  14054. if (ret != 0) {
  14055. flag = ret;
  14056. }
  14057. if (!flag) {
  14058. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14059. if (ret != 0) {
  14060. flag = ret;
  14061. }
  14062. }
  14063. if (!flag) {
  14064. ret = wc_HmacFinal(&hmac, hash);
  14065. if (ret != 0) {
  14066. flag = ret;
  14067. }
  14068. }
  14069. if (!flag) {
  14070. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  14071. flag = WOLFSSL_FATAL_ERROR;
  14072. }
  14073. }
  14074. /* Try bad parameters. */
  14075. if (!flag) {
  14076. ret = wc_HmacFinal(NULL, hash);
  14077. if (ret != BAD_FUNC_ARG) {
  14078. flag = WOLFSSL_FATAL_ERROR;
  14079. }
  14080. }
  14081. #ifndef HAVE_FIPS
  14082. if (!flag) {
  14083. ret = wc_HmacFinal(&hmac, NULL);
  14084. if (ret != BAD_FUNC_ARG) {
  14085. flag = WOLFSSL_FATAL_ERROR;
  14086. }
  14087. }
  14088. #endif
  14089. wc_HmacFree(&hmac);
  14090. res = TEST_RES_CHECK(flag == 0);
  14091. #endif
  14092. return res;
  14093. } /* END test_wc_Sha224HmacFinal */
  14094. /*
  14095. * Testing wc_HmacFinal() with SHA256
  14096. */
  14097. static int test_wc_Sha256HmacFinal(void)
  14098. {
  14099. int res = TEST_SKIPPED;
  14100. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14101. Hmac hmac;
  14102. byte hash[WC_SHA256_DIGEST_SIZE];
  14103. testVector a;
  14104. int ret;
  14105. int flag = 0;
  14106. const char* key;
  14107. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14108. "\x0b\x0b\x0b";
  14109. a.input = "Hi There";
  14110. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14111. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14112. "\xcf\xf7";
  14113. a.inLen = XSTRLEN(a.input);
  14114. a.outLen = XSTRLEN(a.output);
  14115. flag = 0;
  14116. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14117. if (ret != 0)
  14118. return TEST_FAIL;
  14119. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  14120. if (ret != 0) {
  14121. flag = ret;
  14122. }
  14123. if (!flag) {
  14124. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14125. if (ret != 0) {
  14126. flag = ret;
  14127. }
  14128. }
  14129. if (!flag) {
  14130. ret = wc_HmacFinal(&hmac, hash);
  14131. if (ret != 0) {
  14132. flag = ret;
  14133. }
  14134. }
  14135. if (!flag) {
  14136. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  14137. flag = WOLFSSL_FATAL_ERROR;
  14138. }
  14139. }
  14140. /* Try bad parameters. */
  14141. if (!flag) {
  14142. ret = wc_HmacFinal(NULL, hash);
  14143. if (ret != BAD_FUNC_ARG) {
  14144. flag = WOLFSSL_FATAL_ERROR;
  14145. }
  14146. }
  14147. #ifndef HAVE_FIPS
  14148. if (!flag) {
  14149. ret = wc_HmacFinal(&hmac, NULL);
  14150. if (ret != BAD_FUNC_ARG) {
  14151. flag = WOLFSSL_FATAL_ERROR;
  14152. }
  14153. }
  14154. #endif
  14155. wc_HmacFree(&hmac);
  14156. res = TEST_RES_CHECK(flag == 0);
  14157. #endif
  14158. return res;
  14159. } /* END test_wc_Sha256HmacFinal */
  14160. /*
  14161. * Testing wc_HmacFinal() with SHA384
  14162. */
  14163. static int test_wc_Sha384HmacFinal(void)
  14164. {
  14165. int res = TEST_SKIPPED;
  14166. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14167. Hmac hmac;
  14168. byte hash[WC_SHA384_DIGEST_SIZE];
  14169. testVector a;
  14170. int ret;
  14171. int flag = 0;
  14172. const char* key;
  14173. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14174. "\x0b\x0b\x0b";
  14175. a.input = "Hi There";
  14176. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14177. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14178. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14179. "\xfa\x9c\xb6";
  14180. a.inLen = XSTRLEN(a.input);
  14181. a.outLen = XSTRLEN(a.output);
  14182. flag = 0;
  14183. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  14184. if (ret != 0)
  14185. return ret;
  14186. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  14187. if (ret != 0) {
  14188. flag = ret;
  14189. }
  14190. if (!flag) {
  14191. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  14192. if (ret != 0) {
  14193. flag = ret;
  14194. }
  14195. }
  14196. if (!flag) {
  14197. ret = wc_HmacFinal(&hmac, hash);
  14198. if (ret != 0) {
  14199. flag = ret;
  14200. }
  14201. }
  14202. if (!flag) {
  14203. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  14204. flag = WOLFSSL_FATAL_ERROR;
  14205. }
  14206. }
  14207. /* Try bad parameters. */
  14208. if (!flag) {
  14209. ret = wc_HmacFinal(NULL, hash);
  14210. if (ret != BAD_FUNC_ARG) {
  14211. flag = WOLFSSL_FATAL_ERROR;
  14212. }
  14213. }
  14214. #ifndef HAVE_FIPS
  14215. if (!flag) {
  14216. ret = wc_HmacFinal(&hmac, NULL);
  14217. if (ret != BAD_FUNC_ARG) {
  14218. flag = WOLFSSL_FATAL_ERROR;
  14219. }
  14220. }
  14221. #endif
  14222. wc_HmacFree(&hmac);
  14223. res = TEST_RES_CHECK(flag == 0);
  14224. #endif
  14225. return res;
  14226. } /* END test_wc_Sha384HmacFinal */
  14227. /*
  14228. * Testing wc_InitCmac()
  14229. */
  14230. static int test_wc_InitCmac(void)
  14231. {
  14232. int res = TEST_SKIPPED;
  14233. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14234. Cmac cmac1, cmac2, cmac3;
  14235. /* AES 128 key. */
  14236. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14237. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14238. /* AES 192 key. */
  14239. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14240. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14241. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14242. /* AES 256 key. */
  14243. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14244. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14245. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14246. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14247. word32 key1Sz = (word32)sizeof(key1) - 1;
  14248. word32 key2Sz = (word32)sizeof(key2) - 1;
  14249. word32 key3Sz = (word32)sizeof(key3) - 1;
  14250. int type = WC_CMAC_AES;
  14251. int ret = 0;
  14252. #ifdef WOLFSSL_AES_128
  14253. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  14254. #endif
  14255. #ifdef WOLFSSL_AES_192
  14256. if (ret == 0) {
  14257. wc_AesFree(&cmac1.aes);
  14258. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  14259. }
  14260. #endif
  14261. #ifdef WOLFSSL_AES_256
  14262. if (ret == 0) {
  14263. wc_AesFree(&cmac2.aes);
  14264. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  14265. }
  14266. #endif
  14267. /* Test bad args. */
  14268. if (ret == 0) {
  14269. wc_AesFree(&cmac3.aes);
  14270. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  14271. if (ret == BAD_FUNC_ARG) {
  14272. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  14273. }
  14274. if (ret == BAD_FUNC_ARG) {
  14275. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  14276. }
  14277. if (ret == BAD_FUNC_ARG) {
  14278. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  14279. }
  14280. if (ret == BAD_FUNC_ARG) {
  14281. ret = 0;
  14282. }
  14283. else {
  14284. ret = WOLFSSL_FATAL_ERROR;
  14285. }
  14286. }
  14287. (void)key1;
  14288. (void)key1Sz;
  14289. (void)key2;
  14290. (void)key2Sz;
  14291. (void)cmac1;
  14292. (void)cmac2;
  14293. res = TEST_RES_CHECK(ret == 0);
  14294. #endif
  14295. return res;
  14296. } /* END test_wc_InitCmac */
  14297. /*
  14298. * Testing wc_CmacUpdate()
  14299. */
  14300. static int test_wc_CmacUpdate(void)
  14301. {
  14302. int res = TEST_SKIPPED;
  14303. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14304. Cmac cmac;
  14305. byte key[] =
  14306. {
  14307. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14308. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14309. };
  14310. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14311. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14312. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14313. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14314. "\xf4";
  14315. word32 inSz = (word32)sizeof(in) - 1;
  14316. word32 keySz = (word32)sizeof(key);
  14317. int type = WC_CMAC_AES;
  14318. int ret = 0;
  14319. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14320. if (ret != 0) {
  14321. return ret;
  14322. }
  14323. ret = wc_CmacUpdate(&cmac, in, inSz);
  14324. /* Test bad args. */
  14325. if (ret == 0) {
  14326. ret = wc_CmacUpdate(NULL, in, inSz);
  14327. if (ret == BAD_FUNC_ARG) {
  14328. ret = wc_CmacUpdate(&cmac, NULL, 30);
  14329. }
  14330. if (ret == BAD_FUNC_ARG) {
  14331. ret = 0;
  14332. }
  14333. else if (ret == 0) {
  14334. ret = WOLFSSL_FATAL_ERROR;
  14335. }
  14336. wc_AesFree(&cmac.aes);
  14337. }
  14338. res = TEST_RES_CHECK(ret == 0);
  14339. #endif
  14340. return res;
  14341. } /* END test_wc_CmacUpdate */
  14342. /*
  14343. * Testing wc_CmacFinal()
  14344. */
  14345. static int test_wc_CmacFinal(void)
  14346. {
  14347. int res = TEST_SKIPPED;
  14348. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14349. Cmac cmac;
  14350. byte key[] =
  14351. {
  14352. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14353. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14354. };
  14355. byte msg[] =
  14356. {
  14357. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14358. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14359. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14360. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14361. 0xf4
  14362. };
  14363. /* Test vectors from CMACGenAES128.rsp from
  14364. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14365. * Per RFC4493 truncation of lsb is possible.
  14366. */
  14367. byte expMac[] =
  14368. {
  14369. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14370. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14371. };
  14372. byte mac[AES_BLOCK_SIZE];
  14373. word32 msgSz = (word32)sizeof(msg);
  14374. word32 keySz = (word32)sizeof(key);
  14375. word32 macSz = sizeof(mac);
  14376. word32 badMacSz = 17;
  14377. int expMacSz = sizeof(expMac);
  14378. int type = WC_CMAC_AES;
  14379. int ret = 0;
  14380. XMEMSET(mac, 0, macSz);
  14381. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14382. if (ret != 0) {
  14383. return ret;
  14384. }
  14385. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14386. if (ret == 0) {
  14387. ret = wc_CmacFinal(&cmac, mac, &macSz);
  14388. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  14389. ret = WOLFSSL_FATAL_ERROR;
  14390. }
  14391. /* Pass in bad args. */
  14392. if (ret == 0) {
  14393. ret = wc_CmacFinal(NULL, mac, &macSz);
  14394. if (ret == BAD_FUNC_ARG) {
  14395. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  14396. }
  14397. if (ret == BAD_FUNC_ARG) {
  14398. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  14399. if (ret == BUFFER_E) {
  14400. ret = 0;
  14401. }
  14402. }
  14403. else if (ret == 0) {
  14404. ret = WOLFSSL_FATAL_ERROR;
  14405. }
  14406. }
  14407. }
  14408. res = TEST_RES_CHECK(ret == 0);
  14409. #endif
  14410. return res;
  14411. } /* END test_wc_CmacFinal */
  14412. /*
  14413. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14414. */
  14415. static int test_wc_AesCmacGenerate(void)
  14416. {
  14417. int res = TEST_SKIPPED;
  14418. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14419. Cmac cmac;
  14420. byte key[] =
  14421. {
  14422. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14423. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14424. };
  14425. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14426. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14427. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14428. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14429. byte mac[AES_BLOCK_SIZE];
  14430. word32 keySz = sizeof(key);
  14431. word32 macSz = sizeof(mac);
  14432. word32 msgSz = sizeof(msg) - 1;
  14433. word32 expMacSz = sizeof(expMac) - 1;
  14434. int type = WC_CMAC_AES;
  14435. int ret = 0;
  14436. XMEMSET(mac, 0, macSz);
  14437. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  14438. if (ret != 0) {
  14439. return ret;
  14440. }
  14441. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  14442. if (ret != 0) {
  14443. return ret;
  14444. }
  14445. else {
  14446. wc_AesFree(&cmac.aes);
  14447. }
  14448. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  14449. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  14450. ret = WOLFSSL_FATAL_ERROR;
  14451. }
  14452. /* Pass in bad args. */
  14453. if (ret == 0) {
  14454. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  14455. if (ret == BAD_FUNC_ARG) {
  14456. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  14457. }
  14458. if (ret == BAD_FUNC_ARG) {
  14459. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  14460. }
  14461. if (ret == BAD_FUNC_ARG) {
  14462. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  14463. }
  14464. if (ret == BAD_FUNC_ARG) {
  14465. ret = 0;
  14466. }
  14467. else if (ret == 0) {
  14468. ret = WOLFSSL_FATAL_ERROR;
  14469. }
  14470. }
  14471. if (ret == 0) {
  14472. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  14473. /* Test bad args. */
  14474. if (ret == 0) {
  14475. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  14476. if (ret == BAD_FUNC_ARG) {
  14477. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  14478. }
  14479. if (ret == BAD_FUNC_ARG) {
  14480. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  14481. }
  14482. if (ret == BAD_FUNC_ARG) {
  14483. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  14484. }
  14485. if (ret == BAD_FUNC_ARG) {
  14486. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  14487. }
  14488. if (ret == BAD_FUNC_ARG) {
  14489. ret = 0;
  14490. }
  14491. else if (ret == 0) {
  14492. ret = WOLFSSL_FATAL_ERROR;
  14493. }
  14494. }
  14495. }
  14496. res = TEST_RES_CHECK(ret == 0);
  14497. #endif
  14498. return res;
  14499. } /* END test_wc_AesCmacGenerate */
  14500. /*
  14501. * Testing streaming AES-GCM API.
  14502. */
  14503. static int test_wc_AesGcmStream(void)
  14504. {
  14505. int res = TEST_SKIPPED;
  14506. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14507. defined(WOLFSSL_AESGCM_STREAM)
  14508. int ret = 0;
  14509. int i;
  14510. WC_RNG rng[1];
  14511. Aes aesEnc[1];
  14512. Aes aesDec[1];
  14513. byte tag[AES_BLOCK_SIZE];
  14514. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14515. byte out[AES_BLOCK_SIZE * 3 + 2];
  14516. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14517. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14518. byte key[AES_128_KEY_SIZE] = { 0, };
  14519. byte iv[AES_IV_SIZE] = { 1, };
  14520. byte ivOut[AES_IV_SIZE];
  14521. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14522. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14523. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14524. };
  14525. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14526. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14527. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14528. };
  14529. static const byte expTag[AES_BLOCK_SIZE] = {
  14530. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14531. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14532. };
  14533. /* Create a random for generating IV/nonce. */
  14534. AssertIntEQ(wc_InitRng(rng), 0);
  14535. /* Initialize data structures. */
  14536. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14537. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14538. /* BadParameters to streaming init. */
  14539. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14540. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14541. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14542. BAD_FUNC_ARG);
  14543. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14544. BAD_FUNC_ARG);
  14545. /* Bad parameters to encrypt update. */
  14546. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14547. BAD_FUNC_ARG);
  14548. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14549. BAD_FUNC_ARG);
  14550. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14551. BAD_FUNC_ARG);
  14552. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14553. BAD_FUNC_ARG);
  14554. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14555. BAD_FUNC_ARG);
  14556. /* Bad parameters to decrypt update. */
  14557. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14558. BAD_FUNC_ARG);
  14559. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14560. BAD_FUNC_ARG);
  14561. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14562. BAD_FUNC_ARG);
  14563. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14564. BAD_FUNC_ARG);
  14565. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14566. BAD_FUNC_ARG);
  14567. /* Bad parameters to encrypt final. */
  14568. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14569. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14570. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14571. BAD_FUNC_ARG);
  14572. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14573. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14574. BAD_FUNC_ARG);
  14575. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14576. BAD_FUNC_ARG);
  14577. /* Bad parameters to decrypt final. */
  14578. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14579. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14580. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14581. BAD_FUNC_ARG);
  14582. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14583. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14584. BAD_FUNC_ARG);
  14585. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14586. BAD_FUNC_ARG);
  14587. /* Check calling final before setting key fails. */
  14588. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14589. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14590. /* Check calling update before setting key else fails. */
  14591. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14592. MISSING_KEY);
  14593. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14594. MISSING_KEY);
  14595. /* Set key but not IV. */
  14596. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14597. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14598. /* Check calling final before setting IV fails. */
  14599. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14600. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14601. /* Check calling update before setting IV else fails. */
  14602. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14603. MISSING_IV);
  14604. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14605. MISSING_IV);
  14606. /* Set IV using fixed part IV and external IV APIs. */
  14607. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14608. rng), 0);
  14609. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14610. GCM_NONCE_MID_SZ), 0);
  14611. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14612. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14613. /* Encrypt and decrypt data. */
  14614. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14615. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14616. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14617. /* Finalize and check tag matches. */
  14618. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14619. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14620. /* Set key and IV through streaming init API. */
  14621. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14622. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14623. /* Encrypt/decrypt one block and AAD of one block. */
  14624. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14625. AES_BLOCK_SIZE), 0);
  14626. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14627. AES_BLOCK_SIZE), 0);
  14628. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14629. /* Finalize and check tag matches. */
  14630. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14631. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14632. /* Set key and IV through streaming init API. */
  14633. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14634. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14635. /* No data to encrypt/decrypt one byte of AAD. */
  14636. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14637. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14638. /* Finalize and check tag matches. */
  14639. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14640. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14641. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14642. /* Set key and IV through streaming init API. */
  14643. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14644. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14645. /* Encrypt/decrypt one byte and no AAD. */
  14646. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14647. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14648. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  14649. /* Finalize and check tag matches. */
  14650. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14651. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14652. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14653. /* Set key and IV through streaming init API. */
  14654. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14655. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14656. /* Encryption AES is one byte at a time */
  14657. for (i = 0; i < (int)sizeof(aad); i++) {
  14658. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14659. 0);
  14660. }
  14661. for (i = 0; i < (int)sizeof(in); i++) {
  14662. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14663. 0);
  14664. }
  14665. /* Decryption AES is two bytes at a time */
  14666. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14667. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14668. 0);
  14669. }
  14670. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14671. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14672. 0), 0);
  14673. }
  14674. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14675. /* Finalize and check tag matches. */
  14676. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14677. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14678. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14679. /* Check streaming encryption can be decrypted with one shot. */
  14680. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14681. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14682. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14683. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14684. wc_AesFree(aesEnc);
  14685. wc_AesFree(aesDec);
  14686. wc_FreeRng(rng);
  14687. res = TEST_RES_CHECK(ret == 0);
  14688. #endif
  14689. return res;
  14690. } /* END test_wc_AesGcmStream */
  14691. /*
  14692. * unit test for wc_Des3_SetIV()
  14693. */
  14694. static int test_wc_Des3_SetIV(void)
  14695. {
  14696. int res = TEST_SKIPPED;
  14697. #ifndef NO_DES3
  14698. Des3 des;
  14699. int ret = 0;
  14700. const byte key[] =
  14701. {
  14702. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14703. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14704. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14705. };
  14706. const byte iv[] =
  14707. {
  14708. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14709. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14710. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14711. };
  14712. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14713. if (ret != 0)
  14714. return ret;
  14715. /* DES_ENCRYPTION or DES_DECRYPTION */
  14716. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14717. if (ret == 0) {
  14718. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14719. ret = WOLFSSL_FATAL_ERROR;
  14720. }
  14721. }
  14722. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14723. /* Test explicitly wc_Des3_SetIV() */
  14724. if (ret == 0) {
  14725. ret = wc_Des3_SetIV(NULL, iv);
  14726. if (ret == BAD_FUNC_ARG) {
  14727. ret = wc_Des3_SetIV(&des, NULL);
  14728. }
  14729. else if (ret == 0) {
  14730. ret = WOLFSSL_FATAL_ERROR;
  14731. }
  14732. }
  14733. #endif
  14734. wc_Des3Free(&des);
  14735. res = TEST_RES_CHECK(ret == 0);
  14736. #endif
  14737. return res;
  14738. } /* END test_wc_Des3_SetIV */
  14739. /*
  14740. * unit test for wc_Des3_SetKey()
  14741. */
  14742. static int test_wc_Des3_SetKey(void)
  14743. {
  14744. int res = TEST_SKIPPED;
  14745. #ifndef NO_DES3
  14746. Des3 des;
  14747. int ret = 0;
  14748. const byte key[] =
  14749. {
  14750. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14751. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14752. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14753. };
  14754. const byte iv[] =
  14755. {
  14756. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14757. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14758. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14759. };
  14760. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14761. if (ret != 0)
  14762. return ret;
  14763. /* DES_ENCRYPTION or DES_DECRYPTION */
  14764. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14765. if (ret == 0) {
  14766. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  14767. ret = WOLFSSL_FATAL_ERROR;
  14768. }
  14769. }
  14770. /* Test bad args. */
  14771. if (ret == 0) {
  14772. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  14773. if (ret == BAD_FUNC_ARG) {
  14774. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  14775. }
  14776. if (ret == BAD_FUNC_ARG) {
  14777. ret = wc_Des3_SetKey(&des, key, iv, -1);
  14778. }
  14779. if (ret == BAD_FUNC_ARG) {
  14780. /* Default case. Should return 0. */
  14781. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  14782. }
  14783. } /* END if ret != 0 */
  14784. wc_Des3Free(&des);
  14785. res = TEST_RES_CHECK(ret == 0);
  14786. #endif
  14787. return res;
  14788. } /* END test_wc_Des3_SetKey */
  14789. /*
  14790. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14791. */
  14792. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14793. {
  14794. int res = TEST_SKIPPED;
  14795. #ifndef NO_DES3
  14796. Des3 des;
  14797. int ret = 0;
  14798. byte cipher[24];
  14799. byte plain[24];
  14800. const byte key[] =
  14801. {
  14802. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14803. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14804. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14805. };
  14806. const byte iv[] =
  14807. {
  14808. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14809. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14810. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14811. };
  14812. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14813. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14814. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14815. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14816. };
  14817. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14818. if (ret != 0)
  14819. return ret;
  14820. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14821. if (ret == 0) {
  14822. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14823. if (ret == 0) {
  14824. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14825. }
  14826. if (ret == 0) {
  14827. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14828. }
  14829. }
  14830. if (ret == 0) {
  14831. if (XMEMCMP(plain, vector, 24) != 0) {
  14832. ret = WOLFSSL_FATAL_ERROR;
  14833. }
  14834. }
  14835. /* Pass in bad args. */
  14836. if (ret == 0) {
  14837. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14838. if (ret == BAD_FUNC_ARG) {
  14839. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14840. }
  14841. if (ret == BAD_FUNC_ARG) {
  14842. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14843. }
  14844. if (ret != BAD_FUNC_ARG) {
  14845. ret = WOLFSSL_FATAL_ERROR;
  14846. }
  14847. else {
  14848. ret = 0;
  14849. }
  14850. }
  14851. if (ret == 0) {
  14852. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14853. if (ret == BAD_FUNC_ARG) {
  14854. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14855. }
  14856. if (ret == BAD_FUNC_ARG) {
  14857. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14858. }
  14859. if (ret != BAD_FUNC_ARG) {
  14860. ret = WOLFSSL_FATAL_ERROR;
  14861. }
  14862. else {
  14863. ret = 0;
  14864. }
  14865. }
  14866. wc_Des3Free(&des);
  14867. res = TEST_RES_CHECK(ret == 0);
  14868. #endif
  14869. return res;
  14870. } /* END wc_Des3_CbcEncrypt */
  14871. /*
  14872. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14873. */
  14874. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14875. {
  14876. int res = TEST_SKIPPED;
  14877. #ifndef NO_DES3
  14878. int ret = 0;
  14879. word32 vectorSz, cipherSz;
  14880. byte cipher[24];
  14881. byte plain[24];
  14882. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14883. {
  14884. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14885. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14886. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14887. };
  14888. byte key[] =
  14889. {
  14890. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14891. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14892. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14893. };
  14894. byte iv[] =
  14895. {
  14896. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14897. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14898. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14899. };
  14900. vectorSz = sizeof(byte) * 24;
  14901. cipherSz = sizeof(byte) * 24;
  14902. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14903. if (ret == 0) {
  14904. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14905. if (ret == 0) {
  14906. if (XMEMCMP(plain, vector, 24) != 0) {
  14907. ret = WOLFSSL_FATAL_ERROR;
  14908. }
  14909. }
  14910. }
  14911. /* pass in bad args. */
  14912. if (ret == 0) {
  14913. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14914. if (ret == BAD_FUNC_ARG) {
  14915. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14916. }
  14917. if (ret == BAD_FUNC_ARG) {
  14918. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14919. }
  14920. if (ret == BAD_FUNC_ARG) {
  14921. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14922. key, NULL);
  14923. }
  14924. else {
  14925. /* Return code catch. */
  14926. ret = WOLFSSL_FAILURE;
  14927. }
  14928. }
  14929. if (ret == 0) {
  14930. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14931. if (ret == BAD_FUNC_ARG) {
  14932. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14933. }
  14934. if (ret == BAD_FUNC_ARG) {
  14935. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14936. }
  14937. if (ret == BAD_FUNC_ARG) {
  14938. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14939. }
  14940. else {
  14941. ret = WOLFSSL_FAILURE;
  14942. }
  14943. }
  14944. res = TEST_RES_CHECK(ret == 0);
  14945. #endif
  14946. return res;
  14947. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14948. /*
  14949. * Unit test for wc_Des3_EcbEncrypt
  14950. */
  14951. static int test_wc_Des3_EcbEncrypt(void)
  14952. {
  14953. int res = TEST_SKIPPED;
  14954. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14955. Des3 des;
  14956. int ret = 0;
  14957. byte cipher[24];
  14958. word32 cipherSz = sizeof(cipher);
  14959. const byte key[] =
  14960. {
  14961. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14962. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14963. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14964. };
  14965. const byte iv[] =
  14966. {
  14967. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14968. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14969. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14970. };
  14971. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14972. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14973. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14974. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14975. };
  14976. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14977. if (ret != 0) {
  14978. return ret;
  14979. }
  14980. if (ret == 0 ) {
  14981. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14982. }
  14983. /* Bad Cases */
  14984. if (ret == 0) {
  14985. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14986. if (ret == BAD_FUNC_ARG) {
  14987. ret = 0;
  14988. }
  14989. }
  14990. if (ret == 0) {
  14991. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14992. if (ret == BAD_FUNC_ARG) {
  14993. ret = 0;
  14994. }
  14995. }
  14996. if (ret == 0) {
  14997. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14998. if (ret == BAD_FUNC_ARG) {
  14999. ret = 0;
  15000. }
  15001. }
  15002. if (ret == 0) {
  15003. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  15004. if (ret == BAD_FUNC_ARG) {
  15005. ret = 0;
  15006. }
  15007. }
  15008. if (ret == 0) {
  15009. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  15010. if (ret == BAD_FUNC_ARG) {
  15011. ret = 0;
  15012. }
  15013. }
  15014. /* Good Cases */
  15015. if (ret == 0) {
  15016. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  15017. }
  15018. wc_Des3Free(&des);
  15019. res = TEST_RES_CHECK(ret == 0);
  15020. #endif
  15021. return res;
  15022. } /* END test_wc_Des3_EcbEncrypt */
  15023. /*
  15024. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15025. */
  15026. static int test_wc_Chacha_SetKey(void)
  15027. {
  15028. int res = TEST_SKIPPED;
  15029. #ifdef HAVE_CHACHA
  15030. ChaCha ctx;
  15031. const byte key[] =
  15032. {
  15033. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15035. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15036. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15037. };
  15038. byte cipher[128];
  15039. int ret = 0;
  15040. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15041. /* Test bad args. */
  15042. if (ret == 0) {
  15043. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15044. if (ret == BAD_FUNC_ARG) {
  15045. ret = wc_Chacha_SetKey(&ctx, key, 18);
  15046. }
  15047. if (ret == BAD_FUNC_ARG) {
  15048. ret = 0;
  15049. }
  15050. else {
  15051. ret = WOLFSSL_FATAL_ERROR;
  15052. }
  15053. }
  15054. if (ret == 0) {
  15055. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  15056. }
  15057. if (ret == 0) {
  15058. /* Test bad args. */
  15059. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  15060. if (ret == BAD_FUNC_ARG) {
  15061. ret = 0;
  15062. }
  15063. else {
  15064. ret = WOLFSSL_FAILURE;
  15065. }
  15066. }
  15067. res = TEST_RES_CHECK(ret == 0);
  15068. #endif
  15069. return res;
  15070. } /* END test_wc_Chacha_SetKey */
  15071. /*
  15072. * unit test for wc_Poly1305SetKey()
  15073. */
  15074. static int test_wc_Poly1305SetKey(void)
  15075. {
  15076. int res = TEST_SKIPPED;
  15077. #ifdef HAVE_POLY1305
  15078. Poly1305 ctx;
  15079. const byte key[] =
  15080. {
  15081. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15082. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15083. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15084. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15085. };
  15086. int ret = 0;
  15087. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  15088. /* Test bad args. */
  15089. if (ret == 0) {
  15090. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  15091. if (ret == BAD_FUNC_ARG) {
  15092. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  15093. }
  15094. if (ret == BAD_FUNC_ARG) {
  15095. ret = wc_Poly1305SetKey(&ctx, key, 18);
  15096. }
  15097. if (ret == BAD_FUNC_ARG) {
  15098. ret = 0;
  15099. }
  15100. else {
  15101. ret = WOLFSSL_FATAL_ERROR;
  15102. }
  15103. }
  15104. res = TEST_RES_CHECK(ret == 0);
  15105. #endif
  15106. return res;
  15107. } /* END test_wc_Poly1305_SetKey() */
  15108. /*
  15109. * Testing wc_Chacha_Process()
  15110. */
  15111. static int test_wc_Chacha_Process(void)
  15112. {
  15113. int res = TEST_SKIPPED;
  15114. #ifdef HAVE_CHACHA
  15115. ChaCha enc, dec;
  15116. byte cipher[128];
  15117. byte plain[128];
  15118. const byte key[] =
  15119. {
  15120. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15123. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15124. };
  15125. const char* input = "Everybody gets Friday off.";
  15126. word32 keySz = sizeof(key)/sizeof(byte);
  15127. unsigned long int inlen = XSTRLEN(input);
  15128. int ret = 0;
  15129. /*Initialize stack varialbes.*/
  15130. XMEMSET(cipher, 0, 128);
  15131. XMEMSET(plain, 0, 128);
  15132. ret = wc_Chacha_SetKey(&enc, key, keySz);
  15133. AssertIntEQ(ret, 0);
  15134. ret = wc_Chacha_SetKey(&dec, key, keySz);
  15135. AssertIntEQ(ret, 0);
  15136. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15137. AssertIntEQ(ret, 0);
  15138. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15139. AssertIntEQ(ret, 0);
  15140. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  15141. AssertIntEQ(ret, 0);
  15142. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  15143. AssertIntEQ(ret, 0);
  15144. ret = XMEMCMP(input, plain, (int)inlen);
  15145. AssertIntEQ(ret, 0);
  15146. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15147. /* test checking and using leftovers, currently just in C code */
  15148. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  15149. AssertIntEQ(ret, 0);
  15150. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  15151. AssertIntEQ(ret, 0);
  15152. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  15153. AssertIntEQ(ret, 0);
  15154. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15155. (byte*)input + (inlen - 2), 2);
  15156. AssertIntEQ(ret, 0);
  15157. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  15158. AssertIntEQ(ret, 0);
  15159. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15160. (byte*)input + (inlen - 2), 2);
  15161. AssertIntEQ(ret, 0);
  15162. ret = XMEMCMP(input, plain, (int)inlen);
  15163. AssertIntEQ(ret, 0);
  15164. /* check edge cases with counter increment */
  15165. {
  15166. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15167. const byte expected[] = {
  15168. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15169. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15170. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15171. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15172. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15173. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15174. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15175. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15176. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15177. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15178. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15179. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15180. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15181. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15182. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15183. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15184. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15185. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15186. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15187. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15188. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15189. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15190. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15191. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15192. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15193. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15194. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15195. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15196. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15197. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15198. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15199. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15200. };
  15201. const byte iv2[] = {
  15202. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15203. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15204. };
  15205. byte input2[256];
  15206. int i;
  15207. for (i = 0; i < 256; i++)
  15208. input2[i] = i;
  15209. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  15210. AssertIntEQ(ret, 0);
  15211. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  15212. AssertIntEQ(ret, 0);
  15213. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15214. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  15215. AssertIntEQ(ret, 0);
  15216. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15217. /* partial */
  15218. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  15219. AssertIntEQ(ret, 0);
  15220. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15221. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  15222. AssertIntEQ(ret, 0);
  15223. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15224. }
  15225. #endif
  15226. /* Test bad args. */
  15227. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  15228. AssertIntEQ(ret, BAD_FUNC_ARG);
  15229. if (ret == BAD_FUNC_ARG) {
  15230. ret = 0;
  15231. }
  15232. res = TEST_RES_CHECK(ret == 0);
  15233. #endif
  15234. return res;
  15235. } /* END test_wc_Chacha_Process */
  15236. /*
  15237. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15238. */
  15239. static int test_wc_ChaCha20Poly1305_aead(void)
  15240. {
  15241. int res = TEST_SKIPPED;
  15242. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15243. const byte key[] = {
  15244. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15245. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15246. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15247. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15248. };
  15249. const byte plaintext[] = {
  15250. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15251. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15252. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15253. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15254. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15255. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15256. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15257. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15258. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15259. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15260. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15261. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15262. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15263. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15264. 0x74, 0x2e
  15265. };
  15266. const byte iv[] = {
  15267. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15268. 0x44, 0x45, 0x46, 0x47
  15269. };
  15270. const byte aad[] = { /* additional data */
  15271. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15272. 0xc4, 0xc5, 0xc6, 0xc7
  15273. };
  15274. const byte cipher[] = { /* expected output from operation */
  15275. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15276. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15277. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15278. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15279. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15280. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15281. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15282. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15283. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15284. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15285. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15286. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15287. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15288. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15289. 0x61, 0x16
  15290. };
  15291. const byte authTag[] = { /* expected output from operation */
  15292. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15293. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15294. };
  15295. byte generatedCiphertext[272];
  15296. byte generatedPlaintext[272];
  15297. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15298. int ret = 0;
  15299. /* Initialize stack variables. */
  15300. XMEMSET(generatedCiphertext, 0, 272);
  15301. XMEMSET(generatedPlaintext, 0, 272);
  15302. /* Test Encrypt */
  15303. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  15304. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15305. AssertIntEQ(ret, 0);
  15306. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  15307. AssertIntEQ(ret, 0);
  15308. /* Test bad args. */
  15309. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  15310. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15311. AssertIntEQ(ret, BAD_FUNC_ARG);
  15312. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15313. plaintext, sizeof(plaintext),
  15314. generatedCiphertext, generatedAuthTag);
  15315. AssertIntEQ(ret, BAD_FUNC_ARG);
  15316. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15317. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15318. AssertIntEQ(ret, BAD_FUNC_ARG);
  15319. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15320. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  15321. AssertIntEQ(ret, BAD_FUNC_ARG);
  15322. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15323. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  15324. AssertIntEQ(ret, BAD_FUNC_ARG);
  15325. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15326. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  15327. if (ret == BAD_FUNC_ARG) {
  15328. ret = 0;
  15329. (void)ret; /* suppress never read */
  15330. }
  15331. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15332. sizeof(cipher), authTag, generatedPlaintext);
  15333. AssertIntEQ(ret, 0);
  15334. ret = XMEMCMP(generatedPlaintext, plaintext,
  15335. sizeof(plaintext)/sizeof(byte));
  15336. AssertIntEQ(ret, 0);
  15337. /* Test bad args. */
  15338. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15339. sizeof(cipher), authTag, generatedPlaintext);
  15340. AssertIntEQ(ret, BAD_FUNC_ARG);
  15341. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15342. cipher, sizeof(cipher), authTag, generatedPlaintext);
  15343. AssertIntEQ(ret, BAD_FUNC_ARG);
  15344. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15345. sizeof(cipher), authTag, generatedPlaintext);
  15346. AssertIntEQ(ret, BAD_FUNC_ARG);
  15347. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15348. sizeof(cipher), NULL, generatedPlaintext);
  15349. AssertIntEQ(ret, BAD_FUNC_ARG);
  15350. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15351. sizeof(cipher), authTag, NULL);
  15352. AssertIntEQ(ret, BAD_FUNC_ARG);
  15353. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15354. sizeof(cipher), authTag, generatedPlaintext);
  15355. AssertIntEQ(ret, BAD_FUNC_ARG);
  15356. if (ret == BAD_FUNC_ARG) {
  15357. ret = 0;
  15358. }
  15359. res = TEST_RES_CHECK(ret == 0);
  15360. #endif
  15361. return res;
  15362. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  15363. /*
  15364. * Testing function for wc_Rc2SetKey().
  15365. */
  15366. static int test_wc_Rc2SetKey(void)
  15367. {
  15368. int res = TEST_SKIPPED;
  15369. #ifdef WC_RC2
  15370. Rc2 rc2;
  15371. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15372. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15373. int ret = 0;
  15374. /* valid key and IV */
  15375. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15376. iv, 40);
  15377. if (ret == 0) {
  15378. /* valid key, no IV */
  15379. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15380. NULL, 40);
  15381. }
  15382. /* bad arguments */
  15383. if (ret == 0) {
  15384. /* null Rc2 struct */
  15385. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15386. iv, 40);
  15387. if (ret == BAD_FUNC_ARG) {
  15388. ret = 0;
  15389. }
  15390. }
  15391. if (ret == 0) {
  15392. /* null key */
  15393. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15394. iv, 40);
  15395. if (ret == BAD_FUNC_ARG) {
  15396. ret = 0;
  15397. }
  15398. }
  15399. if (ret == 0) {
  15400. /* key size == 0 */
  15401. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  15402. if (ret == WC_KEY_SIZE_E) {
  15403. ret = 0;
  15404. }
  15405. }
  15406. if (ret == 0) {
  15407. /* key size > 128 */
  15408. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  15409. if (ret == WC_KEY_SIZE_E) {
  15410. ret = 0;
  15411. }
  15412. }
  15413. if (ret == 0) {
  15414. /* effective bits == 0 */
  15415. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15416. iv, 0);
  15417. if (ret == WC_KEY_SIZE_E) {
  15418. ret = 0;
  15419. }
  15420. }
  15421. if (ret == 0) {
  15422. /* effective bits > 1024 */
  15423. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15424. iv, 1025);
  15425. if (ret == WC_KEY_SIZE_E) {
  15426. ret = 0;
  15427. }
  15428. }
  15429. res = TEST_RES_CHECK(ret == 0);
  15430. #endif
  15431. return res;
  15432. } /* END test_wc_Rc2SetKey */
  15433. /*
  15434. * Testing function for wc_Rc2SetIV().
  15435. */
  15436. static int test_wc_Rc2SetIV(void)
  15437. {
  15438. int res = TEST_SKIPPED;
  15439. #ifdef WC_RC2
  15440. Rc2 rc2;
  15441. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15442. int ret = 0;
  15443. /* valid IV */
  15444. ret = wc_Rc2SetIV(&rc2, iv);
  15445. if (ret == 0) {
  15446. /* valid NULL IV */
  15447. ret = wc_Rc2SetIV(&rc2, NULL);
  15448. }
  15449. /* bad arguments */
  15450. if (ret == 0) {
  15451. ret = wc_Rc2SetIV(NULL, iv);
  15452. if (ret == BAD_FUNC_ARG) {
  15453. ret = 0;
  15454. }
  15455. }
  15456. res = TEST_RES_CHECK(ret == 0);
  15457. #endif
  15458. return res;
  15459. } /* END test_wc_Rc2SetKey */
  15460. /*
  15461. * Testing function for wc_Rc2EcbEncrypt().
  15462. */
  15463. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15464. {
  15465. int res = TEST_SKIPPED;
  15466. #ifdef WC_RC2
  15467. Rc2 rc2;
  15468. int ret = 0;
  15469. int effectiveKeyBits = 63;
  15470. byte cipher[RC2_BLOCK_SIZE];
  15471. byte plain[RC2_BLOCK_SIZE];
  15472. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15473. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15474. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15475. XMEMSET(cipher, 0, sizeof(cipher));
  15476. XMEMSET(plain, 0, sizeof(plain));
  15477. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15478. NULL, effectiveKeyBits);
  15479. if (ret == 0) {
  15480. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  15481. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  15482. ret = WOLFSSL_FATAL_ERROR;
  15483. }
  15484. if (ret == 0) {
  15485. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  15486. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  15487. ret = WOLFSSL_FATAL_ERROR;
  15488. }
  15489. }
  15490. }
  15491. /* Rc2EcbEncrypt bad arguments */
  15492. if (ret == 0) {
  15493. /* null Rc2 struct */
  15494. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  15495. if (ret == BAD_FUNC_ARG) {
  15496. ret = 0;
  15497. }
  15498. }
  15499. if (ret == 0) {
  15500. /* null out buffer */
  15501. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  15502. if (ret == BAD_FUNC_ARG) {
  15503. ret = 0;
  15504. }
  15505. }
  15506. if (ret == 0) {
  15507. /* null input buffer */
  15508. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  15509. if (ret == BAD_FUNC_ARG) {
  15510. ret = 0;
  15511. }
  15512. }
  15513. if (ret == 0) {
  15514. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15515. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  15516. if (ret == BUFFER_E) {
  15517. ret = 0;
  15518. }
  15519. }
  15520. /* Rc2EcbDecrypt bad arguments */
  15521. if (ret == 0) {
  15522. /* null Rc2 struct */
  15523. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  15524. if (ret == BAD_FUNC_ARG) {
  15525. ret = 0;
  15526. }
  15527. }
  15528. if (ret == 0) {
  15529. /* null out buffer */
  15530. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  15531. if (ret == BAD_FUNC_ARG) {
  15532. ret = 0;
  15533. }
  15534. }
  15535. if (ret == 0) {
  15536. /* null input buffer */
  15537. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  15538. if (ret == BAD_FUNC_ARG) {
  15539. ret = 0;
  15540. }
  15541. }
  15542. if (ret == 0) {
  15543. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15544. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  15545. if (ret == BUFFER_E) {
  15546. ret = 0;
  15547. }
  15548. }
  15549. res = TEST_RES_CHECK(ret == 0);
  15550. #endif
  15551. return res;
  15552. } /* END test_wc_Rc2SetKey */
  15553. /*
  15554. * Testing function for wc_Rc2CbcEncrypt().
  15555. */
  15556. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15557. {
  15558. int res = TEST_SKIPPED;
  15559. #ifdef WC_RC2
  15560. Rc2 rc2;
  15561. int ret = 0;
  15562. int effectiveKeyBits = 63;
  15563. byte cipher[RC2_BLOCK_SIZE*2];
  15564. byte plain[RC2_BLOCK_SIZE*2];
  15565. /* vector taken from test.c */
  15566. byte key[] = {
  15567. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15568. };
  15569. byte iv[] = {
  15570. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15571. };
  15572. byte input[] = {
  15573. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15574. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15575. };
  15576. byte output[] = {
  15577. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15578. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15579. };
  15580. XMEMSET(cipher, 0, sizeof(cipher));
  15581. XMEMSET(plain, 0, sizeof(plain));
  15582. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15583. iv, effectiveKeyBits);
  15584. if (ret == 0) {
  15585. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  15586. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  15587. ret = WOLFSSL_FATAL_ERROR;
  15588. }
  15589. else {
  15590. /* reset IV for decrypt */
  15591. ret = wc_Rc2SetIV(&rc2, iv);
  15592. }
  15593. if (ret == 0) {
  15594. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  15595. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  15596. ret = WOLFSSL_FATAL_ERROR;
  15597. }
  15598. }
  15599. }
  15600. /* Rc2CbcEncrypt bad arguments */
  15601. if (ret == 0) {
  15602. /* null Rc2 struct */
  15603. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  15604. if (ret == BAD_FUNC_ARG) {
  15605. ret = 0;
  15606. }
  15607. }
  15608. if (ret == 0) {
  15609. /* null out buffer */
  15610. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  15611. if (ret == BAD_FUNC_ARG) {
  15612. ret = 0;
  15613. }
  15614. }
  15615. if (ret == 0) {
  15616. /* null input buffer */
  15617. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  15618. if (ret == BAD_FUNC_ARG) {
  15619. ret = 0;
  15620. }
  15621. }
  15622. /* Rc2CbcDecrypt bad arguments */
  15623. if (ret == 0) {
  15624. /* in size is 0 */
  15625. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  15626. if (ret != 0) {
  15627. ret = WOLFSSL_FATAL_ERROR;
  15628. }
  15629. }
  15630. if (ret == 0) {
  15631. /* null Rc2 struct */
  15632. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  15633. if (ret == BAD_FUNC_ARG) {
  15634. ret = 0;
  15635. }
  15636. }
  15637. if (ret == 0) {
  15638. /* null out buffer */
  15639. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  15640. if (ret == BAD_FUNC_ARG) {
  15641. ret = 0;
  15642. }
  15643. }
  15644. if (ret == 0) {
  15645. /* null input buffer */
  15646. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  15647. if (ret == BAD_FUNC_ARG) {
  15648. ret = 0;
  15649. }
  15650. }
  15651. res = TEST_RES_CHECK(ret == 0);
  15652. #endif
  15653. return res;
  15654. } /* END test_wc_Rc2SetKey */
  15655. /*
  15656. * Testing function for wc_AesSetIV
  15657. */
  15658. static int test_wc_AesSetIV(void)
  15659. {
  15660. int res = TEST_SKIPPED;
  15661. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15662. Aes aes;
  15663. int ret = 0;
  15664. byte key16[] =
  15665. {
  15666. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15667. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15668. };
  15669. byte iv1[] = "1234567890abcdef";
  15670. byte iv2[] = "0987654321fedcba";
  15671. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15672. if (ret != 0)
  15673. return ret;
  15674. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15675. iv1, AES_ENCRYPTION);
  15676. if (ret == 0) {
  15677. ret = wc_AesSetIV(&aes, iv2);
  15678. }
  15679. /* Test bad args. */
  15680. if (ret == 0) {
  15681. ret = wc_AesSetIV(NULL, iv1);
  15682. if (ret == BAD_FUNC_ARG) {
  15683. /* NULL iv should return 0. */
  15684. ret = wc_AesSetIV(&aes, NULL);
  15685. }
  15686. else {
  15687. ret = WOLFSSL_FATAL_ERROR;
  15688. }
  15689. }
  15690. wc_AesFree(&aes);
  15691. res = TEST_RES_CHECK(ret == 0);
  15692. #endif
  15693. return res;
  15694. } /* test_wc_AesSetIV */
  15695. /*
  15696. * Testing function for wc_AesSetKey().
  15697. */
  15698. static int test_wc_AesSetKey(void)
  15699. {
  15700. int res = TEST_SKIPPED;
  15701. #ifndef NO_AES
  15702. Aes aes;
  15703. int ret = 0;
  15704. byte key16[] =
  15705. {
  15706. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15707. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15708. };
  15709. #ifdef WOLFSSL_AES_192
  15710. byte key24[] =
  15711. {
  15712. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15713. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15714. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15715. };
  15716. #endif
  15717. #ifdef WOLFSSL_AES_256
  15718. byte key32[] =
  15719. {
  15720. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15721. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15722. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15723. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15724. };
  15725. #endif
  15726. byte badKey16[] =
  15727. {
  15728. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15729. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15730. };
  15731. byte iv[] = "1234567890abcdef";
  15732. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15733. if (ret != 0)
  15734. return ret;
  15735. #ifdef WOLFSSL_AES_128
  15736. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15737. iv, AES_ENCRYPTION);
  15738. #endif
  15739. #ifdef WOLFSSL_AES_192
  15740. if (ret == 0) {
  15741. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  15742. iv, AES_ENCRYPTION);
  15743. }
  15744. #endif
  15745. #ifdef WOLFSSL_AES_256
  15746. if (ret == 0) {
  15747. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  15748. iv, AES_ENCRYPTION);
  15749. }
  15750. #endif
  15751. /* Pass in bad args. */
  15752. if (ret == 0) {
  15753. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  15754. iv, AES_ENCRYPTION);
  15755. if (ret == BAD_FUNC_ARG) {
  15756. ret = wc_AesSetKey(&aes, badKey16,
  15757. (word32) sizeof(badKey16) / sizeof(byte),
  15758. iv, AES_ENCRYPTION);
  15759. }
  15760. if (ret == BAD_FUNC_ARG) {
  15761. ret = 0;
  15762. }
  15763. else {
  15764. ret = WOLFSSL_FATAL_ERROR;
  15765. }
  15766. }
  15767. wc_AesFree(&aes);
  15768. res = TEST_RES_CHECK(ret == 0);
  15769. #endif
  15770. return res;
  15771. } /* END test_wc_AesSetKey */
  15772. /*
  15773. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15774. * and wc_AesCbcDecryptWithKey()
  15775. */
  15776. static int test_wc_AesCbcEncryptDecrypt(void)
  15777. {
  15778. int res = TEST_SKIPPED;
  15779. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15780. defined(WOLFSSL_AES_256)
  15781. Aes aes;
  15782. int ret = 0;
  15783. byte key32[] =
  15784. {
  15785. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15786. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15787. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15788. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15789. };
  15790. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  15791. {
  15792. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15793. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15794. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  15795. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  15796. };
  15797. byte iv[] = "1234567890abcdef";
  15798. byte enc[sizeof(vector)];
  15799. byte dec[sizeof(vector)];
  15800. int cbcE = WOLFSSL_FATAL_ERROR;
  15801. int cbcD = WOLFSSL_FATAL_ERROR;
  15802. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15803. byte dec2[sizeof(vector)];
  15804. /* Init stack variables. */
  15805. XMEMSET(enc, 0, sizeof(enc));
  15806. XMEMSET(dec, 0, sizeof(vector));
  15807. XMEMSET(dec2, 0, sizeof(vector));
  15808. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15809. if (ret != 0)
  15810. return ret;
  15811. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15812. if (ret == 0) {
  15813. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15814. if (ret == 0) {
  15815. /* Re init for decrypt and set flag. */
  15816. cbcE = 0;
  15817. wc_AesFree(&aes);
  15818. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15819. iv, AES_DECRYPTION);
  15820. }
  15821. if (ret == 0) {
  15822. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15823. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15824. ret = WOLFSSL_FATAL_ERROR;
  15825. }
  15826. else {
  15827. /* Set flag. */
  15828. cbcD = 0;
  15829. }
  15830. }
  15831. }
  15832. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15833. if (ret == 0 || cbcE == 0) {
  15834. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15835. key32, sizeof(key32)/sizeof(byte), iv);
  15836. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15837. cbcDWK = 0;
  15838. }
  15839. }
  15840. /* Pass in bad args */
  15841. if (cbcE == 0) {
  15842. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15843. if (cbcE == BAD_FUNC_ARG) {
  15844. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15845. }
  15846. if (cbcE == BAD_FUNC_ARG) {
  15847. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15848. }
  15849. if (cbcE == BAD_FUNC_ARG) {
  15850. cbcE = 0;
  15851. }
  15852. else {
  15853. cbcE = WOLFSSL_FATAL_ERROR;
  15854. }
  15855. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15856. if (cbcE == 0) {
  15857. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15858. }
  15859. if (cbcE == BAD_LENGTH_E) {
  15860. cbcE = 0;
  15861. }
  15862. else {
  15863. cbcE = WOLFSSL_FATAL_ERROR;
  15864. }
  15865. #endif
  15866. }
  15867. if (cbcE == 0) {
  15868. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15869. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15870. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15871. "mode (v2), skip test");
  15872. #else
  15873. /* Test passing in size of 0 */
  15874. XMEMSET(enc, 0, sizeof(enc));
  15875. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15876. if (cbcE == 0) {
  15877. /* Check enc was not modified */
  15878. int i;
  15879. for (i = 0; i < (int)sizeof(enc); i++)
  15880. cbcE |= enc[i];
  15881. }
  15882. #endif
  15883. }
  15884. if (cbcE != 0) {
  15885. wc_AesFree(&aes);
  15886. return TEST_FAIL;
  15887. }
  15888. if (cbcD == 0) {
  15889. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15890. if (cbcD == BAD_FUNC_ARG) {
  15891. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15892. }
  15893. if (cbcD == BAD_FUNC_ARG) {
  15894. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15895. }
  15896. if (cbcD == BAD_FUNC_ARG) {
  15897. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15898. }
  15899. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15900. if (cbcD == BAD_LENGTH_E) {
  15901. cbcD = 0;
  15902. }
  15903. else {
  15904. cbcD = WOLFSSL_FATAL_ERROR;
  15905. }
  15906. #else
  15907. if (cbcD == BAD_FUNC_ARG) {
  15908. cbcD = 0;
  15909. }
  15910. else {
  15911. cbcD = WOLFSSL_FATAL_ERROR;
  15912. }
  15913. #endif
  15914. }
  15915. if (cbcD == 0) {
  15916. /* Test passing in size of 0 */
  15917. XMEMSET(dec, 0, sizeof(dec));
  15918. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15919. if (cbcD == 0) {
  15920. /* Check dec was not modified */
  15921. int i;
  15922. for (i = 0; i < (int)sizeof(dec); i++)
  15923. cbcD |= dec[i];
  15924. }
  15925. }
  15926. if (cbcD != 0) {
  15927. wc_AesFree(&aes);
  15928. return TEST_FAIL;
  15929. }
  15930. if (cbcDWK == 0) {
  15931. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15932. key32, sizeof(key32)/sizeof(byte), iv);
  15933. if (cbcDWK == BAD_FUNC_ARG) {
  15934. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15935. key32, sizeof(key32)/sizeof(byte), iv);
  15936. }
  15937. if (cbcDWK == BAD_FUNC_ARG) {
  15938. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15939. NULL, sizeof(key32)/sizeof(byte), iv);
  15940. }
  15941. if (cbcDWK == BAD_FUNC_ARG) {
  15942. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15943. key32, sizeof(key32)/sizeof(byte), NULL);
  15944. }
  15945. if (cbcDWK == BAD_FUNC_ARG) {
  15946. cbcDWK = 0;
  15947. }
  15948. else {
  15949. cbcDWK = WOLFSSL_FATAL_ERROR;
  15950. }
  15951. }
  15952. wc_AesFree(&aes);
  15953. res = TEST_RES_CHECK(cbcDWK == 0);
  15954. #endif
  15955. return res;
  15956. } /* END test_wc_AesCbcEncryptDecrypt */
  15957. /*
  15958. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15959. */
  15960. static int test_wc_AesCtrEncryptDecrypt(void)
  15961. {
  15962. int res = TEST_SKIPPED;
  15963. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15964. Aes aesEnc, aesDec;
  15965. int ret = 0;
  15966. byte key32[] =
  15967. {
  15968. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15969. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15970. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15971. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15972. };
  15973. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15974. {
  15975. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15976. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15977. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15978. };
  15979. byte iv[] = "1234567890abcdef";
  15980. byte enc[AES_BLOCK_SIZE * 2];
  15981. byte dec[AES_BLOCK_SIZE * 2];
  15982. /* Init stack variables. */
  15983. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15984. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15985. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15986. if (ret != 0)
  15987. return ret;
  15988. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15989. if (ret != 0) {
  15990. wc_AesFree(&aesEnc);
  15991. return ret;
  15992. }
  15993. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15994. iv, AES_ENCRYPTION);
  15995. if (ret == 0) {
  15996. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15997. sizeof(vector)/sizeof(byte));
  15998. if (ret == 0) {
  15999. /* Decrypt with wc_AesCtrEncrypt() */
  16000. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  16001. iv, AES_ENCRYPTION);
  16002. }
  16003. if (ret == 0) {
  16004. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  16005. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  16006. ret = WOLFSSL_FATAL_ERROR;
  16007. }
  16008. }
  16009. }
  16010. /* Test bad args. */
  16011. if (ret == 0) {
  16012. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  16013. if (ret == BAD_FUNC_ARG) {
  16014. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  16015. }
  16016. if (ret == BAD_FUNC_ARG) {
  16017. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  16018. }
  16019. if (ret == BAD_FUNC_ARG) {
  16020. ret = 0;
  16021. }
  16022. else {
  16023. ret = WOLFSSL_FATAL_ERROR;
  16024. }
  16025. }
  16026. wc_AesFree(&aesEnc);
  16027. wc_AesFree(&aesDec);
  16028. res = TEST_RES_CHECK(ret == 0);
  16029. #endif
  16030. return res;
  16031. } /* END test_wc_AesCtrEncryptDecrypt */
  16032. /*
  16033. * test function for wc_AesGcmSetKey()
  16034. */
  16035. static int test_wc_AesGcmSetKey(void)
  16036. {
  16037. int res = TEST_SKIPPED;
  16038. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16039. Aes aes;
  16040. int ret = 0;
  16041. #ifdef WOLFSSL_AES_128
  16042. byte key16[] =
  16043. {
  16044. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16045. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16046. };
  16047. #endif
  16048. #ifdef WOLFSSL_AES_192
  16049. byte key24[] =
  16050. {
  16051. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16052. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16053. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16054. };
  16055. #endif
  16056. #ifdef WOLFSSL_AES_256
  16057. byte key32[] =
  16058. {
  16059. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16060. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16061. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16062. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16063. };
  16064. #endif
  16065. byte badKey16[] =
  16066. {
  16067. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16068. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16069. };
  16070. byte badKey24[] =
  16071. {
  16072. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16073. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16074. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16075. };
  16076. byte badKey32[] =
  16077. {
  16078. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16079. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16080. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16081. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16082. };
  16083. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16084. if (ret != 0)
  16085. return ret;
  16086. #ifdef WOLFSSL_AES_128
  16087. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  16088. #endif
  16089. #ifdef WOLFSSL_AES_192
  16090. if (ret == 0) {
  16091. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  16092. }
  16093. #endif
  16094. #ifdef WOLFSSL_AES_256
  16095. if (ret == 0) {
  16096. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16097. }
  16098. #endif
  16099. /* Pass in bad args. */
  16100. if (ret == 0) {
  16101. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  16102. if (ret == BAD_FUNC_ARG) {
  16103. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  16104. }
  16105. if (ret == BAD_FUNC_ARG) {
  16106. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  16107. }
  16108. if (ret == BAD_FUNC_ARG) {
  16109. ret = 0;
  16110. }
  16111. else {
  16112. ret = WOLFSSL_FATAL_ERROR;
  16113. }
  16114. }
  16115. wc_AesFree(&aes);
  16116. res = TEST_RES_CHECK(ret == 0);
  16117. #endif
  16118. return res;
  16119. } /* END test_wc_AesGcmSetKey */
  16120. /*
  16121. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16122. */
  16123. static int test_wc_AesGcmEncryptDecrypt(void)
  16124. {
  16125. int res = TEST_SKIPPED;
  16126. /* WOLFSSL_AFALG requires 12 byte IV */
  16127. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16128. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16129. Aes aes;
  16130. byte key32[] =
  16131. {
  16132. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16133. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16134. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16135. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16136. };
  16137. byte vector[] = /* Now is the time for all w/o trailing 0 */
  16138. {
  16139. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16140. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16141. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16142. };
  16143. const byte a[] =
  16144. {
  16145. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16146. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16147. 0xab, 0xad, 0xda, 0xd2
  16148. };
  16149. byte iv[] = "1234567890a";
  16150. byte longIV[] = "1234567890abcdefghij";
  16151. byte enc[sizeof(vector)];
  16152. byte resultT[AES_BLOCK_SIZE];
  16153. byte dec[sizeof(vector)];
  16154. int gcmD = WOLFSSL_FATAL_ERROR;
  16155. int gcmE = WOLFSSL_FATAL_ERROR;
  16156. int ret = 0;
  16157. /* Init stack variables. */
  16158. XMEMSET(enc, 0, sizeof(vector));
  16159. XMEMSET(dec, 0, sizeof(vector));
  16160. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16161. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16162. if (ret != 0)
  16163. return ret;
  16164. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  16165. if (ret == 0) {
  16166. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  16167. iv, sizeof(iv)/sizeof(byte), resultT,
  16168. sizeof(resultT), a, sizeof(a));
  16169. }
  16170. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  16171. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  16172. iv, sizeof(iv)/sizeof(byte), resultT,
  16173. sizeof(resultT), a, sizeof(a));
  16174. if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  16175. gcmD = WOLFSSL_FATAL_ERROR;
  16176. }
  16177. }
  16178. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16179. if (gcmE == 0) {
  16180. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  16181. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  16182. a, sizeof(a));
  16183. if (gcmE == BAD_FUNC_ARG) {
  16184. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16185. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  16186. resultT, sizeof(resultT) + 1, a, sizeof(a));
  16187. }
  16188. if (gcmE == BAD_FUNC_ARG) {
  16189. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16190. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  16191. resultT, sizeof(resultT) - 5, a, sizeof(a));
  16192. }
  16193. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16194. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16195. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16196. /* FIPS does not check the lower bound of ivSz */
  16197. #else
  16198. if (gcmE == BAD_FUNC_ARG) {
  16199. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  16200. sizeof(vector), iv, 0,
  16201. resultT, sizeof(resultT), a, sizeof(a));
  16202. }
  16203. #endif
  16204. if (gcmE == BAD_FUNC_ARG) {
  16205. gcmE = 0;
  16206. }
  16207. else {
  16208. gcmE = WOLFSSL_FATAL_ERROR;
  16209. }
  16210. }
  16211. /* This case is now considered good. Long IVs are now allowed.
  16212. * Except for the original FIPS release, it still has an upper
  16213. * bound on the IV length. */
  16214. #if (!defined(HAVE_FIPS) || \
  16215. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16216. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16217. if (gcmE == 0) {
  16218. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16219. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  16220. a, sizeof(a));
  16221. }
  16222. #else
  16223. (void)longIV;
  16224. #endif /* Old FIPS */
  16225. /* END wc_AesGcmEncrypt */
  16226. if (gcmE != 0) {
  16227. wc_AesFree(&aes);
  16228. return TEST_FAIL;
  16229. }
  16230. #ifdef HAVE_AES_DECRYPT
  16231. if (gcmD == 0) {
  16232. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  16233. iv, sizeof(iv)/sizeof(byte), resultT,
  16234. sizeof(resultT), a, sizeof(a));
  16235. if (gcmD == BAD_FUNC_ARG) {
  16236. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  16237. iv, sizeof(iv)/sizeof(byte), resultT,
  16238. sizeof(resultT), a, sizeof(a));
  16239. }
  16240. if (gcmD == BAD_FUNC_ARG) {
  16241. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  16242. iv, sizeof(iv)/sizeof(byte), resultT,
  16243. sizeof(resultT), a, sizeof(a));
  16244. }
  16245. if (gcmD == BAD_FUNC_ARG) {
  16246. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16247. NULL, sizeof(iv)/sizeof(byte), resultT,
  16248. sizeof(resultT), a, sizeof(a));
  16249. }
  16250. if (gcmD == BAD_FUNC_ARG) {
  16251. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16252. iv, sizeof(iv)/sizeof(byte), NULL,
  16253. sizeof(resultT), a, sizeof(a));
  16254. }
  16255. if (gcmD == BAD_FUNC_ARG) {
  16256. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16257. iv, sizeof(iv)/sizeof(byte), resultT,
  16258. sizeof(resultT) + 1, a, sizeof(a));
  16259. }
  16260. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16261. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16262. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16263. /* FIPS does not check the lower bound of ivSz */
  16264. #else
  16265. if (gcmD == BAD_FUNC_ARG) {
  16266. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16267. iv, 0, resultT,
  16268. sizeof(resultT), a, sizeof(a));
  16269. }
  16270. #endif
  16271. if (gcmD == BAD_FUNC_ARG) {
  16272. gcmD = 0;
  16273. }
  16274. else {
  16275. gcmD = WOLFSSL_FATAL_ERROR;
  16276. }
  16277. res = TEST_RES_CHECK(gcmD == 0);
  16278. } /* END wc_AesGcmDecrypt */
  16279. #endif /* HAVE_AES_DECRYPT */
  16280. wc_AesFree(&aes);
  16281. #endif
  16282. return res;
  16283. } /* END test_wc_AesGcmEncryptDecrypt */
  16284. /*
  16285. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16286. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16287. */
  16288. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16289. {
  16290. int ret = TEST_SKIPPED;
  16291. #if (!defined(HAVE_FIPS) || \
  16292. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16293. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16294. const byte key[] = {
  16295. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16296. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16297. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16298. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16299. };
  16300. const byte in[] = {
  16301. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16302. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16303. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16304. };
  16305. const byte aad[] = {
  16306. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16307. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16308. 0xab, 0xad, 0xda, 0xd2
  16309. };
  16310. Aes aesEnc, aesDec;
  16311. byte iv[] = "1234567890abcdefghij";
  16312. byte out[sizeof(in)];
  16313. byte plain[sizeof(in)];
  16314. byte tag[AES_BLOCK_SIZE];
  16315. XMEMSET(out, 0, sizeof(out));
  16316. XMEMSET(plain, 0, sizeof(plain));
  16317. XMEMSET(tag, 0, sizeof(tag));
  16318. /* Perform one-shot encryption using long IV */
  16319. AssertIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16320. AssertIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16321. AssertIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16322. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16323. /* Perform streaming decryption using long IV */
  16324. AssertIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16325. AssertIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16326. AssertIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16327. sizeof(aad)), 0);
  16328. AssertIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16329. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16330. /* Free resources */
  16331. wc_AesFree(&aesEnc);
  16332. wc_AesFree(&aesDec);
  16333. ret = TEST_SUCCESS;
  16334. #endif
  16335. return ret;
  16336. } /* END wc_AesGcmMixedEncDecLongIV */
  16337. /*
  16338. * unit test for wc_GmacSetKey()
  16339. */
  16340. static int test_wc_GmacSetKey(void)
  16341. {
  16342. int res = TEST_SKIPPED;
  16343. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16344. Gmac gmac;
  16345. byte key16[] =
  16346. {
  16347. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16348. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16349. };
  16350. #ifdef WOLFSSL_AES_192
  16351. byte key24[] =
  16352. {
  16353. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16354. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16355. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16356. };
  16357. #endif
  16358. #ifdef WOLFSSL_AES_256
  16359. byte key32[] =
  16360. {
  16361. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16362. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16363. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16364. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16365. };
  16366. #endif
  16367. byte badKey16[] =
  16368. {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16371. };
  16372. byte badKey24[] =
  16373. {
  16374. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16375. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16376. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16377. };
  16378. byte badKey32[] =
  16379. {
  16380. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16381. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16382. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16383. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16384. };
  16385. int ret = 0;
  16386. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  16387. if (ret != 0)
  16388. return ret;
  16389. #ifdef WOLFSSL_AES_128
  16390. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  16391. #endif
  16392. #ifdef WOLFSSL_AES_192
  16393. if (ret == 0) {
  16394. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  16395. }
  16396. #endif
  16397. #ifdef WOLFSSL_AES_256
  16398. if (ret == 0) {
  16399. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  16400. }
  16401. #endif
  16402. /* Pass in bad args. */
  16403. if (ret == 0) {
  16404. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  16405. if (ret == BAD_FUNC_ARG) {
  16406. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  16407. }
  16408. if (ret == BAD_FUNC_ARG) {
  16409. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  16410. }
  16411. if (ret == BAD_FUNC_ARG) {
  16412. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  16413. }
  16414. if (ret == BAD_FUNC_ARG) {
  16415. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  16416. }
  16417. if (ret == BAD_FUNC_ARG) {
  16418. ret = 0;
  16419. }
  16420. else {
  16421. ret = WOLFSSL_FATAL_ERROR;
  16422. }
  16423. }
  16424. wc_AesFree(&gmac.aes);
  16425. res = TEST_RES_CHECK(ret == 0);
  16426. #endif
  16427. return res;
  16428. } /* END test_wc_GmacSetKey */
  16429. /*
  16430. * unit test for wc_GmacUpdate
  16431. */
  16432. static int test_wc_GmacUpdate(void)
  16433. {
  16434. int res = TEST_SKIPPED;
  16435. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16436. Gmac gmac;
  16437. #ifdef WOLFSSL_AES_128
  16438. const byte key16[] =
  16439. {
  16440. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16441. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16442. };
  16443. #endif
  16444. #ifdef WOLFSSL_AES_192
  16445. byte key24[] =
  16446. {
  16447. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16448. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16449. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16450. };
  16451. #endif
  16452. #ifdef WOLFSSL_AES_256
  16453. byte key32[] =
  16454. {
  16455. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16456. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16457. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16458. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16459. };
  16460. #endif
  16461. #ifdef WOLFSSL_AES_128
  16462. const byte authIn[] =
  16463. {
  16464. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16465. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16466. };
  16467. #endif
  16468. #ifdef WOLFSSL_AES_192
  16469. const byte authIn2[] =
  16470. {
  16471. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16472. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16473. };
  16474. #endif
  16475. const byte authIn3[] =
  16476. {
  16477. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16478. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16479. };
  16480. #ifdef WOLFSSL_AES_128
  16481. const byte tag1[] = /* Known. */
  16482. {
  16483. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16484. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16485. };
  16486. #endif
  16487. #ifdef WOLFSSL_AES_192
  16488. const byte tag2[] = /* Known */
  16489. {
  16490. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16491. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16492. };
  16493. #endif
  16494. const byte tag3[] = /* Known */
  16495. {
  16496. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16497. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16498. };
  16499. #ifdef WOLFSSL_AES_128
  16500. const byte iv[] =
  16501. {
  16502. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16503. 0xe2, 0x8c, 0x8f, 0x16
  16504. };
  16505. #endif
  16506. #ifdef WOLFSSL_AES_192
  16507. const byte iv2[] =
  16508. {
  16509. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16510. 0x7e, 0x1a, 0x6f, 0xbc
  16511. };
  16512. #endif
  16513. const byte iv3[] =
  16514. {
  16515. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16516. 0xc3, 0xfb, 0x6c, 0x8a
  16517. };
  16518. byte tagOut[16];
  16519. byte tagOut2[24];
  16520. byte tagOut3[32];
  16521. int ret = 0;
  16522. /* Init stack variables. */
  16523. XMEMSET(tagOut, 0, sizeof(tagOut));
  16524. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16525. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16526. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  16527. if (ret != 0)
  16528. return ret;
  16529. #ifdef WOLFSSL_AES_128
  16530. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  16531. if (ret == 0) {
  16532. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16533. tagOut, sizeof(tag1));
  16534. if (ret == 0) {
  16535. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  16536. }
  16537. wc_AesFree(&gmac.aes);
  16538. }
  16539. #endif
  16540. #ifdef WOLFSSL_AES_192
  16541. if (ret == 0) {
  16542. XMEMSET(&gmac, 0, sizeof(Gmac));
  16543. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  16544. }
  16545. if (ret == 0) {
  16546. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  16547. sizeof(authIn2), tagOut2, sizeof(tag2));
  16548. }
  16549. if (ret == 0) {
  16550. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  16551. wc_AesFree(&gmac.aes);
  16552. }
  16553. #endif
  16554. #ifdef WOLFSSL_AES_256
  16555. if (ret == 0) {
  16556. XMEMSET(&gmac, 0, sizeof(Gmac));
  16557. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  16558. }
  16559. if (ret == 0) {
  16560. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16561. sizeof(authIn3), tagOut3, sizeof(tag3));
  16562. }
  16563. if (ret == 0) {
  16564. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  16565. }
  16566. #endif
  16567. /*Pass bad args. */
  16568. if (ret == 0) {
  16569. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  16570. sizeof(authIn3), tagOut3, sizeof(tag3));
  16571. if (ret == BAD_FUNC_ARG) {
  16572. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16573. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  16574. }
  16575. if (ret == BAD_FUNC_ARG) {
  16576. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  16577. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  16578. }
  16579. if (ret == BAD_FUNC_ARG) {
  16580. ret = 0;
  16581. }
  16582. else {
  16583. ret = WOLFSSL_FATAL_ERROR;
  16584. }
  16585. }
  16586. wc_AesFree(&gmac.aes);
  16587. res = TEST_RES_CHECK(ret == 0);
  16588. #endif
  16589. return res;
  16590. } /* END test_wc_GmacUpdate */
  16591. /*
  16592. * testing wc_CamelliaSetKey
  16593. */
  16594. static int test_wc_CamelliaSetKey(void)
  16595. {
  16596. int res = TEST_SKIPPED;
  16597. #ifdef HAVE_CAMELLIA
  16598. Camellia camellia;
  16599. /*128-bit key*/
  16600. static const byte key16[] =
  16601. {
  16602. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16603. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16604. };
  16605. /* 192-bit key */
  16606. static const byte key24[] =
  16607. {
  16608. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16609. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16610. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16611. };
  16612. /* 256-bit key */
  16613. static const byte key32[] =
  16614. {
  16615. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16616. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16617. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16618. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16619. };
  16620. static const byte iv[] =
  16621. {
  16622. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16623. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16624. };
  16625. int ret = 0;
  16626. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  16627. if (ret == 0) {
  16628. ret = wc_CamelliaSetKey(&camellia, key16,
  16629. (word32)sizeof(key16), NULL);
  16630. if (ret == 0) {
  16631. ret = wc_CamelliaSetKey(&camellia, key24,
  16632. (word32)sizeof(key24), iv);
  16633. }
  16634. if (ret == 0) {
  16635. ret = wc_CamelliaSetKey(&camellia, key24,
  16636. (word32)sizeof(key24), NULL);
  16637. }
  16638. if (ret == 0) {
  16639. ret = wc_CamelliaSetKey(&camellia, key32,
  16640. (word32)sizeof(key32), iv);
  16641. }
  16642. if (ret == 0) {
  16643. ret = wc_CamelliaSetKey(&camellia, key32,
  16644. (word32)sizeof(key32), NULL);
  16645. }
  16646. }
  16647. /* Bad args. */
  16648. if (ret == 0) {
  16649. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  16650. if (ret != BAD_FUNC_ARG) {
  16651. ret = WOLFSSL_FATAL_ERROR;
  16652. }
  16653. else {
  16654. ret = 0;
  16655. }
  16656. } /* END bad args. */
  16657. res = TEST_RES_CHECK(ret == 0);
  16658. #endif
  16659. return res;
  16660. } /* END test_wc_CammeliaSetKey */
  16661. /*
  16662. * Testing wc_CamelliaSetIV()
  16663. */
  16664. static int test_wc_CamelliaSetIV(void)
  16665. {
  16666. int res = TEST_SKIPPED;
  16667. #ifdef HAVE_CAMELLIA
  16668. Camellia camellia;
  16669. static const byte iv[] =
  16670. {
  16671. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16672. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16673. };
  16674. int ret = 0;
  16675. ret = wc_CamelliaSetIV(&camellia, iv);
  16676. if (ret == 0) {
  16677. ret = wc_CamelliaSetIV(&camellia, NULL);
  16678. }
  16679. /* Bad args. */
  16680. if (ret == 0) {
  16681. ret = wc_CamelliaSetIV(NULL, NULL);
  16682. if (ret != BAD_FUNC_ARG) {
  16683. ret = WOLFSSL_FATAL_ERROR;
  16684. }
  16685. else {
  16686. ret = 0;
  16687. }
  16688. }
  16689. res = TEST_RES_CHECK(ret == 0);
  16690. #endif
  16691. return res;
  16692. } /*END test_wc_CamelliaSetIV*/
  16693. /*
  16694. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16695. */
  16696. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16697. {
  16698. int res = TEST_SKIPPED;
  16699. #ifdef HAVE_CAMELLIA
  16700. Camellia camellia;
  16701. static const byte key24[] =
  16702. {
  16703. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16704. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16705. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16706. };
  16707. static const byte iv[] =
  16708. {
  16709. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16710. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16711. };
  16712. static const byte plainT[] =
  16713. {
  16714. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16715. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16716. };
  16717. byte enc[sizeof(plainT)];
  16718. byte dec[sizeof(enc)];
  16719. int camE = WOLFSSL_FATAL_ERROR;
  16720. int camD = WOLFSSL_FATAL_ERROR;
  16721. int ret = 0;
  16722. /*Init stack variables.*/
  16723. XMEMSET(enc, 0, 16);
  16724. XMEMSET(enc, 0, 16);
  16725. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  16726. if (ret == 0) {
  16727. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  16728. if (ret == 0) {
  16729. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  16730. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16731. ret = WOLFSSL_FATAL_ERROR;
  16732. }
  16733. }
  16734. }
  16735. /* Pass bad args. */
  16736. if (ret == 0) {
  16737. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  16738. if (camE == BAD_FUNC_ARG) {
  16739. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  16740. }
  16741. if (camE == BAD_FUNC_ARG) {
  16742. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  16743. }
  16744. if (camE == BAD_FUNC_ARG) {
  16745. camE = 0;
  16746. }
  16747. else {
  16748. camE = WOLFSSL_FATAL_ERROR;
  16749. }
  16750. }
  16751. if (camE != 0) {
  16752. return TEST_FAIL;
  16753. }
  16754. if (ret == 0) {
  16755. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  16756. if (camD == BAD_FUNC_ARG) {
  16757. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  16758. }
  16759. if (camD == BAD_FUNC_ARG) {
  16760. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  16761. }
  16762. if (camD == BAD_FUNC_ARG) {
  16763. camD = 0;
  16764. }
  16765. else {
  16766. camD = WOLFSSL_FATAL_ERROR;
  16767. }
  16768. }
  16769. res = TEST_RES_CHECK(camD == 0);
  16770. #endif
  16771. return res;
  16772. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16773. /*
  16774. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16775. */
  16776. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16777. {
  16778. int res = TEST_SKIPPED;
  16779. #ifdef HAVE_CAMELLIA
  16780. Camellia camellia;
  16781. static const byte key24[] =
  16782. {
  16783. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16784. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16785. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16786. };
  16787. static const byte plainT[] =
  16788. {
  16789. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16790. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16791. };
  16792. byte enc[CAMELLIA_BLOCK_SIZE];
  16793. byte dec[CAMELLIA_BLOCK_SIZE];
  16794. int camCbcE = WOLFSSL_FATAL_ERROR;
  16795. int camCbcD = WOLFSSL_FATAL_ERROR;
  16796. int ret = 0;
  16797. /* Init stack variables. */
  16798. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16799. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16800. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16801. if (ret == 0) {
  16802. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16803. if (ret != 0) {
  16804. ret = WOLFSSL_FATAL_ERROR;
  16805. }
  16806. }
  16807. if (ret == 0) {
  16808. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  16809. if (ret == 0) {
  16810. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  16811. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  16812. ret = WOLFSSL_FATAL_ERROR;
  16813. }
  16814. }
  16815. }
  16816. /* Pass in bad args. */
  16817. if (ret == 0) {
  16818. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  16819. if (camCbcE == BAD_FUNC_ARG) {
  16820. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16821. CAMELLIA_BLOCK_SIZE);
  16822. }
  16823. if (camCbcE == BAD_FUNC_ARG) {
  16824. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16825. CAMELLIA_BLOCK_SIZE);
  16826. }
  16827. if (camCbcE == BAD_FUNC_ARG) {
  16828. camCbcE = 0;
  16829. }
  16830. else {
  16831. camCbcE = WOLFSSL_FATAL_ERROR;
  16832. }
  16833. }
  16834. if (camCbcE != 0) {
  16835. return TEST_FAIL;
  16836. }
  16837. if (ret == 0) {
  16838. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  16839. if (camCbcD == BAD_FUNC_ARG) {
  16840. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16841. CAMELLIA_BLOCK_SIZE);
  16842. }
  16843. if (camCbcD == BAD_FUNC_ARG) {
  16844. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16845. CAMELLIA_BLOCK_SIZE);
  16846. }
  16847. if (camCbcD == BAD_FUNC_ARG) {
  16848. camCbcD = 0;
  16849. }
  16850. else {
  16851. camCbcD = WOLFSSL_FATAL_ERROR;
  16852. }
  16853. } /* END bad args. */
  16854. res = TEST_RES_CHECK(camCbcD == 0);
  16855. #endif
  16856. return res;
  16857. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16858. /*
  16859. * Testing wc_Arc4SetKey()
  16860. */
  16861. static int test_wc_Arc4SetKey(void)
  16862. {
  16863. int res = TEST_SKIPPED;
  16864. #ifndef NO_RC4
  16865. Arc4 arc;
  16866. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16867. int keyLen = 8;
  16868. int ret = 0;
  16869. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16870. /* Test bad args. */
  16871. if (ret == 0) {
  16872. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16873. if (ret == BAD_FUNC_ARG)
  16874. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16875. if (ret == BAD_FUNC_ARG)
  16876. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16877. if (ret == BAD_FUNC_ARG)
  16878. ret = WOLFSSL_ERROR_NONE;
  16879. else
  16880. ret = WOLFSSL_FATAL_ERROR;
  16881. } /* END test bad args. */
  16882. res = TEST_RES_CHECK(ret == 0);
  16883. #endif
  16884. return res;
  16885. } /* END test_wc_Arc4SetKey */
  16886. /*
  16887. * Testing wc_Arc4Process for ENC/DEC.
  16888. */
  16889. static int test_wc_Arc4Process(void)
  16890. {
  16891. int res = TEST_SKIPPED;
  16892. #ifndef NO_RC4
  16893. Arc4 enc, dec;
  16894. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16895. int keyLen = 8;
  16896. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16897. byte cipher[8];
  16898. byte plain[8];
  16899. int ret;
  16900. /* Init stack variables */
  16901. XMEMSET(cipher, 0, sizeof(cipher));
  16902. XMEMSET(plain, 0, sizeof(plain));
  16903. /* Use for async. */
  16904. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16905. if (ret == 0) {
  16906. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16907. }
  16908. if (ret == 0) {
  16909. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16910. }
  16911. if (ret == 0) {
  16912. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16913. }
  16914. if (ret == 0) {
  16915. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16916. }
  16917. if (ret == 0) {
  16918. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16919. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16920. ret = WOLFSSL_FATAL_ERROR;
  16921. }
  16922. else {
  16923. ret = 0;
  16924. }
  16925. }
  16926. /* Bad args. */
  16927. if (ret == 0) {
  16928. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16929. if (ret == BAD_FUNC_ARG) {
  16930. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16931. }
  16932. if (ret == BAD_FUNC_ARG) {
  16933. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16934. }
  16935. if (ret == BAD_FUNC_ARG) {
  16936. ret = 0;
  16937. }
  16938. else {
  16939. ret = WOLFSSL_FATAL_ERROR;
  16940. }
  16941. }
  16942. wc_Arc4Free(&enc);
  16943. wc_Arc4Free(&dec);
  16944. res = TEST_RES_CHECK(ret == 0);
  16945. #endif
  16946. return res;
  16947. }/* END test_wc_Arc4Process */
  16948. /*
  16949. * Testing wc_Init RsaKey()
  16950. */
  16951. static int test_wc_InitRsaKey(void)
  16952. {
  16953. int res = TEST_SKIPPED;
  16954. #ifndef NO_RSA
  16955. RsaKey key;
  16956. int ret = 0;
  16957. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16958. /* Test bad args. */
  16959. if (ret == 0) {
  16960. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16961. #ifndef HAVE_USER_RSA
  16962. if (ret == BAD_FUNC_ARG) {
  16963. ret = 0;
  16964. }
  16965. else {
  16966. #else
  16967. if (ret == USER_CRYPTO_ERROR) {
  16968. ret = 0;
  16969. }
  16970. else {
  16971. #endif
  16972. ret = WOLFSSL_FATAL_ERROR;
  16973. }
  16974. } /* end if */
  16975. if (wc_FreeRsaKey(&key) || ret != 0) {
  16976. ret = WOLFSSL_FATAL_ERROR;
  16977. }
  16978. res = TEST_RES_CHECK(ret == 0);
  16979. #endif
  16980. return res;
  16981. } /* END test_wc_InitRsaKey */
  16982. /*
  16983. * Testing wc_RsaPrivateKeyDecode()
  16984. */
  16985. static int test_wc_RsaPrivateKeyDecode(void)
  16986. {
  16987. int res = TEST_SKIPPED;
  16988. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16989. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16990. RsaKey key;
  16991. byte* tmp;
  16992. word32 idx = 0;
  16993. int bytes = 0;
  16994. int ret = 0;
  16995. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16996. if (tmp == NULL) {
  16997. ret = WOLFSSL_FATAL_ERROR;
  16998. }
  16999. if (ret == 0) {
  17000. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17001. }
  17002. if (ret == 0) {
  17003. #ifdef USE_CERT_BUFFERS_1024
  17004. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17005. bytes = sizeof_client_key_der_1024;
  17006. #else
  17007. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17008. bytes = sizeof_client_key_der_2048;
  17009. #endif /* Use cert buffers. */
  17010. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  17011. }
  17012. #ifndef HAVE_USER_RSA
  17013. /* Test bad args. */
  17014. if (ret == 0) {
  17015. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17016. if (ret == BAD_FUNC_ARG) {
  17017. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17018. }
  17019. if (ret == BAD_FUNC_ARG) {
  17020. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17021. }
  17022. if (ret == BAD_FUNC_ARG) {
  17023. ret = 0;
  17024. }
  17025. else {
  17026. ret = WOLFSSL_FATAL_ERROR;
  17027. }
  17028. }
  17029. #else
  17030. /* Test bad args. User RSA. */
  17031. if (ret == 0) {
  17032. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  17033. if (ret == USER_CRYPTO_ERROR) {
  17034. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  17035. }
  17036. if (ret == USER_CRYPTO_ERROR) {
  17037. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17038. }
  17039. if (ret == USER_CRYPTO_ERROR) {
  17040. ret = 0;
  17041. }
  17042. else {
  17043. ret = WOLFSSL_FATAL_ERROR;
  17044. }
  17045. }
  17046. #endif
  17047. if (tmp != NULL) {
  17048. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17049. }
  17050. if (wc_FreeRsaKey(&key) || ret != 0) {
  17051. ret = WOLFSSL_FATAL_ERROR;
  17052. }
  17053. res = TEST_RES_CHECK(ret == 0);
  17054. #endif
  17055. return res;
  17056. } /* END test_wc_RsaPrivateKeyDecode */
  17057. /*
  17058. * Testing wc_RsaPublicKeyDecode()
  17059. */
  17060. static int test_wc_RsaPublicKeyDecode(void)
  17061. {
  17062. int res = TEST_SKIPPED;
  17063. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17064. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17065. RsaKey keyPub;
  17066. byte* tmp;
  17067. word32 idx = 0;
  17068. int bytes = 0;
  17069. word32 keySz = 0;
  17070. word32 tstKeySz = 0;
  17071. int ret = 0;
  17072. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17073. XFILE f;
  17074. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17075. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17076. byte buf[4096];
  17077. #endif
  17078. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17079. if (tmp == NULL) {
  17080. ret = WOLFSSL_FATAL_ERROR;
  17081. }
  17082. if (ret == 0) {
  17083. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  17084. }
  17085. if (ret == 0) {
  17086. #ifdef USE_CERT_BUFFERS_1024
  17087. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17088. bytes = sizeof_client_keypub_der_1024;
  17089. keySz = 1024;
  17090. #else
  17091. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17092. bytes = sizeof_client_keypub_der_2048;
  17093. keySz = 2048;
  17094. #endif
  17095. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  17096. }
  17097. #ifndef HAVE_USER_RSA
  17098. /* Pass in bad args. */
  17099. if (ret == 0) {
  17100. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17101. if (ret == BAD_FUNC_ARG) {
  17102. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17103. }
  17104. if (ret == BAD_FUNC_ARG) {
  17105. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17106. }
  17107. if (ret == BAD_FUNC_ARG) {
  17108. ret = 0;
  17109. }
  17110. else {
  17111. ret = WOLFSSL_FATAL_ERROR;
  17112. }
  17113. }
  17114. #else
  17115. /* Pass in bad args. */
  17116. if (ret == 0) {
  17117. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  17118. if (ret == USER_CRYPTO_ERROR) {
  17119. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  17120. }
  17121. if (ret == USER_CRYPTO_ERROR) {
  17122. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  17123. }
  17124. if (ret == USER_CRYPTO_ERROR) {
  17125. ret = 0;
  17126. }
  17127. else {
  17128. ret = WOLFSSL_FATAL_ERROR;
  17129. }
  17130. }
  17131. #endif
  17132. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  17133. ret = WOLFSSL_FATAL_ERROR;
  17134. }
  17135. if (ret == 0) {
  17136. /* Test for getting modulus key size */
  17137. idx = 0;
  17138. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17139. &tstKeySz, NULL, NULL);
  17140. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  17141. }
  17142. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17143. f = XFOPEN(rsaPssPubKey, "rb");
  17144. AssertTrue((f != XBADFILE));
  17145. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17146. XFCLOSE(f);
  17147. idx = 0;
  17148. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17149. NULL), 0);
  17150. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  17151. AssertTrue((f != XBADFILE));
  17152. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  17153. XFCLOSE(f);
  17154. idx = 0;
  17155. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17156. NULL), 0);
  17157. #endif
  17158. if (tmp != NULL) {
  17159. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17160. }
  17161. res = TEST_RES_CHECK(ret == 0);
  17162. #endif
  17163. return res;
  17164. } /* END test_wc_RsaPublicKeyDecode */
  17165. /*
  17166. * Testing wc_RsaPublicKeyDecodeRaw()
  17167. */
  17168. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17169. {
  17170. int res = TEST_SKIPPED;
  17171. #if !defined(NO_RSA)
  17172. RsaKey key;
  17173. const byte n = 0x23;
  17174. const byte e = 0x03;
  17175. int nSz = sizeof(n);
  17176. int eSz = sizeof(e);
  17177. int ret;
  17178. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17179. if (ret == 0) {
  17180. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  17181. }
  17182. #ifndef HAVE_USER_RSA
  17183. /* Pass in bad args. */
  17184. if (ret == 0) {
  17185. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17186. if (ret == BAD_FUNC_ARG) {
  17187. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17188. }
  17189. if (ret == BAD_FUNC_ARG) {
  17190. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17191. }
  17192. if (ret == BAD_FUNC_ARG) {
  17193. ret = 0;
  17194. }
  17195. else {
  17196. ret = WOLFSSL_FATAL_ERROR;
  17197. }
  17198. }
  17199. #else
  17200. /* Pass in bad args. User RSA. */
  17201. if (ret == 0) {
  17202. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  17203. if (ret == USER_CRYPTO_ERROR) {
  17204. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  17205. }
  17206. if (ret == USER_CRYPTO_ERROR) {
  17207. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  17208. }
  17209. if (ret == USER_CRYPTO_ERROR) {
  17210. ret = 0;
  17211. }
  17212. else {
  17213. ret = WOLFSSL_FATAL_ERROR;
  17214. }
  17215. }
  17216. #endif
  17217. if (wc_FreeRsaKey(&key) || ret != 0) {
  17218. ret = WOLFSSL_FATAL_ERROR;
  17219. }
  17220. res = TEST_RES_CHECK(ret == 0);
  17221. #endif
  17222. return res;
  17223. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17224. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  17225. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17226. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17227. * trying until it gets a probable prime. */
  17228. #ifdef HAVE_FIPS
  17229. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17230. {
  17231. int ret;
  17232. for (;;) {
  17233. ret = wc_MakeRsaKey(key, size, e, rng);
  17234. if (ret != PRIME_GEN_E) break;
  17235. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17236. "trying again.\n");
  17237. }
  17238. return ret;
  17239. }
  17240. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17241. #else
  17242. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17243. #endif
  17244. #endif
  17245. /*
  17246. * Testing wc_MakeRsaKey()
  17247. */
  17248. static int test_wc_MakeRsaKey(void)
  17249. {
  17250. int res = TEST_SKIPPED;
  17251. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17252. RsaKey genKey;
  17253. WC_RNG rng;
  17254. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17255. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17256. int bits = 1024;
  17257. #else
  17258. int bits = 2048;
  17259. #endif
  17260. int ret = 0;
  17261. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17262. if (ret == 0) {
  17263. ret = wc_InitRng(&rng);
  17264. if (ret == 0) {
  17265. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17266. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  17267. ret = WOLFSSL_FATAL_ERROR;
  17268. }
  17269. }
  17270. }
  17271. #ifndef HAVE_USER_RSA
  17272. /* Test bad args. */
  17273. if (ret == 0) {
  17274. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17275. if (ret == BAD_FUNC_ARG) {
  17276. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17277. }
  17278. if (ret == BAD_FUNC_ARG) {
  17279. /* e < 3 */
  17280. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17281. }
  17282. if (ret == BAD_FUNC_ARG) {
  17283. /* e & 1 == 0 */
  17284. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17285. }
  17286. if (ret == BAD_FUNC_ARG) {
  17287. ret = 0;
  17288. }
  17289. else {
  17290. ret = WOLFSSL_FATAL_ERROR;
  17291. }
  17292. }
  17293. #else
  17294. /* Test bad args. */
  17295. if (ret == 0) {
  17296. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  17297. if (ret == USER_CRYPTO_ERROR) {
  17298. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  17299. }
  17300. if (ret == USER_CRYPTO_ERROR) {
  17301. /* e < 3 */
  17302. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  17303. }
  17304. if (ret == USER_CRYPTO_ERROR) {
  17305. /* e & 1 == 0 */
  17306. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  17307. }
  17308. if (ret == USER_CRYPTO_ERROR) {
  17309. ret = 0;
  17310. }
  17311. else {
  17312. ret = WOLFSSL_FATAL_ERROR;
  17313. }
  17314. }
  17315. #endif
  17316. if (wc_FreeRng(&rng) || ret != 0) {
  17317. ret = WOLFSSL_FATAL_ERROR;
  17318. }
  17319. res = TEST_RES_CHECK(ret == 0);
  17320. #endif
  17321. return res;
  17322. } /* END test_wc_MakeRsaKey */
  17323. /*
  17324. * Test the bounds checking on the cipher text versus the key modulus.
  17325. * 1. Make a new RSA key.
  17326. * 2. Set c to 1.
  17327. * 3. Decrypt c into k. (error)
  17328. * 4. Copy the key modulus to c and sub 1 from the copy.
  17329. * 5. Decrypt c into k. (error)
  17330. * Valid bounds test cases are covered by all the other RSA tests.
  17331. */
  17332. static int test_RsaDecryptBoundsCheck(void)
  17333. {
  17334. int res = TEST_SKIPPED;
  17335. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17336. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17337. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17338. WC_RNG rng;
  17339. RsaKey key;
  17340. byte flatC[256];
  17341. word32 flatCSz;
  17342. byte out[256];
  17343. word32 outSz = sizeof(out);
  17344. int ret;
  17345. XMEMSET(&rng, 0, sizeof(rng));
  17346. ret = wc_InitRng(&rng);
  17347. if (ret == 0)
  17348. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17349. if (ret == 0) {
  17350. const byte* derKey;
  17351. word32 derKeySz;
  17352. word32 idx = 0;
  17353. #ifdef USE_CERT_BUFFERS_1024
  17354. derKey = server_key_der_1024;
  17355. derKeySz = (word32)sizeof_server_key_der_1024;
  17356. flatCSz = 128;
  17357. #else
  17358. derKey = server_key_der_2048;
  17359. derKeySz = (word32)sizeof_server_key_der_2048;
  17360. flatCSz = 256;
  17361. #endif
  17362. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  17363. }
  17364. if (ret == 0) {
  17365. XMEMSET(flatC, 0, flatCSz);
  17366. flatC[flatCSz-1] = 1;
  17367. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17368. RSA_PRIVATE_DECRYPT, &rng);
  17369. if (ret == RSA_OUT_OF_RANGE_E) {
  17370. mp_int c;
  17371. mp_init_copy(&c, &key.n);
  17372. mp_sub_d(&c, 1, &c);
  17373. mp_to_unsigned_bin(&c, flatC);
  17374. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17375. RSA_PRIVATE_DECRYPT, NULL);
  17376. mp_clear(&c);
  17377. }
  17378. if (ret == RSA_OUT_OF_RANGE_E)
  17379. ret = 0;
  17380. else
  17381. ret = WOLFSSL_FATAL_ERROR;
  17382. }
  17383. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  17384. ret = WOLFSSL_FATAL_ERROR;
  17385. res = TEST_RES_CHECK(ret == 0);
  17386. #endif
  17387. return res;
  17388. } /* END test_wc_RsaDecryptBoundsCheck */
  17389. /*
  17390. * Testing wc_SetKeyUsage()
  17391. */
  17392. static int test_wc_SetKeyUsage(void)
  17393. {
  17394. int res = TEST_SKIPPED;
  17395. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17396. Cert myCert;
  17397. int ret = 0;
  17398. ret = wc_InitCert(&myCert);
  17399. if (ret == 0) {
  17400. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  17401. if (ret == 0) {
  17402. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  17403. }
  17404. if (ret == 0) {
  17405. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  17406. }
  17407. if (ret == 0) {
  17408. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  17409. }
  17410. if (ret == 0) {
  17411. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  17412. }
  17413. }
  17414. /* Test bad args. */
  17415. if (ret == 0) {
  17416. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  17417. if (ret == BAD_FUNC_ARG) {
  17418. ret = wc_SetKeyUsage(&myCert, NULL);
  17419. }
  17420. if (ret == BAD_FUNC_ARG) {
  17421. ret = wc_SetKeyUsage(&myCert, "");
  17422. }
  17423. if (ret == KEYUSAGE_E) {
  17424. ret = wc_SetKeyUsage(&myCert, ",");
  17425. }
  17426. if (ret == KEYUSAGE_E) {
  17427. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  17428. }
  17429. if (ret == KEYUSAGE_E) {
  17430. ret = 0;
  17431. }
  17432. else {
  17433. ret = WOLFSSL_FATAL_ERROR;
  17434. }
  17435. }
  17436. res = TEST_RES_CHECK(ret == 0);
  17437. #endif
  17438. return res;
  17439. } /* END test_wc_SetKeyUsage */
  17440. /*
  17441. * Testing wc_CheckProbablePrime()
  17442. */
  17443. static int test_wc_CheckProbablePrime(void)
  17444. {
  17445. int res = TEST_SKIPPED;
  17446. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17447. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17448. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17449. RsaKey key;
  17450. WC_RNG rng;
  17451. byte e[3];
  17452. word32 eSz = (word32)sizeof(e);
  17453. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17454. word32 nSz = (word32)sizeof(n);
  17455. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17456. word32 dSz = (word32)sizeof(d);
  17457. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17458. word32 pSz = (word32)sizeof(p);
  17459. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17460. word32 qSz = (word32)sizeof(q);
  17461. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17462. int ret = 0;
  17463. int* isPrime;
  17464. int test[5];
  17465. isPrime = test;
  17466. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17467. if (ret == 0) {
  17468. ret = wc_InitRng(&rng);
  17469. }
  17470. if (ret == 0) {
  17471. ret = wc_RsaSetRNG(&key, &rng);
  17472. }
  17473. if (ret == 0) {
  17474. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  17475. }
  17476. if (ret == 0) {
  17477. PRIVATE_KEY_UNLOCK();
  17478. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  17479. p, &pSz, q, &qSz);
  17480. PRIVATE_KEY_LOCK();
  17481. }
  17482. /* Bad cases */
  17483. if (ret == 0) {
  17484. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  17485. nlen, isPrime);
  17486. if (ret == BAD_FUNC_ARG) {
  17487. ret = 0;
  17488. }
  17489. }
  17490. if (ret == 0) {
  17491. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  17492. nlen, isPrime);
  17493. if (ret == BAD_FUNC_ARG) {
  17494. ret = 0;
  17495. }
  17496. }
  17497. if (ret == 0) {
  17498. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  17499. nlen, isPrime);
  17500. if (ret == BAD_FUNC_ARG) {
  17501. ret = 0;
  17502. }
  17503. }
  17504. if (ret == 0) {
  17505. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  17506. nlen, isPrime);
  17507. if (ret == BAD_FUNC_ARG) {
  17508. ret = 0;
  17509. }
  17510. }
  17511. if (ret == 0) {
  17512. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  17513. nlen, isPrime);
  17514. if (ret == BAD_FUNC_ARG) {
  17515. ret = 0;
  17516. }
  17517. }
  17518. if (ret == 0) {
  17519. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  17520. nlen, isPrime);
  17521. if (ret == BAD_FUNC_ARG) {
  17522. ret = 0;
  17523. }
  17524. }
  17525. if (ret == 0) {
  17526. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  17527. nlen, isPrime);
  17528. if (ret == BAD_FUNC_ARG) {
  17529. ret = 0;
  17530. }
  17531. }
  17532. /* Good case */
  17533. if (ret == 0) {
  17534. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  17535. nlen, isPrime);
  17536. }
  17537. wc_FreeRsaKey(&key);
  17538. wc_FreeRng(&rng);
  17539. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17540. res = TEST_RES_CHECK(ret == 0);
  17541. #endif
  17542. return res;
  17543. } /* END test_wc_CheckProbablePrime */
  17544. /*
  17545. * Testing wc_RsaPSS_Verify()
  17546. */
  17547. static int test_wc_RsaPSS_Verify(void)
  17548. {
  17549. int res = TEST_SKIPPED;
  17550. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17551. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17552. RsaKey key;
  17553. WC_RNG rng;
  17554. int sz = 256;
  17555. byte* pt;
  17556. const char* szMessage = "This is the string to be signed";
  17557. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17558. unsigned char pDecrypted[2048/8];
  17559. word32 outLen = sizeof(pDecrypted);
  17560. int ret = 0;
  17561. pt = pDecrypted;
  17562. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17563. if (ret == 0) {
  17564. ret = wc_InitRng(&rng);
  17565. }
  17566. if (ret == 0) {
  17567. ret = wc_RsaSetRNG(&key, &rng);
  17568. }
  17569. if (ret == 0) {
  17570. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17571. }
  17572. if (ret == 0) {
  17573. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  17574. pSignature, sizeof(pSignature),
  17575. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17576. if (ret > 0) {
  17577. sz = ret;
  17578. ret = 0;
  17579. }
  17580. }
  17581. /* Bad cases */
  17582. if (ret == 0) {
  17583. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17584. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17585. if (ret == BAD_FUNC_ARG) {
  17586. ret = 0;
  17587. }
  17588. }
  17589. if (ret == 0) {
  17590. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17591. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17592. if (ret == BAD_FUNC_ARG) {
  17593. ret = 0;
  17594. }
  17595. }
  17596. if (ret == 0) {
  17597. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17598. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17599. if (ret == BAD_FUNC_ARG) {
  17600. ret = 0;
  17601. }
  17602. }
  17603. if (ret == 0) {
  17604. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17605. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17606. if (ret == BAD_FUNC_ARG) {
  17607. ret = 0;
  17608. }
  17609. }
  17610. /* Good case */
  17611. if (ret == 0) {
  17612. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17613. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17614. if (ret > 0) {
  17615. ret = 0;
  17616. }
  17617. }
  17618. wc_FreeRsaKey(&key);
  17619. wc_FreeRng(&rng);
  17620. res = TEST_RES_CHECK(ret == 0);
  17621. #endif
  17622. return res;
  17623. } /* END test_wc_RsaPSS_Verify */
  17624. /*
  17625. * Testing wc_RsaPSS_VerifyCheck()
  17626. */
  17627. static int test_wc_RsaPSS_VerifyCheck(void)
  17628. {
  17629. int res = TEST_SKIPPED;
  17630. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17631. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17632. RsaKey key;
  17633. WC_RNG rng;
  17634. int sz = 256; /* 2048/8 */
  17635. byte* pt;
  17636. byte digest[32];
  17637. word32 digestSz = sizeof(digest);
  17638. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17639. word32 pSignatureSz = sizeof(pSignature);
  17640. unsigned char pDecrypted[2048/8];
  17641. word32 outLen = sizeof(pDecrypted);
  17642. int ret = 0;
  17643. pt = pDecrypted;
  17644. XMEMSET(digest, 0, sizeof(digest));
  17645. XMEMSET(pSignature, 0, sizeof(pSignature));
  17646. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17647. if (ret == 0) {
  17648. ret = wc_InitRng(&rng);
  17649. }
  17650. if (ret == 0) {
  17651. ret = wc_RsaSetRNG(&key, &rng);
  17652. }
  17653. if (ret == 0) {
  17654. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17655. }
  17656. if (ret == 0) {
  17657. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17658. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17659. }
  17660. if (ret == 0) {
  17661. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17662. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17663. if (ret > 0) {
  17664. sz = ret;
  17665. ret = 0;
  17666. }
  17667. }
  17668. /* Bad cases */
  17669. if (ret == 0) {
  17670. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  17671. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17672. if (ret == BAD_FUNC_ARG) {
  17673. ret = 0;
  17674. }
  17675. }
  17676. if (ret == 0) {
  17677. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  17678. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17679. if (ret == BAD_FUNC_ARG) {
  17680. ret = 0;
  17681. }
  17682. }
  17683. if (ret == 0) {
  17684. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  17685. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17686. if (ret == BAD_FUNC_ARG) {
  17687. ret = 0;
  17688. }
  17689. }
  17690. if (ret == 0) {
  17691. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  17692. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17693. if (ret == BAD_FUNC_ARG) {
  17694. ret = 0;
  17695. }
  17696. }
  17697. /* Good case */
  17698. if (ret == 0) {
  17699. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  17700. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17701. if (ret > 0) {
  17702. ret = 0;
  17703. }
  17704. }
  17705. wc_FreeRsaKey(&key);
  17706. wc_FreeRng(&rng);
  17707. res = TEST_RES_CHECK(ret == 0);
  17708. #endif
  17709. return res;
  17710. } /* END test_wc_RsaPSS_VerifyCheck */
  17711. /*
  17712. * Testing wc_RsaPSS_VerifyCheckInline()
  17713. */
  17714. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17715. {
  17716. int res = TEST_SKIPPED;
  17717. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17718. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17719. RsaKey key;
  17720. WC_RNG rng;
  17721. int sz = 256;
  17722. byte* pt;
  17723. byte digest[32];
  17724. word32 digestSz = sizeof(digest);
  17725. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17726. unsigned char pDecrypted[2048/8];
  17727. int ret;
  17728. pt = pDecrypted;
  17729. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17730. XMEMSET(digest, 0, sizeof(digest));
  17731. XMEMSET(pSignature, 0, sizeof(pSignature));
  17732. if (ret == 0) {
  17733. ret = wc_InitRng(&rng);
  17734. }
  17735. if (ret == 0) {
  17736. ret = wc_RsaSetRNG(&key, &rng);
  17737. }
  17738. if (ret == 0) {
  17739. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  17740. }
  17741. if (ret == 0) {
  17742. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  17743. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  17744. }
  17745. if (ret == 0) {
  17746. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  17747. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  17748. if (ret > 0) {
  17749. sz = ret;
  17750. ret = 0;
  17751. }
  17752. }
  17753. /* Bad Cases */
  17754. if (ret == 0) {
  17755. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  17756. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17757. if (ret == BAD_FUNC_ARG) {
  17758. ret = 0;
  17759. }
  17760. }
  17761. if (ret == 0) {
  17762. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  17763. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17764. if (ret == BAD_FUNC_ARG) {
  17765. ret = 0;
  17766. }
  17767. }
  17768. if (ret == 0) {
  17769. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  17770. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17771. if (ret == BAD_FUNC_ARG) {
  17772. ret = 0;
  17773. }
  17774. }
  17775. if (ret == 0) {
  17776. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17777. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  17778. if (ret == BAD_FUNC_ARG) {
  17779. ret = 0;
  17780. }
  17781. }
  17782. /* Good case */
  17783. if (ret == 0) {
  17784. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  17785. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  17786. if (ret > 0) {
  17787. ret = 0;
  17788. }
  17789. }
  17790. wc_FreeRsaKey(&key);
  17791. wc_FreeRng(&rng);
  17792. res = TEST_RES_CHECK(ret == 0);
  17793. #endif
  17794. return res;
  17795. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17796. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17797. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17798. {
  17799. (void)flag;
  17800. (void)type;
  17801. (void)file;
  17802. (void)line;
  17803. }
  17804. #endif
  17805. /*
  17806. * Testing wc_LockMutex_ex
  17807. */
  17808. static int test_wc_LockMutex_ex(void)
  17809. {
  17810. int res = TEST_SKIPPED;
  17811. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17812. int ret = 0;
  17813. int flag = CRYPTO_LOCK;
  17814. int type = 0;
  17815. const char* file = "./test-LockMutex_ex.txt";
  17816. int line = 0;
  17817. /* without SetMutexCb */
  17818. ret = wc_LockMutex_ex(flag, type, file, line);
  17819. if (ret == BAD_STATE_E) {
  17820. ret = 0;
  17821. }
  17822. /* with SetMutexCb */
  17823. if (ret == 0) {
  17824. ret = wc_SetMutexCb(sample_mutex_cb);
  17825. if (ret == 0) {
  17826. ret = wc_LockMutex_ex(flag, type, file, line);
  17827. }
  17828. }
  17829. res = TEST_RES_CHECK(ret == 0);
  17830. #endif
  17831. return res;
  17832. }/*End test_wc_LockMutex_ex*/
  17833. /*
  17834. * Testing wc_SetMutexCb
  17835. */
  17836. static int test_wc_SetMutexCb(void)
  17837. {
  17838. int res = TEST_SKIPPED;
  17839. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17840. int ret = wc_SetMutexCb(sample_mutex_cb);
  17841. res = TEST_RES_CHECK(ret == 0);
  17842. #endif
  17843. return res;
  17844. }/*End test_wc_SetMutexCb*/
  17845. /*
  17846. * Testing wc_RsaKeyToDer()
  17847. */
  17848. static int test_wc_RsaKeyToDer(void)
  17849. {
  17850. int res = TEST_SKIPPED;
  17851. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17852. RsaKey genKey;
  17853. WC_RNG rng;
  17854. byte* der;
  17855. int ret = 0;
  17856. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17857. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17858. int bits = 1024;
  17859. word32 derSz = 611;
  17860. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17861. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17862. #else
  17863. int bits = 2048;
  17864. word32 derSz = 1196;
  17865. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17866. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17867. #endif
  17868. XMEMSET(&rng, 0, sizeof(rng));
  17869. XMEMSET(&genKey, 0, sizeof(genKey));
  17870. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17871. if (der == NULL) {
  17872. ret = WOLFSSL_FATAL_ERROR;
  17873. }
  17874. /* Init structures. */
  17875. if (ret == 0) {
  17876. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17877. }
  17878. if (ret == 0) {
  17879. ret = wc_InitRng(&rng);
  17880. }
  17881. /* Make key. */
  17882. if (ret == 0) {
  17883. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17884. if (ret != 0) {
  17885. ret = WOLFSSL_FATAL_ERROR;
  17886. }
  17887. }
  17888. if (ret == 0) {
  17889. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17890. if (ret > 0) {
  17891. ret = 0;
  17892. }
  17893. else {
  17894. ret = WOLFSSL_FATAL_ERROR;
  17895. }
  17896. }
  17897. #ifndef HAVE_USER_RSA
  17898. /* Pass good/bad args. */
  17899. if (ret == 0) {
  17900. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17901. if (ret == BAD_FUNC_ARG) {
  17902. /* Get just the output length */
  17903. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17904. }
  17905. if (ret > 0) {
  17906. /* Try Public Key. */
  17907. genKey.type = 0;
  17908. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17909. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17910. /* Put back to Private Key */
  17911. genKey.type = 1;
  17912. #endif
  17913. }
  17914. if (ret == BAD_FUNC_ARG) {
  17915. ret = 0;
  17916. }
  17917. else {
  17918. ret = WOLFSSL_FATAL_ERROR;
  17919. }
  17920. }
  17921. #else
  17922. /* Pass good/bad args. */
  17923. if (ret == 0) {
  17924. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17925. if (ret == USER_CRYPTO_ERROR) {
  17926. /* Get just the output length */
  17927. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17928. }
  17929. if (ret > 0) {
  17930. /* Try Public Key. */
  17931. genKey.type = 0;
  17932. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17933. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17934. /* Put back to Private Key */
  17935. genKey.type = 1;
  17936. #endif
  17937. }
  17938. if (ret == USER_CRYPTO_ERROR) {
  17939. ret = 0;
  17940. }
  17941. else {
  17942. ret = WOLFSSL_FATAL_ERROR;
  17943. }
  17944. }
  17945. #endif
  17946. if (der != NULL) {
  17947. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17948. }
  17949. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  17950. ret = WOLFSSL_FATAL_ERROR;
  17951. }
  17952. if (wc_FreeRng(&rng) || ret != 0) {
  17953. ret = WOLFSSL_FATAL_ERROR;
  17954. }
  17955. res = TEST_RES_CHECK(ret == 0);
  17956. #endif
  17957. return res;
  17958. } /* END test_wc_RsaKeyToDer */
  17959. /*
  17960. * Testing wc_RsaKeyToPublicDer()
  17961. */
  17962. static int test_wc_RsaKeyToPublicDer(void)
  17963. {
  17964. int res = TEST_SKIPPED;
  17965. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17966. RsaKey key;
  17967. WC_RNG rng;
  17968. byte* der;
  17969. int ret = 0;
  17970. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17971. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17972. int bits = 1024;
  17973. word32 derLen = 162;
  17974. #else
  17975. int bits = 2048;
  17976. word32 derLen = 294;
  17977. #endif
  17978. XMEMSET(&rng, 0, sizeof(rng));
  17979. XMEMSET(&key, 0, sizeof(key));
  17980. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17981. if (der == NULL) {
  17982. ret = WOLFSSL_FATAL_ERROR;
  17983. }
  17984. if (ret == 0) {
  17985. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17986. }
  17987. if (ret == 0) {
  17988. ret = wc_InitRng(&rng);
  17989. }
  17990. if (ret == 0) {
  17991. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17992. }
  17993. if (ret == 0) {
  17994. /* test getting size only */
  17995. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17996. if (ret >= 0)
  17997. ret = 0;
  17998. }
  17999. if (ret == 0) {
  18000. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  18001. if (ret >= 0) {
  18002. ret = 0;
  18003. }
  18004. else {
  18005. ret = WOLFSSL_FATAL_ERROR;
  18006. }
  18007. }
  18008. if (ret == 0) {
  18009. /* test getting size only */
  18010. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  18011. if (ret >= 0)
  18012. ret = 0;
  18013. }
  18014. if (ret == 0) {
  18015. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  18016. if (ret >= 0) {
  18017. ret = 0;
  18018. }
  18019. else {
  18020. ret = WOLFSSL_FATAL_ERROR;
  18021. }
  18022. }
  18023. #ifndef HAVE_USER_RSA
  18024. /* Pass in bad args. */
  18025. if (ret == 0) {
  18026. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18027. if (ret == BAD_FUNC_ARG) {
  18028. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18029. }
  18030. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  18031. ret = 0;
  18032. }
  18033. else {
  18034. ret = WOLFSSL_FATAL_ERROR;
  18035. }
  18036. }
  18037. #else
  18038. /* Pass in bad args. */
  18039. if (ret == 0) {
  18040. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  18041. if (ret == USER_CRYPTO_ERROR) {
  18042. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  18043. }
  18044. if (ret == USER_CRYPTO_ERROR) {
  18045. ret = 0;
  18046. }
  18047. else {
  18048. ret = WOLFSSL_FATAL_ERROR;
  18049. }
  18050. }
  18051. #endif
  18052. if (der != NULL) {
  18053. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18054. }
  18055. if (wc_FreeRsaKey(&key) || ret != 0) {
  18056. ret = WOLFSSL_FATAL_ERROR;
  18057. }
  18058. if (wc_FreeRng(&rng) || ret != 0) {
  18059. ret = WOLFSSL_FATAL_ERROR;
  18060. }
  18061. res = TEST_RES_CHECK(ret == 0);
  18062. #endif
  18063. return res;
  18064. } /* END test_wc_RsaKeyToPublicDer */
  18065. /*
  18066. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  18067. */
  18068. static int test_wc_RsaPublicEncryptDecrypt(void)
  18069. {
  18070. int res = TEST_SKIPPED;
  18071. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18072. RsaKey key;
  18073. WC_RNG rng;
  18074. int ret = 0;
  18075. const char inStr[] = TEST_STRING;
  18076. const word32 plainLen = (word32)TEST_STRING_SZ;
  18077. const word32 inLen = (word32)TEST_STRING_SZ;
  18078. int bits = TEST_RSA_BITS;
  18079. const word32 cipherLen = TEST_RSA_BYTES;
  18080. word32 cipherLenResult = cipherLen;
  18081. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18082. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18083. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18084. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18085. if (in == NULL || plain == NULL || cipher == NULL) {
  18086. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  18087. return MEMORY_E;
  18088. }
  18089. #endif
  18090. XMEMCPY(in, inStr, inLen);
  18091. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18092. if (ret == 0) {
  18093. ret = wc_InitRng(&rng);
  18094. }
  18095. if (ret == 0) {
  18096. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18097. }
  18098. /* Encrypt. */
  18099. if (ret == 0) {
  18100. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  18101. if (ret >= 0) {
  18102. cipherLenResult = ret;
  18103. ret = 0;
  18104. }
  18105. else {
  18106. ret = WOLFSSL_FATAL_ERROR;
  18107. }
  18108. }
  18109. /* Pass bad args. */
  18110. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  18111. if (ret != 0) {
  18112. return TEST_FAIL;
  18113. }
  18114. /* Decrypt */
  18115. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18116. /* Bind rng */
  18117. if (ret == 0) {
  18118. ret = wc_RsaSetRNG(&key, &rng);
  18119. }
  18120. #endif
  18121. if (ret == 0) {
  18122. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  18123. }
  18124. if (ret >= 0) {
  18125. ret = XMEMCMP(plain, inStr, plainLen);
  18126. }
  18127. /* Pass in bad args. */
  18128. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  18129. WC_FREE_VAR(in, NULL);
  18130. WC_FREE_VAR(plain, NULL);
  18131. WC_FREE_VAR(cipher, NULL);
  18132. if (wc_FreeRsaKey(&key) || ret != 0) {
  18133. ret = WOLFSSL_FATAL_ERROR;
  18134. }
  18135. if (wc_FreeRng(&rng) || ret != 0) {
  18136. ret = WOLFSSL_FATAL_ERROR;
  18137. }
  18138. res = TEST_RES_CHECK(ret == 0);
  18139. #endif
  18140. return res;
  18141. } /* END test_wc_RsaPublicEncryptDecrypt */
  18142. /*
  18143. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  18144. */
  18145. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  18146. {
  18147. int result = TEST_SKIPPED;
  18148. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  18149. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  18150. && !defined(NO_SHA256)
  18151. RsaKey key;
  18152. WC_RNG rng;
  18153. int ret;
  18154. const char inStr[] = TEST_STRING;
  18155. const word32 inLen = (word32)TEST_STRING_SZ;
  18156. const word32 plainSz = (word32)TEST_STRING_SZ;
  18157. byte* res = NULL;
  18158. int idx = 0;
  18159. int bits = TEST_RSA_BITS;
  18160. const word32 cipherSz = TEST_RSA_BYTES;
  18161. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18162. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18163. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  18164. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18165. if (in == NULL || plain == NULL || cipher == NULL) {
  18166. fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  18167. return TEST_FAIL;
  18168. }
  18169. #endif
  18170. XMEMCPY(in, inStr, inLen);
  18171. /* Initialize stack structures. */
  18172. XMEMSET(&rng, 0, sizeof(rng));
  18173. XMEMSET(&key, 0, sizeof(key));
  18174. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  18175. if (ret == 0) {
  18176. ret = wc_InitRng(&rng);
  18177. }
  18178. if (ret == 0) {
  18179. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18180. }
  18181. /* Encrypt */
  18182. if (ret == 0) {
  18183. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  18184. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  18185. if (ret >= 0) {
  18186. idx = ret;
  18187. ret = 0;
  18188. }
  18189. else {
  18190. ret = WOLFSSL_FATAL_ERROR;
  18191. }
  18192. }
  18193. /* Pass bad args. */
  18194. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  18195. if (ret != 0) {
  18196. return TEST_FAIL;
  18197. }
  18198. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  18199. /* Decrypt */
  18200. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  18201. if (ret == 0) {
  18202. ret = wc_RsaSetRNG(&key, &rng);
  18203. }
  18204. #endif
  18205. if (ret == 0) {
  18206. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  18207. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18208. WC_MGF1SHA256, NULL, 0);
  18209. }
  18210. if (ret >= 0) {
  18211. if (!XMEMCMP(plain, inStr, plainSz)) {
  18212. ret = 0;
  18213. }
  18214. else {
  18215. ret = WOLFSSL_FATAL_ERROR;
  18216. }
  18217. }
  18218. /*Pass bad args.*/
  18219. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  18220. if (ret != 0) {
  18221. return TEST_FAIL;
  18222. }
  18223. if (ret == 0) {
  18224. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  18225. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
  18226. WC_MGF1SHA256, NULL, 0);
  18227. if (ret >= 0) {
  18228. if (!XMEMCMP(inStr, res, plainSz)) {
  18229. ret = 0;
  18230. }
  18231. else {
  18232. ret = WOLFSSL_FATAL_ERROR;
  18233. }
  18234. }
  18235. }
  18236. #endif
  18237. WC_FREE_VAR(in, NULL);
  18238. WC_FREE_VAR(plain, NULL);
  18239. WC_FREE_VAR(cipher, NULL);
  18240. if (wc_FreeRsaKey(&key) || ret != 0) {
  18241. ret = WOLFSSL_FATAL_ERROR;
  18242. }
  18243. if (wc_FreeRng(&rng) || ret != 0) {
  18244. ret = WOLFSSL_FATAL_ERROR;
  18245. }
  18246. result = TEST_RES_CHECK(ret == 0);
  18247. #endif
  18248. return result;
  18249. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  18250. /*
  18251. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  18252. */
  18253. static int test_wc_RsaSSL_SignVerify(void)
  18254. {
  18255. int res = TEST_SKIPPED;
  18256. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18257. RsaKey key;
  18258. WC_RNG rng;
  18259. int ret = 0;
  18260. const char inStr[] = TEST_STRING;
  18261. const word32 plainSz = (word32)TEST_STRING_SZ;
  18262. const word32 inLen = (word32)TEST_STRING_SZ;
  18263. word32 idx = 0;
  18264. int bits = TEST_RSA_BITS;
  18265. const word32 outSz = TEST_RSA_BYTES;
  18266. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  18267. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  18268. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  18269. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  18270. if (in == NULL || out == NULL || plain == NULL) {
  18271. fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
  18272. return TEST_FAIL;
  18273. }
  18274. #endif
  18275. XMEMCPY(in, inStr, inLen);
  18276. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18277. if (ret == 0) {
  18278. ret = wc_InitRng(&rng);
  18279. }
  18280. if (ret == 0) {
  18281. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18282. }
  18283. /* Sign. */
  18284. if (ret == 0) {
  18285. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  18286. if (ret == (int)outSz) {
  18287. idx = ret;
  18288. ret = 0;
  18289. }
  18290. else {
  18291. ret = WOLFSSL_FATAL_ERROR;
  18292. }
  18293. }
  18294. #ifndef HAVE_USER_RSA
  18295. /* Test bad args. */
  18296. if (ret == 0) {
  18297. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18298. if (ret == BAD_FUNC_ARG) {
  18299. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18300. }
  18301. if (ret == BAD_FUNC_ARG) {
  18302. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18303. }
  18304. if (ret == BAD_FUNC_ARG) {
  18305. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18306. }
  18307. if (ret == BAD_FUNC_ARG) {
  18308. ret = 0;
  18309. }
  18310. else {
  18311. ret = WOLFSSL_FATAL_ERROR;
  18312. }
  18313. }
  18314. #else
  18315. /* Test bad args. */
  18316. if (ret == 0) {
  18317. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  18318. if (ret == USER_CRYPTO_ERROR) {
  18319. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  18320. }
  18321. if (ret == USER_CRYPTO_ERROR) {
  18322. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  18323. }
  18324. if (ret == USER_CRYPTO_ERROR) {
  18325. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  18326. }
  18327. if (ret == USER_CRYPTO_ERROR) {
  18328. ret = 0;
  18329. }
  18330. else {
  18331. ret = WOLFSSL_FATAL_ERROR;
  18332. }
  18333. }
  18334. #endif
  18335. if (ret != 0) {
  18336. return TEST_FAIL;
  18337. }
  18338. /* Verify. */
  18339. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  18340. if (ret == (int)inLen) {
  18341. ret = 0;
  18342. }
  18343. else {
  18344. ret = WOLFSSL_FATAL_ERROR;
  18345. }
  18346. #ifndef HAVE_USER_RSA
  18347. /* Pass bad args. */
  18348. if (ret == 0) {
  18349. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18350. if (ret == BAD_FUNC_ARG) {
  18351. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18352. }
  18353. if (ret == BAD_FUNC_ARG) {
  18354. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18355. }
  18356. if (ret == BAD_FUNC_ARG) {
  18357. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18358. }
  18359. if (ret == BAD_FUNC_ARG) {
  18360. ret = 0;
  18361. }
  18362. else {
  18363. ret = WOLFSSL_FATAL_ERROR;
  18364. }
  18365. }
  18366. #else
  18367. /* Pass bad args. */
  18368. if (ret == 0) {
  18369. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  18370. if (ret == USER_CRYPTO_ERROR) {
  18371. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  18372. }
  18373. if (ret == USER_CRYPTO_ERROR) {
  18374. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  18375. }
  18376. if (ret == USER_CRYPTO_ERROR) {
  18377. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  18378. }
  18379. if (ret == USER_CRYPTO_ERROR) {
  18380. ret = 0;
  18381. }
  18382. else {
  18383. ret = WOLFSSL_FATAL_ERROR;
  18384. }
  18385. }
  18386. #endif
  18387. WC_FREE_VAR(in, NULL);
  18388. WC_FREE_VAR(out, NULL);
  18389. WC_FREE_VAR(plain, NULL);
  18390. if (wc_FreeRsaKey(&key) || ret != 0) {
  18391. ret = WOLFSSL_FATAL_ERROR;
  18392. }
  18393. if (wc_FreeRng(&rng) || ret != 0) {
  18394. ret = WOLFSSL_FATAL_ERROR;
  18395. }
  18396. res = TEST_RES_CHECK(ret == 0);
  18397. #endif
  18398. return res;
  18399. } /* END test_wc_RsaSSL_SignVerify */
  18400. /*
  18401. * Testing wc_RsaEncryptSize()
  18402. */
  18403. static int test_wc_RsaEncryptSize(void)
  18404. {
  18405. int res = TEST_SKIPPED;
  18406. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18407. RsaKey key;
  18408. WC_RNG rng;
  18409. int ret;
  18410. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18411. if (ret == 0) {
  18412. ret = wc_InitRng(&rng);
  18413. }
  18414. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18415. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18416. if (ret == 0) {
  18417. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  18418. if (ret == 0) {
  18419. ret = wc_RsaEncryptSize(&key);
  18420. }
  18421. if (ret == 128) {
  18422. ret = 0;
  18423. }
  18424. else {
  18425. ret = WOLFSSL_FATAL_ERROR;
  18426. }
  18427. }
  18428. if (wc_FreeRsaKey(&key) || ret != 0) {
  18429. ret = WOLFSSL_FATAL_ERROR;
  18430. }
  18431. else {
  18432. ret = 0;
  18433. }
  18434. #endif
  18435. if (ret == 0) {
  18436. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  18437. if (ret == 0) {
  18438. ret = wc_RsaEncryptSize(&key);
  18439. }
  18440. if (ret == 256) {
  18441. ret = 0;
  18442. }
  18443. else {
  18444. ret = WOLFSSL_FATAL_ERROR;
  18445. }
  18446. }
  18447. /* Pass in bad arg. */
  18448. if (ret == 0) {
  18449. ret = wc_RsaEncryptSize(NULL);
  18450. #ifndef HAVE_USER_RSA
  18451. if (ret == BAD_FUNC_ARG) {
  18452. ret = 0;
  18453. }
  18454. else {
  18455. ret = WOLFSSL_FATAL_ERROR;
  18456. }
  18457. #endif
  18458. }
  18459. if (wc_FreeRsaKey(&key) || ret != 0) {
  18460. ret = WOLFSSL_FATAL_ERROR;
  18461. }
  18462. if (wc_FreeRng(&rng) || ret != 0) {
  18463. ret = WOLFSSL_FATAL_ERROR;
  18464. }
  18465. res = TEST_RES_CHECK(ret == 0);
  18466. #endif
  18467. return res;
  18468. } /* END test_wc_RsaEncryptSize*/
  18469. /*
  18470. * Testing wc_RsaFlattenPublicKey()
  18471. */
  18472. static int test_wc_RsaFlattenPublicKey(void)
  18473. {
  18474. int res = TEST_SKIPPED;
  18475. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  18476. RsaKey key;
  18477. WC_RNG rng;
  18478. int ret = 0;
  18479. byte e[256];
  18480. byte n[256];
  18481. word32 eSz = sizeof(e);
  18482. word32 nSz = sizeof(n);
  18483. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  18484. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  18485. int bits = 1024;
  18486. #else
  18487. int bits = 2048;
  18488. #endif
  18489. ret = wc_InitRsaKey(&key, HEAP_HINT);
  18490. if (ret == 0) {
  18491. ret = wc_InitRng(&rng);
  18492. }
  18493. if (ret == 0) {
  18494. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  18495. if (ret >= 0) {
  18496. ret = 0;
  18497. }
  18498. else {
  18499. ret = WOLFSSL_FATAL_ERROR;
  18500. }
  18501. }
  18502. if (ret == 0) {
  18503. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  18504. }
  18505. #ifndef HAVE_USER_RSA
  18506. /* Pass bad args. */
  18507. if (ret == 0) {
  18508. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  18509. if (ret == BAD_FUNC_ARG) {
  18510. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  18511. }
  18512. if (ret == BAD_FUNC_ARG) {
  18513. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  18514. }
  18515. if (ret == BAD_FUNC_ARG) {
  18516. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  18517. }
  18518. if (ret == BAD_FUNC_ARG) {
  18519. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  18520. }
  18521. if (ret == BAD_FUNC_ARG) {
  18522. ret = 0;
  18523. }
  18524. else {
  18525. ret = WOLFSSL_FATAL_ERROR;
  18526. }
  18527. }
  18528. #else
  18529. /* Pass bad args. */
  18530. if (ret == 0) {
  18531. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  18532. if (ret == USER_CRYPTO_ERROR) {
  18533. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  18534. }
  18535. if (ret == USER_CRYPTO_ERROR) {
  18536. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  18537. }
  18538. if (ret == USER_CRYPTO_ERROR) {
  18539. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  18540. }
  18541. if (ret == USER_CRYPTO_ERROR) {
  18542. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  18543. }
  18544. if (ret == USER_CRYPTO_ERROR) {
  18545. ret = 0;
  18546. }
  18547. else {
  18548. ret = WOLFSSL_FATAL_ERROR;
  18549. }
  18550. }
  18551. #endif
  18552. if (wc_FreeRsaKey(&key) || ret != 0) {
  18553. ret = WOLFSSL_FATAL_ERROR;
  18554. }
  18555. if (wc_FreeRng(&rng) || ret != 0) {
  18556. ret = WOLFSSL_FATAL_ERROR;
  18557. }
  18558. res = TEST_RES_CHECK(ret == 0);
  18559. #endif
  18560. return res;
  18561. } /* END test_wc_RsaFlattenPublicKey */
  18562. /*
  18563. * unit test for wc_AesCcmSetKey
  18564. */
  18565. static int test_wc_AesCcmSetKey(void)
  18566. {
  18567. int res = TEST_SKIPPED;
  18568. #ifdef HAVE_AESCCM
  18569. Aes aes;
  18570. int ret = 0;
  18571. const byte key16[] =
  18572. {
  18573. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18574. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18575. };
  18576. const byte key24[] =
  18577. {
  18578. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18579. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18580. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  18581. };
  18582. const byte key32[] =
  18583. {
  18584. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18585. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  18586. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  18587. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  18588. };
  18589. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18590. if (ret != 0)
  18591. return ret;
  18592. #ifdef WOLFSSL_AES_128
  18593. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18594. #endif
  18595. #ifdef WOLFSSL_AES_192
  18596. if (ret == 0) {
  18597. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  18598. }
  18599. #endif
  18600. #ifdef WOLFSSL_AES_256
  18601. if (ret == 0) {
  18602. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  18603. }
  18604. #endif
  18605. /* Test bad args. */
  18606. if (ret == 0) {
  18607. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  18608. if (ret == BAD_FUNC_ARG) {
  18609. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  18610. }
  18611. if (ret == BAD_FUNC_ARG) {
  18612. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  18613. }
  18614. if (ret != BAD_FUNC_ARG) {
  18615. ret = WOLFSSL_FATAL_ERROR;
  18616. }
  18617. else {
  18618. ret = 0;
  18619. }
  18620. }
  18621. wc_AesFree(&aes);
  18622. res = TEST_RES_CHECK(ret == 0);
  18623. #endif
  18624. return res;
  18625. } /* END test_wc_AesCcmSetKey */
  18626. /*
  18627. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18628. */
  18629. static int test_wc_AesCcmEncryptDecrypt(void)
  18630. {
  18631. int res = TEST_SKIPPED;
  18632. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18633. Aes aes;
  18634. int ret = 0;
  18635. const byte key16[] =
  18636. {
  18637. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18638. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18639. };
  18640. /* plaintext */
  18641. const byte plainT[] =
  18642. {
  18643. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18644. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18645. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18646. };
  18647. /* nonce */
  18648. const byte iv[] =
  18649. {
  18650. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18651. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18652. };
  18653. const byte c[] = /* cipher text. */
  18654. {
  18655. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18656. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18657. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18658. };
  18659. const byte t[] = /* Auth tag */
  18660. {
  18661. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18662. };
  18663. const byte authIn[] =
  18664. {
  18665. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18666. };
  18667. byte cipherOut[sizeof(plainT)];
  18668. byte authTag[sizeof(t)];
  18669. int ccmE = WOLFSSL_FATAL_ERROR;
  18670. #ifdef HAVE_AES_DECRYPT
  18671. int ccmD = WOLFSSL_FATAL_ERROR;
  18672. byte plainOut[sizeof(cipherOut)];
  18673. #endif
  18674. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  18675. if (ret != 0)
  18676. return ret;
  18677. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  18678. if (ret == 0) {
  18679. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18680. iv, sizeof(iv), authTag, sizeof(authTag),
  18681. authIn , sizeof(authIn));
  18682. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  18683. XMEMCMP(t, authTag, sizeof(t))) {
  18684. ccmE = WOLFSSL_FATAL_ERROR;
  18685. ret = WOLFSSL_FATAL_ERROR;
  18686. }
  18687. #ifdef HAVE_AES_DECRYPT
  18688. if (ret == 0) {
  18689. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18690. sizeof(plainOut), iv, sizeof(iv),
  18691. authTag, sizeof(authTag),
  18692. authIn, sizeof(authIn));
  18693. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  18694. ccmD = WOLFSSL_FATAL_ERROR;
  18695. }
  18696. }
  18697. #endif
  18698. }
  18699. /* Pass in bad args. Encrypt*/
  18700. if (ret == 0 && ccmE == 0) {
  18701. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18702. iv, sizeof(iv), authTag, sizeof(authTag),
  18703. authIn , sizeof(authIn));
  18704. if (ccmE == BAD_FUNC_ARG) {
  18705. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18706. iv, sizeof(iv), authTag, sizeof(authTag),
  18707. authIn , sizeof(authIn));
  18708. }
  18709. if (ccmE == BAD_FUNC_ARG) {
  18710. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18711. iv, sizeof(iv), authTag, sizeof(authTag),
  18712. authIn , sizeof(authIn));
  18713. }
  18714. if (ccmE == BAD_FUNC_ARG) {
  18715. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18716. NULL, sizeof(iv), authTag, sizeof(authTag),
  18717. authIn , sizeof(authIn));
  18718. }
  18719. if (ccmE == BAD_FUNC_ARG) {
  18720. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18721. iv, sizeof(iv), NULL, sizeof(authTag),
  18722. authIn , sizeof(authIn));
  18723. }
  18724. if (ccmE == BAD_FUNC_ARG) {
  18725. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18726. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  18727. authIn , sizeof(authIn));
  18728. }
  18729. if (ccmE == BAD_FUNC_ARG) {
  18730. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18731. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  18732. authIn , sizeof(authIn));
  18733. }
  18734. if (ccmE != BAD_FUNC_ARG) {
  18735. ccmE = WOLFSSL_FATAL_ERROR;
  18736. }
  18737. else {
  18738. ccmE = 0;
  18739. }
  18740. } /* End Encrypt */
  18741. if (ccmE != 0) {
  18742. wc_AesFree(&aes);
  18743. return TEST_FAIL;
  18744. }
  18745. #ifdef HAVE_AES_DECRYPT
  18746. /* Pass in bad args. Decrypt*/
  18747. if (ret == 0 && ccmD == 0) {
  18748. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18749. iv, sizeof(iv), authTag, sizeof(authTag),
  18750. authIn, sizeof(authIn));
  18751. if (ccmD == BAD_FUNC_ARG) {
  18752. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18753. iv, sizeof(iv), authTag, sizeof(authTag),
  18754. authIn, sizeof(authIn));
  18755. }
  18756. if (ccmD == BAD_FUNC_ARG) {
  18757. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18758. iv, sizeof(iv), authTag, sizeof(authTag),
  18759. authIn, sizeof(authIn));
  18760. }
  18761. if (ccmD == BAD_FUNC_ARG) {
  18762. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18763. sizeof(plainOut), NULL, sizeof(iv),
  18764. authTag, sizeof(authTag),
  18765. authIn, sizeof(authIn));
  18766. }
  18767. if (ccmD == BAD_FUNC_ARG) {
  18768. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18769. sizeof(plainOut), iv, sizeof(iv), NULL,
  18770. sizeof(authTag), authIn, sizeof(authIn));
  18771. }
  18772. if (ccmD == BAD_FUNC_ARG) {
  18773. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18774. sizeof(plainOut), iv, sizeof(iv) + 1,
  18775. authTag, sizeof(authTag),
  18776. authIn, sizeof(authIn));
  18777. }
  18778. if (ccmD == BAD_FUNC_ARG) {
  18779. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  18780. sizeof(plainOut), iv, sizeof(iv) - 7,
  18781. authTag, sizeof(authTag),
  18782. authIn, sizeof(authIn));
  18783. }
  18784. if (ccmD != BAD_FUNC_ARG) {
  18785. ccmD = WOLFSSL_FATAL_ERROR;
  18786. }
  18787. else {
  18788. ccmD = 0;
  18789. }
  18790. } /* END Decrypt */
  18791. res = TEST_RES_CHECK(ccmD == 0);
  18792. #endif
  18793. wc_AesFree(&aes);
  18794. #endif /* HAVE_AESCCM */
  18795. return res;
  18796. } /* END test_wc_AesCcmEncryptDecrypt */
  18797. /*
  18798. * Testing wc_InitDsaKey()
  18799. */
  18800. static int test_wc_InitDsaKey(void)
  18801. {
  18802. int res = TEST_SKIPPED;
  18803. #ifndef NO_DSA
  18804. DsaKey key;
  18805. int ret = 0;
  18806. ret = wc_InitDsaKey(&key);
  18807. /* Pass in bad args. */
  18808. if (ret == 0) {
  18809. ret = wc_InitDsaKey(NULL);
  18810. if (ret == BAD_FUNC_ARG) {
  18811. ret = 0;
  18812. }
  18813. else {
  18814. ret = WOLFSSL_FATAL_ERROR;
  18815. }
  18816. }
  18817. wc_FreeDsaKey(&key);
  18818. res = TEST_RES_CHECK(ret == 0);
  18819. #endif
  18820. return res;
  18821. } /* END test_wc_InitDsaKey */
  18822. /*
  18823. * Testing wc_DsaSign() and wc_DsaVerify()
  18824. */
  18825. static int test_wc_DsaSignVerify(void)
  18826. {
  18827. int res = TEST_SKIPPED;
  18828. #if !defined(NO_DSA)
  18829. DsaKey key;
  18830. WC_RNG rng;
  18831. wc_Sha sha;
  18832. int ret = 0;
  18833. byte signature[DSA_SIG_SIZE];
  18834. byte hash[WC_SHA_DIGEST_SIZE];
  18835. word32 idx = 0;
  18836. word32 bytes;
  18837. int answer;
  18838. #ifdef USE_CERT_BUFFERS_1024
  18839. byte tmp[ONEK_BUF];
  18840. XMEMSET(tmp, 0, sizeof(tmp));
  18841. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18842. bytes = sizeof_dsa_key_der_1024;
  18843. #elif defined(USE_CERT_BUFFERS_2048)
  18844. byte tmp[TWOK_BUF];
  18845. XMEMSET(tmp, 0, sizeof(tmp));
  18846. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18847. bytes = sizeof_dsa_key_der_2048;
  18848. #else
  18849. byte tmp[TWOK_BUF];
  18850. XMEMSET(tmp, 0, sizeof(tmp));
  18851. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18852. if (fp == XBADFILE) {
  18853. return WOLFSSL_BAD_FILE;
  18854. }
  18855. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18856. XFCLOSE(fp);
  18857. #endif /* END USE_CERT_BUFFERS_1024 */
  18858. ret = wc_InitSha(&sha);
  18859. if (ret == 0) {
  18860. ret = wc_ShaUpdate(&sha, tmp, bytes);
  18861. if (ret == 0) {
  18862. ret = wc_ShaFinal(&sha, hash);
  18863. }
  18864. if (ret == 0) {
  18865. ret = wc_InitDsaKey(&key);
  18866. }
  18867. if (ret == 0) {
  18868. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18869. }
  18870. if (ret == 0) {
  18871. ret = wc_InitRng(&rng);
  18872. }
  18873. }
  18874. /* Sign. */
  18875. if (ret == 0) {
  18876. ret = wc_DsaSign(hash, signature, &key, &rng);
  18877. }
  18878. /* Test bad args. */
  18879. if (ret == 0) {
  18880. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18881. if (ret == BAD_FUNC_ARG) {
  18882. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18883. }
  18884. if (ret == BAD_FUNC_ARG) {
  18885. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18886. }
  18887. if (ret == BAD_FUNC_ARG) {
  18888. ret = wc_DsaSign(hash, signature, &key, NULL);
  18889. }
  18890. if (ret == BAD_FUNC_ARG) {
  18891. ret = 0;
  18892. }
  18893. else {
  18894. ret = WOLFSSL_FATAL_ERROR;
  18895. }
  18896. }
  18897. if (ret == 0) {
  18898. /* Verify. */
  18899. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18900. if (ret != 0 || answer != 1) {
  18901. ret = WOLFSSL_FATAL_ERROR;
  18902. }
  18903. else {
  18904. ret = 0;
  18905. }
  18906. }
  18907. /* Pass in bad args. */
  18908. if (ret == 0) {
  18909. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18910. if (ret == BAD_FUNC_ARG) {
  18911. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18912. }
  18913. if (ret == BAD_FUNC_ARG) {
  18914. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18915. }
  18916. if (ret == BAD_FUNC_ARG) {
  18917. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18918. }
  18919. if (ret == BAD_FUNC_ARG) {
  18920. ret = 0;
  18921. }
  18922. else {
  18923. ret = WOLFSSL_FATAL_ERROR;
  18924. }
  18925. }
  18926. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18927. /* hard set q to 0 and test fail case */
  18928. mp_free(&key.q);
  18929. mp_init(&key.q);
  18930. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18931. mp_set(&key.q, 1);
  18932. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18933. #endif
  18934. if (wc_FreeRng(&rng) && ret == 0) {
  18935. ret = WOLFSSL_FATAL_ERROR;
  18936. }
  18937. wc_FreeDsaKey(&key);
  18938. wc_ShaFree(&sha);
  18939. res = TEST_RES_CHECK(ret == 0);
  18940. #endif
  18941. return res;
  18942. } /* END test_wc_DsaSign */
  18943. /*
  18944. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18945. */
  18946. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18947. {
  18948. int res = TEST_SKIPPED;
  18949. #if !defined(NO_DSA)
  18950. DsaKey key;
  18951. word32 bytes;
  18952. word32 idx = 0;
  18953. int priv = 0;
  18954. int pub = 0;
  18955. int ret = 0;
  18956. #ifdef USE_CERT_BUFFERS_1024
  18957. byte tmp[ONEK_BUF];
  18958. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18959. bytes = sizeof_dsa_key_der_1024;
  18960. #elif defined(USE_CERT_BUFFERS_2048)
  18961. byte tmp[TWOK_BUF];
  18962. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18963. bytes = sizeof_dsa_key_der_2048;
  18964. #else
  18965. byte tmp[TWOK_BUF];
  18966. XMEMSET(tmp, 0, sizeof(tmp));
  18967. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18968. if (fp == XBADFILE)
  18969. {
  18970. return WOLFSSL_BAD_FILE;
  18971. }
  18972. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18973. XFCLOSE(fp);
  18974. #endif /* END USE_CERT_BUFFERS_1024 */
  18975. ret = wc_InitDsaKey(&key);
  18976. if (ret == 0) {
  18977. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18978. /* Test bad args. */
  18979. if (priv == 0) {
  18980. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18981. if (priv == BAD_FUNC_ARG) {
  18982. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18983. }
  18984. if (priv == BAD_FUNC_ARG) {
  18985. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18986. }
  18987. if (priv == BAD_FUNC_ARG) {
  18988. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18989. }
  18990. if (priv == ASN_PARSE_E || priv == BUFFER_E) {
  18991. priv = 0;
  18992. }
  18993. else {
  18994. priv = WOLFSSL_FATAL_ERROR;
  18995. }
  18996. }
  18997. wc_FreeDsaKey(&key);
  18998. ret = wc_InitDsaKey(&key);
  18999. }
  19000. if (ret == 0) {
  19001. idx = 0; /* Reset */
  19002. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  19003. /* Test bad args. */
  19004. if (pub == 0) {
  19005. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  19006. if (pub == BAD_FUNC_ARG) {
  19007. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  19008. }
  19009. if (pub == BAD_FUNC_ARG) {
  19010. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  19011. }
  19012. if (pub == BAD_FUNC_ARG) {
  19013. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  19014. }
  19015. if (pub == ASN_PARSE_E || pub == BUFFER_E) {
  19016. pub = 0;
  19017. }
  19018. else {
  19019. pub = WOLFSSL_FATAL_ERROR;
  19020. }
  19021. }
  19022. } /* END Public Key */
  19023. wc_FreeDsaKey(&key);
  19024. res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
  19025. #endif /* !NO_DSA */
  19026. return res;
  19027. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19028. /*
  19029. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19030. */
  19031. static int test_wc_MakeDsaKey(void)
  19032. {
  19033. int res = TEST_SKIPPED;
  19034. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19035. DsaKey genKey;
  19036. WC_RNG rng;
  19037. int ret = 0;
  19038. XMEMSET(&rng, 0, sizeof(rng));
  19039. XMEMSET(&genKey, 0, sizeof(genKey));
  19040. ret = wc_InitRng(&rng);
  19041. if (ret == 0) {
  19042. ret = wc_InitDsaKey(&genKey);
  19043. }
  19044. if (ret == 0) {
  19045. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19046. }
  19047. /* Test bad args. */
  19048. if (ret == 0) {
  19049. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  19050. if (ret == BAD_FUNC_ARG) {
  19051. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  19052. }
  19053. if (ret == BAD_FUNC_ARG) {
  19054. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  19055. }
  19056. if (ret == BAD_FUNC_ARG) {
  19057. ret = 0;
  19058. }
  19059. else {
  19060. ret = WOLFSSL_FATAL_ERROR;
  19061. }
  19062. }
  19063. if (ret == 0) {
  19064. ret = wc_MakeDsaKey(&rng, &genKey);
  19065. }
  19066. /* Test bad args. */
  19067. if (ret == 0) {
  19068. ret = wc_MakeDsaKey(NULL, &genKey);
  19069. if (ret == BAD_FUNC_ARG) {
  19070. ret = wc_MakeDsaKey(&rng, NULL);
  19071. }
  19072. if (ret == BAD_FUNC_ARG) {
  19073. ret = 0;
  19074. }
  19075. else {
  19076. ret = WOLFSSL_FATAL_ERROR;
  19077. }
  19078. }
  19079. if (wc_FreeRng(&rng) && ret == 0) {
  19080. ret = WOLFSSL_FAILURE;
  19081. }
  19082. wc_FreeDsaKey(&genKey);
  19083. res = TEST_RES_CHECK(ret == 0);
  19084. #endif
  19085. return res;
  19086. } /* END test_wc_MakeDsaKey */
  19087. /*
  19088. * Testing wc_DsaKeyToDer()
  19089. */
  19090. static int test_wc_DsaKeyToDer(void)
  19091. {
  19092. int res = TEST_SKIPPED;
  19093. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19094. DsaKey genKey;
  19095. WC_RNG rng;
  19096. word32 bytes;
  19097. word32 idx = 0;
  19098. int ret = 0;
  19099. #ifdef USE_CERT_BUFFERS_1024
  19100. byte tmp[ONEK_BUF];
  19101. byte der[ONEK_BUF];
  19102. XMEMSET(tmp, 0, sizeof(tmp));
  19103. XMEMSET(der, 0, sizeof(der));
  19104. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19105. bytes = sizeof_dsa_key_der_1024;
  19106. #elif defined(USE_CERT_BUFFERS_2048)
  19107. byte tmp[TWOK_BUF];
  19108. byte der[TWOK_BUF];
  19109. XMEMSET(tmp, 0, sizeof(tmp));
  19110. XMEMSET(der, 0, sizeof(der));
  19111. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19112. bytes = sizeof_dsa_key_der_2048;
  19113. #else
  19114. byte tmp[TWOK_BUF];
  19115. byte der[TWOK_BUF];
  19116. XMEMSET(tmp, 0, sizeof(tmp));
  19117. XMEMSET(der, 0, sizeof(der));
  19118. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  19119. if (fp == XBADFILE) {
  19120. return WOLFSSL_BAD_FILE;
  19121. }
  19122. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  19123. XFCLOSE(fp);
  19124. #endif /* END USE_CERT_BUFFERS_1024 */
  19125. XMEMSET(&rng, 0, sizeof(rng));
  19126. XMEMSET(&genKey, 0, sizeof(genKey));
  19127. ret = wc_InitRng(&rng);
  19128. if (ret == 0) {
  19129. ret = wc_InitDsaKey(&genKey);
  19130. }
  19131. if (ret == 0) {
  19132. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  19133. if (ret == 0) {
  19134. wc_FreeDsaKey(&genKey);
  19135. ret = wc_InitDsaKey(&genKey);
  19136. }
  19137. }
  19138. if (ret == 0) {
  19139. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  19140. }
  19141. if (ret == 0) {
  19142. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  19143. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  19144. ret = 0;
  19145. }
  19146. }
  19147. /* Test bad args. */
  19148. if (ret == 0) {
  19149. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  19150. if (ret == BAD_FUNC_ARG) {
  19151. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  19152. }
  19153. if (ret == BAD_FUNC_ARG) {
  19154. ret = 0;
  19155. }
  19156. else {
  19157. ret = WOLFSSL_FATAL_ERROR;
  19158. }
  19159. }
  19160. if (wc_FreeRng(&rng) && ret == 0) {
  19161. ret = WOLFSSL_FATAL_ERROR;
  19162. }
  19163. wc_FreeDsaKey(&genKey);
  19164. res = TEST_RES_CHECK(ret == 0);
  19165. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19166. return res;
  19167. } /* END test_wc_DsaKeyToDer */
  19168. /*
  19169. * Testing wc_DsaKeyToPublicDer()
  19170. * (indirectly testing setDsaPublicKey())
  19171. */
  19172. static int test_wc_DsaKeyToPublicDer(void)
  19173. {
  19174. int res = TEST_SKIPPED;
  19175. #ifndef HAVE_SELFTEST
  19176. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19177. DsaKey genKey;
  19178. WC_RNG rng;
  19179. byte* der;
  19180. word32 sz;
  19181. int ret = 0;
  19182. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19183. if (der == NULL) {
  19184. ret = WOLFSSL_FATAL_ERROR;
  19185. }
  19186. if (ret == 0) {
  19187. ret = wc_InitDsaKey(&genKey);
  19188. }
  19189. if (ret == 0) {
  19190. ret = wc_InitRng(&rng);
  19191. }
  19192. if (ret == 0) {
  19193. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  19194. }
  19195. if (ret == 0) {
  19196. ret = wc_MakeDsaKey(&rng, &genKey);
  19197. }
  19198. if (ret == 0) {
  19199. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  19200. if (ret >= 0) {
  19201. sz = ret;
  19202. ret = 0;
  19203. }
  19204. else {
  19205. ret = WOLFSSL_FATAL_ERROR;
  19206. }
  19207. }
  19208. if (ret == 0) {
  19209. word32 idx = 0;
  19210. wc_FreeDsaKey(&genKey);
  19211. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19212. }
  19213. /* Test without the SubjectPublicKeyInfo header */
  19214. if (ret == 0) {
  19215. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  19216. if (ret >= 0) {
  19217. sz = ret;
  19218. ret = 0;
  19219. }
  19220. else {
  19221. ret = WOLFSSL_FATAL_ERROR;
  19222. }
  19223. }
  19224. if (ret == 0) {
  19225. word32 idx = 0;
  19226. wc_FreeDsaKey(&genKey);
  19227. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  19228. }
  19229. /* Test bad args. */
  19230. if (ret == 0) {
  19231. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  19232. if (ret == BAD_FUNC_ARG) {
  19233. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  19234. }
  19235. if (ret == BAD_FUNC_ARG) {
  19236. ret = 0;
  19237. }
  19238. else {
  19239. ret = WOLFSSL_FATAL_ERROR;
  19240. }
  19241. }
  19242. if (wc_FreeRng(&rng) && ret == 0) {
  19243. ret = WOLFSSL_FATAL_ERROR;
  19244. }
  19245. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19246. wc_FreeDsaKey(&genKey);
  19247. res = TEST_RES_CHECK(ret == 0);
  19248. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19249. #endif /* !HAVE_SELFTEST */
  19250. return res;
  19251. } /* END test_wc_DsaKeyToPublicDer */
  19252. /*
  19253. * Testing wc_DsaImportParamsRaw()
  19254. */
  19255. static int test_wc_DsaImportParamsRaw(void)
  19256. {
  19257. int res = TEST_SKIPPED;
  19258. #if !defined(NO_DSA)
  19259. DsaKey key;
  19260. int ret = 0;
  19261. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19262. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19263. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19264. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19265. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19266. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19267. "80a0093113a8bd73";
  19268. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19269. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19270. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19271. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19272. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19273. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19274. "76341a7e7d9";
  19275. /* invalid p and q parameters */
  19276. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19277. const char* invalidQ = "96c5390a";
  19278. ret = wc_InitDsaKey(&key);
  19279. if (ret == 0) {
  19280. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19281. }
  19282. /* test bad args */
  19283. if (ret == 0) {
  19284. /* null key struct */
  19285. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  19286. if (ret == BAD_FUNC_ARG) {
  19287. /* null param pointers */
  19288. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  19289. }
  19290. if (ret == BAD_FUNC_ARG) {
  19291. /* illegal p length */
  19292. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  19293. }
  19294. if (ret == BAD_FUNC_ARG) {
  19295. /* illegal q length */
  19296. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  19297. if (ret == BAD_FUNC_ARG)
  19298. ret = 0;
  19299. }
  19300. }
  19301. wc_FreeDsaKey(&key);
  19302. res = TEST_RES_CHECK(ret == 0);
  19303. #endif
  19304. return res;
  19305. } /* END test_wc_DsaImportParamsRaw */
  19306. /*
  19307. * Testing wc_DsaImportParamsRawCheck()
  19308. */
  19309. static int test_wc_DsaImportParamsRawCheck(void)
  19310. {
  19311. int res = TEST_SKIPPED;
  19312. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19313. DsaKey key;
  19314. int ret = 0;
  19315. int trusted = 0;
  19316. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19317. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19318. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19319. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19320. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19321. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19322. "80a0093113a8bd73";
  19323. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19324. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19325. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19326. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19327. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19328. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19329. "76341a7e7d9";
  19330. /* invalid p and q parameters */
  19331. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19332. const char* invalidQ = "96c5390a";
  19333. ret = wc_InitDsaKey(&key);
  19334. if (ret == 0) {
  19335. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  19336. }
  19337. /* test bad args */
  19338. if (ret == 0) {
  19339. /* null key struct */
  19340. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  19341. if (ret == BAD_FUNC_ARG) {
  19342. /* null param pointers */
  19343. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  19344. }
  19345. if (ret == BAD_FUNC_ARG) {
  19346. /* illegal p length */
  19347. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  19348. }
  19349. if (ret == BAD_FUNC_ARG) {
  19350. /* illegal q length */
  19351. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  19352. if (ret == BAD_FUNC_ARG)
  19353. ret = 0;
  19354. }
  19355. }
  19356. wc_FreeDsaKey(&key);
  19357. res = TEST_RES_CHECK(ret == 0);
  19358. #endif
  19359. return res;
  19360. } /* END test_wc_DsaImportParamsRawCheck */
  19361. /*
  19362. * Testing wc_DsaExportParamsRaw()
  19363. */
  19364. static int test_wc_DsaExportParamsRaw(void)
  19365. {
  19366. int res = TEST_SKIPPED;
  19367. #if !defined(NO_DSA)
  19368. DsaKey key;
  19369. int ret = 0;
  19370. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19371. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19372. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19373. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19374. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19375. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19376. "80a0093113a8bd73";
  19377. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19378. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19379. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19380. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19381. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19382. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19383. "76341a7e7d9";
  19384. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19385. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19386. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19387. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19388. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19389. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19390. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19391. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19392. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19393. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19394. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19395. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19396. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19397. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19398. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19399. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19400. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19401. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19402. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19403. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19404. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19405. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19406. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19407. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19408. byte pOut[MAX_DSA_PARAM_SIZE];
  19409. byte qOut[MAX_DSA_PARAM_SIZE];
  19410. byte gOut[MAX_DSA_PARAM_SIZE];
  19411. word32 pOutSz, qOutSz, gOutSz;
  19412. ret = wc_InitDsaKey(&key);
  19413. if (ret == 0) {
  19414. /* first test using imported raw parameters, for expected */
  19415. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  19416. }
  19417. if (ret == 0) {
  19418. pOutSz = sizeof(pOut);
  19419. qOutSz = sizeof(qOut);
  19420. gOutSz = sizeof(gOut);
  19421. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19422. gOut, &gOutSz);
  19423. }
  19424. if (ret == 0) {
  19425. /* validate exported parameters are correct */
  19426. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  19427. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  19428. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  19429. ret = -1;
  19430. }
  19431. }
  19432. /* test bad args */
  19433. if (ret == 0) {
  19434. /* null key struct */
  19435. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  19436. gOut, &gOutSz);
  19437. if (ret == BAD_FUNC_ARG) {
  19438. /* null output pointers */
  19439. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  19440. NULL, &gOutSz);
  19441. }
  19442. if (ret == LENGTH_ONLY_E) {
  19443. /* null output size pointers */
  19444. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  19445. gOut, NULL);
  19446. }
  19447. if (ret == BAD_FUNC_ARG) {
  19448. /* p output buffer size too small */
  19449. pOutSz = 1;
  19450. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19451. gOut, &gOutSz);
  19452. pOutSz = sizeof(pOut);
  19453. }
  19454. if (ret == BUFFER_E) {
  19455. /* q output buffer size too small */
  19456. qOutSz = 1;
  19457. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19458. gOut, &gOutSz);
  19459. qOutSz = sizeof(qOut);
  19460. }
  19461. if (ret == BUFFER_E) {
  19462. /* g output buffer size too small */
  19463. gOutSz = 1;
  19464. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  19465. gOut, &gOutSz);
  19466. if (ret == BUFFER_E)
  19467. ret = 0;
  19468. }
  19469. }
  19470. wc_FreeDsaKey(&key);
  19471. res = TEST_RES_CHECK(ret == 0);
  19472. #endif
  19473. return res;
  19474. } /* END test_wc_DsaExportParamsRaw */
  19475. /*
  19476. * Testing wc_DsaExportKeyRaw()
  19477. */
  19478. static int test_wc_DsaExportKeyRaw(void)
  19479. {
  19480. int res = TEST_SKIPPED;
  19481. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19482. DsaKey key;
  19483. WC_RNG rng;
  19484. int ret = 0;
  19485. byte xOut[MAX_DSA_PARAM_SIZE];
  19486. byte yOut[MAX_DSA_PARAM_SIZE];
  19487. word32 xOutSz, yOutSz;
  19488. XMEMSET(&rng, 0, sizeof(rng));
  19489. XMEMSET(&key, 0, sizeof(key));
  19490. ret = wc_InitRng(&rng);
  19491. if (ret == 0) {
  19492. ret = wc_InitDsaKey(&key);
  19493. }
  19494. if (ret == 0) {
  19495. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  19496. if (ret == 0) {
  19497. ret = wc_MakeDsaKey(&rng, &key);
  19498. }
  19499. }
  19500. /* try successful export */
  19501. if (ret == 0) {
  19502. xOutSz = sizeof(xOut);
  19503. yOutSz = sizeof(yOut);
  19504. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19505. }
  19506. /* test bad args */
  19507. if (ret == 0) {
  19508. /* null key struct */
  19509. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  19510. if (ret == BAD_FUNC_ARG) {
  19511. /* null output pointers */
  19512. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  19513. }
  19514. if (ret == LENGTH_ONLY_E) {
  19515. /* null output size pointers */
  19516. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  19517. }
  19518. if (ret == BAD_FUNC_ARG) {
  19519. /* x output buffer size too small */
  19520. xOutSz = 1;
  19521. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19522. xOutSz = sizeof(xOut);
  19523. }
  19524. if (ret == BUFFER_E) {
  19525. /* y output buffer size too small */
  19526. yOutSz = 1;
  19527. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  19528. if (ret == BUFFER_E)
  19529. ret = 0;
  19530. }
  19531. }
  19532. wc_FreeDsaKey(&key);
  19533. wc_FreeRng(&rng);
  19534. res = TEST_RES_CHECK(ret == 0);
  19535. #endif
  19536. return res;
  19537. } /* END test_wc_DsaExportParamsRaw */
  19538. /*
  19539. * Testing wc_ed25519_make_key().
  19540. */
  19541. static int test_wc_ed25519_make_key(void)
  19542. {
  19543. int res = TEST_SKIPPED;
  19544. #if defined(HAVE_ED25519)
  19545. ed25519_key key;
  19546. WC_RNG rng;
  19547. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  19548. int ret = 0;
  19549. ret = wc_InitRng(&rng);
  19550. if (ret == 0) {
  19551. ret = wc_ed25519_init(&key);
  19552. }
  19553. if (ret == 0) {
  19554. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  19555. if (ret == ECC_PRIV_KEY_E) {
  19556. ret = 0;
  19557. }
  19558. else if (ret == 0) {
  19559. ret = -1;
  19560. }
  19561. }
  19562. if (ret == 0) {
  19563. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19564. }
  19565. /* Test bad args. */
  19566. if (ret == 0) {
  19567. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  19568. if (ret == BAD_FUNC_ARG) {
  19569. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  19570. }
  19571. if (ret == BAD_FUNC_ARG) {
  19572. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  19573. }
  19574. if (ret == BAD_FUNC_ARG) {
  19575. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  19576. }
  19577. if (ret == BAD_FUNC_ARG) {
  19578. ret = 0;
  19579. }
  19580. else if (ret == 0) {
  19581. ret = WOLFSSL_FATAL_ERROR;
  19582. }
  19583. }
  19584. if (wc_FreeRng(&rng) && ret == 0) {
  19585. ret = WOLFSSL_FATAL_ERROR;
  19586. }
  19587. wc_ed25519_free(&key);
  19588. res = TEST_RES_CHECK(ret == 0);
  19589. #endif
  19590. return res;
  19591. } /* END test_wc_ed25519_make_key */
  19592. /*
  19593. * Testing wc_ed25519_init()
  19594. */
  19595. static int test_wc_ed25519_init(void)
  19596. {
  19597. int res = TEST_SKIPPED;
  19598. #if defined(HAVE_ED25519)
  19599. ed25519_key key;
  19600. int ret = 0;
  19601. ret = wc_ed25519_init(&key);
  19602. /* Test bad args. */
  19603. if (ret == 0) {
  19604. ret = wc_ed25519_init(NULL);
  19605. if (ret == BAD_FUNC_ARG) {
  19606. ret = 0;
  19607. }
  19608. else if (ret == 0) {
  19609. ret = WOLFSSL_FATAL_ERROR;
  19610. }
  19611. }
  19612. wc_ed25519_free(&key);
  19613. res = TEST_RES_CHECK(ret == 0);
  19614. #endif
  19615. return res;
  19616. } /* END test_wc_ed25519_init */
  19617. /*
  19618. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19619. */
  19620. static int test_wc_ed25519_sign_msg(void)
  19621. {
  19622. int res = TEST_SKIPPED;
  19623. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19624. WC_RNG rng;
  19625. ed25519_key key;
  19626. int ret = 0;
  19627. byte msg[] = "Everybody gets Friday off.\n";
  19628. byte sig[ED25519_SIG_SIZE];
  19629. word32 msglen = sizeof(msg);
  19630. word32 siglen = sizeof(sig);
  19631. word32 badSigLen = sizeof(sig) - 1;
  19632. #ifdef HAVE_ED25519_VERIFY
  19633. int verify_ok = 0; /*1 = Verify success.*/
  19634. #endif
  19635. /* Initialize stack variables. */
  19636. XMEMSET(sig, 0, siglen);
  19637. /* Initialize key. */
  19638. ret = wc_InitRng(&rng);
  19639. if (ret == 0) {
  19640. ret = wc_ed25519_init(&key);
  19641. if (ret == 0) {
  19642. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19643. }
  19644. }
  19645. if (ret == 0) {
  19646. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  19647. }
  19648. /* Test bad args. */
  19649. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  19650. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  19651. if (ret == BAD_FUNC_ARG) {
  19652. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  19653. }
  19654. if (ret == BAD_FUNC_ARG) {
  19655. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  19656. }
  19657. if (ret == BAD_FUNC_ARG) {
  19658. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  19659. }
  19660. if (ret == BAD_FUNC_ARG) {
  19661. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  19662. }
  19663. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  19664. badSigLen -= 1;
  19665. ret = 0;
  19666. }
  19667. else if (ret == 0) {
  19668. ret = WOLFSSL_FATAL_ERROR;
  19669. }
  19670. } /* END sign */
  19671. #ifdef HAVE_ED25519_VERIFY
  19672. if (ret == 0) {
  19673. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  19674. if (ret == 0 && verify_ok == 1) {
  19675. ret = 0;
  19676. }
  19677. else if (ret == 0) {
  19678. ret = WOLFSSL_FATAL_ERROR;
  19679. }
  19680. /* Test bad args. */
  19681. if (ret == 0) {
  19682. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  19683. msglen, &verify_ok, &key),
  19684. BAD_FUNC_ARG);
  19685. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  19686. msglen, &verify_ok, &key),
  19687. BAD_FUNC_ARG);
  19688. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19689. &key);
  19690. if (ret == BAD_FUNC_ARG) {
  19691. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  19692. &verify_ok, &key);
  19693. }
  19694. if (ret == BAD_FUNC_ARG) {
  19695. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19696. NULL, &key);
  19697. }
  19698. if (ret == BAD_FUNC_ARG) {
  19699. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  19700. &verify_ok, NULL);
  19701. }
  19702. if (ret == BAD_FUNC_ARG) {
  19703. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  19704. &verify_ok, &key);
  19705. }
  19706. if (ret == BAD_FUNC_ARG) {
  19707. ret = 0;
  19708. }
  19709. else if (ret == 0) {
  19710. ret = WOLFSSL_FATAL_ERROR;
  19711. }
  19712. }
  19713. } /* END verify. */
  19714. #endif /* Verify. */
  19715. if (wc_FreeRng(&rng) && ret == 0) {
  19716. ret = WOLFSSL_FATAL_ERROR;
  19717. }
  19718. wc_ed25519_free(&key);
  19719. res = TEST_RES_CHECK(ret == 0);
  19720. #endif
  19721. return res;
  19722. } /* END test_wc_ed25519_sign_msg */
  19723. /*
  19724. * Testing wc_ed25519_import_public()
  19725. */
  19726. static int test_wc_ed25519_import_public(void)
  19727. {
  19728. int res = TEST_SKIPPED;
  19729. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19730. WC_RNG rng;
  19731. ed25519_key pubKey;
  19732. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19733. word32 inlen = sizeof(in);
  19734. int ret = 0;
  19735. ret = wc_InitRng(&rng);
  19736. if (ret == 0) {
  19737. ret = wc_ed25519_init(&pubKey);
  19738. if (ret == 0) {
  19739. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  19740. }
  19741. }
  19742. if (ret == 0) {
  19743. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  19744. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19745. ret = 0;
  19746. }
  19747. else {
  19748. ret = WOLFSSL_FATAL_ERROR;
  19749. }
  19750. /* Test bad args. */
  19751. if (ret == 0) {
  19752. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  19753. if (ret == BAD_FUNC_ARG) {
  19754. ret = wc_ed25519_import_public(in, inlen, NULL);
  19755. }
  19756. if (ret == BAD_FUNC_ARG) {
  19757. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  19758. }
  19759. if (ret == BAD_FUNC_ARG) {
  19760. ret = 0;
  19761. }
  19762. else if (ret == 0) {
  19763. ret = WOLFSSL_FATAL_ERROR;
  19764. }
  19765. }
  19766. }
  19767. if (wc_FreeRng(&rng) && ret == 0) {
  19768. ret = WOLFSSL_FATAL_ERROR;
  19769. }
  19770. wc_ed25519_free(&pubKey);
  19771. res = TEST_RES_CHECK(ret == 0);
  19772. #endif
  19773. return res;
  19774. } /* END wc_ed25519_import_public */
  19775. /*
  19776. * Testing wc_ed25519_import_private_key()
  19777. */
  19778. static int test_wc_ed25519_import_private_key(void)
  19779. {
  19780. int res = TEST_SKIPPED;
  19781. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19782. WC_RNG rng;
  19783. ed25519_key key;
  19784. int ret;
  19785. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19786. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19787. word32 privKeySz = sizeof(privKey);
  19788. word32 pubKeySz = sizeof(pubKey);
  19789. #ifdef HAVE_ED25519_KEY_EXPORT
  19790. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19791. word32 bothKeysSz = sizeof(bothKeys);
  19792. #endif
  19793. ret = wc_InitRng(&rng);
  19794. if (ret != 0) {
  19795. return ret;
  19796. }
  19797. ret = wc_ed25519_init(&key);
  19798. if (ret != 0) {
  19799. wc_FreeRng(&rng);
  19800. return ret;
  19801. }
  19802. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19803. if (ret == 0) {
  19804. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19805. pubKeySz, &key, 1);
  19806. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19807. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19808. ret = WOLFSSL_FATAL_ERROR;
  19809. }
  19810. }
  19811. #ifdef HAVE_ED25519_KEY_EXPORT
  19812. if (ret == 0)
  19813. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  19814. if (ret == 0) {
  19815. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19816. &key, 1);
  19817. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  19818. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19819. ret = WOLFSSL_FATAL_ERROR;
  19820. }
  19821. }
  19822. #endif
  19823. /* Test bad args. */
  19824. if (ret == 0) {
  19825. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19826. &key);
  19827. if (ret == BAD_FUNC_ARG) {
  19828. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19829. pubKeySz, &key);
  19830. }
  19831. if (ret == BAD_FUNC_ARG) {
  19832. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19833. pubKeySz, NULL);
  19834. }
  19835. if (ret == BAD_FUNC_ARG) {
  19836. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19837. pubKeySz, &key);
  19838. }
  19839. if (ret == BAD_FUNC_ARG) {
  19840. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19841. pubKeySz - 1, &key);
  19842. }
  19843. if (ret == BAD_FUNC_ARG) {
  19844. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19845. 0, &key);
  19846. }
  19847. if (ret == BAD_FUNC_ARG) {
  19848. ret = 0;
  19849. }
  19850. else if (ret == 0) {
  19851. ret = WOLFSSL_FATAL_ERROR;
  19852. }
  19853. }
  19854. if (wc_FreeRng(&rng) && ret == 0) {
  19855. ret = WOLFSSL_FATAL_ERROR;
  19856. }
  19857. wc_ed25519_free(&key);
  19858. res = TEST_RES_CHECK(ret == 0);
  19859. #endif
  19860. return res;
  19861. } /* END test_wc_ed25519_import_private_key */
  19862. /*
  19863. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19864. */
  19865. static int test_wc_ed25519_export(void)
  19866. {
  19867. int res = TEST_SKIPPED;
  19868. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19869. WC_RNG rng;
  19870. ed25519_key key;
  19871. int ret = 0;
  19872. byte priv[ED25519_PRV_KEY_SIZE];
  19873. byte pub[ED25519_PUB_KEY_SIZE];
  19874. word32 privSz = sizeof(priv);
  19875. word32 pubSz = sizeof(pub);
  19876. ret = wc_InitRng(&rng);
  19877. if (ret != 0) {
  19878. return ret;
  19879. }
  19880. ret = wc_ed25519_init(&key);
  19881. if (ret != 0) {
  19882. wc_FreeRng(&rng);
  19883. return ret;
  19884. }
  19885. if (ret == 0) {
  19886. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19887. }
  19888. if (ret == 0) {
  19889. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19890. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19891. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19892. ret = WOLFSSL_FATAL_ERROR;
  19893. }
  19894. if (ret == 0) {
  19895. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19896. if (ret == BAD_FUNC_ARG) {
  19897. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19898. }
  19899. if (ret == BAD_FUNC_ARG) {
  19900. ret = wc_ed25519_export_public(&key, pub, NULL);
  19901. }
  19902. if (ret == BAD_FUNC_ARG) {
  19903. ret = 0;
  19904. }
  19905. else if (ret == 0) {
  19906. ret = WOLFSSL_FATAL_ERROR;
  19907. }
  19908. }
  19909. }
  19910. if (ret == 0) {
  19911. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19912. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19913. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19914. ret = WOLFSSL_FATAL_ERROR;
  19915. }
  19916. if (ret == 0) {
  19917. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19918. if (ret == BAD_FUNC_ARG) {
  19919. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19920. }
  19921. if (ret == BAD_FUNC_ARG) {
  19922. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19923. }
  19924. if (ret == BAD_FUNC_ARG) {
  19925. ret = 0;
  19926. }
  19927. else if (ret == 0) {
  19928. ret = WOLFSSL_FATAL_ERROR;
  19929. }
  19930. }
  19931. }
  19932. if (wc_FreeRng(&rng) && ret == 0) {
  19933. ret = WOLFSSL_FATAL_ERROR;
  19934. }
  19935. wc_ed25519_free(&key);
  19936. res = TEST_RES_CHECK(ret == 0);
  19937. #endif
  19938. return res;
  19939. } /* END test_wc_ed25519_export */
  19940. /*
  19941. * Testing wc_ed25519_size()
  19942. */
  19943. static int test_wc_ed25519_size(void)
  19944. {
  19945. int res = TEST_SKIPPED;
  19946. #if defined(HAVE_ED25519)
  19947. WC_RNG rng;
  19948. ed25519_key key;
  19949. int ret;
  19950. ret = wc_InitRng(&rng);
  19951. if (ret != 0) {
  19952. return ret;
  19953. }
  19954. ret = wc_ed25519_init(&key);
  19955. if (ret != 0) {
  19956. wc_FreeRng(&rng);
  19957. return ret;
  19958. }
  19959. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19960. if (ret != 0) {
  19961. wc_FreeRng(&rng);
  19962. wc_ed25519_free(&key);
  19963. return ret;
  19964. }
  19965. ret = wc_ed25519_size(&key);
  19966. /* Test bad args. */
  19967. if (ret == ED25519_KEY_SIZE) {
  19968. ret = wc_ed25519_size(NULL);
  19969. if (ret == BAD_FUNC_ARG) {
  19970. ret = 0;
  19971. }
  19972. }
  19973. if (ret == 0) {
  19974. ret = wc_ed25519_sig_size(&key);
  19975. if (ret == ED25519_SIG_SIZE) {
  19976. ret = 0;
  19977. }
  19978. /* Test bad args. */
  19979. if (ret == 0) {
  19980. ret = wc_ed25519_sig_size(NULL);
  19981. if (ret == BAD_FUNC_ARG) {
  19982. ret = 0;
  19983. }
  19984. }
  19985. } /* END wc_ed25519_sig_size() */
  19986. if (ret == 0) {
  19987. ret = wc_ed25519_pub_size(&key);
  19988. if (ret == ED25519_PUB_KEY_SIZE) {
  19989. ret = 0;
  19990. }
  19991. if (ret == 0) {
  19992. ret = wc_ed25519_pub_size(NULL);
  19993. if (ret == BAD_FUNC_ARG) {
  19994. ret = 0;
  19995. }
  19996. }
  19997. } /* END wc_ed25519_pub_size */
  19998. if (ret == 0) {
  19999. ret = wc_ed25519_priv_size(&key);
  20000. if (ret == ED25519_PRV_KEY_SIZE) {
  20001. ret = 0;
  20002. }
  20003. if (ret == 0) {
  20004. ret = wc_ed25519_priv_size(NULL);
  20005. if (ret == BAD_FUNC_ARG) {
  20006. ret = 0;
  20007. }
  20008. }
  20009. } /* END wc_ed25519_pub_size */
  20010. if (wc_FreeRng(&rng) && ret == 0) {
  20011. ret = WOLFSSL_FATAL_ERROR;
  20012. }
  20013. wc_ed25519_free(&key);
  20014. res = TEST_RES_CHECK(ret == 0);
  20015. #endif
  20016. return res;
  20017. } /* END test_wc_ed25519_size */
  20018. /*
  20019. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20020. */
  20021. static int test_wc_ed25519_exportKey(void)
  20022. {
  20023. int res = TEST_SKIPPED;
  20024. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20025. WC_RNG rng;
  20026. ed25519_key key;
  20027. int ret = 0;
  20028. byte priv[ED25519_PRV_KEY_SIZE];
  20029. byte pub[ED25519_PUB_KEY_SIZE];
  20030. byte privOnly[ED25519_PRV_KEY_SIZE];
  20031. word32 privSz = sizeof(priv);
  20032. word32 pubSz = sizeof(pub);
  20033. word32 privOnlySz = sizeof(privOnly);
  20034. ret = wc_InitRng(&rng);
  20035. if (ret != 0) {
  20036. return TEST_FAIL;
  20037. }
  20038. ret = wc_ed25519_init(&key);
  20039. if (ret != 0) {
  20040. wc_FreeRng(&rng);
  20041. return TEST_FAIL;
  20042. }
  20043. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20044. if (ret != 0) {
  20045. wc_FreeRng(&rng);
  20046. wc_ed25519_free(&key);
  20047. return TEST_FAIL;
  20048. }
  20049. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  20050. if (ret == 0) {
  20051. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  20052. if (ret == BAD_FUNC_ARG) {
  20053. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  20054. }
  20055. if (ret == BAD_FUNC_ARG) {
  20056. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  20057. }
  20058. if (ret == BAD_FUNC_ARG) {
  20059. ret = 0;
  20060. }
  20061. else if (ret == 0) {
  20062. ret = WOLFSSL_FATAL_ERROR;
  20063. }
  20064. }
  20065. if (ret == 0) {
  20066. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  20067. if (ret == 0) {
  20068. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  20069. if (ret == BAD_FUNC_ARG) {
  20070. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  20071. }
  20072. if (ret == BAD_FUNC_ARG) {
  20073. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  20074. }
  20075. if (ret == BAD_FUNC_ARG) {
  20076. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  20077. }
  20078. if (ret == BAD_FUNC_ARG) {
  20079. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  20080. }
  20081. if (ret == BAD_FUNC_ARG) {
  20082. ret = 0;
  20083. }
  20084. else if (ret == 0) {
  20085. ret = WOLFSSL_FATAL_ERROR;
  20086. }
  20087. }
  20088. } /* END wc_ed25519_export_key() */
  20089. /* Cross check output. */
  20090. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20091. ret = WOLFSSL_FATAL_ERROR;
  20092. }
  20093. if (wc_FreeRng(&rng) && ret == 0) {
  20094. ret = WOLFSSL_FATAL_ERROR;
  20095. }
  20096. wc_ed25519_free(&key);
  20097. res = TEST_RES_CHECK(ret == 0);
  20098. #endif
  20099. return res;
  20100. } /* END test_wc_ed25519_exportKey */
  20101. /*
  20102. * Testing wc_Ed25519PublicKeyToDer
  20103. */
  20104. static int test_wc_Ed25519PublicKeyToDer(void)
  20105. {
  20106. int res = TEST_SKIPPED;
  20107. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20108. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20109. int tmp;
  20110. ed25519_key key;
  20111. byte derBuf[1024];
  20112. int ret = 0;
  20113. /* Test bad args */
  20114. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  20115. if (tmp != BAD_FUNC_ARG) {
  20116. ret = WOLFSSL_FATAL_ERROR;
  20117. }
  20118. if (ret == 0) {
  20119. wc_ed25519_init(&key);
  20120. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  20121. if (tmp != BUFFER_E) {
  20122. ret = WOLFSSL_FATAL_ERROR;
  20123. }
  20124. wc_ed25519_free(&key);
  20125. }
  20126. /* Test good args */
  20127. if (ret == 0) {
  20128. WC_RNG rng;
  20129. ret = wc_InitRng(&rng);
  20130. if (ret != 0) {
  20131. return TEST_FAIL;
  20132. }
  20133. ret = wc_ed25519_init(&key);
  20134. if (ret != 0) {
  20135. wc_FreeRng(&rng);
  20136. return TEST_FAIL;
  20137. }
  20138. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  20139. if (ret != 0) {
  20140. wc_FreeRng(&rng);
  20141. wc_ed25519_free(&key);
  20142. return TEST_FAIL;
  20143. }
  20144. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  20145. if (tmp <= 0) {
  20146. ret = WOLFSSL_FATAL_ERROR;
  20147. }
  20148. wc_FreeRng(&rng);
  20149. wc_ed25519_free(&key);
  20150. }
  20151. res = TEST_RES_CHECK(ret == 0);
  20152. #endif
  20153. return res;
  20154. } /* END testing wc_Ed25519PublicKeyToDer */
  20155. /*
  20156. * Testing wc_curve25519_init and wc_curve25519_free.
  20157. */
  20158. static int test_wc_curve25519_init(void)
  20159. {
  20160. int res = TEST_SKIPPED;
  20161. #if defined(HAVE_CURVE25519)
  20162. curve25519_key key;
  20163. int ret = 0;
  20164. ret = wc_curve25519_init(&key);
  20165. /* Test bad args for wc_curve25519_init */
  20166. if (ret == 0) {
  20167. ret = wc_curve25519_init(NULL);
  20168. if (ret == BAD_FUNC_ARG) {
  20169. ret = 0;
  20170. }
  20171. else if (ret == 0) {
  20172. ret = WOLFSSL_FATAL_ERROR;
  20173. }
  20174. }
  20175. /* Test good args for wc_curve_25519_free */
  20176. wc_curve25519_free(&key);
  20177. wc_curve25519_free(NULL);
  20178. res = TEST_RES_CHECK(ret == 0);
  20179. #endif
  20180. return res;
  20181. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20182. /*
  20183. * Testing test_wc_curve25519_size.
  20184. */
  20185. static int test_wc_curve25519_size(void)
  20186. {
  20187. int res = TEST_SKIPPED;
  20188. #if defined(HAVE_CURVE25519)
  20189. curve25519_key key;
  20190. int ret = 0;
  20191. ret = wc_curve25519_init(&key);
  20192. /* Test good args for wc_curve25519_size */
  20193. if (ret == 0) {
  20194. ret = wc_curve25519_size(&key);
  20195. }
  20196. /* Test bad args for wc_curve25519_size */
  20197. if (ret != 0) {
  20198. ret = wc_curve25519_size(NULL);
  20199. }
  20200. wc_curve25519_free(&key);
  20201. res = TEST_RES_CHECK(ret == 0);
  20202. #endif
  20203. return res;
  20204. } /* END test_wc_curve25519_size*/
  20205. /*
  20206. * Testing test_wc_curve25519_export_key_raw().
  20207. */
  20208. static int test_wc_curve25519_export_key_raw(void)
  20209. {
  20210. int res = TEST_SKIPPED;
  20211. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20212. curve25519_key key;
  20213. WC_RNG rng;
  20214. int ret = 0;
  20215. byte privateKey[CURVE25519_KEYSIZE];
  20216. byte publicKey[CURVE25519_KEYSIZE];
  20217. word32 prvkSz;
  20218. word32 pubkSz;
  20219. byte prik[CURVE25519_KEYSIZE];
  20220. byte pubk[CURVE25519_KEYSIZE];
  20221. word32 prksz;
  20222. word32 pbksz;
  20223. if (0 != wc_InitRng(&rng)) {
  20224. return TEST_FAIL;
  20225. }
  20226. if (0 != wc_curve25519_init(&key)) {
  20227. wc_FreeRng(&rng);
  20228. return TEST_FAIL;
  20229. }
  20230. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20231. /*
  20232. bad-argument-test cases
  20233. target function sould return BAD_FUNC_ARG
  20234. */
  20235. if (ret == 0) {
  20236. prvkSz = CURVE25519_KEYSIZE;
  20237. pubkSz = CURVE25519_KEYSIZE;
  20238. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20239. NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
  20240. ret = -1;
  20241. }
  20242. }
  20243. if (ret == 0) {
  20244. prvkSz = CURVE25519_KEYSIZE;
  20245. pubkSz = CURVE25519_KEYSIZE;
  20246. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20247. &key, NULL, &prvkSz, publicKey, &pubkSz)) {
  20248. ret = -1;
  20249. }
  20250. }
  20251. if (ret == 0) {
  20252. prvkSz = CURVE25519_KEYSIZE;
  20253. pubkSz = CURVE25519_KEYSIZE;
  20254. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20255. &key, privateKey, NULL, publicKey, &pubkSz)) {
  20256. ret = -1;
  20257. }
  20258. }
  20259. if (ret == 0) {
  20260. /* prvkSz = CURVE25519_KEYSIZE; */
  20261. pubkSz = CURVE25519_KEYSIZE;
  20262. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20263. &key, privateKey, &prvkSz, NULL, &pubkSz)) {
  20264. ret = -1;
  20265. }
  20266. }
  20267. if (ret == 0) {
  20268. prvkSz = CURVE25519_KEYSIZE;
  20269. pubkSz = CURVE25519_KEYSIZE;
  20270. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  20271. &key, privateKey, &prvkSz, publicKey, NULL )) {
  20272. ret = -1;
  20273. }
  20274. }
  20275. /*
  20276. cross-testing
  20277. */
  20278. if (ret == 0) {
  20279. prksz = CURVE25519_KEYSIZE;
  20280. ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
  20281. }
  20282. if (ret == 0) {
  20283. pbksz = CURVE25519_KEYSIZE;
  20284. ret = wc_curve25519_export_public(&key, pubk, &pbksz);
  20285. }
  20286. if (ret == 0) {
  20287. prvkSz = CURVE25519_KEYSIZE;
  20288. /* pubkSz = CURVE25519_KEYSIZE; */
  20289. ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20290. publicKey, &pubkSz);
  20291. }
  20292. if (ret == 0) {
  20293. if ((prksz == CURVE25519_KEYSIZE) &&
  20294. (pbksz == CURVE25519_KEYSIZE) &&
  20295. (prvkSz == CURVE25519_KEYSIZE) &&
  20296. (pubkSz == CURVE25519_KEYSIZE)) {
  20297. if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  20298. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
  20299. ret = -1;
  20300. }
  20301. }
  20302. }
  20303. wc_curve25519_free(&key);
  20304. wc_FreeRng(&rng);
  20305. res = TEST_RES_CHECK(ret == 0);
  20306. #endif
  20307. return res;
  20308. } /* end of test_wc_curve25519_export_key_raw */
  20309. /*
  20310. * Testing test_wc_curve25519_export_key_raw_ex().
  20311. */
  20312. static int test_wc_curve25519_export_key_raw_ex(void)
  20313. {
  20314. int res = TEST_SKIPPED;
  20315. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20316. curve25519_key key;
  20317. WC_RNG rng;
  20318. int ret;
  20319. byte privateKey[CURVE25519_KEYSIZE];
  20320. byte publicKey[CURVE25519_KEYSIZE];
  20321. word32 prvkSz;
  20322. word32 pubkSz;
  20323. byte prik[CURVE25519_KEYSIZE];
  20324. byte pubk[CURVE25519_KEYSIZE];
  20325. word32 prksz;
  20326. word32 pbksz;
  20327. if (0 != wc_InitRng(&rng)) {
  20328. return TEST_FAIL;
  20329. }
  20330. if (0 != wc_curve25519_init(&key)) {
  20331. wc_FreeRng(&rng);
  20332. return TEST_FAIL;
  20333. }
  20334. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20335. /*
  20336. bad-argument-test cases
  20337. target function sould return BAD_FUNC_ARG
  20338. */
  20339. if (ret == 0) {
  20340. prvkSz = CURVE25519_KEYSIZE;
  20341. pubkSz = CURVE25519_KEYSIZE;
  20342. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  20343. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20344. ret = -1;
  20345. }
  20346. }
  20347. if (ret == 0) {
  20348. prvkSz = CURVE25519_KEYSIZE;
  20349. pubkSz = CURVE25519_KEYSIZE;
  20350. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  20351. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20352. ret = -1;
  20353. }
  20354. }
  20355. if (ret == 0) {
  20356. prvkSz = CURVE25519_KEYSIZE;
  20357. pubkSz = CURVE25519_KEYSIZE;
  20358. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  20359. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20360. ret = -1;
  20361. }
  20362. }
  20363. if (ret == 0) {
  20364. /* prvkSz = CURVE25519_KEYSIZE; */
  20365. pubkSz = CURVE25519_KEYSIZE;
  20366. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20367. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
  20368. ret = -1;
  20369. }
  20370. }
  20371. if (ret == 0) {
  20372. prvkSz = CURVE25519_KEYSIZE;
  20373. pubkSz = CURVE25519_KEYSIZE;
  20374. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20375. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
  20376. ret = -1;
  20377. }
  20378. }
  20379. if (ret == 0) {
  20380. prvkSz = CURVE25519_KEYSIZE;
  20381. /* pubkSz = CURVE25519_KEYSIZE; */
  20382. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  20383. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20384. ret = -1;
  20385. }
  20386. }
  20387. if (ret == 0) {
  20388. prvkSz = CURVE25519_KEYSIZE;
  20389. pubkSz = CURVE25519_KEYSIZE;
  20390. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
  20391. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20392. ret = -1;
  20393. }
  20394. }
  20395. if (ret == 0) {
  20396. prvkSz = CURVE25519_KEYSIZE;
  20397. pubkSz = CURVE25519_KEYSIZE;
  20398. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20399. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  20400. ret = -1;
  20401. }
  20402. }
  20403. if (ret == 0) {
  20404. /* prvkSz = CURVE25519_KEYSIZE; */
  20405. pubkSz = CURVE25519_KEYSIZE;
  20406. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20407. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
  20408. ret = -1;
  20409. }
  20410. }
  20411. if (ret == 0) {
  20412. prvkSz = CURVE25519_KEYSIZE;
  20413. pubkSz = CURVE25519_KEYSIZE;
  20414. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  20415. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
  20416. ret = -1;
  20417. }
  20418. }
  20419. /* illegal value for endien */
  20420. if (ret == 0) {
  20421. prvkSz = CURVE25519_KEYSIZE;
  20422. /* pubkSz = CURVE25519_KEYSIZE; */
  20423. if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
  20424. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
  20425. ret = -1;
  20426. }
  20427. }
  20428. /*
  20429. cross-testing
  20430. */
  20431. if (ret == 0) {
  20432. prksz = CURVE25519_KEYSIZE;
  20433. ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
  20434. }
  20435. if (ret == 0) {
  20436. pbksz = CURVE25519_KEYSIZE;
  20437. ret = wc_curve25519_export_public( &key, pubk, &pbksz);
  20438. }
  20439. if (ret == 0) {
  20440. prvkSz = CURVE25519_KEYSIZE;
  20441. /* pubkSz = CURVE25519_KEYSIZE; */
  20442. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20443. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  20444. }
  20445. if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
  20446. pbksz != CURVE25519_KEYSIZE ||
  20447. prvkSz != CURVE25519_KEYSIZE ||
  20448. pubkSz != CURVE25519_KEYSIZE)) {
  20449. ret = -1;
  20450. }
  20451. if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
  20452. 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
  20453. ret = -1;
  20454. }
  20455. if (ret == 0) {
  20456. ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20457. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
  20458. }
  20459. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  20460. pubkSz != CURVE25519_KEYSIZE)) {
  20461. ret = -1;
  20462. }
  20463. /*
  20464. try once with another endian
  20465. */
  20466. if (ret == 0) {
  20467. prvkSz = CURVE25519_KEYSIZE;
  20468. pubkSz = CURVE25519_KEYSIZE;
  20469. ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20470. publicKey, &pubkSz, EC25519_BIG_ENDIAN);
  20471. }
  20472. if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
  20473. pubkSz != CURVE25519_KEYSIZE)) {
  20474. ret = -1;
  20475. }
  20476. wc_curve25519_free(&key);
  20477. wc_FreeRng(&rng);
  20478. res = TEST_RES_CHECK(ret == 0);
  20479. #endif
  20480. return res;
  20481. } /* end of test_wc_curve25519_export_key_raw_ex */
  20482. /*
  20483. * Testing wc_curve25519_make_key
  20484. */
  20485. static int test_wc_curve25519_make_key(void)
  20486. {
  20487. int res = TEST_SKIPPED;
  20488. #if defined(HAVE_CURVE25519)
  20489. WC_RNG rng;
  20490. curve25519_key key;
  20491. int keysize;
  20492. int ret;
  20493. ret = wc_curve25519_init(&key);
  20494. if (ret == 0) {
  20495. ret = wc_InitRng(&rng);
  20496. }
  20497. if (ret == 0) {
  20498. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20499. if (ret == 0) {
  20500. keysize = wc_curve25519_size(&key);
  20501. if (keysize != CURVE25519_KEYSIZE) {
  20502. ret = WOLFSSL_FATAL_ERROR;
  20503. }
  20504. }
  20505. if (ret == 0) {
  20506. ret = wc_curve25519_make_key(&rng, keysize, &key);
  20507. }
  20508. }
  20509. /*test bad cases*/
  20510. if (ret == 0) {
  20511. ret = wc_curve25519_make_key(NULL, 0, NULL);
  20512. if (ret == BAD_FUNC_ARG) {
  20513. ret = 0;
  20514. }
  20515. }
  20516. if (ret == 0) {
  20517. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  20518. if (ret == BAD_FUNC_ARG) {
  20519. ret = 0;
  20520. }
  20521. }
  20522. if (ret == 0) {
  20523. ret = wc_curve25519_make_key(NULL, keysize, &key);
  20524. if (ret == BAD_FUNC_ARG) {
  20525. ret = 0;
  20526. }
  20527. }
  20528. if (ret == 0) {
  20529. ret = wc_curve25519_make_key(&rng, 0, &key);
  20530. if (ret == ECC_BAD_ARG_E) {
  20531. ret = 0;
  20532. }
  20533. }
  20534. wc_curve25519_free(&key);
  20535. wc_FreeRng(&rng);
  20536. res = TEST_RES_CHECK(ret == 0);
  20537. #endif
  20538. return res;
  20539. } /*END test_wc_curve25519_make_key*/
  20540. /*
  20541. * Testing wc_curve25519_shared_secret_ex
  20542. */
  20543. static int test_wc_curve25519_shared_secret_ex(void)
  20544. {
  20545. int res = TEST_SKIPPED;
  20546. #if defined(HAVE_CURVE25519)
  20547. WC_RNG rng;
  20548. curve25519_key private_key, public_key;
  20549. byte out[CURVE25519_KEYSIZE];
  20550. word32 outLen = sizeof(out);
  20551. int endian = EC25519_BIG_ENDIAN;
  20552. int ret;
  20553. ret = wc_curve25519_init(&private_key);
  20554. if (ret == 0) {
  20555. ret = wc_curve25519_init(&public_key);
  20556. }
  20557. if (ret == 0) {
  20558. ret = wc_InitRng(&rng);
  20559. }
  20560. if (ret == 0) {
  20561. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  20562. }
  20563. if (ret == 0) {
  20564. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  20565. }
  20566. if (ret == 0) {
  20567. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20568. &outLen, endian);
  20569. }
  20570. /*test bad cases*/
  20571. if (ret == 0) {
  20572. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  20573. 0, endian);
  20574. if (ret == 0) {
  20575. ret = -1;
  20576. }
  20577. if (ret == BAD_FUNC_ARG) {
  20578. ret = 0;
  20579. }
  20580. }
  20581. if (ret == 0) {
  20582. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  20583. &outLen, endian);
  20584. if (ret == 0) {
  20585. ret = -1;
  20586. }
  20587. else if (ret == BAD_FUNC_ARG) {
  20588. ret = 0;
  20589. }
  20590. }
  20591. if (ret == 0) {
  20592. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  20593. &outLen, endian);
  20594. if (ret == 0) {
  20595. ret = -1;
  20596. }
  20597. else if (ret == BAD_FUNC_ARG) {
  20598. ret = 0;
  20599. }
  20600. }
  20601. if (ret == 0) {
  20602. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20603. &outLen, endian);
  20604. if (ret == 0) {
  20605. ret = -1;
  20606. }
  20607. else if (ret == BAD_FUNC_ARG) {
  20608. ret = 0;
  20609. }
  20610. }
  20611. if (ret == 0) {
  20612. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20613. NULL, endian);
  20614. if (ret == 0) {
  20615. ret = -1;
  20616. }
  20617. else if (ret == BAD_FUNC_ARG) {
  20618. ret = 0;
  20619. }
  20620. }
  20621. if (ret == 0) {
  20622. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  20623. *increasing to 0x8f checks for error being returned*/
  20624. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20625. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20626. &outLen, endian);
  20627. if (ret == 0) {
  20628. ret = -1;
  20629. }
  20630. else if (ret == ECC_BAD_ARG_E) {
  20631. ret = 0;
  20632. }
  20633. }
  20634. outLen = outLen - 2;
  20635. if (ret == 0) {
  20636. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20637. &outLen, endian);
  20638. if (ret == 0) {
  20639. ret = -1;
  20640. }
  20641. else if (ret == BAD_FUNC_ARG) {
  20642. ret = 0;
  20643. }
  20644. }
  20645. wc_curve25519_free(&private_key);
  20646. wc_curve25519_free(&public_key);
  20647. wc_FreeRng(&rng);
  20648. res = TEST_RES_CHECK(ret == 0);
  20649. #endif
  20650. return res;
  20651. } /*END test_wc_curve25519_shared_secret_ex*/
  20652. /*
  20653. * Testing wc_curve25519_make_pub
  20654. */
  20655. static int test_wc_curve25519_make_pub(void)
  20656. {
  20657. int res = TEST_SKIPPED;
  20658. #ifdef HAVE_CURVE25519
  20659. WC_RNG rng;
  20660. curve25519_key key;
  20661. byte out[CURVE25519_KEYSIZE];
  20662. int ret;
  20663. ret = wc_curve25519_init(&key);
  20664. if (ret == 0) {
  20665. ret = wc_InitRng(&rng);
  20666. if (ret == 0) {
  20667. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20668. }
  20669. }
  20670. if (ret == 0) {
  20671. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  20672. }
  20673. /*test bad cases*/
  20674. if (ret == 0) {
  20675. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  20676. if (ret == ECC_BAD_ARG_E) {
  20677. ret = 0;
  20678. }
  20679. }
  20680. if (ret == 0) {
  20681. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  20682. if (ret == ECC_BAD_ARG_E) {
  20683. ret = 0;
  20684. }
  20685. }
  20686. if (ret == 0) {
  20687. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  20688. if (ret == ECC_BAD_ARG_E) {
  20689. ret = 0;
  20690. }
  20691. }
  20692. if (ret == 0) {
  20693. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  20694. if (ret == ECC_BAD_ARG_E) {
  20695. ret = 0;
  20696. }
  20697. }
  20698. if (ret == 0) {
  20699. /* verify clamping test */
  20700. key.k[0] |= ~248;
  20701. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20702. if (ret == ECC_BAD_ARG_E) {
  20703. ret = 0;
  20704. }
  20705. key.k[0] &= 248;
  20706. }
  20707. /* repeat the expected-to-succeed test. */
  20708. if (ret == 0) {
  20709. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  20710. }
  20711. wc_curve25519_free(&key);
  20712. wc_FreeRng(&rng);
  20713. res = TEST_RES_CHECK(ret == 0);
  20714. #endif
  20715. return res;
  20716. } /*END test_wc_curve25519_make_pub */
  20717. /*
  20718. * Testing test_wc_curve25519_export_public_ex
  20719. */
  20720. static int test_wc_curve25519_export_public_ex(void)
  20721. {
  20722. int res = TEST_SKIPPED;
  20723. #if defined(HAVE_CURVE25519)
  20724. WC_RNG rng;
  20725. curve25519_key key;
  20726. byte out[CURVE25519_KEYSIZE];
  20727. word32 outLen = sizeof(out);
  20728. int endian = EC25519_BIG_ENDIAN;
  20729. int ret;
  20730. ret = wc_curve25519_init(&key);
  20731. if (ret == 0) {
  20732. ret = wc_InitRng(&rng);
  20733. }
  20734. if (ret == 0) {
  20735. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20736. if (ret == 0) {
  20737. ret = wc_curve25519_export_public(&key, out, &outLen);
  20738. }
  20739. if (ret == 0) {
  20740. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20741. }
  20742. }
  20743. /*test bad cases*/
  20744. if (ret == 0) {
  20745. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  20746. if (ret == BAD_FUNC_ARG) {
  20747. ret = 0;
  20748. }
  20749. }
  20750. if (ret == 0) {
  20751. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20752. if (ret == BAD_FUNC_ARG) {
  20753. ret = 0;
  20754. }
  20755. }
  20756. if (ret == 0) {
  20757. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20758. if (ret == BAD_FUNC_ARG) {
  20759. ret = 0;
  20760. }
  20761. }
  20762. if (ret == 0) {
  20763. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20764. if (ret == BAD_FUNC_ARG) {
  20765. ret = 0;
  20766. }
  20767. }
  20768. outLen = outLen - 2;
  20769. if (ret == 0) {
  20770. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20771. if (ret == ECC_BAD_ARG_E) {
  20772. ret = 0;
  20773. }
  20774. }
  20775. wc_curve25519_free(&key);
  20776. wc_FreeRng(&rng);
  20777. res = TEST_RES_CHECK(ret == 0);
  20778. #endif
  20779. return res;
  20780. } /*END test_wc_curve25519_export_public_ex*/
  20781. /*
  20782. * Testing test_wc_curve25519_import_private_raw_ex
  20783. */
  20784. static int test_wc_curve25519_import_private_raw_ex(void)
  20785. {
  20786. int res = TEST_SKIPPED;
  20787. #if defined(HAVE_CURVE25519)
  20788. WC_RNG rng;
  20789. curve25519_key key;
  20790. byte priv[CURVE25519_KEYSIZE];
  20791. byte pub[CURVE25519_KEYSIZE];
  20792. word32 privSz = sizeof(priv);
  20793. word32 pubSz = sizeof(pub);
  20794. int endian = EC25519_BIG_ENDIAN;
  20795. int ret;
  20796. ret = wc_curve25519_init(&key);
  20797. if (ret == 0) {
  20798. ret = wc_InitRng(&rng);
  20799. }
  20800. if (ret == 0) {
  20801. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20802. if (ret == 0) {
  20803. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20804. }
  20805. if (ret == 0) {
  20806. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20807. }
  20808. if (ret == 0) {
  20809. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20810. &key, endian);
  20811. }
  20812. }
  20813. /*test bad cases*/
  20814. if (ret == 0) {
  20815. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20816. endian);
  20817. if (ret == BAD_FUNC_ARG) {
  20818. ret = 0;
  20819. }
  20820. }
  20821. if (ret == 0) {
  20822. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20823. &key, endian);
  20824. if (ret == BAD_FUNC_ARG) {
  20825. ret = 0;
  20826. }
  20827. }
  20828. if (ret == 0) {
  20829. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20830. &key, endian);
  20831. if (ret == BAD_FUNC_ARG) {
  20832. ret = 0;
  20833. }
  20834. }
  20835. if (ret == 0) {
  20836. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20837. NULL, endian);
  20838. if (ret == BAD_FUNC_ARG) {
  20839. ret = 0;
  20840. }
  20841. }
  20842. if (ret == 0) {
  20843. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20844. &key, endian);
  20845. if (ret == ECC_BAD_ARG_E) {
  20846. ret = 0;
  20847. }
  20848. }
  20849. if (ret == 0) {
  20850. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20851. &key, endian);
  20852. if (ret == ECC_BAD_ARG_E) {
  20853. ret = 0;
  20854. }
  20855. }
  20856. if (ret == 0) {
  20857. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20858. &key, EC25519_LITTLE_ENDIAN);
  20859. }
  20860. wc_curve25519_free(&key);
  20861. wc_FreeRng(&rng);
  20862. res = TEST_RES_CHECK(ret == 0);
  20863. #endif
  20864. return res;
  20865. } /*END test_wc_curve25519_import_private_raw_ex*/
  20866. /*
  20867. * Testing test_wc_curve25519_import_private
  20868. */
  20869. static int test_wc_curve25519_import_private(void)
  20870. {
  20871. int res = TEST_SKIPPED;
  20872. #if defined(HAVE_CURVE25519)
  20873. curve25519_key key;
  20874. WC_RNG rng;
  20875. byte priv[CURVE25519_KEYSIZE];
  20876. word32 privSz = sizeof(priv);
  20877. int ret;
  20878. ret = wc_curve25519_init(&key);
  20879. if (ret == 0) {
  20880. ret = wc_InitRng(&rng);
  20881. }
  20882. if (ret == 0) {
  20883. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20884. if (ret == 0) {
  20885. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20886. }
  20887. }
  20888. if (ret == 0) {
  20889. ret = wc_curve25519_import_private(priv, privSz, &key);
  20890. }
  20891. wc_curve25519_free(&key);
  20892. wc_FreeRng(&rng);
  20893. res = TEST_RES_CHECK(ret == 0);
  20894. #endif
  20895. return res;
  20896. } /*END test_wc_curve25519_import*/
  20897. /*
  20898. * Testing test_wc_curve25519_export_private_raw_ex
  20899. */
  20900. static int test_wc_curve25519_export_private_raw_ex(void)
  20901. {
  20902. int res = TEST_SKIPPED;
  20903. #if defined(HAVE_CURVE25519)
  20904. curve25519_key key;
  20905. byte out[CURVE25519_KEYSIZE];
  20906. word32 outLen = sizeof(out);
  20907. int endian = EC25519_BIG_ENDIAN;
  20908. int ret;
  20909. ret = wc_curve25519_init(&key);
  20910. if (ret == 0) {
  20911. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20912. }
  20913. /*test bad cases*/
  20914. if (ret == 0) {
  20915. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20916. if (ret == BAD_FUNC_ARG) {
  20917. ret = 0;
  20918. }
  20919. }
  20920. if (ret == 0) {
  20921. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20922. if (ret == BAD_FUNC_ARG) {
  20923. ret = 0;
  20924. }
  20925. }
  20926. if (ret == 0) {
  20927. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20928. if (ret == BAD_FUNC_ARG) {
  20929. ret = 0;
  20930. }
  20931. }
  20932. if (ret == 0) {
  20933. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20934. if (ret == BAD_FUNC_ARG) {
  20935. ret = 0;
  20936. }
  20937. }
  20938. if (ret == 0) {
  20939. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20940. EC25519_LITTLE_ENDIAN);
  20941. }
  20942. outLen = outLen - 2;
  20943. if (ret == 0) {
  20944. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20945. if (ret == ECC_BAD_ARG_E) {
  20946. ret = 0;
  20947. }
  20948. }
  20949. wc_curve25519_free(&key);
  20950. res = TEST_RES_CHECK(ret == 0);
  20951. #endif
  20952. return res;
  20953. }/*END test_wc_curve25519_export_private_raw_ex*/
  20954. /*
  20955. * Testing wc_ed448_make_key().
  20956. */
  20957. static int test_wc_ed448_make_key(void)
  20958. {
  20959. int res = TEST_SKIPPED;
  20960. #if defined(HAVE_ED448)
  20961. ed448_key key;
  20962. WC_RNG rng;
  20963. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20964. int ret;
  20965. ret = wc_InitRng(&rng);
  20966. if (ret == 0) {
  20967. ret = wc_ed448_init(&key);
  20968. }
  20969. if (ret == 0) {
  20970. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20971. if (ret == ECC_PRIV_KEY_E) {
  20972. ret = 0;
  20973. }
  20974. else if (ret == 0) {
  20975. ret = -1;
  20976. }
  20977. }
  20978. if (ret == 0) {
  20979. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20980. }
  20981. /* Test bad args. */
  20982. if (ret == 0) {
  20983. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20984. if (ret == BAD_FUNC_ARG) {
  20985. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20986. }
  20987. if (ret == BAD_FUNC_ARG) {
  20988. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20989. }
  20990. if (ret == BAD_FUNC_ARG) {
  20991. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20992. }
  20993. if (ret == BAD_FUNC_ARG) {
  20994. ret = 0;
  20995. }
  20996. else if (ret == 0) {
  20997. ret = WOLFSSL_FATAL_ERROR;
  20998. }
  20999. }
  21000. if (wc_FreeRng(&rng) && ret == 0) {
  21001. ret = WOLFSSL_FATAL_ERROR;
  21002. }
  21003. wc_ed448_free(&key);
  21004. res = TEST_RES_CHECK(ret == 0);
  21005. #endif
  21006. return res;
  21007. } /* END test_wc_ed448_make_key */
  21008. /*
  21009. * Testing wc_ed448_init()
  21010. */
  21011. static int test_wc_ed448_init(void)
  21012. {
  21013. int res = TEST_SKIPPED;
  21014. #if defined(HAVE_ED448)
  21015. ed448_key key;
  21016. int ret;
  21017. ret = wc_ed448_init(&key);
  21018. /* Test bad args. */
  21019. if (ret == 0) {
  21020. ret = wc_ed448_init(NULL);
  21021. if (ret == BAD_FUNC_ARG) {
  21022. ret = 0;
  21023. }
  21024. else if (ret == 0) {
  21025. ret = WOLFSSL_FATAL_ERROR;
  21026. }
  21027. }
  21028. wc_ed448_free(&key);
  21029. res = TEST_RES_CHECK(ret == 0);
  21030. #endif
  21031. return res;
  21032. } /* END test_wc_ed448_init */
  21033. /*
  21034. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  21035. */
  21036. static int test_wc_ed448_sign_msg(void)
  21037. {
  21038. int res = TEST_SKIPPED;
  21039. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  21040. WC_RNG rng;
  21041. ed448_key key;
  21042. byte msg[] = "Everybody gets Friday off.\n";
  21043. byte sig[ED448_SIG_SIZE];
  21044. word32 msglen = sizeof(msg);
  21045. word32 siglen = sizeof(sig);
  21046. word32 badSigLen = sizeof(sig) - 1;
  21047. #ifdef HAVE_ED448_VERIFY
  21048. int verify_ok = 0; /*1 = Verify success.*/
  21049. #endif
  21050. int ret;
  21051. /* Initialize stack variables. */
  21052. XMEMSET(sig, 0, siglen);
  21053. /* Initialize key. */
  21054. ret = wc_InitRng(&rng);
  21055. if (ret == 0) {
  21056. ret = wc_ed448_init(&key);
  21057. if (ret == 0) {
  21058. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21059. }
  21060. }
  21061. if (ret == 0) {
  21062. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  21063. }
  21064. /* Test bad args. */
  21065. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  21066. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  21067. if (ret == BAD_FUNC_ARG) {
  21068. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  21069. }
  21070. if (ret == BAD_FUNC_ARG) {
  21071. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  21072. }
  21073. if (ret == BAD_FUNC_ARG) {
  21074. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  21075. }
  21076. if (ret == BAD_FUNC_ARG) {
  21077. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  21078. NULL, 0);
  21079. }
  21080. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  21081. badSigLen -= 1;
  21082. ret = 0;
  21083. }
  21084. else if (ret == 0) {
  21085. ret = WOLFSSL_FATAL_ERROR;
  21086. }
  21087. } /* END sign */
  21088. #ifdef HAVE_ED448_VERIFY
  21089. if (ret == 0) {
  21090. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  21091. &key, NULL, 0);
  21092. if (ret == 0 && verify_ok == 1) {
  21093. ret = 0;
  21094. }
  21095. else if (ret == 0) {
  21096. ret = WOLFSSL_FATAL_ERROR;
  21097. }
  21098. /* Test bad args. */
  21099. if (ret == 0) {
  21100. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  21101. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21102. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  21103. msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
  21104. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  21105. &key, NULL, 0);
  21106. if (ret == BAD_FUNC_ARG) {
  21107. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  21108. &verify_ok, &key, NULL, 0);
  21109. }
  21110. if (ret == BAD_FUNC_ARG) {
  21111. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21112. NULL, &key, NULL, 0);
  21113. }
  21114. if (ret == BAD_FUNC_ARG) {
  21115. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  21116. &verify_ok, NULL, NULL, 0);
  21117. }
  21118. if (ret == BAD_FUNC_ARG) {
  21119. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  21120. &verify_ok, &key, NULL, 0);
  21121. }
  21122. if (ret == BAD_FUNC_ARG) {
  21123. ret = 0;
  21124. }
  21125. else if (ret == 0) {
  21126. ret = WOLFSSL_FATAL_ERROR;
  21127. }
  21128. }
  21129. } /* END verify. */
  21130. #endif /* Verify. */
  21131. if (wc_FreeRng(&rng) && ret == 0) {
  21132. ret = WOLFSSL_FATAL_ERROR;
  21133. }
  21134. wc_ed448_free(&key);
  21135. res = TEST_RES_CHECK(ret == 0);
  21136. #endif
  21137. return res;
  21138. } /* END test_wc_ed448_sign_msg */
  21139. /*
  21140. * Testing wc_ed448_import_public()
  21141. */
  21142. static int test_wc_ed448_import_public(void)
  21143. {
  21144. int res = TEST_SKIPPED;
  21145. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21146. WC_RNG rng;
  21147. ed448_key pubKey;
  21148. const byte in[] =
  21149. "Ed448PublicKeyUnitTest.................................\n";
  21150. word32 inlen = sizeof(in);
  21151. int ret = 0;
  21152. ret = wc_InitRng(&rng);
  21153. if (ret == 0) {
  21154. ret = wc_ed448_init(&pubKey);
  21155. if (ret == 0) {
  21156. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  21157. }
  21158. }
  21159. if (ret == 0) {
  21160. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  21161. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  21162. ret = 0;
  21163. }
  21164. else {
  21165. ret = WOLFSSL_FATAL_ERROR;
  21166. }
  21167. /* Test bad args. */
  21168. if (ret == 0) {
  21169. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  21170. if (ret == BAD_FUNC_ARG) {
  21171. ret = wc_ed448_import_public(in, inlen, NULL);
  21172. }
  21173. if (ret == BAD_FUNC_ARG) {
  21174. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  21175. }
  21176. if (ret == BAD_FUNC_ARG) {
  21177. ret = 0;
  21178. }
  21179. else if (ret == 0) {
  21180. ret = WOLFSSL_FATAL_ERROR;
  21181. }
  21182. }
  21183. }
  21184. if (wc_FreeRng(&rng) && ret == 0) {
  21185. ret = WOLFSSL_FATAL_ERROR;
  21186. }
  21187. wc_ed448_free(&pubKey);
  21188. res = TEST_RES_CHECK(ret == 0);
  21189. #endif
  21190. return res;
  21191. } /* END wc_ed448_import_public */
  21192. /*
  21193. * Testing wc_ed448_import_private_key()
  21194. */
  21195. static int test_wc_ed448_import_private_key(void)
  21196. {
  21197. int res = TEST_SKIPPED;
  21198. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21199. WC_RNG rng;
  21200. ed448_key key;
  21201. const byte privKey[] =
  21202. "Ed448PrivateKeyUnitTest................................\n";
  21203. const byte pubKey[] =
  21204. "Ed448PublicKeyUnitTest.................................\n";
  21205. word32 privKeySz = sizeof(privKey);
  21206. word32 pubKeySz = sizeof(pubKey);
  21207. #ifdef HAVE_ED448_KEY_EXPORT
  21208. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21209. word32 bothKeysSz = sizeof(bothKeys);
  21210. #endif
  21211. int ret;
  21212. ret = wc_InitRng(&rng);
  21213. if (ret != 0) {
  21214. return TEST_FAIL;
  21215. }
  21216. ret = wc_ed448_init(&key);
  21217. if (ret != 0) {
  21218. wc_FreeRng(&rng);
  21219. return TEST_FAIL;
  21220. }
  21221. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21222. if (ret == 0) {
  21223. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21224. pubKeySz, &key, 1);
  21225. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21226. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21227. ret = WOLFSSL_FATAL_ERROR;
  21228. }
  21229. }
  21230. #ifdef HAVE_ED448_KEY_EXPORT
  21231. if (ret == 0)
  21232. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  21233. if (ret == 0) {
  21234. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21235. &key, 1);
  21236. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  21237. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  21238. ret = WOLFSSL_FATAL_ERROR;
  21239. }
  21240. }
  21241. #endif
  21242. /* Test bad args. */
  21243. if (ret == 0) {
  21244. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21245. &key);
  21246. if (ret == BAD_FUNC_ARG) {
  21247. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21248. pubKeySz, &key);
  21249. }
  21250. if (ret == BAD_FUNC_ARG) {
  21251. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21252. pubKeySz, NULL);
  21253. }
  21254. if (ret == BAD_FUNC_ARG) {
  21255. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21256. pubKeySz, &key);
  21257. }
  21258. if (ret == BAD_FUNC_ARG) {
  21259. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21260. pubKeySz - 1, &key);
  21261. }
  21262. if (ret == BAD_FUNC_ARG) {
  21263. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  21264. 0, &key);
  21265. }
  21266. if (ret == BAD_FUNC_ARG) {
  21267. ret = 0;
  21268. }
  21269. else if (ret == 0) {
  21270. ret = WOLFSSL_FATAL_ERROR;
  21271. }
  21272. }
  21273. if (wc_FreeRng(&rng) && ret == 0) {
  21274. ret = WOLFSSL_FATAL_ERROR;
  21275. }
  21276. wc_ed448_free(&key);
  21277. res = TEST_RES_CHECK(ret == 0);
  21278. #endif
  21279. return res;
  21280. } /* END test_wc_ed448_import_private_key */
  21281. /*
  21282. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21283. */
  21284. static int test_wc_ed448_export(void)
  21285. {
  21286. int res = TEST_SKIPPED;
  21287. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21288. WC_RNG rng;
  21289. ed448_key key;
  21290. byte priv[ED448_PRV_KEY_SIZE];
  21291. byte pub[ED448_PUB_KEY_SIZE];
  21292. word32 privSz = sizeof(priv);
  21293. word32 pubSz = sizeof(pub);
  21294. int ret;
  21295. ret = wc_InitRng(&rng);
  21296. if (ret != 0) {
  21297. return TEST_FAIL;
  21298. }
  21299. ret = wc_ed448_init(&key);
  21300. if (ret != 0) {
  21301. wc_FreeRng(&rng);
  21302. return TEST_FAIL;
  21303. }
  21304. if (ret == 0) {
  21305. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21306. }
  21307. if (ret == 0) {
  21308. ret = wc_ed448_export_public(&key, pub, &pubSz);
  21309. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  21310. XMEMCMP(key.p, pub, pubSz) != 0)) {
  21311. ret = WOLFSSL_FATAL_ERROR;
  21312. }
  21313. if (ret == 0) {
  21314. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  21315. if (ret == BAD_FUNC_ARG) {
  21316. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  21317. }
  21318. if (ret == BAD_FUNC_ARG) {
  21319. ret = wc_ed448_export_public(&key, pub, NULL);
  21320. }
  21321. if (ret == BAD_FUNC_ARG) {
  21322. ret = 0;
  21323. }
  21324. else if (ret == 0) {
  21325. ret = WOLFSSL_FATAL_ERROR;
  21326. }
  21327. }
  21328. }
  21329. if (ret == 0) {
  21330. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  21331. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  21332. XMEMCMP(key.k, priv, privSz) != 0)) {
  21333. ret = WOLFSSL_FATAL_ERROR;
  21334. }
  21335. if (ret == 0) {
  21336. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  21337. if (ret == BAD_FUNC_ARG) {
  21338. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  21339. }
  21340. if (ret == BAD_FUNC_ARG) {
  21341. ret = wc_ed448_export_private_only(&key, priv, NULL);
  21342. }
  21343. if (ret == BAD_FUNC_ARG) {
  21344. ret = 0;
  21345. }
  21346. else if (ret == 0) {
  21347. ret = WOLFSSL_FATAL_ERROR;
  21348. }
  21349. }
  21350. }
  21351. if (wc_FreeRng(&rng) && ret == 0) {
  21352. ret = WOLFSSL_FATAL_ERROR;
  21353. }
  21354. wc_ed448_free(&key);
  21355. res = TEST_RES_CHECK(ret == 0);
  21356. #endif
  21357. return res;
  21358. } /* END test_wc_ed448_export */
  21359. /*
  21360. * Testing wc_ed448_size()
  21361. */
  21362. static int test_wc_ed448_size(void)
  21363. {
  21364. int res = TEST_SKIPPED;
  21365. #if defined(HAVE_ED448)
  21366. WC_RNG rng;
  21367. ed448_key key;
  21368. int ret = 0;
  21369. ret = wc_InitRng(&rng);
  21370. if (ret != 0) {
  21371. return TEST_FAIL;
  21372. }
  21373. ret = wc_ed448_init(&key);
  21374. if (ret != 0) {
  21375. wc_FreeRng(&rng);
  21376. return TEST_FAIL;
  21377. }
  21378. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21379. if (ret != 0) {
  21380. wc_FreeRng(&rng);
  21381. wc_ed448_free(&key);
  21382. return TEST_FAIL;
  21383. }
  21384. ret = wc_ed448_size(&key);
  21385. /* Test bad args. */
  21386. if (ret == ED448_KEY_SIZE) {
  21387. ret = wc_ed448_size(NULL);
  21388. if (ret == BAD_FUNC_ARG) {
  21389. ret = 0;
  21390. }
  21391. }
  21392. if (ret == 0) {
  21393. ret = wc_ed448_sig_size(&key);
  21394. if (ret == ED448_SIG_SIZE) {
  21395. ret = 0;
  21396. }
  21397. /* Test bad args. */
  21398. if (ret == 0) {
  21399. ret = wc_ed448_sig_size(NULL);
  21400. if (ret == BAD_FUNC_ARG) {
  21401. ret = 0;
  21402. }
  21403. }
  21404. } /* END wc_ed448_sig_size() */
  21405. if (ret == 0) {
  21406. ret = wc_ed448_pub_size(&key);
  21407. if (ret == ED448_PUB_KEY_SIZE) {
  21408. ret = 0;
  21409. }
  21410. if (ret == 0) {
  21411. ret = wc_ed448_pub_size(NULL);
  21412. if (ret == BAD_FUNC_ARG) {
  21413. ret = 0;
  21414. }
  21415. }
  21416. } /* END wc_ed448_pub_size */
  21417. if (ret == 0) {
  21418. ret = wc_ed448_priv_size(&key);
  21419. if (ret == ED448_PRV_KEY_SIZE) {
  21420. ret = 0;
  21421. }
  21422. if (ret == 0) {
  21423. ret = wc_ed448_priv_size(NULL);
  21424. if (ret == BAD_FUNC_ARG) {
  21425. ret = 0;
  21426. }
  21427. }
  21428. } /* END wc_ed448_pub_size */
  21429. if (wc_FreeRng(&rng) && ret == 0) {
  21430. ret = WOLFSSL_FATAL_ERROR;
  21431. }
  21432. wc_ed448_free(&key);
  21433. res = TEST_RES_CHECK(ret == 0);
  21434. #endif
  21435. return res;
  21436. } /* END test_wc_ed448_size */
  21437. /*
  21438. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21439. */
  21440. static int test_wc_ed448_exportKey(void)
  21441. {
  21442. int res = TEST_SKIPPED;
  21443. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21444. WC_RNG rng;
  21445. ed448_key key;
  21446. byte priv[ED448_PRV_KEY_SIZE];
  21447. byte pub[ED448_PUB_KEY_SIZE];
  21448. byte privOnly[ED448_PRV_KEY_SIZE];
  21449. word32 privSz = sizeof(priv);
  21450. word32 pubSz = sizeof(pub);
  21451. word32 privOnlySz = sizeof(privOnly);
  21452. int ret;
  21453. ret = wc_InitRng(&rng);
  21454. if (ret != 0) {
  21455. return TEST_FAIL;
  21456. }
  21457. ret = wc_ed448_init(&key);
  21458. if (ret != 0) {
  21459. wc_FreeRng(&rng);
  21460. return TEST_FAIL;
  21461. }
  21462. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21463. if (ret != 0) {
  21464. wc_FreeRng(&rng);
  21465. wc_ed448_free(&key);
  21466. return TEST_FAIL;
  21467. }
  21468. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  21469. if (ret == 0) {
  21470. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  21471. if (ret == BAD_FUNC_ARG) {
  21472. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  21473. }
  21474. if (ret == BAD_FUNC_ARG) {
  21475. ret = wc_ed448_export_private(&key, privOnly, NULL);
  21476. }
  21477. if (ret == BAD_FUNC_ARG) {
  21478. ret = 0;
  21479. }
  21480. else if (ret == 0) {
  21481. ret = WOLFSSL_FATAL_ERROR;
  21482. }
  21483. }
  21484. if (ret == 0) {
  21485. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  21486. if (ret == 0) {
  21487. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  21488. if (ret == BAD_FUNC_ARG) {
  21489. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  21490. }
  21491. if (ret == BAD_FUNC_ARG) {
  21492. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  21493. }
  21494. if (ret == BAD_FUNC_ARG) {
  21495. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  21496. }
  21497. if (ret == BAD_FUNC_ARG) {
  21498. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  21499. }
  21500. if (ret == BAD_FUNC_ARG) {
  21501. ret = 0;
  21502. }
  21503. else if (ret == 0) {
  21504. ret = WOLFSSL_FATAL_ERROR;
  21505. }
  21506. }
  21507. } /* END wc_ed448_export_key() */
  21508. /* Cross check output. */
  21509. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  21510. ret = WOLFSSL_FATAL_ERROR;
  21511. }
  21512. if (wc_FreeRng(&rng) && ret == 0) {
  21513. ret = WOLFSSL_FATAL_ERROR;
  21514. }
  21515. wc_ed448_free(&key);
  21516. res = TEST_RES_CHECK(ret == 0);
  21517. #endif
  21518. return res;
  21519. } /* END test_wc_ed448_exportKey */
  21520. /*
  21521. * Testing wc_Ed448PublicKeyToDer
  21522. */
  21523. static int test_wc_Ed448PublicKeyToDer(void)
  21524. {
  21525. int res = TEST_SKIPPED;
  21526. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21527. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21528. int tmp;
  21529. ed448_key key;
  21530. byte derBuf[1024];
  21531. int ret = 0;
  21532. /* Test bad args */
  21533. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  21534. if (tmp != BAD_FUNC_ARG) {
  21535. ret = WOLFSSL_FATAL_ERROR;
  21536. }
  21537. if (ret == 0) {
  21538. wc_ed448_init(&key);
  21539. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  21540. if (tmp != BUFFER_E) {
  21541. ret = WOLFSSL_FATAL_ERROR;
  21542. }
  21543. wc_ed448_free(&key);
  21544. }
  21545. /* Test good args */
  21546. if (ret == 0) {
  21547. WC_RNG rng;
  21548. ret = wc_InitRng(&rng);
  21549. if (ret != 0) {
  21550. return TEST_FAIL;
  21551. }
  21552. ret = wc_ed448_init(&key);
  21553. if (ret != 0) {
  21554. wc_FreeRng(&rng);
  21555. return TEST_FAIL;
  21556. }
  21557. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  21558. if (ret != 0) {
  21559. wc_FreeRng(&rng);
  21560. wc_ed448_free(&key);
  21561. return TEST_FAIL;
  21562. }
  21563. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  21564. if (tmp <= 0) {
  21565. ret = WOLFSSL_FATAL_ERROR;
  21566. }
  21567. wc_FreeRng(&rng);
  21568. wc_ed448_free(&key);
  21569. }
  21570. res = TEST_RES_CHECK(ret == 0);
  21571. #endif
  21572. return res;
  21573. } /* END testing wc_Ed448PublicKeyToDer */
  21574. /*
  21575. * Testing wc_curve448_init and wc_curve448_free.
  21576. */
  21577. static int test_wc_curve448_init(void)
  21578. {
  21579. int res = TEST_SKIPPED;
  21580. #if defined(HAVE_CURVE448)
  21581. curve448_key key;
  21582. int ret = 0;
  21583. ret = wc_curve448_init(&key);
  21584. /* Test bad args for wc_curve448_init */
  21585. if (ret == 0) {
  21586. ret = wc_curve448_init(NULL);
  21587. if (ret == BAD_FUNC_ARG) {
  21588. ret = 0;
  21589. }
  21590. else if (ret == 0) {
  21591. ret = WOLFSSL_FATAL_ERROR;
  21592. }
  21593. }
  21594. /* Test good args for wc_curve_448_free */
  21595. wc_curve448_free(&key);
  21596. wc_curve448_free(NULL);
  21597. res = TEST_RES_CHECK(ret == 0);
  21598. #endif
  21599. return res;
  21600. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21601. /*
  21602. * Testing wc_curve448_make_key
  21603. */
  21604. static int test_wc_curve448_make_key(void)
  21605. {
  21606. int res = TEST_SKIPPED;
  21607. #if defined(HAVE_CURVE448)
  21608. WC_RNG rng;
  21609. curve448_key key;
  21610. int keysize;
  21611. int ret;
  21612. ret = wc_curve448_init(&key);
  21613. if (ret == 0) {
  21614. ret = wc_InitRng(&rng);
  21615. }
  21616. if (ret == 0) {
  21617. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21618. if (ret == 0) {
  21619. keysize = wc_curve448_size(&key);
  21620. if (keysize != CURVE448_KEY_SIZE) {
  21621. ret = WOLFSSL_FATAL_ERROR;
  21622. }
  21623. }
  21624. if (ret == 0) {
  21625. ret = wc_curve448_make_key(&rng, keysize, &key);
  21626. }
  21627. }
  21628. /* test bad cases */
  21629. if (ret == 0) {
  21630. ret = wc_curve448_make_key(NULL, 0, NULL);
  21631. if (ret == BAD_FUNC_ARG) {
  21632. ret = 0;
  21633. }
  21634. }
  21635. if (ret == 0) {
  21636. ret = wc_curve448_make_key(&rng, keysize, NULL);
  21637. if (ret == BAD_FUNC_ARG) {
  21638. ret = 0;
  21639. }
  21640. }
  21641. if (ret == 0) {
  21642. ret = wc_curve448_make_key(NULL, keysize, &key);
  21643. if (ret == BAD_FUNC_ARG) {
  21644. ret = 0;
  21645. }
  21646. }
  21647. if (ret == 0) {
  21648. ret = wc_curve448_make_key(&rng, 0, &key);
  21649. if (ret == ECC_BAD_ARG_E) {
  21650. ret = 0;
  21651. }
  21652. }
  21653. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21654. ret = WOLFSSL_FATAL_ERROR;
  21655. }
  21656. wc_curve448_free(&key);
  21657. res = TEST_RES_CHECK(ret == 0);
  21658. #endif
  21659. return res;
  21660. } /*END test_wc_curve448_make_key*/
  21661. /*
  21662. * Testing test_wc_curve448_shared_secret_ex
  21663. */
  21664. static int test_wc_curve448_shared_secret_ex(void)
  21665. {
  21666. int res = TEST_SKIPPED;
  21667. #if defined(HAVE_CURVE448)
  21668. WC_RNG rng;
  21669. curve448_key private_key, public_key;
  21670. byte out[CURVE448_KEY_SIZE];
  21671. word32 outLen = sizeof(out);
  21672. int endian = EC448_BIG_ENDIAN;
  21673. int ret;
  21674. ret = wc_curve448_init(&private_key);
  21675. if (ret == 0) {
  21676. ret = wc_InitRng(&rng);
  21677. if (ret == 0) {
  21678. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  21679. }
  21680. }
  21681. if (ret == 0) {
  21682. ret = wc_curve448_init(&public_key);
  21683. }
  21684. if (ret == 0) {
  21685. if (ret == 0) {
  21686. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  21687. }
  21688. }
  21689. if (ret == 0) {
  21690. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21691. &outLen, endian);
  21692. }
  21693. /* test bad cases */
  21694. if (ret == 0) {
  21695. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
  21696. if (ret == BAD_FUNC_ARG) {
  21697. ret = 0;
  21698. }
  21699. }
  21700. if (ret == 0) {
  21701. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  21702. &outLen, endian);
  21703. if (ret == BAD_FUNC_ARG) {
  21704. ret = 0;
  21705. }
  21706. }
  21707. if (ret == 0) {
  21708. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  21709. &outLen, endian);
  21710. if (ret == BAD_FUNC_ARG) {
  21711. ret = 0;
  21712. }
  21713. }
  21714. if (ret == 0) {
  21715. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21716. &outLen, endian);
  21717. if (ret == BAD_FUNC_ARG) {
  21718. ret = 0;
  21719. }
  21720. }
  21721. if (ret == 0) {
  21722. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21723. NULL, endian);
  21724. if (ret == BAD_FUNC_ARG) {
  21725. ret = 0;
  21726. }
  21727. }
  21728. outLen = outLen - 2;
  21729. if (ret == 0) {
  21730. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21731. &outLen, endian);
  21732. if (ret == BAD_FUNC_ARG) {
  21733. ret = 0;
  21734. }
  21735. }
  21736. wc_curve448_free(&private_key);
  21737. wc_curve448_free(&public_key);
  21738. wc_FreeRng(&rng);
  21739. res = TEST_RES_CHECK(ret == 0);
  21740. #endif
  21741. return res;
  21742. } /*END test_wc_curve448_shared_secret_ex*/
  21743. /*
  21744. * Testing test_wc_curve448_export_public_ex
  21745. */
  21746. static int test_wc_curve448_export_public_ex(void)
  21747. {
  21748. int res = TEST_SKIPPED;
  21749. #if defined(HAVE_CURVE448)
  21750. WC_RNG rng;
  21751. curve448_key key;
  21752. byte out[CURVE448_KEY_SIZE];
  21753. word32 outLen = sizeof(out);
  21754. int endian = EC448_BIG_ENDIAN;
  21755. int ret;
  21756. ret = wc_curve448_init(&key);
  21757. if (ret == 0) {
  21758. ret = wc_InitRng(&rng);
  21759. }
  21760. if (ret == 0) {
  21761. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21762. if (ret == 0) {
  21763. ret = wc_curve448_export_public(&key, out, &outLen);
  21764. }
  21765. if (ret == 0) {
  21766. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21767. }
  21768. }
  21769. /*test bad cases*/
  21770. if (ret == 0) {
  21771. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21772. if (ret == BAD_FUNC_ARG) {
  21773. ret = 0;
  21774. }
  21775. }
  21776. if (ret == 0) {
  21777. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21778. if (ret == BAD_FUNC_ARG) {
  21779. ret = 0;
  21780. }
  21781. }
  21782. if (ret == 0) {
  21783. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21784. if (ret == BAD_FUNC_ARG) {
  21785. ret = 0;
  21786. }
  21787. }
  21788. if (ret == 0) {
  21789. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21790. if (ret == BAD_FUNC_ARG) {
  21791. ret = 0;
  21792. }
  21793. }
  21794. outLen = outLen - 2;
  21795. if (ret == 0) {
  21796. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21797. if (ret == ECC_BAD_ARG_E) {
  21798. ret = 0;
  21799. }
  21800. }
  21801. wc_curve448_free(&key);
  21802. wc_FreeRng(&rng);
  21803. res = TEST_RES_CHECK(ret == 0);
  21804. #endif
  21805. return res;
  21806. } /*END test_wc_curve448_export_public_ex*/
  21807. /*
  21808. * Testing test_wc_curve448_export_private_raw_ex
  21809. */
  21810. static int test_wc_curve448_export_private_raw_ex(void)
  21811. {
  21812. int res = TEST_SKIPPED;
  21813. #if defined(HAVE_CURVE448)
  21814. curve448_key key;
  21815. byte out[CURVE448_KEY_SIZE];
  21816. word32 outLen = sizeof(out);
  21817. int endian = EC448_BIG_ENDIAN;
  21818. int ret;
  21819. ret = wc_curve448_init(&key);
  21820. if (ret == 0) {
  21821. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21822. }
  21823. /*test bad cases*/
  21824. if (ret == 0) {
  21825. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21826. if (ret == BAD_FUNC_ARG) {
  21827. ret = 0;
  21828. }
  21829. }
  21830. if (ret == 0) {
  21831. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21832. if (ret == BAD_FUNC_ARG) {
  21833. ret = 0;
  21834. }
  21835. }
  21836. if (ret == 0) {
  21837. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21838. if (ret == BAD_FUNC_ARG) {
  21839. ret = 0;
  21840. }
  21841. }
  21842. if (ret == 0) {
  21843. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21844. if (ret == BAD_FUNC_ARG) {
  21845. ret = 0;
  21846. }
  21847. }
  21848. if (ret == 0) {
  21849. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21850. EC448_LITTLE_ENDIAN);
  21851. }
  21852. outLen = outLen - 2;
  21853. if (ret == 0) {
  21854. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21855. if (ret == ECC_BAD_ARG_E) {
  21856. ret = 0;
  21857. }
  21858. }
  21859. wc_curve448_free(&key);
  21860. res = TEST_RES_CHECK(ret == 0);
  21861. #endif
  21862. return res;
  21863. }/*END test_wc_curve448_export_private_raw_ex*/
  21864. /*
  21865. * Testing test_wc_curve448_import_private_raw_ex
  21866. */
  21867. static int test_wc_curve448_import_private_raw_ex(void)
  21868. {
  21869. int res = TEST_SKIPPED;
  21870. #if defined(HAVE_CURVE448)
  21871. WC_RNG rng;
  21872. curve448_key key;
  21873. byte priv[CURVE448_KEY_SIZE];
  21874. byte pub[CURVE448_KEY_SIZE];
  21875. word32 privSz = sizeof(priv);
  21876. word32 pubSz = sizeof(pub);
  21877. int endian = EC448_BIG_ENDIAN;
  21878. int ret;
  21879. ret = wc_curve448_init(&key);
  21880. if (ret == 0) {
  21881. ret = wc_InitRng(&rng);
  21882. }
  21883. if (ret == 0) {
  21884. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21885. if (ret == 0) {
  21886. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21887. }
  21888. if (ret == 0) {
  21889. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21890. }
  21891. if (ret == 0) {
  21892. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21893. &key, endian);
  21894. }
  21895. }
  21896. /* test bad cases */
  21897. if (ret == 0) {
  21898. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21899. if (ret == BAD_FUNC_ARG) {
  21900. ret = 0;
  21901. }
  21902. }
  21903. if (ret == 0) {
  21904. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21905. &key, endian);
  21906. if (ret == BAD_FUNC_ARG) {
  21907. ret = 0;
  21908. }
  21909. }
  21910. if (ret == 0) {
  21911. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21912. &key, endian);
  21913. if (ret == BAD_FUNC_ARG) {
  21914. ret = 0;
  21915. }
  21916. }
  21917. if (ret == 0) {
  21918. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21919. NULL, endian);
  21920. if (ret == BAD_FUNC_ARG) {
  21921. ret = 0;
  21922. }
  21923. }
  21924. if (ret == 0) {
  21925. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21926. &key, endian);
  21927. if (ret == ECC_BAD_ARG_E) {
  21928. ret = 0;
  21929. }
  21930. }
  21931. if (ret == 0) {
  21932. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21933. &key, endian);
  21934. if (ret == ECC_BAD_ARG_E) {
  21935. ret = 0;
  21936. }
  21937. }
  21938. if (ret == 0) {
  21939. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21940. &key, EC448_LITTLE_ENDIAN);
  21941. }
  21942. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21943. ret = WOLFSSL_FATAL_ERROR;
  21944. }
  21945. wc_curve448_free(&key);
  21946. res = TEST_RES_CHECK(ret == 0);
  21947. #endif
  21948. return res;
  21949. } /*END test_wc_curve448_import_private_raw_ex*/
  21950. /*
  21951. * Testing test_curve448_export_key_raw
  21952. */
  21953. static int test_wc_curve448_export_key_raw(void)
  21954. {
  21955. int res = TEST_SKIPPED;
  21956. #if defined(HAVE_CURVE448)
  21957. WC_RNG rng;
  21958. curve448_key key;
  21959. byte priv[CURVE448_KEY_SIZE];
  21960. byte pub[CURVE448_KEY_SIZE];
  21961. word32 privSz = sizeof(priv);
  21962. word32 pubSz = sizeof(pub);
  21963. int ret;
  21964. ret = wc_curve448_init(&key);
  21965. if (ret == 0) {
  21966. ret = wc_InitRng(&rng);
  21967. }
  21968. if (ret == 0) {
  21969. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21970. if (ret == 0) {
  21971. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21972. }
  21973. if (ret == 0) {
  21974. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21975. }
  21976. if (ret == 0) {
  21977. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21978. }
  21979. }
  21980. wc_curve448_free(&key);
  21981. wc_FreeRng(&rng);
  21982. res = TEST_RES_CHECK(ret == 0);
  21983. #endif
  21984. return res;
  21985. }/*END test_wc_curve448_import_private_raw_ex*/
  21986. /*
  21987. * Testing test_wc_curve448_import_private
  21988. */
  21989. static int test_wc_curve448_import_private(void)
  21990. {
  21991. int res = TEST_SKIPPED;
  21992. #if defined(HAVE_CURVE448)
  21993. curve448_key key;
  21994. WC_RNG rng;
  21995. byte priv[CURVE448_KEY_SIZE];
  21996. word32 privSz = sizeof(priv);
  21997. int ret;
  21998. ret = wc_curve448_init(&key);
  21999. if (ret == 0) {
  22000. ret = wc_InitRng(&rng);
  22001. }
  22002. if (ret == 0) {
  22003. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  22004. if (ret == 0) {
  22005. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  22006. }
  22007. }
  22008. if (ret == 0) {
  22009. ret = wc_curve448_import_private(priv, privSz, &key);
  22010. }
  22011. wc_curve448_free(&key);
  22012. wc_FreeRng(&rng);
  22013. res = TEST_RES_CHECK(ret == 0);
  22014. #endif
  22015. return res;
  22016. } /*END test_wc_curve448_import*/
  22017. /*
  22018. * Testing test_wc_curve448_size.
  22019. */
  22020. static int test_wc_curve448_size(void)
  22021. {
  22022. int res = TEST_SKIPPED;
  22023. #if defined(HAVE_CURVE448)
  22024. curve448_key key;
  22025. int ret = 0;
  22026. ret = wc_curve448_init(&key);
  22027. /* Test good args for wc_curve448_size */
  22028. if (ret == 0) {
  22029. ret = wc_curve448_size(&key);
  22030. }
  22031. /* Test bad args for wc_curve448_size */
  22032. if (ret != 0) {
  22033. ret = wc_curve448_size(NULL);
  22034. }
  22035. wc_curve448_free(&key);
  22036. res = TEST_RES_CHECK(ret == 0);
  22037. #endif
  22038. return res;
  22039. } /* END test_wc_curve448_size*/
  22040. /*
  22041. * Testing wc_ecc_make_key.
  22042. */
  22043. static int test_wc_ecc_make_key(void)
  22044. {
  22045. int res = TEST_SKIPPED;
  22046. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22047. WC_RNG rng;
  22048. ecc_key key;
  22049. int ret;
  22050. ret = wc_InitRng(&rng);
  22051. if (ret != 0)
  22052. return TEST_FAIL;
  22053. ret = wc_ecc_init(&key);
  22054. if (ret == 0) {
  22055. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22056. #if defined(WOLFSSL_ASYNC_CRYPT)
  22057. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22058. #endif
  22059. /* Pass in bad args. */
  22060. if (ret == 0) {
  22061. ret = wc_ecc_make_key(NULL, KEY14, &key);
  22062. if (ret == BAD_FUNC_ARG) {
  22063. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  22064. }
  22065. if (ret == BAD_FUNC_ARG) {
  22066. ret = 0;
  22067. }
  22068. else if (ret == 0) {
  22069. ret = WOLFSSL_FATAL_ERROR;
  22070. }
  22071. }
  22072. wc_ecc_free(&key);
  22073. }
  22074. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  22075. ret = WOLFSSL_FATAL_ERROR;
  22076. }
  22077. #ifdef FP_ECC
  22078. wc_ecc_fp_free();
  22079. #endif
  22080. res = TEST_RES_CHECK(ret == 0);
  22081. #endif
  22082. return res;
  22083. } /* END test_wc_ecc_make_key */
  22084. /*
  22085. * Testing wc_ecc_init()
  22086. */
  22087. static int test_wc_ecc_init(void)
  22088. {
  22089. int res = TEST_SKIPPED;
  22090. #ifdef HAVE_ECC
  22091. ecc_key key;
  22092. int ret;
  22093. ret = wc_ecc_init(&key);
  22094. /* Pass in bad args. */
  22095. if (ret == 0) {
  22096. ret = wc_ecc_init(NULL);
  22097. if (ret == BAD_FUNC_ARG) {
  22098. ret = 0;
  22099. }
  22100. else if (ret == 0) {
  22101. ret = WOLFSSL_FATAL_ERROR;
  22102. }
  22103. }
  22104. wc_ecc_free(&key);
  22105. res = TEST_RES_CHECK(ret == 0);
  22106. #endif
  22107. return res;
  22108. } /* END test_wc_ecc_init */
  22109. /*
  22110. * Testing wc_ecc_check_key()
  22111. */
  22112. static int test_wc_ecc_check_key(void)
  22113. {
  22114. int res = TEST_SKIPPED;
  22115. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22116. WC_RNG rng;
  22117. ecc_key key;
  22118. int ret;
  22119. XMEMSET(&rng, 0, sizeof(rng));
  22120. XMEMSET(&key, 0, sizeof(key));
  22121. ret = wc_InitRng(&rng);
  22122. if (ret == 0) {
  22123. ret = wc_ecc_init(&key);
  22124. if (ret == 0) {
  22125. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22126. #if defined(WOLFSSL_ASYNC_CRYPT)
  22127. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22128. #endif
  22129. }
  22130. }
  22131. if (ret == 0) {
  22132. ret = wc_ecc_check_key(&key);
  22133. }
  22134. /* Pass in bad args. */
  22135. if (ret == 0) {
  22136. ret = wc_ecc_check_key(NULL);
  22137. if (ret == BAD_FUNC_ARG) {
  22138. ret = 0;
  22139. }
  22140. else if (ret == 0) {
  22141. ret = WOLFSSL_FATAL_ERROR;
  22142. }
  22143. }
  22144. if (wc_FreeRng(&rng) && ret == 0) {
  22145. ret = WOLFSSL_FATAL_ERROR;
  22146. }
  22147. wc_ecc_free(&key);
  22148. #ifdef FP_ECC
  22149. wc_ecc_fp_free();
  22150. #endif
  22151. res = TEST_RES_CHECK(ret == 0);
  22152. #endif
  22153. return res;
  22154. } /* END test_wc_ecc_check_key */
  22155. /*
  22156. * Testing wc_ecc_get_generator()
  22157. */
  22158. static int test_wc_ecc_get_generator(void)
  22159. {
  22160. int res = TEST_SKIPPED;
  22161. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  22162. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  22163. ecc_point* pt;
  22164. int ret = 0;
  22165. pt = wc_ecc_new_point();
  22166. if (!pt) {
  22167. ret = WOLFSSL_FATAL_ERROR;
  22168. }
  22169. if (ret == 0) {
  22170. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22171. }
  22172. /* Test bad args. */
  22173. if (ret == MP_OKAY) {
  22174. /* Returns Zero for bad arg. */
  22175. ret = wc_ecc_get_generator(pt, -1);
  22176. if (ret != MP_OKAY)
  22177. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  22178. if (ret != MP_OKAY)
  22179. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  22180. * increase this number */
  22181. if (ret != MP_OKAY)
  22182. wc_ecc_get_generator(NULL, -1);
  22183. ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
  22184. }
  22185. wc_ecc_del_point(pt);
  22186. res = TEST_RES_CHECK(ret == 0);
  22187. #endif
  22188. return res;
  22189. } /* END test_wc_ecc_get_generator */
  22190. /*
  22191. * Testing wc_ecc_size()
  22192. */
  22193. static int test_wc_ecc_size(void)
  22194. {
  22195. int res = TEST_SKIPPED;
  22196. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22197. WC_RNG rng;
  22198. ecc_key key;
  22199. int ret;
  22200. XMEMSET(&rng, 0, sizeof(rng));
  22201. XMEMSET(&key, 0, sizeof(key));
  22202. ret = wc_InitRng(&rng);
  22203. if (ret == 0) {
  22204. ret = wc_ecc_init(&key);
  22205. if (ret == 0) {
  22206. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22207. #if defined(WOLFSSL_ASYNC_CRYPT)
  22208. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22209. #endif
  22210. }
  22211. }
  22212. if (ret == 0) {
  22213. ret = wc_ecc_size(&key);
  22214. if (ret == KEY14) {
  22215. ret = 0;
  22216. }
  22217. else if (ret == 0) {
  22218. ret = WOLFSSL_FATAL_ERROR;
  22219. }
  22220. }
  22221. /* Test bad args. */
  22222. if (ret == 0) {
  22223. /* Returns Zero for bad arg. */
  22224. ret = wc_ecc_size(NULL);
  22225. }
  22226. if (wc_FreeRng(&rng) && ret == 0) {
  22227. ret = WOLFSSL_FATAL_ERROR;
  22228. }
  22229. wc_ecc_free(&key);
  22230. res = TEST_RES_CHECK(ret == 0);
  22231. #endif
  22232. return res;
  22233. } /* END test_wc_ecc_size */
  22234. static int test_wc_ecc_params(void)
  22235. {
  22236. int res = TEST_SKIPPED;
  22237. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  22238. It was added after certifications */
  22239. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  22240. const ecc_set_type* ecc_set;
  22241. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  22242. /* Test for SECP256R1 curve */
  22243. int curve_id = ECC_SECP256R1;
  22244. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  22245. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  22246. ecc_set = wc_ecc_get_curve_params(curve_idx);
  22247. AssertNotNull(ecc_set);
  22248. AssertIntEQ(ecc_set->id, curve_id);
  22249. #endif
  22250. /* Test case when SECP256R1 is not enabled */
  22251. /* Test that we get curve params for index 0 */
  22252. ecc_set = wc_ecc_get_curve_params(0);
  22253. AssertNotNull(ecc_set);
  22254. res = TEST_RES_CHECK(1);
  22255. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  22256. return res;
  22257. }
  22258. /*
  22259. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  22260. */
  22261. static int test_wc_ecc_signVerify_hash(void)
  22262. {
  22263. int res = TEST_SKIPPED;
  22264. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  22265. WC_RNG rng;
  22266. ecc_key key;
  22267. int ret;
  22268. int signH = WOLFSSL_FATAL_ERROR;
  22269. #ifdef HAVE_ECC_VERIFY
  22270. int verifyH = WOLFSSL_FATAL_ERROR;
  22271. int verify = 0;
  22272. #endif
  22273. word32 siglen = ECC_BUFSIZE;
  22274. byte sig[ECC_BUFSIZE];
  22275. byte adjustedSig[ECC_BUFSIZE+1];
  22276. byte digest[] = TEST_STRING;
  22277. word32 digestlen = (word32)TEST_STRING_SZ;
  22278. /* Init stack var */
  22279. XMEMSET(sig, 0, siglen);
  22280. XMEMSET(&key, 0, sizeof(key));
  22281. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  22282. /* Init structs. */
  22283. ret = wc_InitRng(&rng);
  22284. if (ret == 0) {
  22285. ret = wc_ecc_init(&key);
  22286. if (ret == 0) {
  22287. ret = wc_ecc_make_key(&rng, KEY14, &key);
  22288. #if defined(WOLFSSL_ASYNC_CRYPT)
  22289. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22290. #endif
  22291. }
  22292. }
  22293. if (ret == 0) {
  22294. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  22295. }
  22296. /* Check bad args. */
  22297. if (ret == 0) {
  22298. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  22299. if (signH == ECC_BAD_ARG_E) {
  22300. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  22301. &rng, &key);
  22302. }
  22303. if (signH == ECC_BAD_ARG_E) {
  22304. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  22305. &rng, &key);
  22306. }
  22307. if (signH == ECC_BAD_ARG_E) {
  22308. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22309. NULL, &key);
  22310. }
  22311. if (signH == ECC_BAD_ARG_E) {
  22312. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  22313. &rng, NULL);
  22314. }
  22315. if (signH == ECC_BAD_ARG_E) {
  22316. signH = 0;
  22317. }
  22318. else if (ret == 0) {
  22319. signH = WOLFSSL_FATAL_ERROR;
  22320. }
  22321. }
  22322. #ifdef HAVE_ECC_VERIFY
  22323. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  22324. if (verify != 1 && ret == 0) {
  22325. ret = WOLFSSL_FATAL_ERROR;
  22326. }
  22327. /* test check on length of signature passed in */
  22328. XMEMCPY(adjustedSig, sig, siglen);
  22329. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  22330. #ifndef NO_STRICT_ECDSA_LEN
  22331. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22332. &verify, &key), 0);
  22333. #else
  22334. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  22335. * is allowed */
  22336. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  22337. &verify, &key), 0);
  22338. #endif
  22339. /* Test bad args. */
  22340. if (ret == 0) {
  22341. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  22342. &verify, &key);
  22343. if (verifyH == ECC_BAD_ARG_E) {
  22344. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  22345. &verify, &key);
  22346. }
  22347. if (verifyH == ECC_BAD_ARG_E) {
  22348. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22349. NULL, &key);
  22350. }
  22351. if (verifyH == ECC_BAD_ARG_E) {
  22352. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  22353. &verify, NULL);
  22354. }
  22355. if (verifyH == ECC_BAD_ARG_E) {
  22356. verifyH = 0;
  22357. }
  22358. else if (ret == 0) {
  22359. verifyH = WOLFSSL_FATAL_ERROR;
  22360. }
  22361. }
  22362. #endif /* HAVE_ECC_VERIFY */
  22363. if (wc_FreeRng(&rng) && ret == 0) {
  22364. ret = WOLFSSL_FATAL_ERROR;
  22365. }
  22366. wc_ecc_free(&key);
  22367. #ifdef FP_ECC
  22368. wc_ecc_fp_free();
  22369. #endif
  22370. res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
  22371. #endif
  22372. return res;
  22373. } /* END test_wc_ecc_sign_hash */
  22374. /*
  22375. * Testing wc_ecc_shared_secret()
  22376. */
  22377. static int test_wc_ecc_shared_secret(void)
  22378. {
  22379. int res = TEST_SKIPPED;
  22380. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  22381. ecc_key key, pubKey;
  22382. WC_RNG rng;
  22383. int ret;
  22384. byte out[KEY32];
  22385. int keySz = sizeof(out);
  22386. word32 outlen = (word32)sizeof(out);
  22387. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22388. const char* qx =
  22389. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22390. const char* qy =
  22391. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22392. const char* d =
  22393. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22394. const char* curveName = "SECP256R1";
  22395. const byte expected_shared_secret[] =
  22396. {
  22397. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  22398. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  22399. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  22400. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  22401. };
  22402. #endif
  22403. PRIVATE_KEY_UNLOCK();
  22404. /* Initialize variables. */
  22405. XMEMSET(out, 0, keySz);
  22406. XMEMSET(&rng, 0, sizeof(rng));
  22407. XMEMSET(&key, 0, sizeof(key));
  22408. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22409. ret = wc_InitRng(&rng);
  22410. if (ret == 0) {
  22411. ret = wc_ecc_init(&key);
  22412. if (ret == 0) {
  22413. ret = wc_ecc_init(&pubKey);
  22414. }
  22415. }
  22416. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22417. if (ret == 0) {
  22418. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22419. }
  22420. if (ret == 0) {
  22421. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  22422. }
  22423. #else
  22424. if (ret == 0) {
  22425. ret = wc_ecc_make_key(&rng, keySz, &key);
  22426. #if defined(WOLFSSL_ASYNC_CRYPT)
  22427. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22428. #endif
  22429. }
  22430. if (ret == 0) {
  22431. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  22432. #if defined(WOLFSSL_ASYNC_CRYPT)
  22433. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22434. #endif
  22435. }
  22436. #endif
  22437. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22438. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22439. !defined(HAVE_SELFTEST)
  22440. if (ret == 0) {
  22441. ret = wc_ecc_set_rng(&key, &rng);
  22442. }
  22443. #endif
  22444. if (ret == 0) {
  22445. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  22446. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22447. if (ret == 0) {
  22448. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  22449. ret = WOLFSSL_FATAL_ERROR;
  22450. }
  22451. }
  22452. #endif
  22453. /* Test bad args. */
  22454. if (ret == 0) {
  22455. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  22456. if (ret == BAD_FUNC_ARG) {
  22457. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  22458. }
  22459. if (ret == BAD_FUNC_ARG) {
  22460. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  22461. }
  22462. if (ret == BAD_FUNC_ARG) {
  22463. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  22464. }
  22465. if (ret == BAD_FUNC_ARG) {
  22466. /* Invalid length */
  22467. outlen = 1;
  22468. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  22469. }
  22470. if (ret == BUFFER_E) {
  22471. ret = 0;
  22472. }
  22473. else if (ret == 0) {
  22474. ret = WOLFSSL_FATAL_ERROR;
  22475. }
  22476. }
  22477. }
  22478. if (wc_FreeRng(&rng) && ret == 0) {
  22479. ret = WOLFSSL_FATAL_ERROR;
  22480. }
  22481. wc_ecc_free(&key);
  22482. wc_ecc_free(&pubKey);
  22483. #ifdef FP_ECC
  22484. wc_ecc_fp_free();
  22485. #endif
  22486. PRIVATE_KEY_LOCK();
  22487. res = TEST_RES_CHECK(ret == 0);
  22488. #endif
  22489. return res;
  22490. } /* END tests_wc_ecc_shared_secret */
  22491. /*
  22492. * testint wc_ecc_export_x963()
  22493. */
  22494. static int test_wc_ecc_export_x963(void)
  22495. {
  22496. int res = TEST_SKIPPED;
  22497. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22498. ecc_key key;
  22499. WC_RNG rng;
  22500. byte out[ECC_ASN963_MAX_BUF_SZ];
  22501. word32 outlen = sizeof(out);
  22502. int ret = 0;
  22503. PRIVATE_KEY_UNLOCK();
  22504. /* Initialize variables. */
  22505. XMEMSET(out, 0, outlen);
  22506. XMEMSET(&rng, 0, sizeof(rng));
  22507. XMEMSET(&key, 0, sizeof(key));
  22508. ret = wc_InitRng(&rng);
  22509. if (ret == 0) {
  22510. ret = wc_ecc_init(&key);
  22511. if (ret == 0) {
  22512. ret = wc_ecc_make_key(&rng, KEY20, &key);
  22513. #if defined(WOLFSSL_ASYNC_CRYPT)
  22514. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22515. #endif
  22516. }
  22517. }
  22518. if (ret == 0) {
  22519. ret = wc_ecc_export_x963(&key, out, &outlen);
  22520. }
  22521. /* Test bad args. */
  22522. if (ret == 0) {
  22523. ret = wc_ecc_export_x963(NULL, out, &outlen);
  22524. if (ret == ECC_BAD_ARG_E) {
  22525. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  22526. }
  22527. if (ret == LENGTH_ONLY_E) {
  22528. ret = wc_ecc_export_x963(&key, out, NULL);
  22529. }
  22530. if (ret == ECC_BAD_ARG_E) {
  22531. key.idx = -4;
  22532. ret = wc_ecc_export_x963(&key, out, &outlen);
  22533. }
  22534. if (ret == ECC_BAD_ARG_E) {
  22535. ret = 0;
  22536. }
  22537. else {
  22538. ret = WOLFSSL_FATAL_ERROR;
  22539. }
  22540. }
  22541. if (wc_FreeRng(&rng) && ret == 0) {
  22542. ret = WOLFSSL_FATAL_ERROR;
  22543. }
  22544. wc_ecc_free(&key);
  22545. #ifdef FP_ECC
  22546. wc_ecc_fp_free();
  22547. #endif
  22548. PRIVATE_KEY_LOCK();
  22549. res = TEST_RES_CHECK(ret == 0);
  22550. #endif
  22551. return res;
  22552. } /* END test_wc_ecc_export_x963 */
  22553. /*
  22554. * Testing wc_ecc_export_x963_ex()
  22555. * compile with --enable-compkey will use compression.
  22556. */
  22557. static int test_wc_ecc_export_x963_ex(void)
  22558. {
  22559. int res = TEST_SKIPPED;
  22560. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22561. ecc_key key;
  22562. WC_RNG rng;
  22563. int ret = 0;
  22564. byte out[ECC_ASN963_MAX_BUF_SZ];
  22565. word32 outlen = sizeof(out);
  22566. #ifdef HAVE_COMP_KEY
  22567. word32 badOutLen = 5;
  22568. #endif
  22569. /* Init stack variables. */
  22570. XMEMSET(out, 0, outlen);
  22571. XMEMSET(&rng, 0, sizeof(rng));
  22572. XMEMSET(&key, 0, sizeof(key));
  22573. ret = wc_InitRng(&rng);
  22574. if (ret == 0) {
  22575. ret = wc_ecc_init(&key);
  22576. if (ret == 0) {
  22577. ret = wc_ecc_make_key(&rng, KEY64, &key);
  22578. #if defined(WOLFSSL_ASYNC_CRYPT)
  22579. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22580. #endif
  22581. }
  22582. }
  22583. #ifdef HAVE_COMP_KEY
  22584. if (ret == 0) {
  22585. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22586. }
  22587. #else
  22588. if (ret == 0) {
  22589. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22590. }
  22591. #endif
  22592. /* Test bad args. */
  22593. #ifdef HAVE_COMP_KEY
  22594. if (ret == 0) {
  22595. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  22596. if (ret == BAD_FUNC_ARG) {
  22597. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  22598. }
  22599. if (ret == BAD_FUNC_ARG) {
  22600. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  22601. }
  22602. if (ret == BAD_FUNC_ARG) {
  22603. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  22604. }
  22605. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  22606. if (ret == BUFFER_E)
  22607. #else
  22608. if (ret == LENGTH_ONLY_E)
  22609. #endif
  22610. {
  22611. key.idx = -4;
  22612. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  22613. }
  22614. if (ret == ECC_BAD_ARG_E) {
  22615. ret = 0;
  22616. }
  22617. else {
  22618. ret = WOLFSSL_FATAL_ERROR;
  22619. }
  22620. }
  22621. #else
  22622. if (ret == 0) {
  22623. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  22624. if (ret == BAD_FUNC_ARG) {
  22625. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  22626. }
  22627. if (ret == BAD_FUNC_ARG) {
  22628. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  22629. }
  22630. if (ret == NOT_COMPILED_IN) {
  22631. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  22632. }
  22633. if (ret == BAD_FUNC_ARG) {
  22634. key.idx = -4;
  22635. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  22636. }
  22637. if (ret == ECC_BAD_ARG_E) {
  22638. ret = 0;
  22639. }
  22640. else if (ret == 0) {
  22641. ret = WOLFSSL_FATAL_ERROR;
  22642. }
  22643. }
  22644. #endif
  22645. if (wc_FreeRng(&rng) && ret == 0) {
  22646. ret = WOLFSSL_FATAL_ERROR;
  22647. }
  22648. wc_ecc_free(&key);
  22649. #ifdef FP_ECC
  22650. wc_ecc_fp_free();
  22651. #endif
  22652. res = TEST_RES_CHECK(ret == 0);
  22653. #endif
  22654. return res;
  22655. } /* END test_wc_ecc_export_x963_ex */
  22656. /*
  22657. * testing wc_ecc_import_x963()
  22658. */
  22659. static int test_wc_ecc_import_x963(void)
  22660. {
  22661. int res = TEST_SKIPPED;
  22662. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22663. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22664. ecc_key pubKey, key;
  22665. WC_RNG rng;
  22666. byte x963[ECC_ASN963_MAX_BUF_SZ];
  22667. word32 x963Len = (word32)sizeof(x963);
  22668. int ret;
  22669. /* Init stack variables. */
  22670. XMEMSET(x963, 0, x963Len);
  22671. XMEMSET(&rng, 0, sizeof(rng));
  22672. XMEMSET(&key, 0, sizeof(key));
  22673. XMEMSET(&pubKey, 0, sizeof(pubKey));
  22674. ret = wc_InitRng(&rng);
  22675. if (ret == 0) {
  22676. ret = wc_ecc_init(&pubKey);
  22677. if (ret == 0) {
  22678. ret = wc_ecc_init(&key);
  22679. }
  22680. if (ret == 0) {
  22681. ret = wc_ecc_make_key(&rng, KEY24, &key);
  22682. #if defined(WOLFSSL_ASYNC_CRYPT)
  22683. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22684. #endif
  22685. }
  22686. if (ret == 0) {
  22687. PRIVATE_KEY_UNLOCK();
  22688. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  22689. PRIVATE_KEY_LOCK();
  22690. }
  22691. }
  22692. if (ret == 0) {
  22693. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  22694. }
  22695. /* Test bad args. */
  22696. if (ret == 0) {
  22697. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  22698. if (ret == BAD_FUNC_ARG) {
  22699. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  22700. }
  22701. if (ret == BAD_FUNC_ARG) {
  22702. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  22703. }
  22704. if (ret == ECC_BAD_ARG_E) {
  22705. ret = 0;
  22706. }
  22707. else if (ret == 0) {
  22708. ret = WOLFSSL_FATAL_ERROR;
  22709. }
  22710. }
  22711. if (wc_FreeRng(&rng) && ret == 0) {
  22712. ret = WOLFSSL_FATAL_ERROR;
  22713. }
  22714. wc_ecc_free(&key);
  22715. wc_ecc_free(&pubKey);
  22716. #ifdef FP_ECC
  22717. wc_ecc_fp_free();
  22718. #endif
  22719. res = TEST_RES_CHECK(ret == 0);
  22720. #endif
  22721. return res;
  22722. } /* END wc_ecc_import_x963 */
  22723. /*
  22724. * testing wc_ecc_import_private_key()
  22725. */
  22726. static int ecc_import_private_key(void)
  22727. {
  22728. int res = TEST_SKIPPED;
  22729. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22730. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22731. ecc_key key, keyImp;
  22732. WC_RNG rng;
  22733. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22734. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22735. word32 privKeySz = (word32)sizeof(privKey);
  22736. word32 x963KeySz = (word32)sizeof(x963Key);
  22737. int ret;
  22738. /* Init stack variables. */
  22739. XMEMSET(privKey, 0, privKeySz);
  22740. XMEMSET(x963Key, 0, x963KeySz);
  22741. XMEMSET(&rng, 0, sizeof(rng));
  22742. XMEMSET(&key, 0, sizeof(key));
  22743. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22744. ret = wc_InitRng(&rng);
  22745. if (ret == 0) {
  22746. ret = wc_ecc_init(&key);
  22747. if (ret == 0) {
  22748. ret = wc_ecc_init(&keyImp);
  22749. }
  22750. if (ret == 0) {
  22751. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22752. #if defined(WOLFSSL_ASYNC_CRYPT)
  22753. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22754. #endif
  22755. }
  22756. if (ret == 0) {
  22757. PRIVATE_KEY_UNLOCK();
  22758. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22759. PRIVATE_KEY_LOCK();
  22760. }
  22761. if (ret == 0) {
  22762. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22763. }
  22764. }
  22765. if (ret == 0) {
  22766. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22767. x963KeySz, &keyImp);
  22768. }
  22769. /* Pass in bad args. */
  22770. if (ret == 0) {
  22771. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22772. x963KeySz, NULL);
  22773. if (ret == BAD_FUNC_ARG) {
  22774. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22775. x963KeySz, &keyImp);
  22776. }
  22777. if (ret == BAD_FUNC_ARG) {
  22778. ret = 0;
  22779. }
  22780. else if (ret == 0) {
  22781. ret = WOLFSSL_FATAL_ERROR;
  22782. }
  22783. }
  22784. if (wc_FreeRng(&rng) && ret == 0) {
  22785. ret = WOLFSSL_FATAL_ERROR;
  22786. }
  22787. wc_ecc_free(&key);
  22788. wc_ecc_free(&keyImp);
  22789. #ifdef FP_ECC
  22790. wc_ecc_fp_free();
  22791. #endif
  22792. res = TEST_RES_CHECK(ret == 0);
  22793. #endif
  22794. return res;
  22795. } /* END wc_ecc_import_private_key */
  22796. /*
  22797. * Testing wc_ecc_export_private_only()
  22798. */
  22799. static int test_wc_ecc_export_private_only(void)
  22800. {
  22801. int res = TEST_SKIPPED;
  22802. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22803. ecc_key key;
  22804. WC_RNG rng;
  22805. byte out[ECC_PRIV_KEY_BUF];
  22806. word32 outlen = sizeof(out);
  22807. int ret;
  22808. /* Init stack variables. */
  22809. XMEMSET(out, 0, outlen);
  22810. XMEMSET(&rng, 0, sizeof(rng));
  22811. XMEMSET(&key, 0, sizeof(key));
  22812. ret = wc_InitRng(&rng);
  22813. if (ret == 0) {
  22814. ret = wc_ecc_init(&key);
  22815. if (ret == 0) {
  22816. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22817. #if defined(WOLFSSL_ASYNC_CRYPT)
  22818. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22819. #endif
  22820. }
  22821. }
  22822. if (ret == 0) {
  22823. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22824. }
  22825. /* Pass in bad args. */
  22826. if (ret == 0) {
  22827. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22828. if (ret == BAD_FUNC_ARG) {
  22829. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22830. }
  22831. if (ret == BAD_FUNC_ARG) {
  22832. ret = wc_ecc_export_private_only(&key, out, NULL);
  22833. }
  22834. if (ret == BAD_FUNC_ARG) {
  22835. ret = 0;
  22836. }
  22837. else if (ret == 0) {
  22838. ret = WOLFSSL_FATAL_ERROR;
  22839. }
  22840. }
  22841. if (wc_FreeRng(&rng) && ret == 0) {
  22842. ret = WOLFSSL_FATAL_ERROR;
  22843. }
  22844. wc_ecc_free(&key);
  22845. #ifdef FP_ECC
  22846. wc_ecc_fp_free();
  22847. #endif
  22848. res = TEST_RES_CHECK(ret == 0);
  22849. #endif
  22850. return res;
  22851. } /* END test_wc_ecc_export_private_only */
  22852. /*
  22853. * Testing wc_ecc_rs_to_sig()
  22854. */
  22855. static int test_wc_ecc_rs_to_sig(void)
  22856. {
  22857. int res = TEST_SKIPPED;
  22858. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22859. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22860. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22861. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22862. const char* zeroStr = "0";
  22863. byte sig[ECC_MAX_SIG_SIZE];
  22864. word32 siglen = (word32)sizeof(sig);
  22865. /*R and S max size is the order of curve. 2^192.*/
  22866. int keySz = KEY24;
  22867. byte r[KEY24];
  22868. byte s[KEY24];
  22869. word32 rlen = (word32)sizeof(r);
  22870. word32 slen = (word32)sizeof(s);
  22871. int ret;
  22872. /* Init stack variables. */
  22873. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22874. XMEMSET(r, 0, keySz);
  22875. XMEMSET(s, 0, keySz);
  22876. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22877. if (ret == 0) {
  22878. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22879. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS) || \
  22880. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  22881. if (ret == ASN_PARSE_E) {
  22882. ret = 0;
  22883. }
  22884. #endif
  22885. }
  22886. /* Test bad args. */
  22887. if (ret == 0) {
  22888. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22889. if (ret == ECC_BAD_ARG_E) {
  22890. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22891. }
  22892. if (ret == ECC_BAD_ARG_E) {
  22893. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22894. }
  22895. if (ret == ECC_BAD_ARG_E) {
  22896. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22897. }
  22898. if (ret == ECC_BAD_ARG_E) {
  22899. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22900. }
  22901. if (ret == MP_ZERO_E) {
  22902. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22903. }
  22904. if (ret == MP_ZERO_E) {
  22905. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22906. }
  22907. if (ret == ECC_BAD_ARG_E) {
  22908. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22909. }
  22910. if (ret == ECC_BAD_ARG_E) {
  22911. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22912. }
  22913. if (ret == ECC_BAD_ARG_E) {
  22914. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22915. }
  22916. if (ret == ECC_BAD_ARG_E) {
  22917. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22918. }
  22919. if (ret == ECC_BAD_ARG_E) {
  22920. ret = 0;
  22921. }
  22922. else if (ret == 0) {
  22923. ret = WOLFSSL_FATAL_ERROR;
  22924. }
  22925. }
  22926. res = TEST_RES_CHECK(ret == 0);
  22927. #endif
  22928. return res;
  22929. } /* END test_wc_ecc_rs_to_sig */
  22930. static int test_wc_ecc_import_raw(void)
  22931. {
  22932. int res = TEST_SKIPPED;
  22933. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22934. ecc_key key;
  22935. int ret = 0;
  22936. const char* qx =
  22937. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22938. const char* qy =
  22939. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22940. const char* d =
  22941. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22942. const char* curveName = "SECP256R1";
  22943. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22944. const char* kNullStr = "";
  22945. #endif
  22946. ret = wc_ecc_init(&key);
  22947. /* Test good import */
  22948. if (ret == 0) {
  22949. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22950. }
  22951. /* Test bad args. */
  22952. if (ret == 0) {
  22953. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22954. if (ret == BAD_FUNC_ARG) {
  22955. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22956. }
  22957. if (ret == BAD_FUNC_ARG) {
  22958. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22959. }
  22960. if (ret == BAD_FUNC_ARG) {
  22961. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22962. }
  22963. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22964. if (ret == BAD_FUNC_ARG) {
  22965. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22966. wc_ecc_free(&key);
  22967. #endif
  22968. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22969. if (ret == ECC_INF_E)
  22970. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22971. }
  22972. #endif
  22973. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22974. if (ret == BAD_FUNC_ARG) {
  22975. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22976. wc_ecc_free(&key);
  22977. #endif
  22978. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22979. /* Note: SP math "is point" failure returns MP_VAL */
  22980. if (ret == ECC_INF_E || ret == MP_VAL) {
  22981. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22982. }
  22983. }
  22984. if (ret == BAD_FUNC_ARG) {
  22985. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22986. wc_ecc_free(&key);
  22987. #endif
  22988. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22989. /* Note: SP math "is point" failure returns MP_VAL */
  22990. if (ret == ECC_INF_E || ret == MP_VAL) {
  22991. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22992. }
  22993. }
  22994. #endif
  22995. if (ret == BAD_FUNC_ARG) {
  22996. ret = 0;
  22997. }
  22998. }
  22999. wc_ecc_free(&key);
  23000. res = TEST_RES_CHECK(ret == 0);
  23001. #endif
  23002. return res;
  23003. } /* END test_wc_ecc_import_raw */
  23004. static int test_wc_ecc_import_unsigned(void)
  23005. {
  23006. int res = TEST_SKIPPED;
  23007. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23008. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  23009. ecc_key key;
  23010. const byte qx[] = {
  23011. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  23012. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  23013. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  23014. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  23015. };
  23016. const byte qy[] = {
  23017. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  23018. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  23019. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  23020. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  23021. };
  23022. const byte d[] = {
  23023. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  23024. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  23025. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  23026. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  23027. };
  23028. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23029. const byte nullBytes[32] = {0};
  23030. #endif
  23031. int curveId = ECC_SECP256R1;
  23032. int ret;
  23033. ret = wc_ecc_init(&key);
  23034. if (ret == 0) {
  23035. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23036. curveId);
  23037. }
  23038. /* Test bad args. */
  23039. if (ret == 0) {
  23040. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  23041. curveId);
  23042. if (ret == BAD_FUNC_ARG) {
  23043. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  23044. curveId);
  23045. }
  23046. if (ret == BAD_FUNC_ARG) {
  23047. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  23048. curveId);
  23049. }
  23050. if (ret == BAD_FUNC_ARG) {
  23051. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  23052. ECC_CURVE_INVALID);
  23053. }
  23054. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  23055. if (ret == BAD_FUNC_ARG) {
  23056. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  23057. (byte*)nullBytes, (byte*)nullBytes, curveId);
  23058. }
  23059. #endif
  23060. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  23061. ret = 0;
  23062. }
  23063. }
  23064. wc_ecc_free(&key);
  23065. res = TEST_RES_CHECK(ret == 0);
  23066. #endif
  23067. return res;
  23068. } /* END test_wc_ecc_import_unsigned */
  23069. /*
  23070. * Testing wc_ecc_sig_size()
  23071. */
  23072. static int test_wc_ecc_sig_size(void)
  23073. {
  23074. int res = TEST_SKIPPED;
  23075. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23076. ecc_key key;
  23077. WC_RNG rng;
  23078. int keySz = KEY16;
  23079. int ret = 0;
  23080. XMEMSET(&rng, 0, sizeof(rng));
  23081. XMEMSET(&key, 0, sizeof(key));
  23082. ret = wc_InitRng(&rng);
  23083. if (ret == 0) {
  23084. ret = wc_ecc_init(&key);
  23085. if (ret == 0) {
  23086. ret = wc_ecc_make_key(&rng, keySz, &key);
  23087. #if defined(WOLFSSL_ASYNC_CRYPT)
  23088. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23089. #endif
  23090. }
  23091. }
  23092. if (ret == 0) {
  23093. ret = wc_ecc_sig_size(&key);
  23094. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  23095. ret = 0;
  23096. }
  23097. }
  23098. if (wc_FreeRng(&rng) && ret == 0) {
  23099. ret = WOLFSSL_FATAL_ERROR;
  23100. }
  23101. wc_ecc_free(&key);
  23102. res = TEST_RES_CHECK(ret == 0);
  23103. #endif
  23104. return res;
  23105. } /* END test_wc_ecc_sig_size */
  23106. /*
  23107. * Testing wc_ecc_ctx_new()
  23108. */
  23109. static int test_wc_ecc_ctx_new(void)
  23110. {
  23111. int res = TEST_SKIPPED;
  23112. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23113. WC_RNG rng;
  23114. int ret = 0;
  23115. ecEncCtx* cli = NULL;
  23116. ecEncCtx* srv = NULL;
  23117. ret = wc_InitRng(&rng);
  23118. if (ret == 0) {
  23119. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  23120. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  23121. }
  23122. if (ret == 0 && (cli == NULL || srv == NULL)) {
  23123. ret = WOLFSSL_FATAL_ERROR;
  23124. }
  23125. wc_ecc_ctx_free(cli);
  23126. wc_ecc_ctx_free(srv);
  23127. /* Test bad args. */
  23128. if (ret == 0) {
  23129. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  23130. cli = wc_ecc_ctx_new(0, &rng);
  23131. if (cli != NULL) {
  23132. ret = WOLFSSL_FATAL_ERROR;
  23133. }
  23134. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  23135. if (cli != NULL) {
  23136. ret = WOLFSSL_FATAL_ERROR;
  23137. }
  23138. }
  23139. if (wc_FreeRng(&rng) && ret == 0) {
  23140. ret = WOLFSSL_FATAL_ERROR;
  23141. }
  23142. wc_ecc_ctx_free(cli);
  23143. res = TEST_RES_CHECK(ret == 0);
  23144. #endif
  23145. return res;
  23146. } /* END test_wc_ecc_ctx_new */
  23147. /*
  23148. * Tesing wc_ecc_reset()
  23149. */
  23150. static int test_wc_ecc_ctx_reset(void)
  23151. {
  23152. int res = TEST_SKIPPED;
  23153. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23154. ecEncCtx* ctx = NULL;
  23155. WC_RNG rng;
  23156. int ret = 0;
  23157. ret = wc_InitRng(&rng);
  23158. if (ret == 0) {
  23159. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  23160. ret = WOLFSSL_FATAL_ERROR;
  23161. }
  23162. }
  23163. if (ret == 0) {
  23164. ret = wc_ecc_ctx_reset(ctx, &rng);
  23165. }
  23166. /* Pass in bad args. */
  23167. if (ret == 0) {
  23168. ret = wc_ecc_ctx_reset(NULL, &rng);
  23169. if (ret == BAD_FUNC_ARG) {
  23170. ret = wc_ecc_ctx_reset(ctx, NULL);
  23171. }
  23172. if (ret == BAD_FUNC_ARG) {
  23173. ret = 0;
  23174. }
  23175. else if (ret == 0) {
  23176. ret = WOLFSSL_FATAL_ERROR;
  23177. }
  23178. }
  23179. if (wc_FreeRng(&rng) && ret == 0) {
  23180. ret = WOLFSSL_FATAL_ERROR;
  23181. }
  23182. wc_ecc_ctx_free(ctx);
  23183. res = TEST_RES_CHECK(ret == 0);
  23184. #endif
  23185. return res;
  23186. } /* END test_wc_ecc_ctx_reset */
  23187. /*
  23188. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  23189. */
  23190. static int test_wc_ecc_ctx_set_peer_salt(void)
  23191. {
  23192. int res = TEST_SKIPPED;
  23193. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23194. WC_RNG rng;
  23195. ecEncCtx* cliCtx = NULL;
  23196. ecEncCtx* servCtx = NULL;
  23197. const byte* cliSalt = NULL;
  23198. const byte* servSalt = NULL;
  23199. int ret = 0;
  23200. ret = wc_InitRng(&rng);
  23201. if (ret == 0) {
  23202. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  23203. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  23204. ret = WOLFSSL_FATAL_ERROR;
  23205. }
  23206. }
  23207. /* Test bad args. */
  23208. if (ret == 0) {
  23209. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  23210. if (cliSalt != NULL) {
  23211. ret = WOLFSSL_FATAL_ERROR;
  23212. }
  23213. }
  23214. if (ret == 0) {
  23215. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23216. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  23217. if (cliSalt == NULL || servSalt == NULL) {
  23218. ret = WOLFSSL_FATAL_ERROR;
  23219. }
  23220. }
  23221. if (ret == 0) {
  23222. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  23223. }
  23224. /* Test bad args. */
  23225. if (ret == 0) {
  23226. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  23227. if (ret == BAD_FUNC_ARG) {
  23228. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  23229. }
  23230. if (ret == BAD_FUNC_ARG) {
  23231. ret = 0;
  23232. }
  23233. else if (ret == 0) {
  23234. ret = WOLFSSL_FATAL_ERROR;
  23235. }
  23236. }
  23237. if (wc_FreeRng(&rng) && ret == 0) {
  23238. ret = WOLFSSL_FATAL_ERROR;
  23239. }
  23240. wc_ecc_ctx_free(cliCtx);
  23241. wc_ecc_ctx_free(servCtx);
  23242. res = TEST_RES_CHECK(ret == 0);
  23243. #endif
  23244. return res;
  23245. } /* END test_wc_ecc_ctx_set_peer_salt */
  23246. /*
  23247. * Testing wc_ecc_ctx_set_info()
  23248. */
  23249. static int test_wc_ecc_ctx_set_info(void)
  23250. {
  23251. int res = TEST_SKIPPED;
  23252. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  23253. ecEncCtx* ctx = NULL;
  23254. WC_RNG rng;
  23255. int ret;
  23256. const char* optInfo = "Optional Test Info.";
  23257. int optInfoSz = (int)XSTRLEN(optInfo);
  23258. const char* badOptInfo = NULL;
  23259. ret = wc_InitRng(&rng);
  23260. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  23261. ret = WOLFSSL_FATAL_ERROR;
  23262. }
  23263. if (ret == 0) {
  23264. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  23265. }
  23266. /* Test bad args. */
  23267. if (ret == 0) {
  23268. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  23269. if (ret == BAD_FUNC_ARG) {
  23270. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  23271. }
  23272. if (ret == BAD_FUNC_ARG) {
  23273. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  23274. }
  23275. if (ret == BAD_FUNC_ARG) {
  23276. ret = 0;
  23277. }
  23278. else if (ret == 0) {
  23279. ret = WOLFSSL_FATAL_ERROR;
  23280. }
  23281. }
  23282. if (wc_FreeRng(&rng) && ret == 0) {
  23283. ret = WOLFSSL_FATAL_ERROR;
  23284. }
  23285. wc_ecc_ctx_free(ctx);
  23286. res = TEST_RES_CHECK(ret == 0);
  23287. #endif
  23288. return res;
  23289. } /* END test_wc_ecc_ctx_set_info */
  23290. /*
  23291. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  23292. */
  23293. static int test_wc_ecc_encryptDecrypt(void)
  23294. {
  23295. int res = TEST_SKIPPED;
  23296. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  23297. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23298. ecc_key srvKey, cliKey, tmpKey;
  23299. WC_RNG rng;
  23300. int ret;
  23301. const char* msg = "EccBlock Size 16";
  23302. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  23303. #ifdef WOLFSSL_ECIES_OLD
  23304. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23305. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23306. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  23307. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23308. #else
  23309. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  23310. #endif
  23311. word32 outSz = (word32)sizeof(out);
  23312. byte plain[sizeof("EccBlock Size 16")];
  23313. word32 plainSz = (word32)sizeof(plain);
  23314. int keySz = KEY20;
  23315. /* Init stack variables. */
  23316. XMEMSET(out, 0, outSz);
  23317. XMEMSET(plain, 0, plainSz);
  23318. XMEMSET(&rng, 0, sizeof(rng));
  23319. XMEMSET(&srvKey, 0, sizeof(srvKey));
  23320. XMEMSET(&cliKey, 0, sizeof(cliKey));
  23321. ret = wc_InitRng(&rng);
  23322. if (ret == 0) {
  23323. ret = wc_ecc_init(&cliKey);
  23324. if (ret == 0) {
  23325. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  23326. #if defined(WOLFSSL_ASYNC_CRYPT)
  23327. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23328. #endif
  23329. }
  23330. if (ret == 0) {
  23331. ret = wc_ecc_init(&srvKey);
  23332. }
  23333. if (ret == 0) {
  23334. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  23335. #if defined(WOLFSSL_ASYNC_CRYPT)
  23336. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23337. #endif
  23338. }
  23339. if (ret == 0) {
  23340. ret = wc_ecc_init(&tmpKey);
  23341. }
  23342. }
  23343. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23344. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23345. !defined(HAVE_SELFTEST)
  23346. if (ret == 0) {
  23347. ret = wc_ecc_set_rng(&srvKey, &rng);
  23348. }
  23349. if (ret == 0) {
  23350. ret = wc_ecc_set_rng(&cliKey, &rng);
  23351. }
  23352. #endif
  23353. if (ret == 0) {
  23354. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23355. &outSz, NULL);
  23356. }
  23357. if (ret == 0) {
  23358. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  23359. &outSz, NULL);
  23360. if (ret == BAD_FUNC_ARG) {
  23361. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  23362. &outSz, NULL);
  23363. }
  23364. if (ret == BAD_FUNC_ARG) {
  23365. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  23366. &outSz, NULL);
  23367. }
  23368. if (ret == BAD_FUNC_ARG) {
  23369. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  23370. &outSz, NULL);
  23371. }
  23372. if (ret == BAD_FUNC_ARG) {
  23373. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  23374. NULL, NULL);
  23375. }
  23376. if (ret == BAD_FUNC_ARG) {
  23377. ret = 0;
  23378. }
  23379. else if (ret == 0) {
  23380. ret = WOLFSSL_FATAL_ERROR;
  23381. }
  23382. }
  23383. #ifdef WOLFSSL_ECIES_OLD
  23384. if (ret == 0) {
  23385. tmpKey.dp = cliKey.dp;
  23386. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  23387. }
  23388. #endif
  23389. if (ret == 0) {
  23390. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  23391. &plainSz, NULL);
  23392. }
  23393. if (ret == 0) {
  23394. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  23395. &plainSz, NULL);
  23396. #ifdef WOLFSSL_ECIES_OLD
  23397. /* NULL parameter allowed in new implementations - public key comes from
  23398. * the message. */
  23399. if (ret == BAD_FUNC_ARG) {
  23400. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  23401. &plainSz, NULL);
  23402. }
  23403. #endif
  23404. if (ret == BAD_FUNC_ARG) {
  23405. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  23406. &plainSz, NULL);
  23407. }
  23408. if (ret == BAD_FUNC_ARG) {
  23409. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  23410. &plainSz, NULL);
  23411. }
  23412. if (ret == BAD_FUNC_ARG) {
  23413. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  23414. plain, NULL, NULL);
  23415. }
  23416. if (ret == BAD_FUNC_ARG) {
  23417. ret = 0;
  23418. }
  23419. else if (ret == 0) {
  23420. ret = WOLFSSL_FATAL_ERROR;
  23421. }
  23422. }
  23423. if (XMEMCMP(msg, plain, msgSz) != 0) {
  23424. ret = WOLFSSL_FATAL_ERROR;
  23425. }
  23426. if (wc_FreeRng(&rng) && ret == 0) {
  23427. ret = WOLFSSL_FATAL_ERROR;
  23428. }
  23429. wc_ecc_free(&tmpKey);
  23430. wc_ecc_free(&cliKey);
  23431. wc_ecc_free(&srvKey);
  23432. res = TEST_RES_CHECK(ret == 0);
  23433. #endif
  23434. return res;
  23435. } /* END test_wc_ecc_encryptDecrypt */
  23436. /*
  23437. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  23438. */
  23439. static int test_wc_ecc_del_point(void)
  23440. {
  23441. int res = TEST_SKIPPED;
  23442. #if defined(HAVE_ECC)
  23443. ecc_point* pt;
  23444. pt = wc_ecc_new_point();
  23445. wc_ecc_del_point(pt);
  23446. res = TEST_RES_CHECK(pt != NULL);
  23447. #endif
  23448. return res;
  23449. } /* END test_wc_ecc_del_point */
  23450. /*
  23451. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  23452. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  23453. * and wc_ecc_cmp_point()
  23454. */
  23455. static int test_wc_ecc_pointFns(void)
  23456. {
  23457. int res = TEST_SKIPPED;
  23458. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  23459. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23460. !defined(WOLFSSL_ATECC608A)
  23461. ecc_key key;
  23462. WC_RNG rng;
  23463. int ret;
  23464. ecc_point* point = NULL;
  23465. ecc_point* cpypt = NULL;
  23466. int idx = 0;
  23467. int keySz = KEY32;
  23468. byte der[DER_SZ(KEY32)];
  23469. word32 derlenChk = 0;
  23470. word32 derSz = DER_SZ(KEY32);
  23471. /* Init stack variables. */
  23472. XMEMSET(der, 0, derSz);
  23473. XMEMSET(&rng, 0, sizeof(rng));
  23474. XMEMSET(&key, 0, sizeof(key));
  23475. ret = wc_InitRng(&rng);
  23476. if (ret == 0) {
  23477. ret = wc_ecc_init(&key);
  23478. if (ret == 0) {
  23479. ret = wc_ecc_make_key(&rng, keySz, &key);
  23480. #if defined(WOLFSSL_ASYNC_CRYPT)
  23481. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23482. #endif
  23483. }
  23484. }
  23485. if (ret == 0) {
  23486. point = wc_ecc_new_point();
  23487. if (!point) {
  23488. ret = WOLFSSL_FATAL_ERROR;
  23489. }
  23490. }
  23491. if (ret == 0) {
  23492. cpypt = wc_ecc_new_point();
  23493. if (!cpypt) {
  23494. ret = WOLFSSL_FATAL_ERROR;
  23495. }
  23496. }
  23497. /* Export */
  23498. if (ret == 0) {
  23499. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23500. NULL, &derlenChk);
  23501. /* Check length value. */
  23502. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  23503. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23504. der, &derSz);
  23505. }
  23506. }
  23507. /* Test bad args. */
  23508. if (ret == 0) {
  23509. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  23510. if (ret == ECC_BAD_ARG_E) {
  23511. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  23512. }
  23513. if (ret == ECC_BAD_ARG_E) {
  23514. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  23515. der, NULL);
  23516. }
  23517. if (ret == ECC_BAD_ARG_E) {
  23518. ret = 0;
  23519. }
  23520. else if (ret == 0) {
  23521. ret = WOLFSSL_FATAL_ERROR;
  23522. }
  23523. }
  23524. /* Import */
  23525. if (ret == 0) {
  23526. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  23527. /* Condition double checks wc_ecc_cmp_point(). */
  23528. if (ret == 0 &&
  23529. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  23530. ret = wc_ecc_cmp_point(&key.pubkey, point);
  23531. }
  23532. }
  23533. /* Test bad args. */
  23534. if (ret == 0) {
  23535. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  23536. if (ret == ECC_BAD_ARG_E) {
  23537. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  23538. }
  23539. if (ret == ECC_BAD_ARG_E) {
  23540. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  23541. }
  23542. if (ret == ECC_BAD_ARG_E) {
  23543. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  23544. }
  23545. if (ret == ECC_BAD_ARG_E) {
  23546. ret = 0;
  23547. }
  23548. else if (ret == 0) {
  23549. ret = WOLFSSL_FATAL_ERROR;
  23550. }
  23551. }
  23552. /* Copy */
  23553. if (ret == 0) {
  23554. ret = wc_ecc_copy_point(point, cpypt);
  23555. }
  23556. /* Test bad args. */
  23557. if (ret == 0) {
  23558. ret = wc_ecc_copy_point(NULL, cpypt);
  23559. if (ret == ECC_BAD_ARG_E) {
  23560. ret = wc_ecc_copy_point(point, NULL);
  23561. }
  23562. if (ret == ECC_BAD_ARG_E) {
  23563. ret = 0;
  23564. }
  23565. else if (ret == 0) {
  23566. ret = WOLFSSL_FATAL_ERROR;
  23567. }
  23568. }
  23569. /* Compare point */
  23570. if (ret == 0) {
  23571. ret = wc_ecc_cmp_point(point, cpypt);
  23572. }
  23573. /* Test bad args. */
  23574. if (ret == 0) {
  23575. ret = wc_ecc_cmp_point(NULL, cpypt);
  23576. if (ret == BAD_FUNC_ARG) {
  23577. ret = wc_ecc_cmp_point(point, NULL);
  23578. }
  23579. if (ret == BAD_FUNC_ARG) {
  23580. ret = 0;
  23581. }
  23582. else if (ret == 0) {
  23583. ret = WOLFSSL_FATAL_ERROR;
  23584. }
  23585. }
  23586. /* At infinity if return == 1, otherwise return == 0. */
  23587. if (ret == 0) {
  23588. ret = wc_ecc_point_is_at_infinity(point);
  23589. }
  23590. /* Test bad args. */
  23591. if (ret == 0) {
  23592. ret = wc_ecc_point_is_at_infinity(NULL);
  23593. if (ret == BAD_FUNC_ARG) {
  23594. ret = 0;
  23595. }
  23596. else if (ret == 0) {
  23597. ret = WOLFSSL_FATAL_ERROR;
  23598. }
  23599. }
  23600. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  23601. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  23602. #ifdef USE_ECC_B_PARAM
  23603. /* On curve if ret == 0 */
  23604. if (ret == 0) {
  23605. ret = wc_ecc_point_is_on_curve(point, idx);
  23606. }
  23607. /* Test bad args. */
  23608. if (ret == 0) {
  23609. ret = wc_ecc_point_is_on_curve(NULL, idx);
  23610. if (ret == BAD_FUNC_ARG) {
  23611. ret = wc_ecc_point_is_on_curve(point, 1000);
  23612. }
  23613. if (ret == ECC_BAD_ARG_E) {
  23614. ret = 0;
  23615. }
  23616. else if (ret == 0) {
  23617. ret = WOLFSSL_FATAL_ERROR;
  23618. }
  23619. }
  23620. #endif /* USE_ECC_B_PARAM */
  23621. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  23622. /* Free */
  23623. wc_ecc_del_point(point);
  23624. wc_ecc_del_point(cpypt);
  23625. wc_ecc_free(&key);
  23626. if (wc_FreeRng(&rng) && ret == 0) {
  23627. ret = WOLFSSL_FATAL_ERROR;
  23628. }
  23629. res = TEST_RES_CHECK(ret == 0);
  23630. #endif
  23631. return res;
  23632. } /* END test_wc_ecc_pointFns */
  23633. /*
  23634. * Testing wc_ecc_sahred_secret_ssh()
  23635. */
  23636. static int test_wc_ecc_shared_secret_ssh(void)
  23637. {
  23638. int res = TEST_SKIPPED;
  23639. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  23640. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23641. !defined(WOLFSSL_ATECC608A)
  23642. ecc_key key, key2;
  23643. WC_RNG rng;
  23644. int ret;
  23645. int keySz = KEY32;
  23646. int key2Sz = KEY24;
  23647. byte secret[KEY32];
  23648. word32 secretLen = keySz;
  23649. /* Init stack variables. */
  23650. XMEMSET(secret, 0, secretLen);
  23651. XMEMSET(&rng, 0, sizeof(rng));
  23652. XMEMSET(&key, 0, sizeof(key));
  23653. XMEMSET(&key2, 0, sizeof(key2));
  23654. /* Make keys */
  23655. ret = wc_InitRng(&rng);
  23656. if (ret == 0) {
  23657. ret = wc_ecc_init(&key);
  23658. if (ret == 0) {
  23659. ret = wc_ecc_make_key(&rng, keySz, &key);
  23660. #if defined(WOLFSSL_ASYNC_CRYPT)
  23661. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23662. #endif
  23663. }
  23664. if (wc_FreeRng(&rng) && ret == 0) {
  23665. ret = WOLFSSL_FATAL_ERROR;
  23666. }
  23667. }
  23668. if (ret == 0) {
  23669. ret = wc_InitRng(&rng);
  23670. if (ret == 0) {
  23671. ret = wc_ecc_init(&key2);
  23672. }
  23673. if (ret == 0) {
  23674. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  23675. #if defined(WOLFSSL_ASYNC_CRYPT)
  23676. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  23677. #endif
  23678. }
  23679. }
  23680. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23681. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23682. !defined(HAVE_SELFTEST)
  23683. if (ret == 0) {
  23684. ret = wc_ecc_set_rng(&key, &rng);
  23685. }
  23686. #endif
  23687. if (ret == 0) {
  23688. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23689. }
  23690. /* Pass in bad args. */
  23691. if (ret == 0) {
  23692. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  23693. if (ret == BAD_FUNC_ARG) {
  23694. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  23695. }
  23696. if (ret == BAD_FUNC_ARG) {
  23697. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  23698. }
  23699. if (ret == BAD_FUNC_ARG) {
  23700. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  23701. }
  23702. if (ret == BAD_FUNC_ARG) {
  23703. key.type = ECC_PUBLICKEY;
  23704. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23705. if (ret == ECC_BAD_ARG_E) {
  23706. ret = 0;
  23707. }
  23708. else if (ret == 0) {
  23709. ret = WOLFSSL_FATAL_ERROR;
  23710. }
  23711. }
  23712. else if (ret == 0) {
  23713. ret = WOLFSSL_FATAL_ERROR;
  23714. }
  23715. }
  23716. if (wc_FreeRng(&rng) && ret == 0) {
  23717. ret = WOLFSSL_FATAL_ERROR;
  23718. }
  23719. wc_ecc_free(&key);
  23720. wc_ecc_free(&key2);
  23721. #ifdef FP_ECC
  23722. wc_ecc_fp_free();
  23723. #endif
  23724. res = TEST_RES_CHECK(ret == 0);
  23725. #endif
  23726. return res;
  23727. } /* END test_wc_ecc_shared_secret_ssh */
  23728. /*
  23729. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23730. */
  23731. static int test_wc_ecc_verify_hash_ex(void)
  23732. {
  23733. int res = TEST_SKIPPED;
  23734. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23735. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23736. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23737. ecc_key key;
  23738. WC_RNG rng;
  23739. int ret;
  23740. mp_int r;
  23741. mp_int s;
  23742. mp_int z;
  23743. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23744. unsigned char iHash[] = "Everyone gets Friday off.......";
  23745. unsigned char shortHash[] = TEST_STRING;
  23746. word32 hashlen = sizeof(hash);
  23747. word32 iHashLen = sizeof(iHash);
  23748. word32 shortHashLen = sizeof(shortHash);
  23749. int keySz = KEY32;
  23750. int sig = WOLFSSL_FATAL_ERROR;
  23751. int ver = WOLFSSL_FATAL_ERROR;
  23752. int verify_ok = 0;
  23753. /* Initialize r and s. */
  23754. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23755. if (ret != MP_OKAY) {
  23756. return MP_INIT_E;
  23757. }
  23758. ret = wc_InitRng(&rng);
  23759. if (ret == 0) {
  23760. ret = wc_ecc_init(&key);
  23761. if (ret == 0) {
  23762. ret = wc_ecc_make_key(&rng, keySz, &key);
  23763. #if defined(WOLFSSL_ASYNC_CRYPT)
  23764. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23765. #endif
  23766. }
  23767. }
  23768. if (ret == 0) {
  23769. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23770. if (ret == 0) {
  23771. /* verify_ok should be 1. */
  23772. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23773. if (verify_ok != 1 && ret == 0) {
  23774. ret = WOLFSSL_FATAL_ERROR;
  23775. }
  23776. }
  23777. if (ret == 0) {
  23778. /* verify_ok should be 0 */
  23779. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23780. &verify_ok, &key);
  23781. if (verify_ok != 0 && ret == 0) {
  23782. ret = WOLFSSL_FATAL_ERROR;
  23783. }
  23784. }
  23785. if (ret == 0) {
  23786. /* verify_ok should be 0. */
  23787. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23788. &verify_ok, &key);
  23789. if (verify_ok != 0 && ret == 0) {
  23790. ret = WOLFSSL_FATAL_ERROR;
  23791. }
  23792. }
  23793. }
  23794. /* Test bad args. */
  23795. if (ret == 0) {
  23796. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23797. == ECC_BAD_ARG_E) {
  23798. sig = 0;
  23799. }
  23800. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23801. != ECC_BAD_ARG_E) {
  23802. sig = WOLFSSL_FATAL_ERROR;
  23803. }
  23804. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23805. != ECC_BAD_ARG_E) {
  23806. sig = WOLFSSL_FATAL_ERROR;
  23807. }
  23808. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23809. != ECC_BAD_ARG_E) {
  23810. sig = WOLFSSL_FATAL_ERROR;
  23811. }
  23812. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23813. != ECC_BAD_ARG_E) {
  23814. sig = WOLFSSL_FATAL_ERROR;
  23815. }
  23816. }
  23817. /* Test bad args. */
  23818. if (ret == 0) {
  23819. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23820. == ECC_BAD_ARG_E) {
  23821. ver = 0;
  23822. }
  23823. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23824. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23825. ver = WOLFSSL_FATAL_ERROR;
  23826. }
  23827. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23828. != MP_ZERO_E) {
  23829. ver = WOLFSSL_FATAL_ERROR;
  23830. }
  23831. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23832. != MP_ZERO_E) {
  23833. ver = WOLFSSL_FATAL_ERROR;
  23834. }
  23835. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23836. != MP_ZERO_E) {
  23837. ver = WOLFSSL_FATAL_ERROR;
  23838. }
  23839. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23840. &key) != ECC_BAD_ARG_E) {
  23841. ver = WOLFSSL_FATAL_ERROR;
  23842. }
  23843. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23844. NULL, &key) != ECC_BAD_ARG_E) {
  23845. ver = WOLFSSL_FATAL_ERROR;
  23846. }
  23847. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23848. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23849. ver = WOLFSSL_FATAL_ERROR;
  23850. }
  23851. }
  23852. wc_ecc_free(&key);
  23853. mp_free(&r);
  23854. mp_free(&s);
  23855. if (wc_FreeRng(&rng)) {
  23856. return WOLFSSL_FATAL_ERROR;
  23857. }
  23858. if (ret == 0 && (sig != 0 || ver != 0)) {
  23859. ret = WOLFSSL_FATAL_ERROR;
  23860. }
  23861. res = TEST_RES_CHECK(ret == 0);
  23862. #endif
  23863. return res;
  23864. } /* END test_wc_ecc_verify_hash_ex */
  23865. /*
  23866. * Testing wc_ecc_mulmod()
  23867. */
  23868. static int test_wc_ecc_mulmod(void)
  23869. {
  23870. int res = TEST_SKIPPED;
  23871. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23872. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23873. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23874. ecc_key key1, key2, key3;
  23875. WC_RNG rng;
  23876. int ret = 0;
  23877. ret = wc_InitRng(&rng);
  23878. if (ret == 0) {
  23879. ret = wc_ecc_init(&key1);
  23880. if (ret == 0) {
  23881. ret = wc_ecc_init(&key2);
  23882. }
  23883. if (ret == 0) {
  23884. ret = wc_ecc_init(&key3);
  23885. }
  23886. if (ret == 0) {
  23887. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23888. #if defined(WOLFSSL_ASYNC_CRYPT)
  23889. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23890. #endif
  23891. }
  23892. wc_FreeRng(&rng);
  23893. }
  23894. if (ret == 0) {
  23895. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23896. ECC_SECP256R1);
  23897. if (ret == 0) {
  23898. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23899. key1.dp->prime, ECC_SECP256R1);
  23900. }
  23901. }
  23902. if (ret == 0) {
  23903. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  23904. &key3.pubkey, wc_ecc_key_get_priv(&key2),
  23905. wc_ecc_key_get_priv(&key3), 1);
  23906. }
  23907. /* Test bad args. */
  23908. if (ret == 0) {
  23909. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  23910. wc_ecc_key_get_priv(&key2),
  23911. wc_ecc_key_get_priv(&key3), 1);
  23912. if (ret == ECC_BAD_ARG_E) {
  23913. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  23914. wc_ecc_key_get_priv(&key2),
  23915. wc_ecc_key_get_priv(&key3), 1);
  23916. }
  23917. if (ret == ECC_BAD_ARG_E) {
  23918. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  23919. wc_ecc_key_get_priv(&key2),
  23920. wc_ecc_key_get_priv(&key3), 1);
  23921. }
  23922. if (ret == ECC_BAD_ARG_E) {
  23923. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  23924. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL,
  23925. 1);
  23926. }
  23927. if (ret == ECC_BAD_ARG_E) {
  23928. ret = 0;
  23929. }
  23930. else if (ret == 0) {
  23931. ret = WOLFSSL_FATAL_ERROR;
  23932. }
  23933. }
  23934. wc_ecc_free(&key1);
  23935. wc_ecc_free(&key2);
  23936. wc_ecc_free(&key3);
  23937. #ifdef FP_ECC
  23938. wc_ecc_fp_free();
  23939. #endif
  23940. res = TEST_RES_CHECK(ret == 0);
  23941. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23942. return res;
  23943. } /* END test_wc_ecc_mulmod */
  23944. /*
  23945. * Testing wc_ecc_is_valid_idx()
  23946. */
  23947. static int test_wc_ecc_is_valid_idx(void)
  23948. {
  23949. int res = TEST_SKIPPED;
  23950. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23951. ecc_key key;
  23952. WC_RNG rng;
  23953. int ret;
  23954. int iVal = -2;
  23955. int iVal2 = 3000;
  23956. XMEMSET(&rng, 0, sizeof(rng));
  23957. XMEMSET(&key, 0, sizeof(key));
  23958. ret = wc_InitRng(&rng);
  23959. if (ret == 0) {
  23960. ret = wc_ecc_init(&key);
  23961. if (ret == 0) {
  23962. ret = wc_ecc_make_key(&rng, 32, &key);
  23963. #if defined(WOLFSSL_ASYNC_CRYPT)
  23964. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23965. #endif
  23966. }
  23967. }
  23968. if (ret == 0) {
  23969. ret = wc_ecc_is_valid_idx(key.idx);
  23970. if (ret == 1) {
  23971. ret = 0;
  23972. }
  23973. else {
  23974. ret = WOLFSSL_FATAL_ERROR;
  23975. }
  23976. }
  23977. /* Test bad args. */
  23978. if (ret == 0) {
  23979. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23980. if (ret == 0) {
  23981. ret = wc_ecc_is_valid_idx(iVal2);
  23982. }
  23983. if (ret != 0) {
  23984. ret = WOLFSSL_FATAL_ERROR;
  23985. }
  23986. }
  23987. if (wc_FreeRng(&rng) && ret == 0) {
  23988. ret = WOLFSSL_FATAL_ERROR;
  23989. }
  23990. wc_ecc_free(&key);
  23991. #ifdef FP_ECC
  23992. wc_ecc_fp_free();
  23993. #endif
  23994. res = TEST_RES_CHECK(ret == 0);
  23995. #endif
  23996. return res;
  23997. } /* END test_wc_ecc_is_valid_idx */
  23998. /*
  23999. * Testing wc_ecc_get_curve_id_from_oid()
  24000. */
  24001. static int test_wc_ecc_get_curve_id_from_oid(void)
  24002. {
  24003. int res = TEST_SKIPPED;
  24004. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  24005. !defined(HAVE_FIPS)
  24006. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  24007. word32 len = sizeof(oid);
  24008. int ret;
  24009. /* Bad Cases */
  24010. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  24011. if (ret == BAD_FUNC_ARG) {
  24012. ret = 0;
  24013. }
  24014. if (ret == 0) {
  24015. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  24016. if (ret == ECC_CURVE_INVALID) {
  24017. ret = 0;
  24018. }
  24019. }
  24020. /* Good Case */
  24021. if (ret == 0) {
  24022. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  24023. if (ret == ECC_SECP256R1) {
  24024. ret = 0;
  24025. }
  24026. }
  24027. res = TEST_RES_CHECK(ret == 0);
  24028. #endif
  24029. return res;
  24030. }/* END test_wc_ecc_get_curve_id_from_oid */
  24031. /*
  24032. * Testing wc_ecc_sig_size_calc()
  24033. */
  24034. static int test_wc_ecc_sig_size_calc(void)
  24035. {
  24036. int res = TEST_SKIPPED;
  24037. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  24038. ecc_key key;
  24039. WC_RNG rng;
  24040. int sz = 0;
  24041. int ret = 0;
  24042. ret = wc_InitRng(&rng);
  24043. if (ret == 0) {
  24044. ret = wc_ecc_init(&key);
  24045. if (ret == 0) {
  24046. ret = wc_ecc_make_key(&rng, 16, &key);
  24047. #if defined(WOLFSSL_ASYNC_CRYPT)
  24048. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  24049. #endif
  24050. }
  24051. sz = key.dp->size;
  24052. }
  24053. if (ret == 0) {
  24054. ret = wc_ecc_sig_size_calc(sz);
  24055. if (ret > 0) {
  24056. ret = 0;
  24057. }
  24058. }
  24059. wc_ecc_free(&key);
  24060. wc_FreeRng(&rng);
  24061. res = TEST_RES_CHECK(ret == 0);
  24062. #endif
  24063. return res;
  24064. } /* END test_wc_ecc_sig_size_calc */
  24065. /*
  24066. * Testing ToTraditional
  24067. */
  24068. static int test_ToTraditional(void)
  24069. {
  24070. int res = TEST_SKIPPED;
  24071. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  24072. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  24073. defined(OPENSSL_EXTRA_X509_SMALL))
  24074. XFILE f;
  24075. byte input[TWOK_BUF];
  24076. word32 sz;
  24077. int ret;
  24078. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  24079. AssertTrue((f != XBADFILE));
  24080. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  24081. XFCLOSE(f);
  24082. /* Good case */
  24083. ret = ToTraditional(input, sz);
  24084. if (ret > 0) {
  24085. ret = 0;
  24086. }
  24087. /* Bad cases */
  24088. if (ret == 0) {
  24089. ret = ToTraditional(NULL, 0);
  24090. if (ret == BAD_FUNC_ARG) {
  24091. ret = 0;
  24092. }
  24093. }
  24094. if (ret == 0) {
  24095. ret = ToTraditional(NULL, sz);
  24096. if (ret == BAD_FUNC_ARG) {
  24097. ret = 0;
  24098. }
  24099. }
  24100. if (ret == 0) {
  24101. ret = ToTraditional(input, 0);
  24102. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  24103. ret = 0;
  24104. }
  24105. }
  24106. res = TEST_RES_CHECK(ret == 0);
  24107. #endif
  24108. return res;
  24109. }/* End test_ToTraditional*/
  24110. /*
  24111. * Testing wc_EccPrivateKeyToDer
  24112. */
  24113. static int test_wc_EccPrivateKeyToDer(void)
  24114. {
  24115. int res = TEST_SKIPPED;
  24116. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24117. byte output[ONEK_BUF];
  24118. ecc_key eccKey;
  24119. WC_RNG rng;
  24120. word32 inLen;
  24121. int ret;
  24122. ret = wc_InitRng(&rng);
  24123. if (ret == 0) {
  24124. ret = wc_ecc_init(&eccKey);
  24125. if (ret == 0) {
  24126. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24127. #if defined(WOLFSSL_ASYNC_CRYPT)
  24128. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24129. #endif
  24130. }
  24131. inLen = (word32)sizeof(output);
  24132. /* Bad Cases */
  24133. if (ret == 0) {
  24134. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  24135. if (ret == BAD_FUNC_ARG) {
  24136. ret = 0;
  24137. }
  24138. }
  24139. if (ret == 0) {
  24140. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  24141. if (ret == BAD_FUNC_ARG) {
  24142. ret = 0;
  24143. }
  24144. }
  24145. if (ret == 0) {
  24146. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  24147. if (ret == LENGTH_ONLY_E) {
  24148. ret = 0;
  24149. }
  24150. }
  24151. if (ret == 0) {
  24152. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  24153. if (ret == BAD_FUNC_ARG) {
  24154. ret = 0;
  24155. }
  24156. }
  24157. /*Good Case */
  24158. if (ret == 0) {
  24159. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  24160. if (ret > 0) {
  24161. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  24162. /* test importing private only into a PKEY struct */
  24163. EC_KEY* ec;
  24164. EVP_PKEY* pkey;
  24165. const unsigned char* der = output;
  24166. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  24167. AssertNotNull(pkey);
  24168. der = output;
  24169. ec = d2i_ECPrivateKey(NULL, &der, ret);
  24170. AssertNotNull(ec);
  24171. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  24172. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  24173. #endif
  24174. ret = 0;
  24175. }
  24176. }
  24177. wc_ecc_free(&eccKey);
  24178. }
  24179. wc_FreeRng(&rng);
  24180. res = TEST_RES_CHECK(ret == 0);
  24181. #endif
  24182. return res;
  24183. }/* End test_wc_EccPrivateKeyToDer*/
  24184. /*
  24185. * Testing wc_DhPublicKeyDecode
  24186. */
  24187. static int test_wc_DhPublicKeyDecode(void)
  24188. {
  24189. int res = TEST_SKIPPED;
  24190. #ifndef NO_DH
  24191. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  24192. DhKey key;
  24193. word32 inOutIdx;
  24194. AssertIntEQ(wc_InitDhKey(&key), 0);
  24195. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  24196. BAD_FUNC_ARG);
  24197. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24198. BAD_FUNC_ARG);
  24199. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  24200. BAD_FUNC_ARG);
  24201. inOutIdx = 0;
  24202. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  24203. BAD_FUNC_ARG);
  24204. inOutIdx = 0;
  24205. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  24206. BAD_FUNC_ARG);
  24207. inOutIdx = 0;
  24208. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  24209. sizeof_dh_pub_key_der_2048), 0);
  24210. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  24211. key.pub.used != 0 && key.priv.used == 0);
  24212. wc_FreeDhKey(&key);
  24213. res = TEST_RES_CHECK(1);
  24214. #endif
  24215. #endif /* !NO_DH */
  24216. return res;
  24217. }
  24218. /*
  24219. * Testing wc_Ed25519KeyToDer
  24220. */
  24221. static int test_wc_Ed25519KeyToDer(void)
  24222. {
  24223. int res = TEST_SKIPPED;
  24224. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24225. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24226. byte output[ONEK_BUF];
  24227. ed25519_key ed25519Key;
  24228. WC_RNG rng;
  24229. word32 inLen;
  24230. int ret;
  24231. ret = wc_InitRng(&rng);
  24232. if (ret == 0) {
  24233. ret = wc_ed25519_init(&ed25519Key);
  24234. if (ret == 0) {
  24235. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24236. }
  24237. inLen = (word32)sizeof(output);
  24238. /* Bad Cases */
  24239. if (ret == 0) {
  24240. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  24241. if (ret == BAD_FUNC_ARG) {
  24242. ret = 0;
  24243. }
  24244. }
  24245. if (ret == 0) {
  24246. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  24247. if (ret == BAD_FUNC_ARG) {
  24248. ret = 0;
  24249. }
  24250. }
  24251. if (ret == 0) {
  24252. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  24253. if (ret == BAD_FUNC_ARG) {
  24254. ret = 0;
  24255. }
  24256. }
  24257. /* Good Cases */
  24258. if (ret == 0) {
  24259. /* length only */
  24260. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  24261. if (ret > 0) {
  24262. ret = 0;
  24263. }
  24264. }
  24265. if (ret == 0) {
  24266. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  24267. if (ret > 0) {
  24268. ret = 0;
  24269. }
  24270. }
  24271. wc_ed25519_free(&ed25519Key);
  24272. }
  24273. wc_FreeRng(&rng);
  24274. res = TEST_RES_CHECK(ret == 0);
  24275. #endif
  24276. return res;
  24277. }/* End test_wc_Ed25519KeyToDer*/
  24278. /*
  24279. * Testing wc_Ed25519PrivateKeyToDer
  24280. */
  24281. static int test_wc_Ed25519PrivateKeyToDer(void)
  24282. {
  24283. int res = TEST_SKIPPED;
  24284. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24285. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24286. byte output[ONEK_BUF];
  24287. ed25519_key ed25519PrivKey;
  24288. WC_RNG rng;
  24289. word32 inLen;
  24290. int ret;
  24291. ret = wc_InitRng(&rng);
  24292. if (ret == 0) {
  24293. ret = wc_ed25519_init(&ed25519PrivKey);
  24294. if (ret == 0) {
  24295. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  24296. }
  24297. inLen = (word32)sizeof(output);
  24298. /* Bad Cases */
  24299. if (ret == 0) {
  24300. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  24301. if (ret == BAD_FUNC_ARG) {
  24302. ret = 0;
  24303. }
  24304. }
  24305. if (ret == 0) {
  24306. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  24307. if (ret == BAD_FUNC_ARG) {
  24308. ret = 0;
  24309. }
  24310. }
  24311. if (ret == 0) {
  24312. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  24313. if (ret == BAD_FUNC_ARG) {
  24314. ret = 0;
  24315. }
  24316. }
  24317. /* Good Cases */
  24318. if (ret == 0) {
  24319. /* length only */
  24320. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  24321. if (ret > 0) {
  24322. ret = 0;
  24323. }
  24324. }
  24325. if (ret == 0) {
  24326. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  24327. if (ret > 0) {
  24328. ret = 0;
  24329. }
  24330. }
  24331. wc_ed25519_free(&ed25519PrivKey);
  24332. }
  24333. wc_FreeRng(&rng);
  24334. res = TEST_RES_CHECK(ret == 0);
  24335. #endif
  24336. return res;
  24337. }/* End test_wc_Ed25519PrivateKeyToDer*/
  24338. /*
  24339. * Testing wc_Ed448KeyToDer
  24340. */
  24341. static int test_wc_Ed448KeyToDer(void)
  24342. {
  24343. int res = TEST_SKIPPED;
  24344. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24345. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24346. byte output[ONEK_BUF];
  24347. ed448_key ed448Key;
  24348. WC_RNG rng;
  24349. word32 inLen;
  24350. int ret;
  24351. ret = wc_InitRng(&rng);
  24352. if (ret == 0) {
  24353. ret = wc_ed448_init(&ed448Key);
  24354. if (ret == 0) {
  24355. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24356. }
  24357. inLen = sizeof(output);
  24358. /* Bad Cases */
  24359. if (ret == 0) {
  24360. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  24361. if (ret == BAD_FUNC_ARG) {
  24362. ret = 0;
  24363. }
  24364. }
  24365. if (ret == 0) {
  24366. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  24367. if (ret == BAD_FUNC_ARG) {
  24368. ret = 0;
  24369. }
  24370. }
  24371. if (ret == 0) {
  24372. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  24373. if (ret == BAD_FUNC_ARG) {
  24374. ret = 0;
  24375. }
  24376. }
  24377. /* Good Cases */
  24378. if (ret == 0) {
  24379. /* length only */
  24380. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  24381. if (ret > 0) {
  24382. ret = 0;
  24383. }
  24384. }
  24385. if (ret == 0) {
  24386. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  24387. if (ret > 0) {
  24388. ret = 0;
  24389. }
  24390. }
  24391. wc_ed448_free(&ed448Key);
  24392. }
  24393. wc_FreeRng(&rng);
  24394. res = TEST_RES_CHECK(ret == 0);
  24395. #endif
  24396. return res;
  24397. }/* End test_wc_Ed448KeyToDer*/
  24398. /*
  24399. * Testing wc_Ed448PrivateKeyToDer
  24400. */
  24401. static int test_wc_Ed448PrivateKeyToDer(void)
  24402. {
  24403. int res = TEST_SKIPPED;
  24404. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  24405. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  24406. byte output[ONEK_BUF];
  24407. ed448_key ed448PrivKey;
  24408. WC_RNG rng;
  24409. word32 inLen;
  24410. int ret;
  24411. ret = wc_InitRng(&rng);
  24412. if (ret == 0) {
  24413. ret = wc_ed448_init(&ed448PrivKey);
  24414. if (ret == 0) {
  24415. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  24416. }
  24417. inLen = sizeof(output);
  24418. /* Bad Cases */
  24419. if (ret == 0) {
  24420. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  24421. if (ret == BAD_FUNC_ARG) {
  24422. ret = 0;
  24423. }
  24424. }
  24425. if (ret == 0) {
  24426. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  24427. if (ret == BAD_FUNC_ARG) {
  24428. ret = 0;
  24429. }
  24430. }
  24431. if (ret == 0) {
  24432. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  24433. if (ret == BAD_FUNC_ARG) {
  24434. ret = 0;
  24435. }
  24436. }
  24437. /* Good cases */
  24438. if (ret == 0) {
  24439. /* length only */
  24440. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  24441. if (ret > 0) {
  24442. ret = 0;
  24443. }
  24444. }
  24445. if (ret == 0) {
  24446. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  24447. if (ret > 0) {
  24448. ret = 0;
  24449. }
  24450. }
  24451. wc_ed448_free(&ed448PrivKey);
  24452. }
  24453. wc_FreeRng(&rng);
  24454. res = TEST_RES_CHECK(ret == 0);
  24455. #endif
  24456. return res;
  24457. }/* End test_wc_Ed448PrivateKeyToDer*/
  24458. /*
  24459. * Testing wc_SetSubjectBuffer
  24460. */
  24461. static int test_wc_SetSubjectBuffer(void)
  24462. {
  24463. int res = TEST_SKIPPED;
  24464. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  24465. Cert cert;
  24466. FILE* file;
  24467. byte* der;
  24468. word32 derSz;
  24469. int ret = 0;
  24470. derSz = FOURK_BUF;
  24471. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24472. if (der == NULL) {
  24473. ret = -1;
  24474. }
  24475. if (ret == 0) {
  24476. file = XFOPEN("./certs/ca-cert.der", "rb");
  24477. if (file != NULL) {
  24478. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  24479. XFCLOSE(file);
  24480. }
  24481. else {
  24482. ret = -1;
  24483. }
  24484. }
  24485. if (ret == 0) {
  24486. ret = wc_InitCert(&cert);
  24487. }
  24488. if (ret == 0) {
  24489. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  24490. }
  24491. if (ret == 0) {
  24492. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  24493. if (ret == BAD_FUNC_ARG) {
  24494. ret = 0;
  24495. }
  24496. }
  24497. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24498. res = TEST_RES_CHECK(ret == 0);
  24499. #endif
  24500. return res;
  24501. }/* End test_wc_SetSubjectBuffer*/
  24502. /*
  24503. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  24504. */
  24505. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  24506. {
  24507. int res = TEST_SKIPPED;
  24508. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24509. WC_RNG rng;
  24510. Cert cert;
  24511. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24512. ed25519_key ed25519Key;
  24513. #endif
  24514. #if !defined(NO_RSA) && defined(HAVE_RSA)
  24515. RsaKey rsaKey;
  24516. int bits = 2048;
  24517. #endif
  24518. #if defined(HAVE_ECC)
  24519. ecc_key eccKey;
  24520. #endif
  24521. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24522. ed448_key ed448Key;
  24523. #endif
  24524. int ret = 0;
  24525. #ifndef HAVE_FIPS
  24526. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24527. #else
  24528. ret = wc_InitRng(&rng);
  24529. #endif
  24530. wc_InitCert(&cert);
  24531. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24532. if (ret == 0) { /*ED25519*/
  24533. ret = wc_ed25519_init(&ed25519Key);
  24534. if (ret == 0) {
  24535. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24536. }
  24537. if (ret == 0) {
  24538. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24539. &ed25519Key);
  24540. }
  24541. wc_ed25519_free(&ed25519Key);
  24542. }
  24543. #endif
  24544. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24545. if (ret == 0) { /*RSA*/
  24546. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24547. if (ret == 0) {
  24548. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24549. }
  24550. if (ret == 0) {
  24551. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24552. }
  24553. wc_FreeRsaKey(&rsaKey);
  24554. }
  24555. #endif
  24556. #if defined(HAVE_ECC)
  24557. if (ret == 0) { /*ECC*/
  24558. ret = wc_ecc_init(&eccKey);
  24559. if (ret == 0) {
  24560. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24561. #if defined(WOLFSSL_ASYNC_CRYPT)
  24562. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24563. #endif
  24564. }
  24565. if (ret == 0) {
  24566. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24567. }
  24568. wc_ecc_free(&eccKey);
  24569. }
  24570. #endif
  24571. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24572. if (ret == 0) { /*ED448*/
  24573. ret = wc_ed448_init(&ed448Key);
  24574. if (ret == 0) {
  24575. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24576. }
  24577. if (ret == 0) {
  24578. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24579. &ed448Key);
  24580. }
  24581. wc_ed448_free(&ed448Key);
  24582. }
  24583. #endif
  24584. wc_FreeRng(&rng);
  24585. res = TEST_RES_CHECK(ret == 0);
  24586. #endif
  24587. return res;
  24588. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  24589. /*
  24590. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  24591. */
  24592. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  24593. {
  24594. int res = TEST_SKIPPED;
  24595. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24596. WC_RNG rng;
  24597. Cert cert;
  24598. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24599. ed25519_key ed25519Key;
  24600. #endif
  24601. #if !defined(NO_RSA) && defined(HAVE_RSA)
  24602. RsaKey rsaKey;
  24603. int bits = 2048;
  24604. #endif
  24605. #if defined(HAVE_ECC)
  24606. ecc_key eccKey;
  24607. #endif
  24608. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24609. ed448_key ed448Key;
  24610. #endif
  24611. int ret = 0;
  24612. #ifndef HAVE_FIPS
  24613. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  24614. #else
  24615. ret = wc_InitRng(&rng);
  24616. #endif
  24617. wc_InitCert(&cert);
  24618. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  24619. if (ret == 0) { /*ED25519*/
  24620. ret = wc_ed25519_init(&ed25519Key);
  24621. if (ret == 0) {
  24622. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  24623. }
  24624. if (ret == 0) {
  24625. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  24626. &ed25519Key);
  24627. }
  24628. wc_ed25519_free(&ed25519Key);
  24629. }
  24630. #endif
  24631. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  24632. if (ret == 0) { /*RSA*/
  24633. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  24634. if (ret == 0) {
  24635. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  24636. }
  24637. if (ret == 0) {
  24638. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  24639. }
  24640. wc_FreeRsaKey(&rsaKey);
  24641. }
  24642. #endif
  24643. #if defined(HAVE_ECC)
  24644. if (ret == 0) { /*ECC*/
  24645. ret = wc_ecc_init(&eccKey);
  24646. if (ret == 0) {
  24647. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  24648. #if defined(WOLFSSL_ASYNC_CRYPT)
  24649. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  24650. #endif
  24651. }
  24652. if (ret == 0) {
  24653. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  24654. }
  24655. wc_ecc_free(&eccKey);
  24656. }
  24657. #endif
  24658. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  24659. if (ret == 0) { /*ED448*/
  24660. ret = wc_ed448_init(&ed448Key);
  24661. if (ret == 0) {
  24662. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  24663. }
  24664. if (ret == 0) {
  24665. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  24666. &ed448Key);
  24667. }
  24668. wc_ed448_free(&ed448Key);
  24669. }
  24670. #endif
  24671. wc_FreeRng(&rng);
  24672. res = TEST_RES_CHECK(ret == 0);
  24673. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  24674. return res;
  24675. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  24676. /*
  24677. * Testing wc_PKCS7_New()
  24678. */
  24679. static int test_wc_PKCS7_New(void)
  24680. {
  24681. int res = TEST_SKIPPED;
  24682. #if defined(HAVE_PKCS7)
  24683. PKCS7* pkcs7;
  24684. pkcs7 = wc_PKCS7_New(NULL, testDevId);
  24685. wc_PKCS7_Free(pkcs7);
  24686. res = TEST_RES_CHECK(pkcs7 != NULL);
  24687. #endif
  24688. return res;
  24689. } /* END test-wc_PKCS7_New */
  24690. /*
  24691. * Testing wc_PKCS7_Init()
  24692. */
  24693. static int test_wc_PKCS7_Init(void)
  24694. {
  24695. int res = TEST_SKIPPED;
  24696. #if defined(HAVE_PKCS7)
  24697. PKCS7* pkcs7;
  24698. void* heap = NULL;
  24699. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24700. AssertNotNull(pkcs7);
  24701. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24702. /* Pass in bad args. */
  24703. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24704. wc_PKCS7_Free(pkcs7);
  24705. res = TEST_RES_CHECK(1);
  24706. #endif
  24707. return res;
  24708. } /* END test-wc_PKCS7_Init */
  24709. /*
  24710. * Testing wc_PKCS7_InitWithCert()
  24711. */
  24712. static int test_wc_PKCS7_InitWithCert(void)
  24713. {
  24714. int res = TEST_SKIPPED;
  24715. #if defined(HAVE_PKCS7)
  24716. PKCS7* pkcs7;
  24717. #ifndef NO_RSA
  24718. #if defined(USE_CERT_BUFFERS_2048)
  24719. unsigned char cert[sizeof(client_cert_der_2048)];
  24720. int certSz = (int)sizeof(cert);
  24721. XMEMSET(cert, 0, certSz);
  24722. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24723. #elif defined(USE_CERT_BUFFERS_1024)
  24724. unsigned char cert[sizeof(client_cert_der_1024)];
  24725. int certSz = (int)sizeof(cert);
  24726. XMEMSET(cert, 0, certSz);
  24727. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24728. #else
  24729. unsigned char cert[ONEK_BUF];
  24730. XFILE fp;
  24731. int certSz;
  24732. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24733. AssertTrue(fp != XBADFILE);
  24734. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24735. XFCLOSE(fp);
  24736. #endif
  24737. #elif defined(HAVE_ECC)
  24738. #if defined(USE_CERT_BUFFERS_256)
  24739. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24740. int certSz = (int)sizeof(cert);
  24741. XMEMSET(cert, 0, certSz);
  24742. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24743. #else
  24744. unsigned char cert[ONEK_BUF];
  24745. XFILE fp;
  24746. int certSz;
  24747. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24748. AssertTrue(fp != XBADFILE);
  24749. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24750. XFCLOSE(fp);
  24751. #endif
  24752. #else
  24753. #error PKCS7 requires ECC or RSA
  24754. #endif
  24755. #ifdef HAVE_ECC
  24756. {
  24757. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24758. static unsigned char certWithInvalidEccKey[] = {
  24759. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24760. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24761. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24762. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24763. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24764. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24765. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24766. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24767. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24768. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24769. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24770. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24771. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24772. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24773. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24774. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24775. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24776. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24777. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24778. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24779. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24780. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24781. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24782. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24783. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24784. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24785. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24786. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24787. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24788. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24789. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24790. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24791. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24792. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24793. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24794. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24795. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24796. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24797. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24798. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24799. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24800. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24801. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24802. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24803. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24804. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24805. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24806. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24807. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24808. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24809. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24810. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24811. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24812. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24813. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24814. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24815. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24816. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24817. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24818. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24819. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24820. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24821. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24822. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24823. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24824. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24825. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24826. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24827. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24828. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24829. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24830. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24831. 0x08, 0xA8, 0xB4};
  24832. #endif
  24833. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24834. /* If initialization is not successful, it's free'd in init func. */
  24835. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24836. wc_PKCS7_Free(pkcs7);
  24837. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24838. /* Valid initialization usage. */
  24839. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24840. /* Pass in bad args. No need free for null checks, free at end.*/
  24841. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24842. BAD_FUNC_ARG);
  24843. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24844. BAD_FUNC_ARG);
  24845. #ifdef HAVE_ECC
  24846. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24847. sizeof(certWithInvalidEccKey)), 0);
  24848. }
  24849. #endif
  24850. wc_PKCS7_Free(pkcs7);
  24851. res = TEST_RES_CHECK(1);
  24852. #endif
  24853. return res;
  24854. } /* END test_wc_PKCS7_InitWithCert */
  24855. /*
  24856. * Testing wc_PKCS7_EncodeData()
  24857. */
  24858. static int test_wc_PKCS7_EncodeData(void)
  24859. {
  24860. int res = TEST_SKIPPED;
  24861. #if defined(HAVE_PKCS7)
  24862. PKCS7* pkcs7;
  24863. byte output[FOURK_BUF];
  24864. byte data[] = "My encoded DER cert.";
  24865. #ifndef NO_RSA
  24866. #if defined(USE_CERT_BUFFERS_2048)
  24867. unsigned char cert[sizeof(client_cert_der_2048)];
  24868. unsigned char key[sizeof(client_key_der_2048)];
  24869. int certSz = (int)sizeof(cert);
  24870. int keySz = (int)sizeof(key);
  24871. XMEMSET(cert, 0, certSz);
  24872. XMEMSET(key, 0, keySz);
  24873. XMEMCPY(cert, client_cert_der_2048, certSz);
  24874. XMEMCPY(key, client_key_der_2048, keySz);
  24875. #elif defined(USE_CERT_BUFFERS_1024)
  24876. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24877. unsigned char key[sizeof_client_key_der_1024];
  24878. int certSz = (int)sizeof(cert);
  24879. int keySz = (int)sizeof(key);
  24880. XMEMSET(cert, 0, certSz);
  24881. XMEMSET(key, 0, keySz);
  24882. XMEMCPY(cert, client_cert_der_1024, certSz);
  24883. XMEMCPY(key, client_key_der_1024, keySz);
  24884. #else
  24885. unsigned char cert[ONEK_BUF];
  24886. unsigned char key[ONEK_BUF];
  24887. XFILE fp;
  24888. int certSz;
  24889. int keySz;
  24890. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24891. AssertTrue(fp != XBADFILE);
  24892. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24893. XFCLOSE(fp);
  24894. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24895. AssertTrue(fp != XBADFILE);
  24896. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24897. XFCLOSE(fp);
  24898. #endif
  24899. #elif defined(HAVE_ECC)
  24900. #if defined(USE_CERT_BUFFERS_256)
  24901. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24902. unsigned char key[sizeof(ecc_clikey_der_256)];
  24903. int certSz = (int)sizeof(cert);
  24904. int keySz = (int)sizeof(key);
  24905. XMEMSET(cert, 0, certSz);
  24906. XMEMSET(key, 0, keySz);
  24907. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24908. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24909. #else
  24910. unsigned char cert[ONEK_BUF];
  24911. unsigned char key[ONEK_BUF];
  24912. XFILE fp;
  24913. int certSz, keySz;
  24914. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24915. AssertTrue(fp != XBADFILE);
  24916. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24917. XFCLOSE(fp);
  24918. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24919. AssertTrue(fp != XBADFILE);
  24920. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24921. XFCLOSE(fp);
  24922. #endif
  24923. #endif
  24924. XMEMSET(output, 0, sizeof(output));
  24925. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24926. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24927. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24928. pkcs7->content = data;
  24929. pkcs7->contentSz = sizeof(data);
  24930. pkcs7->privateKey = key;
  24931. pkcs7->privateKeySz = keySz;
  24932. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24933. /* Test bad args. */
  24934. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24935. BAD_FUNC_ARG);
  24936. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24937. BAD_FUNC_ARG);
  24938. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24939. wc_PKCS7_Free(pkcs7);
  24940. res = TEST_RES_CHECK(1);
  24941. #endif
  24942. return res;
  24943. } /* END test_wc_PKCS7_EncodeData */
  24944. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24945. !defined(NO_RSA) && !defined(NO_SHA256)
  24946. /* RSA sign raw digest callback */
  24947. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24948. byte* out, word32 outSz, byte* privateKey,
  24949. word32 privateKeySz, int devid, int hashOID)
  24950. {
  24951. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24952. byte digInfoEncoding[] = {
  24953. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24954. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24955. 0x00, 0x04, 0x20
  24956. };
  24957. int ret;
  24958. byte digestInfo[ONEK_BUF];
  24959. byte sig[FOURK_BUF];
  24960. word32 digestInfoSz = 0;
  24961. word32 idx = 0;
  24962. RsaKey rsa;
  24963. /* SHA-256 required only for this example callback due to above
  24964. * digInfoEncoding[] */
  24965. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24966. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24967. (hashOID != SHA256h)) {
  24968. return -1;
  24969. }
  24970. /* build DigestInfo */
  24971. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24972. digestInfoSz += sizeof(digInfoEncoding);
  24973. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24974. digestInfoSz += digestSz;
  24975. /* set up RSA key */
  24976. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24977. if (ret != 0) {
  24978. return ret;
  24979. }
  24980. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24981. /* sign DigestInfo */
  24982. if (ret == 0) {
  24983. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24984. &rsa, pkcs7->rng);
  24985. if (ret > 0) {
  24986. if (ret > (int)outSz) {
  24987. /* output buffer too small */
  24988. ret = -1;
  24989. }
  24990. else {
  24991. /* success, ret holds sig size */
  24992. XMEMCPY(out, sig, ret);
  24993. }
  24994. }
  24995. }
  24996. wc_FreeRsaKey(&rsa);
  24997. return ret;
  24998. }
  24999. #endif
  25000. /*
  25001. * Testing wc_PKCS7_EncodeSignedData()
  25002. */
  25003. static int test_wc_PKCS7_EncodeSignedData(void)
  25004. {
  25005. int res = TEST_SKIPPED;
  25006. #if defined(HAVE_PKCS7)
  25007. PKCS7* pkcs7;
  25008. WC_RNG rng;
  25009. byte output[FOURK_BUF];
  25010. byte badOut[1];
  25011. word32 outputSz = (word32)sizeof(output);
  25012. word32 badOutSz = 0;
  25013. byte data[] = "Test data to encode.";
  25014. #ifndef NO_RSA
  25015. #if defined(USE_CERT_BUFFERS_2048)
  25016. byte key[sizeof(client_key_der_2048)];
  25017. byte cert[sizeof(client_cert_der_2048)];
  25018. word32 keySz = (word32)sizeof(key);
  25019. word32 certSz = (word32)sizeof(cert);
  25020. XMEMSET(key, 0, keySz);
  25021. XMEMSET(cert, 0, certSz);
  25022. XMEMCPY(key, client_key_der_2048, keySz);
  25023. XMEMCPY(cert, client_cert_der_2048, certSz);
  25024. #elif defined(USE_CERT_BUFFERS_1024)
  25025. byte key[sizeof_client_key_der_1024];
  25026. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25027. word32 keySz = (word32)sizeof(key);
  25028. word32 certSz = (word32)sizeof(cert);
  25029. XMEMSET(key, 0, keySz);
  25030. XMEMSET(cert, 0, certSz);
  25031. XMEMCPY(key, client_key_der_1024, keySz);
  25032. XMEMCPY(cert, client_cert_der_1024, certSz);
  25033. #else
  25034. unsigned char cert[ONEK_BUF];
  25035. unsigned char key[ONEK_BUF];
  25036. XFILE fp;
  25037. int certSz;
  25038. int keySz;
  25039. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25040. AssertTrue(fp != XBADFILE);
  25041. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25042. XFCLOSE(fp);
  25043. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25044. AssertTrue(fp != XBADFILE);
  25045. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25046. XFCLOSE(fp);
  25047. #endif
  25048. #elif defined(HAVE_ECC)
  25049. #if defined(USE_CERT_BUFFERS_256)
  25050. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25051. unsigned char key[sizeof(ecc_clikey_der_256)];
  25052. int certSz = (int)sizeof(cert);
  25053. int keySz = (int)sizeof(key);
  25054. XMEMSET(cert, 0, certSz);
  25055. XMEMSET(key, 0, keySz);
  25056. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  25057. XMEMCPY(key, ecc_clikey_der_256, keySz);
  25058. #else
  25059. unsigned char cert[ONEK_BUF];
  25060. unsigned char key[ONEK_BUF];
  25061. XFILE fp;
  25062. int certSz, keySz;
  25063. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  25064. AssertTrue(fp != XBADFILE);
  25065. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  25066. XFCLOSE(fp);
  25067. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25068. AssertTrue(fp != XBADFILE);
  25069. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  25070. XFCLOSE(fp);
  25071. #endif
  25072. #endif
  25073. XMEMSET(output, 0, outputSz);
  25074. AssertIntEQ(wc_InitRng(&rng), 0);
  25075. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25076. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25077. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25078. pkcs7->content = data;
  25079. pkcs7->contentSz = (word32)sizeof(data);
  25080. pkcs7->privateKey = key;
  25081. pkcs7->privateKeySz = (word32)sizeof(key);
  25082. pkcs7->encryptOID = RSAk;
  25083. #ifdef NO_SHA
  25084. pkcs7->hashOID = SHA256h;
  25085. #else
  25086. pkcs7->hashOID = SHAh;
  25087. #endif
  25088. pkcs7->rng = &rng;
  25089. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25090. wc_PKCS7_Free(pkcs7);
  25091. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25092. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25093. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25094. /* Pass in bad args. */
  25095. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  25096. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  25097. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  25098. badOutSz), BAD_FUNC_ARG);
  25099. pkcs7->hashOID = 0; /* bad hashOID */
  25100. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  25101. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25102. !defined(NO_RSA) && !defined(NO_SHA256)
  25103. /* test RSA sign raw digest callback, if using RSA and compiled in.
  25104. * Example callback assumes SHA-256, so only run test if compiled in. */
  25105. wc_PKCS7_Free(pkcs7);
  25106. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25107. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25108. pkcs7->content = data;
  25109. pkcs7->contentSz = (word32)sizeof(data);
  25110. pkcs7->privateKey = key;
  25111. pkcs7->privateKeySz = (word32)sizeof(key);
  25112. pkcs7->encryptOID = RSAk;
  25113. pkcs7->hashOID = SHA256h;
  25114. pkcs7->rng = &rng;
  25115. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  25116. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25117. #endif
  25118. wc_PKCS7_Free(pkcs7);
  25119. wc_FreeRng(&rng);
  25120. res = TEST_RES_CHECK(1);
  25121. #endif
  25122. return res;
  25123. } /* END test_wc_PKCS7_EncodeSignedData */
  25124. /*
  25125. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  25126. */
  25127. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  25128. {
  25129. int res = TEST_SKIPPED;
  25130. #if defined(HAVE_PKCS7)
  25131. int ret, i;
  25132. PKCS7* pkcs7;
  25133. WC_RNG rng;
  25134. byte outputHead[FOURK_BUF/2];
  25135. byte outputFoot[FOURK_BUF/2];
  25136. word32 outputHeadSz = (word32)sizeof(outputHead);
  25137. word32 outputFootSz = (word32)sizeof(outputFoot);
  25138. byte data[FOURK_BUF];
  25139. wc_HashAlg hash;
  25140. #ifdef NO_SHA
  25141. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25142. #else
  25143. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25144. #endif
  25145. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25146. word32 hashSz = wc_HashGetDigestSize(hashType);
  25147. #ifndef NO_RSA
  25148. #if defined(USE_CERT_BUFFERS_2048)
  25149. byte key[sizeof(client_key_der_2048)];
  25150. byte cert[sizeof(client_cert_der_2048)];
  25151. word32 keySz = (word32)sizeof(key);
  25152. word32 certSz = (word32)sizeof(cert);
  25153. XMEMSET(key, 0, keySz);
  25154. XMEMSET(cert, 0, certSz);
  25155. XMEMCPY(key, client_key_der_2048, keySz);
  25156. XMEMCPY(cert, client_cert_der_2048, certSz);
  25157. #elif defined(USE_CERT_BUFFERS_1024)
  25158. byte key[sizeof_client_key_der_1024];
  25159. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25160. word32 keySz = (word32)sizeof(key);
  25161. word32 certSz = (word32)sizeof(cert);
  25162. XMEMSET(key, 0, keySz);
  25163. XMEMSET(cert, 0, certSz);
  25164. XMEMCPY(key, client_key_der_1024, keySz);
  25165. XMEMCPY(cert, client_cert_der_1024, certSz);
  25166. #else
  25167. unsigned char cert[ONEK_BUF];
  25168. unsigned char key[ONEK_BUF];
  25169. XFILE fp;
  25170. int certSz;
  25171. int keySz;
  25172. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  25173. AssertTrue((fp != XBADFILE));
  25174. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  25175. XFCLOSE(fp);
  25176. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  25177. AssertTrue(fp != XBADFILE);
  25178. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  25179. XFCLOSE(fp);
  25180. #endif
  25181. #elif defined(HAVE_ECC)
  25182. #if defined(USE_CERT_BUFFERS_256)
  25183. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25184. unsigned char key[sizeof(ecc_clikey_der_256)];
  25185. int certSz = (int)sizeof(cert);
  25186. int keySz = (int)sizeof(key);
  25187. XMEMSET(cert, 0, certSz);
  25188. XMEMSET(key, 0, keySz);
  25189. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25190. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25191. #else
  25192. unsigned char cert[ONEK_BUF];
  25193. unsigned char key[ONEK_BUF];
  25194. XFILE fp;
  25195. int certSz, keySz;
  25196. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  25197. AssertTrue(fp != XBADFILE);
  25198. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  25199. XFCLOSE(fp);
  25200. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  25201. AssertTrue(fp != XBADFILE);
  25202. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  25203. XFCLOSE(fp);
  25204. #endif
  25205. #endif
  25206. /* initialize large data with sequence */
  25207. for (i=0; i<(int)sizeof(data); i++)
  25208. data[i] = i & 0xff;
  25209. XMEMSET(outputHead, 0, outputHeadSz);
  25210. XMEMSET(outputFoot, 0, outputFootSz);
  25211. AssertIntEQ(wc_InitRng(&rng), 0);
  25212. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25213. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25214. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25215. pkcs7->content = NULL; /* not used for ex */
  25216. pkcs7->contentSz = (word32)sizeof(data);
  25217. pkcs7->privateKey = key;
  25218. pkcs7->privateKeySz = (word32)sizeof(key);
  25219. pkcs7->encryptOID = RSAk;
  25220. #ifdef NO_SHA
  25221. pkcs7->hashOID = SHA256h;
  25222. #else
  25223. pkcs7->hashOID = SHAh;
  25224. #endif
  25225. pkcs7->rng = &rng;
  25226. /* calculate hash for content */
  25227. ret = wc_HashInit(&hash, hashType);
  25228. if (ret == 0) {
  25229. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25230. if (ret == 0) {
  25231. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25232. }
  25233. wc_HashFree(&hash, hashType);
  25234. }
  25235. AssertIntEQ(ret, 0);
  25236. /* Perform PKCS7 sign using hash directly */
  25237. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25238. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  25239. AssertIntGT(outputHeadSz, 0);
  25240. AssertIntGT(outputFootSz, 0);
  25241. wc_PKCS7_Free(pkcs7);
  25242. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25243. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25244. /* required parameter even on verify when using _ex, if using outputHead
  25245. * and outputFoot */
  25246. pkcs7->contentSz = (word32)sizeof(data);
  25247. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25248. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  25249. wc_PKCS7_Free(pkcs7);
  25250. /* assembly complete PKCS7 sign and use normal verify */
  25251. {
  25252. byte* output = (byte*)XMALLOC(
  25253. outputHeadSz + sizeof(data) + outputFootSz,
  25254. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25255. word32 outputSz = 0;
  25256. AssertNotNull(output);
  25257. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  25258. outputSz += outputHeadSz;
  25259. XMEMCPY(&output[outputSz], data, sizeof(data));
  25260. outputSz += sizeof(data);
  25261. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  25262. outputSz += outputFootSz;
  25263. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25264. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25265. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25266. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25267. }
  25268. /* Pass in bad args. */
  25269. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25270. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25271. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25272. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25273. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25274. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25275. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25276. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25277. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25278. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25279. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25280. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  25281. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25282. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  25283. pkcs7->hashOID = 0; /* bad hashOID */
  25284. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25285. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25286. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25287. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25288. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25289. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25290. #ifndef NO_PKCS7_STREAM
  25291. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25292. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25293. #else
  25294. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25295. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  25296. #endif
  25297. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25298. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25299. #ifndef NO_PKCS7_STREAM
  25300. /* can pass in 0 buffer length with streaming API */
  25301. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25302. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  25303. #else
  25304. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25305. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  25306. #endif
  25307. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25308. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  25309. #ifndef NO_PKCS7_STREAM
  25310. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25311. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  25312. #else
  25313. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25314. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  25315. #endif
  25316. wc_PKCS7_Free(pkcs7);
  25317. wc_FreeRng(&rng);
  25318. res = TEST_RES_CHECK(1);
  25319. #endif
  25320. return res;
  25321. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  25322. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25323. /**
  25324. * Loads certs/keys from files or buffers into the argument buffers,
  25325. * helper function called by CreatePKCS7SignedData().
  25326. *
  25327. * Returns 0 on success, negative on error.
  25328. */
  25329. static int LoadPKCS7SignedDataCerts(
  25330. int useIntermediateCertChain, int pkAlgoType,
  25331. byte* intCARoot, word32* intCARootSz,
  25332. byte* intCA1, word32* intCA1Sz,
  25333. byte* intCA2, word32* intCA2Sz,
  25334. byte* cert, word32* certSz,
  25335. byte* key, word32* keySz)
  25336. {
  25337. int ret = 0;
  25338. FILE* fp = NULL;
  25339. #ifndef NO_RSA
  25340. const char* intCARootRSA = "./certs/ca-cert.der";
  25341. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  25342. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  25343. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  25344. const char* intServKeyRSA = "./certs/server-key.der";
  25345. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  25346. const char* cli1024Cert = "./certs/1024/client-cert.der";
  25347. const char* cli1024Key = "./certs/1024/client-key.der";
  25348. #endif
  25349. #endif
  25350. #ifdef HAVE_ECC
  25351. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  25352. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  25353. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  25354. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  25355. const char* intServKeyECC = "./certs/ecc-key.der";
  25356. #ifndef USE_CERT_BUFFERS_256
  25357. const char* cliEccCert = "./certs/client-ecc-cert.der";
  25358. const char* cliEccKey = "./certs/client-ecc-key.der";
  25359. #endif
  25360. #endif
  25361. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  25362. ((useIntermediateCertChain == 1) &&
  25363. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  25364. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  25365. return BAD_FUNC_ARG;
  25366. }
  25367. /* Read/load certs and keys to use for signing based on PK type and chain */
  25368. switch (pkAlgoType) {
  25369. #ifndef NO_RSA
  25370. case RSA_TYPE:
  25371. if (useIntermediateCertChain == 1) {
  25372. fp = XFOPEN(intCARootRSA, "rb");
  25373. AssertNotNull(fp);
  25374. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25375. XFCLOSE(fp);
  25376. AssertIntGT(*intCARootSz, 0);
  25377. fp = XFOPEN(intCA1RSA, "rb");
  25378. AssertNotNull(fp);
  25379. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25380. XFCLOSE(fp);
  25381. AssertIntGT(*intCA1Sz, 0);
  25382. fp = XFOPEN(intCA2RSA, "rb");
  25383. AssertNotNull(fp);
  25384. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25385. XFCLOSE(fp);
  25386. AssertIntGT(*intCA2Sz, 0);
  25387. fp = XFOPEN(intServCertRSA, "rb");
  25388. AssertNotNull(fp);
  25389. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25390. XFCLOSE(fp);
  25391. AssertIntGT(*certSz, 0);
  25392. fp = XFOPEN(intServKeyRSA, "rb");
  25393. AssertNotNull(fp);
  25394. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25395. XFCLOSE(fp);
  25396. AssertIntGT(*keySz, 0);
  25397. }
  25398. else {
  25399. #if defined(USE_CERT_BUFFERS_2048)
  25400. *keySz = sizeof_client_key_der_2048;
  25401. *certSz = sizeof_client_cert_der_2048;
  25402. XMEMCPY(key, client_key_der_2048, *keySz);
  25403. XMEMCPY(cert, client_cert_der_2048, *certSz);
  25404. #elif defined(USE_CERT_BUFFERS_1024)
  25405. *keySz = sizeof_client_key_der_1024;
  25406. *certSz = sizeof_client_cert_der_1024;
  25407. XMEMCPY(key, client_key_der_1024, *keySz);
  25408. XMEMCPY(cert, client_cert_der_1024, *certSz);
  25409. #else
  25410. fp = XFOPEN(cli1024Key, "rb");
  25411. AssertNotNull(fp);
  25412. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25413. XFCLOSE(fp);
  25414. AssertIntGT(*keySz, 0);
  25415. fp = XFOPEN(cli1024Cert, "rb");
  25416. AssertNotNull(fp);
  25417. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25418. XFCLOSE(fp);
  25419. AssertIntGT(*certSz, 0);
  25420. #endif /* USE_CERT_BUFFERS_2048 */
  25421. }
  25422. break;
  25423. #endif /* !NO_RSA */
  25424. #ifdef HAVE_ECC
  25425. case ECC_TYPE:
  25426. if (useIntermediateCertChain == 1) {
  25427. fp = XFOPEN(intCARootECC, "rb");
  25428. AssertNotNull(fp);
  25429. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  25430. XFCLOSE(fp);
  25431. AssertIntGT(*intCARootSz, 0);
  25432. fp = XFOPEN(intCA1ECC, "rb");
  25433. AssertNotNull(fp);
  25434. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  25435. XFCLOSE(fp);
  25436. AssertIntGT(*intCA1Sz, 0);
  25437. fp = XFOPEN(intCA2ECC, "rb");
  25438. AssertNotNull(fp);
  25439. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  25440. XFCLOSE(fp);
  25441. AssertIntGT(*intCA2Sz, 0);
  25442. fp = XFOPEN(intServCertECC, "rb");
  25443. AssertNotNull(fp);
  25444. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25445. XFCLOSE(fp);
  25446. AssertIntGT(*certSz, 0);
  25447. fp = XFOPEN(intServKeyECC, "rb");
  25448. AssertNotNull(fp);
  25449. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25450. XFCLOSE(fp);
  25451. AssertIntGT(*keySz, 0);
  25452. }
  25453. else {
  25454. #if defined(USE_CERT_BUFFERS_256)
  25455. *keySz = sizeof_ecc_clikey_der_256;
  25456. *certSz = sizeof_cliecc_cert_der_256;
  25457. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  25458. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  25459. #else
  25460. fp = XFOPEN(cliEccKey, "rb");
  25461. AssertNotNull(fp);
  25462. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  25463. XFCLOSE(fp);
  25464. AssertIntGT(*keySz, 0);
  25465. fp = XFOPEN(cliEccCert, "rb");
  25466. AssertNotNull(fp);
  25467. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  25468. XFCLOSE(fp);
  25469. AssertIntGT(*certSz, 0);
  25470. #endif /* USE_CERT_BUFFERS_256 */
  25471. }
  25472. break;
  25473. #endif /* HAVE_ECC */
  25474. default:
  25475. WOLFSSL_MSG("Unsupported SignedData PK type");
  25476. ret = BAD_FUNC_ARG;
  25477. break;
  25478. }
  25479. return ret;
  25480. }
  25481. /**
  25482. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  25483. *
  25484. * output output buffer to place SignedData
  25485. * outputSz size of output buffer
  25486. * data data buffer to be signed
  25487. * dataSz size of data buffer
  25488. * withAttribs [1/0] include attributes in SignedData message
  25489. * detachedSig [1/0] create detached signature, no content
  25490. * useIntCertChain [1/0] use certificate chain and include intermediate and
  25491. * root CAs in bundle
  25492. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  25493. *
  25494. * Return size of bundle created on success, negative on error */
  25495. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  25496. byte* data, word32 dataSz,
  25497. int withAttribs, int detachedSig,
  25498. int useIntermediateCertChain,
  25499. int pkAlgoType)
  25500. {
  25501. int ret = 0;
  25502. WC_RNG rng;
  25503. PKCS7* pkcs7 = NULL;
  25504. static byte messageTypeOid[] =
  25505. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  25506. 0x09, 0x02 };
  25507. static byte messageType[] = { 0x13, 2, '1', '9' };
  25508. PKCS7Attrib attribs[] =
  25509. {
  25510. { messageTypeOid, sizeof(messageTypeOid), messageType,
  25511. sizeof(messageType) }
  25512. };
  25513. byte intCARoot[TWOK_BUF];
  25514. byte intCA1[TWOK_BUF];
  25515. byte intCA2[TWOK_BUF];
  25516. byte cert[TWOK_BUF];
  25517. byte key[TWOK_BUF];
  25518. word32 intCARootSz = sizeof(intCARoot);
  25519. word32 intCA1Sz = sizeof(intCA1);
  25520. word32 intCA2Sz = sizeof(intCA2);
  25521. word32 certSz = sizeof(cert);
  25522. word32 keySz = sizeof(key);
  25523. XMEMSET(intCARoot, 0, intCARootSz);
  25524. XMEMSET(intCA1, 0, intCA1Sz);
  25525. XMEMSET(intCA2, 0, intCA2Sz);
  25526. XMEMSET(cert, 0, certSz);
  25527. XMEMSET(key, 0, keySz);
  25528. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  25529. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  25530. cert, &certSz, key, &keySz);
  25531. AssertIntEQ(ret, 0);
  25532. XMEMSET(output, 0, outputSz);
  25533. AssertIntEQ(wc_InitRng(&rng), 0);
  25534. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25535. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25536. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25537. if (useIntermediateCertChain == 1) {
  25538. /* Add intermediate and root CA certs into SignedData Certs SET */
  25539. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  25540. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  25541. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  25542. }
  25543. pkcs7->content = data;
  25544. pkcs7->contentSz = dataSz;
  25545. pkcs7->privateKey = key;
  25546. pkcs7->privateKeySz = (word32)sizeof(key);
  25547. if (pkAlgoType == RSA_TYPE) {
  25548. pkcs7->encryptOID = RSAk;
  25549. }
  25550. else {
  25551. pkcs7->encryptOID = ECDSAk;
  25552. }
  25553. #ifdef NO_SHA
  25554. pkcs7->hashOID = SHA256h;
  25555. #else
  25556. pkcs7->hashOID = SHAh;
  25557. #endif
  25558. pkcs7->rng = &rng;
  25559. if (withAttribs) {
  25560. /* include a signed attribute */
  25561. pkcs7->signedAttribs = attribs;
  25562. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  25563. }
  25564. if (detachedSig) {
  25565. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  25566. }
  25567. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  25568. AssertIntGT(outputSz, 0);
  25569. wc_PKCS7_Free(pkcs7);
  25570. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25571. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25572. if (detachedSig) {
  25573. pkcs7->content = data;
  25574. pkcs7->contentSz = dataSz;
  25575. }
  25576. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25577. wc_PKCS7_Free(pkcs7);
  25578. wc_FreeRng(&rng);
  25579. return outputSz;
  25580. }
  25581. #endif
  25582. /*
  25583. * Testing wc_PKCS_VerifySignedData()
  25584. */
  25585. static int test_wc_PKCS7_VerifySignedData(void)
  25586. {
  25587. int res = TEST_SKIPPED;
  25588. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25589. PKCS7* pkcs7;
  25590. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25591. word32 outputSz = sizeof(output);
  25592. byte data[] = "Test data to encode.";
  25593. byte badOut[1];
  25594. word32 badOutSz = 0;
  25595. byte badContent[] = "This is different content than was signed";
  25596. int ret;
  25597. wc_HashAlg hash;
  25598. #ifdef NO_SHA
  25599. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25600. #else
  25601. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25602. #endif
  25603. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25604. word32 hashSz = wc_HashGetDigestSize(hashType);
  25605. #ifndef NO_RSA
  25606. PKCS7DecodedAttrib* decodedAttrib = NULL;
  25607. /* contentType OID (1.2.840.113549.1.9.3) */
  25608. static const byte contentTypeOid[] =
  25609. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  25610. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  25611. static const byte dataType[] =
  25612. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  25613. /* messageDigest OID (1.2.840.113549.1.9.4) */
  25614. static const byte messageDigestOid[] =
  25615. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  25616. #ifndef NO_ASN_TIME
  25617. /* signingTime OID () */
  25618. static const byte signingTimeOid[] =
  25619. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  25620. #endif
  25621. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25622. int dateLength = 0;
  25623. byte dateFormat;
  25624. const byte* datePart = NULL;
  25625. struct tm timearg;
  25626. time_t now;
  25627. struct tm* nowTm = NULL;
  25628. #ifdef NEED_TMP_TIME
  25629. struct tm tmpTimeStorage;
  25630. struct tm* tmpTime = &tmpTimeStorage;
  25631. #endif
  25632. #endif /* !NO_ASN && !NO_ASN_TIME */
  25633. /* Success test with RSA certs/key */
  25634. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25635. (word32)sizeof(data),
  25636. 0, 0, 0, RSA_TYPE)), 0);
  25637. /* calculate hash for content, used later */
  25638. ret = wc_HashInit(&hash, hashType);
  25639. if (ret == 0) {
  25640. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25641. if (ret == 0) {
  25642. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25643. }
  25644. wc_HashFree(&hash, hashType);
  25645. }
  25646. AssertIntEQ(ret, 0);
  25647. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25648. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25649. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25650. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25651. /* Check that decoded signed attributes are correct */
  25652. /* messageDigest should be first */
  25653. decodedAttrib = pkcs7->decodedAttrib;
  25654. AssertNotNull(decodedAttrib);
  25655. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  25656. AssertIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  25657. decodedAttrib->oidSz), 0);
  25658. /* + 2 for OCTET STRING and length bytes */
  25659. AssertIntEQ(decodedAttrib->valueSz, hashSz + 2);
  25660. AssertNotNull(decodedAttrib->value);
  25661. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  25662. #ifndef NO_ASN_TIME
  25663. /* signingTime should be second */
  25664. decodedAttrib = decodedAttrib->next;
  25665. AssertNotNull(decodedAttrib);
  25666. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  25667. AssertIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  25668. decodedAttrib->oidSz), 0);
  25669. AssertIntGT(decodedAttrib->valueSz, 0);
  25670. AssertNotNull(decodedAttrib->value);
  25671. #endif
  25672. /* Verify signingTime if ASN and time are available */
  25673. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25674. AssertIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  25675. &datePart, &dateFormat, &dateLength), 0);
  25676. AssertNotNull(datePart);
  25677. AssertIntGT(dateLength, 0);
  25678. XMEMSET(&timearg, 0, sizeof(timearg));
  25679. AssertIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  25680. &timearg), 0);
  25681. /* Get current time and compare year/month/day against attribute value */
  25682. AssertIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  25683. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  25684. AssertNotNull(nowTm);
  25685. AssertIntEQ(timearg.tm_year, nowTm->tm_year);
  25686. AssertIntEQ(timearg.tm_mon, nowTm->tm_mon);
  25687. AssertIntEQ(timearg.tm_mday, nowTm->tm_mday);
  25688. #endif /* !NO_ASN && !NO_ASN_TIME */
  25689. /* contentType should be third */
  25690. decodedAttrib = decodedAttrib->next;
  25691. AssertNotNull(decodedAttrib);
  25692. AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  25693. AssertIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  25694. decodedAttrib->oidSz), 0);
  25695. AssertIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  25696. AssertNotNull(decodedAttrib->value);
  25697. AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType,
  25698. sizeof(dataType)), 0);
  25699. #endif /* !NO_RSA */
  25700. #ifdef HAVE_ECC
  25701. #ifndef NO_RSA
  25702. wc_PKCS7_Free(pkcs7);
  25703. #endif
  25704. /* Success test with ECC certs/key */
  25705. outputSz = sizeof(output);
  25706. XMEMSET(output, 0, outputSz);
  25707. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25708. (word32)sizeof(data),
  25709. 0, 0, 0, ECC_TYPE)), 0);
  25710. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25711. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25712. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25713. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25714. #endif /* HAVE_ECC */
  25715. /* Test bad args. */
  25716. #if !defined(NO_RSA) || defined(HAVE_ECC)
  25717. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25718. BAD_FUNC_ARG);
  25719. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25720. BAD_FUNC_ARG);
  25721. #ifndef NO_PKCS7_STREAM
  25722. /* can pass in 0 buffer length with streaming API */
  25723. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25724. badOutSz), WC_PKCS7_WANT_READ_E);
  25725. #else
  25726. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25727. badOutSz), BAD_FUNC_ARG);
  25728. #endif
  25729. wc_PKCS7_Free(pkcs7);
  25730. #endif /* !NO_RSA || HAVE_ECC */
  25731. /* Invalid content should error, use detached signature so we can
  25732. * easily change content */
  25733. #ifndef NO_RSA
  25734. /* Try RSA certs/key/sig first */
  25735. outputSz = sizeof(output);
  25736. XMEMSET(output, 0, outputSz);
  25737. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25738. (word32)sizeof(data),
  25739. 1, 1, 0, RSA_TYPE)), 0);
  25740. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25741. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25742. pkcs7->content = badContent;
  25743. pkcs7->contentSz = sizeof(badContent);
  25744. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25745. SIG_VERIFY_E);
  25746. wc_PKCS7_Free(pkcs7);
  25747. /* Test success case with detached signature and valid content */
  25748. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25749. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25750. pkcs7->content = data;
  25751. pkcs7->contentSz = sizeof(data);
  25752. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25753. wc_PKCS7_Free(pkcs7);
  25754. /* verify using pre-computed content digest only (no content) */
  25755. {
  25756. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25757. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25758. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25759. output, outputSz,
  25760. NULL, 0), 0);
  25761. wc_PKCS7_Free(pkcs7);
  25762. }
  25763. #endif /* !NO_RSA */
  25764. #ifdef HAVE_ECC
  25765. /* Try ECC certs/key/sig next */
  25766. outputSz = sizeof(output);
  25767. XMEMSET(output, 0, outputSz);
  25768. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25769. (word32)sizeof(data),
  25770. 1, 1, 0, ECC_TYPE)), 0);
  25771. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25772. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25773. pkcs7->content = badContent;
  25774. pkcs7->contentSz = sizeof(badContent);
  25775. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25776. SIG_VERIFY_E);
  25777. wc_PKCS7_Free(pkcs7);
  25778. /* Test success case with detached signature and valid content */
  25779. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25780. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25781. pkcs7->content = data;
  25782. pkcs7->contentSz = sizeof(data);
  25783. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25784. wc_PKCS7_Free(pkcs7);
  25785. /* verify using pre-computed content digest only (no content) */
  25786. {
  25787. /* calculate hash for content */
  25788. ret = wc_HashInit(&hash, hashType);
  25789. if (ret == 0) {
  25790. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25791. if (ret == 0) {
  25792. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25793. }
  25794. wc_HashFree(&hash, hashType);
  25795. }
  25796. AssertIntEQ(ret, 0);
  25797. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25798. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25799. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25800. output, outputSz,
  25801. NULL, 0), 0);
  25802. wc_PKCS7_Free(pkcs7);
  25803. }
  25804. #endif
  25805. /* Test verify on signedData containing intermediate/root CA certs */
  25806. #ifndef NO_RSA
  25807. outputSz = sizeof(output);
  25808. XMEMSET(output, 0, outputSz);
  25809. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25810. (word32)sizeof(data),
  25811. 0, 0, 1, RSA_TYPE)), 0);
  25812. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25813. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25814. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25815. wc_PKCS7_Free(pkcs7);
  25816. #endif /* !NO_RSA */
  25817. #ifdef HAVE_ECC
  25818. outputSz = sizeof(output);
  25819. XMEMSET(output, 0, outputSz);
  25820. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25821. (word32)sizeof(data),
  25822. 0, 0, 1, ECC_TYPE)), 0);
  25823. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25824. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25825. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25826. wc_PKCS7_Free(pkcs7);
  25827. #endif /* HAVE_ECC */
  25828. res = TEST_RES_CHECK(1);
  25829. #endif
  25830. return res;
  25831. } /* END test_wc_PKCS7_VerifySignedData() */
  25832. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25833. !defined(NO_AES_256)
  25834. static const byte defKey[] = {
  25835. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25836. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25837. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25838. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25839. };
  25840. static byte aesHandle[32]; /* simulated hardware key handle */
  25841. /* return 0 on success */
  25842. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25843. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25844. byte* in, int inSz, byte* out, void* usrCtx)
  25845. {
  25846. int ret;
  25847. Aes aes;
  25848. if (usrCtx == NULL) {
  25849. /* no simulated handle passed in */
  25850. return -1;
  25851. }
  25852. switch (encryptOID) {
  25853. case AES256CBCb:
  25854. if (ivSz != AES_BLOCK_SIZE)
  25855. return BAD_FUNC_ARG;
  25856. break;
  25857. default:
  25858. WOLFSSL_MSG("Unsupported content cipher type for test");
  25859. return ALGO_ID_E;
  25860. };
  25861. /* simulate using handle to get key */
  25862. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25863. if (ret == 0) {
  25864. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25865. if (ret == 0)
  25866. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25867. wc_AesFree(&aes);
  25868. }
  25869. (void)aad;
  25870. (void)aadSz;
  25871. (void)authTag;
  25872. (void)authTagSz;
  25873. (void)pkcs7;
  25874. return ret;
  25875. }
  25876. /* returns key size on success */
  25877. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25878. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25879. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25880. {
  25881. int ret = -1;
  25882. if (out == NULL)
  25883. return BAD_FUNC_ARG;
  25884. if (keyId[0] != 0x00) {
  25885. return -1;
  25886. }
  25887. if (type != (int)PKCS7_KEKRI) {
  25888. return -1;
  25889. }
  25890. switch (keyWrapAlgo) {
  25891. case AES256_WRAP:
  25892. /* simulate setting a handle for later decryption but use key
  25893. * as handle in the test case here */
  25894. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25895. aesHandle, sizeof(aesHandle), NULL);
  25896. if (ret < 0)
  25897. return ret;
  25898. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25899. if (ret < 0)
  25900. return ret;
  25901. /* return key size on success */
  25902. return sizeof(defKey);
  25903. default:
  25904. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25905. return BAD_KEYWRAP_ALG_E;
  25906. };
  25907. (void)cekSz;
  25908. (void)cek;
  25909. (void)outSz;
  25910. (void)keyIdSz;
  25911. (void)direction;
  25912. (void)orginKey; /* used with KAKRI */
  25913. (void)orginKeySz;
  25914. return ret;
  25915. }
  25916. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25917. /*
  25918. * Testing wc_PKCS7_EncodeEnvelopedData()
  25919. */
  25920. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25921. {
  25922. int res = TEST_SKIPPED;
  25923. #if defined(HAVE_PKCS7)
  25924. PKCS7* pkcs7;
  25925. #ifdef ECC_TIMING_RESISTANT
  25926. WC_RNG rng;
  25927. #endif
  25928. word32 tempWrd32 = 0;
  25929. byte* tmpBytePtr = NULL;
  25930. const char input[] = "Test data to encode.";
  25931. int i;
  25932. int testSz = 0;
  25933. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25934. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25935. byte* rsaCert = NULL;
  25936. byte* rsaPrivKey = NULL;
  25937. word32 rsaCertSz;
  25938. word32 rsaPrivKeySz;
  25939. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25940. !defined(USE_CERT_BUFFERS_2048) )
  25941. static const char* rsaClientCert = "./certs/client-cert.der";
  25942. static const char* rsaClientKey = "./certs/client-key.der";
  25943. rsaCertSz = (word32)sizeof(rsaClientCert);
  25944. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25945. #endif
  25946. #endif
  25947. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25948. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25949. byte* eccCert = NULL;
  25950. byte* eccPrivKey = NULL;
  25951. word32 eccCertSz;
  25952. word32 eccPrivKeySz;
  25953. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25954. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25955. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25956. #endif
  25957. #endif
  25958. /* Generic buffer size. */
  25959. byte output[ONEK_BUF];
  25960. byte decoded[sizeof(input)/sizeof(char)];
  25961. int decodedSz = 0;
  25962. #ifndef NO_FILESYSTEM
  25963. XFILE certFile;
  25964. XFILE keyFile;
  25965. #endif
  25966. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25967. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25968. /* RSA certs and keys. */
  25969. #if defined(USE_CERT_BUFFERS_1024)
  25970. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25971. /* Allocate buffer space. */
  25972. AssertNotNull(rsaCert =
  25973. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25974. /* Init buffer. */
  25975. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25976. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25977. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25978. DYNAMIC_TYPE_TMP_BUFFER));
  25979. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25980. #elif defined(USE_CERT_BUFFERS_2048)
  25981. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25982. /* Allocate buffer */
  25983. AssertNotNull(rsaCert =
  25984. (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25985. /* Init buffer. */
  25986. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25987. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25988. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25989. DYNAMIC_TYPE_TMP_BUFFER));
  25990. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25991. #else
  25992. /* File system. */
  25993. certFile = XFOPEN(rsaClientCert, "rb");
  25994. AssertTrue(certFile != XBADFILE);
  25995. rsaCertSz = (word32)FOURK_BUF;
  25996. AssertNotNull(rsaCert =
  25997. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25998. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25999. XFCLOSE(certFile);
  26000. keyFile = XFOPEN(rsaClientKey, "rb");
  26001. AssertTrue(keyFile != XBADFILE);
  26002. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26003. DYNAMIC_TYPE_TMP_BUFFER));
  26004. rsaPrivKeySz = (word32)FOURK_BUF;
  26005. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  26006. XFCLOSE(keyFile);
  26007. #endif /* USE_CERT_BUFFERS */
  26008. #endif /* NO_RSA */
  26009. /* ECC */
  26010. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26011. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26012. #ifdef USE_CERT_BUFFERS_256
  26013. AssertNotNull(eccCert =
  26014. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26015. /* Init buffer. */
  26016. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  26017. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  26018. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  26019. DYNAMIC_TYPE_TMP_BUFFER));
  26020. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  26021. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  26022. #else /* File system. */
  26023. certFile = XFOPEN(eccClientCert, "rb");
  26024. AssertTrue(certFile != XBADFILE);
  26025. eccCertSz = (word32)FOURK_BUF;
  26026. AssertNotNull(eccCert =
  26027. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26028. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  26029. XFCLOSE(certFile);
  26030. keyFile = XFOPEN(eccClientKey, "rb");
  26031. AssertTrue(keyFile != XBADFILE);
  26032. eccPrivKeySz = (word32)FOURK_BUF;
  26033. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  26034. DYNAMIC_TYPE_TMP_BUFFER));
  26035. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  26036. XFCLOSE(keyFile);
  26037. #endif /* USE_CERT_BUFFERS_256 */
  26038. #endif /* END HAVE_ECC */
  26039. #ifndef NO_FILESYSTEM
  26040. /* Silence. */
  26041. (void)keyFile;
  26042. (void)certFile;
  26043. #endif
  26044. {
  26045. const pkcs7EnvelopedVector testVectors[] = {
  26046. /* DATA is a global variable defined in the makefile. */
  26047. #if !defined(NO_RSA)
  26048. #ifndef NO_DES3
  26049. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  26050. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26051. #endif /* NO_DES3 */
  26052. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26053. #ifndef NO_AES_128
  26054. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26055. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26056. #endif
  26057. #ifndef NO_AES_192
  26058. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  26059. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26060. #endif
  26061. #ifndef NO_AES_256
  26062. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26063. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  26064. #endif
  26065. #endif /* NO_AES && HAVE_AES_CBC */
  26066. #endif /* NO_RSA */
  26067. #if defined(HAVE_ECC)
  26068. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26069. #if !defined(NO_SHA) && !defined(NO_AES_128)
  26070. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  26071. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  26072. eccCertSz, eccPrivKey, eccPrivKeySz},
  26073. #endif
  26074. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  26075. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26076. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  26077. eccCertSz, eccPrivKey, eccPrivKeySz},
  26078. #endif
  26079. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  26080. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  26081. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  26082. eccCertSz, eccPrivKey, eccPrivKeySz},
  26083. #endif
  26084. #endif /* NO_AES && HAVE_AES_CBC*/
  26085. #endif /* END HAVE_ECC */
  26086. }; /* END pkcs7EnvelopedVector */
  26087. #ifdef ECC_TIMING_RESISTANT
  26088. AssertIntEQ(wc_InitRng(&rng), 0);
  26089. #endif
  26090. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26091. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26092. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  26093. for (i = 0; i < testSz; i++) {
  26094. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  26095. (word32)(testVectors + i)->certSz), 0);
  26096. #ifdef ECC_TIMING_RESISTANT
  26097. pkcs7->rng = &rng;
  26098. #endif
  26099. pkcs7->content = (byte*)(testVectors + i)->content;
  26100. pkcs7->contentSz = (testVectors + i)->contentSz;
  26101. pkcs7->contentOID = (testVectors + i)->contentOID;
  26102. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  26103. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  26104. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  26105. pkcs7->privateKey = (testVectors + i)->privateKey;
  26106. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  26107. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26108. (word32)sizeof(output)), 0);
  26109. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26110. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  26111. AssertIntGE(decodedSz, 0);
  26112. /* Verify the size of each buffer. */
  26113. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  26114. /* Don't free the last time through the loop. */
  26115. if (i < testSz - 1) {
  26116. wc_PKCS7_Free(pkcs7);
  26117. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26118. }
  26119. } /* END test loop. */
  26120. }
  26121. /* Test bad args. */
  26122. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  26123. (word32)sizeof(output)), BAD_FUNC_ARG);
  26124. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  26125. (word32)sizeof(output)), BAD_FUNC_ARG);
  26126. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  26127. /* Decode. */
  26128. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  26129. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26130. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26131. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26132. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26133. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  26134. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  26135. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26136. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  26137. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26138. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  26139. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  26140. /* only a failure for KARI test cases */
  26141. tempWrd32 = pkcs7->singleCertSz;
  26142. pkcs7->singleCertSz = 0;
  26143. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26144. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26145. pkcs7->singleCertSz = tempWrd32;
  26146. tmpBytePtr = pkcs7->singleCert;
  26147. pkcs7->singleCert = NULL;
  26148. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26149. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26150. pkcs7->singleCert = tmpBytePtr;
  26151. #endif
  26152. tempWrd32 = pkcs7->privateKeySz;
  26153. pkcs7->privateKeySz = 0;
  26154. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26155. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26156. pkcs7->privateKeySz = tempWrd32;
  26157. tmpBytePtr = pkcs7->privateKey;
  26158. pkcs7->privateKey = NULL;
  26159. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26160. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  26161. pkcs7->privateKey = tmpBytePtr;
  26162. wc_PKCS7_Free(pkcs7);
  26163. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  26164. /* test of decrypt callback with KEKRI enveloped data */
  26165. {
  26166. int envelopedSz;
  26167. const byte keyId[] = { 0x00 };
  26168. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26169. pkcs7->content = (byte*)input;
  26170. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  26171. pkcs7->contentOID = DATA;
  26172. pkcs7->encryptOID = AES256CBCb;
  26173. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  26174. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  26175. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  26176. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  26177. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  26178. (word32)sizeof(output))), 0);
  26179. wc_PKCS7_Free(pkcs7);
  26180. /* decode envelopedData */
  26181. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26182. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  26183. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  26184. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  26185. envelopedSz, decoded, sizeof(decoded))), 0);
  26186. wc_PKCS7_Free(pkcs7);
  26187. }
  26188. #endif /* !NO_AES && !NO_AES_256 */
  26189. #ifndef NO_RSA
  26190. if (rsaCert) {
  26191. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26192. }
  26193. if (rsaPrivKey) {
  26194. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26195. }
  26196. #endif /*NO_RSA */
  26197. #ifdef HAVE_ECC
  26198. if (eccCert) {
  26199. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26200. }
  26201. if (eccPrivKey) {
  26202. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26203. }
  26204. #endif /* HAVE_ECC */
  26205. #ifdef ECC_TIMING_RESISTANT
  26206. wc_FreeRng(&rng);
  26207. #endif
  26208. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  26209. !defined(NO_RSA) && !defined(NO_SHA)
  26210. {
  26211. byte out[7];
  26212. byte *cms;
  26213. word32 cmsSz;
  26214. XFILE cmsFile;
  26215. XMEMSET(out, 0, sizeof(out));
  26216. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26217. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  26218. AssertTrue(cmsFile != XBADFILE);
  26219. cmsSz = (word32)FOURK_BUF;
  26220. AssertNotNull(cms =
  26221. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  26222. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  26223. XFCLOSE(cmsFile);
  26224. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  26225. sizeof_client_cert_der_2048), 0);
  26226. pkcs7->privateKey = (byte*)client_key_der_2048;
  26227. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  26228. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26229. 2), 0);
  26230. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  26231. sizeof(out)), 0);
  26232. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26233. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  26234. wc_PKCS7_Free(pkcs7);
  26235. }
  26236. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  26237. res = TEST_RES_CHECK(1);
  26238. #endif /* HAVE_PKCS7 */
  26239. return res;
  26240. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  26241. /*
  26242. * Testing wc_PKCS7_EncodeEncryptedData()
  26243. */
  26244. static int test_wc_PKCS7_EncodeEncryptedData(void)
  26245. {
  26246. int res = TEST_SKIPPED;
  26247. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26248. PKCS7* pkcs7 = NULL;
  26249. byte* tmpBytePtr = NULL;
  26250. byte encrypted[TWOK_BUF];
  26251. byte decoded[TWOK_BUF];
  26252. word32 tmpWrd32 = 0;
  26253. int tmpInt = 0;
  26254. int decodedSz;
  26255. int encryptedSz;
  26256. int testSz;
  26257. int i;
  26258. const byte data[] = { /* Hello World */
  26259. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26260. 0x72,0x6c,0x64
  26261. };
  26262. #ifndef NO_DES3
  26263. byte desKey[] = {
  26264. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  26265. };
  26266. byte des3Key[] = {
  26267. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  26268. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  26269. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  26270. };
  26271. #endif
  26272. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26273. #ifndef NO_AES_128
  26274. byte aes128Key[] = {
  26275. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26276. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26277. };
  26278. #endif
  26279. #ifndef NO_AES_192
  26280. byte aes192Key[] = {
  26281. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26282. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26283. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26284. };
  26285. #endif
  26286. #ifndef NO_AES_256
  26287. byte aes256Key[] = {
  26288. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26289. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26290. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26291. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26292. };
  26293. #endif
  26294. #endif /* !NO_AES && HAVE_AES_CBC */
  26295. const pkcs7EncryptedVector testVectors[] =
  26296. {
  26297. #ifndef NO_DES3
  26298. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  26299. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  26300. #endif /* !NO_DES3 */
  26301. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26302. #ifndef NO_AES_128
  26303. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  26304. sizeof(aes128Key)},
  26305. #endif
  26306. #ifndef NO_AES_192
  26307. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  26308. sizeof(aes192Key)},
  26309. #endif
  26310. #ifndef NO_AES_256
  26311. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  26312. sizeof(aes256Key)},
  26313. #endif
  26314. #endif /* !NO_AES && HAVE_AES_CBC */
  26315. };
  26316. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  26317. for (i = 0; i < testSz; i++) {
  26318. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26319. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26320. pkcs7->content = (byte*)testVectors[i].content;
  26321. pkcs7->contentSz = testVectors[i].contentSz;
  26322. pkcs7->contentOID = testVectors[i].contentOID;
  26323. pkcs7->encryptOID = testVectors[i].encryptOID;
  26324. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  26325. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  26326. pkcs7->heap = HEAP_HINT;
  26327. /* encode encryptedData */
  26328. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26329. sizeof(encrypted));
  26330. AssertIntGT(encryptedSz, 0);
  26331. /* Decode encryptedData */
  26332. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26333. decoded, sizeof(decoded));
  26334. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  26335. /* Keep values for last itr. */
  26336. if (i < testSz - 1) {
  26337. wc_PKCS7_Free(pkcs7);
  26338. }
  26339. }
  26340. if (pkcs7 == NULL || testSz == 0) {
  26341. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26342. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26343. }
  26344. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26345. sizeof(encrypted)),BAD_FUNC_ARG);
  26346. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26347. sizeof(encrypted)), BAD_FUNC_ARG);
  26348. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26349. 0), BAD_FUNC_ARG);
  26350. /* Testing the struct. */
  26351. tmpBytePtr = pkcs7->content;
  26352. pkcs7->content = NULL;
  26353. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26354. sizeof(encrypted)), BAD_FUNC_ARG);
  26355. pkcs7->content = tmpBytePtr;
  26356. tmpWrd32 = pkcs7->contentSz;
  26357. pkcs7->contentSz = 0;
  26358. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26359. sizeof(encrypted)), BAD_FUNC_ARG);
  26360. pkcs7->contentSz = tmpWrd32;
  26361. tmpInt = pkcs7->encryptOID;
  26362. pkcs7->encryptOID = 0;
  26363. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26364. sizeof(encrypted)), BAD_FUNC_ARG);
  26365. pkcs7->encryptOID = tmpInt;
  26366. tmpBytePtr = pkcs7->encryptionKey;
  26367. pkcs7->encryptionKey = NULL;
  26368. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26369. sizeof(encrypted)), BAD_FUNC_ARG);
  26370. pkcs7->encryptionKey = tmpBytePtr;
  26371. tmpWrd32 = pkcs7->encryptionKeySz;
  26372. pkcs7->encryptionKeySz = 0;
  26373. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26374. sizeof(encrypted)), BAD_FUNC_ARG);
  26375. pkcs7->encryptionKeySz = tmpWrd32;
  26376. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  26377. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26378. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  26379. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26380. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26381. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26382. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26383. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26384. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26385. decoded, 0), BAD_FUNC_ARG);
  26386. /* Test struct fields */
  26387. tmpBytePtr = pkcs7->encryptionKey;
  26388. pkcs7->encryptionKey = NULL;
  26389. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26390. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26391. pkcs7->encryptionKey = tmpBytePtr;
  26392. pkcs7->encryptionKeySz = 0;
  26393. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26394. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26395. wc_PKCS7_Free(pkcs7);
  26396. res = TEST_RES_CHECK(1);
  26397. #endif
  26398. return res;
  26399. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26400. /*
  26401. * Testing wc_PKCS7_Degenerate()
  26402. */
  26403. static int test_wc_PKCS7_Degenerate(void)
  26404. {
  26405. int res = TEST_SKIPPED;
  26406. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26407. PKCS7* pkcs7;
  26408. char fName[] = "./certs/test-degenerate.p7b";
  26409. XFILE f;
  26410. byte der[4096];
  26411. word32 derSz;
  26412. int ret;
  26413. AssertNotNull(f = XFOPEN(fName, "rb"));
  26414. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26415. derSz = (word32)ret;
  26416. XFCLOSE(f);
  26417. /* test degenerate success */
  26418. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26419. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26420. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26421. #ifndef NO_RSA
  26422. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26423. #else
  26424. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26425. #endif
  26426. wc_PKCS7_Free(pkcs7);
  26427. /* test with turning off degenerate cases */
  26428. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26429. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26430. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26431. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26432. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  26433. wc_PKCS7_Free(pkcs7);
  26434. res = TEST_RES_CHECK(1);
  26435. #endif
  26436. return res;
  26437. } /* END test_wc_PKCS7_Degenerate() */
  26438. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26439. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26440. static byte berContent[] = {
  26441. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26442. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26443. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26444. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26445. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26446. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26447. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26448. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26449. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26450. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26451. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26452. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26453. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26454. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26455. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26456. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26457. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26458. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26459. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26460. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26461. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26462. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26463. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26464. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26465. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26466. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26467. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26468. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26469. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26470. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26471. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26472. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26473. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26474. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26475. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26476. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26477. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26478. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26479. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26480. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26481. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26482. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26483. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26484. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26485. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26486. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26487. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26488. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26489. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26490. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26491. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26492. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26493. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26494. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26495. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26496. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26497. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26498. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26499. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26500. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26501. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26502. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26503. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26504. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26505. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26506. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26507. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26508. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26509. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26510. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26511. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26512. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26513. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26514. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26515. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26516. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26517. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26518. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26519. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26520. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26521. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26522. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26523. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26524. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26525. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26526. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26527. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26528. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26529. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26530. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26531. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26532. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26533. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26534. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26535. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26536. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26537. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26538. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26539. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26540. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26541. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26542. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26543. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26544. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26545. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26546. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26547. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26548. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26549. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26550. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26551. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26552. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26553. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26554. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26555. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26556. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26557. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26558. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26559. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26560. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26561. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26562. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26563. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26564. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26565. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26566. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26567. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26568. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26569. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26570. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26571. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26572. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26573. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26574. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26575. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26576. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26577. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26578. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26579. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26580. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26581. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26582. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26583. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26584. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26585. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26586. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26587. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26588. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26589. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26590. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26591. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26592. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26593. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26594. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26595. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26596. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26597. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26598. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26599. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26600. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26601. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26602. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26603. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26604. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26605. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26606. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26607. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26608. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26609. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26610. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26611. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26612. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26613. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26614. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26615. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26616. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26617. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26618. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26619. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26620. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26621. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26622. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26623. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26624. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26625. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26626. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26627. 0x00, 0x00, 0x00, 0x00, 0x00
  26628. };
  26629. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26630. * !NO_DES3 && !NO_SHA
  26631. */
  26632. /*
  26633. * Testing wc_PKCS7_BER()
  26634. */
  26635. static int test_wc_PKCS7_BER(void)
  26636. {
  26637. int res = TEST_SKIPPED;
  26638. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26639. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26640. PKCS7* pkcs7;
  26641. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26642. XFILE f;
  26643. byte der[4096];
  26644. #ifndef NO_DES3
  26645. byte decoded[2048];
  26646. #endif
  26647. word32 derSz;
  26648. int ret;
  26649. AssertNotNull(f = XFOPEN(fName, "rb"));
  26650. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26651. derSz = (word32)ret;
  26652. XFCLOSE(f);
  26653. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26654. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26655. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26656. #ifndef NO_RSA
  26657. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26658. #else
  26659. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26660. #endif
  26661. wc_PKCS7_Free(pkcs7);
  26662. #ifndef NO_DES3
  26663. /* decode BER content */
  26664. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  26665. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26666. derSz = (word32)ret;
  26667. XFCLOSE(f);
  26668. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26669. #ifndef NO_RSA
  26670. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26671. #else
  26672. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26673. #endif
  26674. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  26675. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  26676. derSz = (word32)ret;
  26677. XFCLOSE(f);
  26678. pkcs7->privateKey = der;
  26679. pkcs7->privateKeySz = derSz;
  26680. #ifndef NO_RSA
  26681. #ifdef WOLFSSL_SP_MATH
  26682. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26683. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26684. #else
  26685. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26686. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26687. #endif
  26688. #else
  26689. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26690. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26691. #endif
  26692. wc_PKCS7_Free(pkcs7);
  26693. #endif /* !NO_DES3 */
  26694. res = TEST_RES_CHECK(1);
  26695. #endif
  26696. return res;
  26697. } /* END test_wc_PKCS7_BER() */
  26698. static int test_PKCS7_signed_enveloped(void)
  26699. {
  26700. int res = TEST_SKIPPED;
  26701. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26702. !defined(NO_FILESYSTEM)
  26703. XFILE f;
  26704. PKCS7* pkcs7;
  26705. #ifdef HAVE_AES_CBC
  26706. PKCS7* inner;
  26707. #endif
  26708. void* pt;
  26709. WC_RNG rng;
  26710. unsigned char key[FOURK_BUF/2];
  26711. unsigned char cert[FOURK_BUF/2];
  26712. unsigned char env[FOURK_BUF];
  26713. int envSz = FOURK_BUF;
  26714. int keySz;
  26715. int certSz;
  26716. unsigned char sig[FOURK_BUF * 2];
  26717. int sigSz = FOURK_BUF * 2;
  26718. #ifdef HAVE_AES_CBC
  26719. unsigned char decoded[FOURK_BUF];
  26720. int decodedSz = FOURK_BUF;
  26721. #endif
  26722. /* load cert */
  26723. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  26724. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26725. XFCLOSE(f);
  26726. /* load key */
  26727. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  26728. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26729. XFCLOSE(f);
  26730. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  26731. /* sign cert for envelope */
  26732. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26733. AssertIntEQ(wc_InitRng(&rng), 0);
  26734. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26735. pkcs7->content = cert;
  26736. pkcs7->contentSz = certSz;
  26737. pkcs7->contentOID = DATA;
  26738. pkcs7->privateKey = key;
  26739. pkcs7->privateKeySz = keySz;
  26740. pkcs7->encryptOID = RSAk;
  26741. pkcs7->hashOID = SHA256h;
  26742. pkcs7->rng = &rng;
  26743. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26744. wc_PKCS7_Free(pkcs7);
  26745. wc_FreeRng(&rng);
  26746. #ifdef HAVE_AES_CBC
  26747. /* create envelope */
  26748. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26749. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26750. pkcs7->content = sig;
  26751. pkcs7->contentSz = sigSz;
  26752. pkcs7->contentOID = DATA;
  26753. pkcs7->encryptOID = AES256CBCb;
  26754. pkcs7->privateKey = key;
  26755. pkcs7->privateKeySz = keySz;
  26756. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26757. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26758. wc_PKCS7_Free(pkcs7);
  26759. #endif
  26760. /* create bad signed enveloped data */
  26761. sigSz = FOURK_BUF * 2;
  26762. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26763. AssertIntEQ(wc_InitRng(&rng), 0);
  26764. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26765. pkcs7->content = env;
  26766. pkcs7->contentSz = envSz;
  26767. pkcs7->contentOID = DATA;
  26768. pkcs7->privateKey = key;
  26769. pkcs7->privateKeySz = keySz;
  26770. pkcs7->encryptOID = RSAk;
  26771. pkcs7->hashOID = SHA256h;
  26772. pkcs7->rng = &rng;
  26773. /* Set no certs in bundle for this test. Hang on to the pointer though to
  26774. * free it later. */
  26775. pt = (void*)pkcs7->certList;
  26776. pkcs7->certList = NULL; /* no certs in bundle */
  26777. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26778. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  26779. wc_PKCS7_Free(pkcs7);
  26780. /* check verify fails */
  26781. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26782. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26783. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26784. PKCS7_SIGNEEDS_CHECK);
  26785. /* try verifying the signature manually */
  26786. {
  26787. RsaKey rKey;
  26788. word32 idx = 0;
  26789. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26790. WC_MAX_DIGEST_SIZE];
  26791. int digestSz;
  26792. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26793. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26794. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26795. digest, sizeof(digest), &rKey);
  26796. AssertIntGT(digestSz, 0);
  26797. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26798. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26799. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26800. /* verify was success */
  26801. }
  26802. wc_PKCS7_Free(pkcs7);
  26803. /* initializing the PKCS7 struct with the signing certificate should pass */
  26804. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26805. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26806. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26807. wc_PKCS7_Free(pkcs7);
  26808. /* create valid degenerate bundle */
  26809. sigSz = FOURK_BUF * 2;
  26810. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26811. pkcs7->content = env;
  26812. pkcs7->contentSz = envSz;
  26813. pkcs7->contentOID = DATA;
  26814. pkcs7->privateKey = key;
  26815. pkcs7->privateKeySz = keySz;
  26816. pkcs7->encryptOID = RSAk;
  26817. pkcs7->hashOID = SHA256h;
  26818. pkcs7->rng = &rng;
  26819. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26820. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26821. wc_PKCS7_Free(pkcs7);
  26822. wc_FreeRng(&rng);
  26823. /* check verify */
  26824. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26825. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26826. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26827. AssertNotNull(pkcs7->content);
  26828. #ifdef HAVE_AES_CBC
  26829. /* check decode */
  26830. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26831. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26832. inner->privateKey = key;
  26833. inner->privateKeySz = keySz;
  26834. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26835. pkcs7->contentSz, decoded, decodedSz)), 0);
  26836. wc_PKCS7_Free(inner);
  26837. #endif
  26838. wc_PKCS7_Free(pkcs7);
  26839. #ifdef HAVE_AES_CBC
  26840. /* check cert set */
  26841. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26842. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26843. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26844. AssertNotNull(pkcs7->singleCert);
  26845. AssertIntNE(pkcs7->singleCertSz, 0);
  26846. wc_PKCS7_Free(pkcs7);
  26847. #endif
  26848. res = TEST_RES_CHECK(1);
  26849. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26850. return res;
  26851. }
  26852. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26853. {
  26854. int res = TEST_SKIPPED;
  26855. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26856. && !defined(NO_AES)
  26857. PKCS7* pkcs7;
  26858. void* heap = NULL;
  26859. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26860. AssertNotNull(pkcs7);
  26861. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26862. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26863. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26864. wc_PKCS7_Free(pkcs7);
  26865. res = TEST_RES_CHECK(1);
  26866. #endif
  26867. return res;
  26868. }
  26869. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26870. {
  26871. int res = TEST_SKIPPED;
  26872. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26873. && !defined(NO_AES)
  26874. PKCS7* pkcs7;
  26875. void* heap = NULL;
  26876. WOLFSSL_CTX* ctx;
  26877. ctx = NULL;
  26878. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26879. AssertNotNull(pkcs7);
  26880. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26881. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26882. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26883. wc_PKCS7_Free(pkcs7);
  26884. res = TEST_RES_CHECK(1);
  26885. #endif
  26886. return res;
  26887. }
  26888. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26889. {
  26890. int res = TEST_SKIPPED;
  26891. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26892. && !defined(NO_AES)
  26893. PKCS7* pkcs7;
  26894. void* heap = NULL;
  26895. WOLFSSL_CTX* ctx;
  26896. ctx = NULL;
  26897. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26898. AssertNotNull(pkcs7);
  26899. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26900. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26901. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26902. wc_PKCS7_Free(pkcs7);
  26903. res = TEST_RES_CHECK(1);
  26904. #endif
  26905. return res;
  26906. }
  26907. static int test_wc_PKCS7_DecodeCompressedData(void)
  26908. {
  26909. int res = TEST_SKIPPED;
  26910. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26911. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26912. PKCS7* pkcs7;
  26913. void* heap = NULL;
  26914. byte out[4096];
  26915. byte *decompressed;
  26916. int outSz, decompressedSz;
  26917. const char* cert = "./certs/client-cert.pem";
  26918. byte* cert_buf = NULL;
  26919. size_t cert_sz = 0;
  26920. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26921. AssertNotNull((decompressed =
  26922. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26923. decompressedSz = (int)cert_sz;
  26924. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26925. pkcs7->content = (byte*)cert_buf;
  26926. pkcs7->contentSz = (word32)cert_sz;
  26927. pkcs7->contentOID = DATA;
  26928. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26929. sizeof(out))), 0);
  26930. wc_PKCS7_Free(pkcs7);
  26931. /* compressed key should be smaller than when started */
  26932. AssertIntLT(outSz, cert_sz);
  26933. /* test decompression */
  26934. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26935. AssertIntEQ(pkcs7->contentOID, 0);
  26936. /* fail case with out buffer too small */
  26937. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26938. decompressed, outSz), 0);
  26939. /* success case */
  26940. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26941. decompressed, decompressedSz), cert_sz);
  26942. AssertIntEQ(pkcs7->contentOID, DATA);
  26943. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26944. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26945. decompressed = NULL;
  26946. /* test decompression function with different 'max' inputs */
  26947. outSz = sizeof(out);
  26948. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26949. 0);
  26950. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26951. out, outSz, 0, heap), 0);
  26952. AssertNull(decompressed);
  26953. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26954. out, outSz, 0, heap), 0);
  26955. AssertNotNull(decompressed);
  26956. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26957. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26958. decompressed = NULL;
  26959. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26960. out, outSz, 0, heap), 0);
  26961. AssertNotNull(decompressed);
  26962. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26963. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26964. if (cert_buf)
  26965. free(cert_buf);
  26966. wc_PKCS7_Free(pkcs7);
  26967. res = TEST_RES_CHECK(1);
  26968. #endif
  26969. return res;
  26970. }
  26971. static int test_wc_i2d_PKCS12(void)
  26972. {
  26973. int res = TEST_SKIPPED;
  26974. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26975. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26976. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26977. WC_PKCS12* pkcs12 = NULL;
  26978. unsigned char der[FOURK_BUF * 2];
  26979. unsigned char* pt;
  26980. int derSz;
  26981. unsigned char out[FOURK_BUF * 2];
  26982. int outSz = FOURK_BUF * 2;
  26983. const char p12_f[] = "./certs/test-servercert.p12";
  26984. XFILE f;
  26985. f = XFOPEN(p12_f, "rb");
  26986. AssertNotNull(f);
  26987. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26988. AssertIntGT(derSz, 0);
  26989. XFCLOSE(f);
  26990. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26991. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26992. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26993. AssertIntEQ(outSz, derSz);
  26994. outSz = derSz - 1;
  26995. pt = out;
  26996. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26997. outSz = derSz;
  26998. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26999. AssertIntEQ((pt == out), 0);
  27000. pt = NULL;
  27001. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  27002. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  27003. wc_PKCS12_free(pkcs12);
  27004. /* Run the same test but use wc_d2i_PKCS12_fp. */
  27005. AssertNotNull(pkcs12 = wc_PKCS12_new());
  27006. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  27007. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27008. AssertIntEQ(outSz, derSz);
  27009. wc_PKCS12_free(pkcs12);
  27010. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  27011. pkcs12 = NULL;
  27012. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  27013. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  27014. AssertIntEQ(outSz, derSz);
  27015. wc_PKCS12_free(pkcs12);
  27016. res = TEST_RES_CHECK(1);
  27017. #endif
  27018. return res;
  27019. }
  27020. /* Testing wc_SignatureGetSize() for signature type ECC */
  27021. static int test_wc_SignatureGetSize_ecc(void)
  27022. {
  27023. int res = TEST_SKIPPED;
  27024. #ifndef NO_SIG_WRAPPER
  27025. int ret;
  27026. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  27027. enum wc_SignatureType sig_type;
  27028. word32 key_len;
  27029. /* Initialize ECC Key */
  27030. ecc_key ecc;
  27031. const char* qx =
  27032. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  27033. const char* qy =
  27034. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  27035. const char* d =
  27036. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  27037. ret = wc_ecc_init(&ecc);
  27038. if (ret == 0) {
  27039. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  27040. }
  27041. if (ret == 0) {
  27042. /* Input for signature type ECC */
  27043. sig_type = WC_SIGNATURE_TYPE_ECC;
  27044. key_len = sizeof(ecc_key);
  27045. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27046. /* Test bad args */
  27047. if (ret > 0) {
  27048. sig_type = (enum wc_SignatureType) 100;
  27049. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27050. if (ret == BAD_FUNC_ARG) {
  27051. sig_type = WC_SIGNATURE_TYPE_ECC;
  27052. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27053. }
  27054. if (ret >= 0) {
  27055. key_len = (word32) 0;
  27056. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  27057. }
  27058. if (ret == BAD_FUNC_ARG) {
  27059. ret = SIG_TYPE_E;
  27060. }
  27061. }
  27062. }
  27063. else {
  27064. ret = WOLFSSL_FATAL_ERROR;
  27065. }
  27066. wc_ecc_free(&ecc);
  27067. #else
  27068. ret = SIG_TYPE_E;
  27069. #endif
  27070. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27071. #endif /* NO_SIG_WRAPPER */
  27072. return res;
  27073. }/* END test_wc_SignatureGetSize_ecc() */
  27074. /* Testing wc_SignatureGetSize() for signature type rsa */
  27075. static int test_wc_SignatureGetSize_rsa(void)
  27076. {
  27077. int res = TEST_SKIPPED;
  27078. #ifndef NO_SIG_WRAPPER
  27079. int ret = 0;
  27080. #ifndef NO_RSA
  27081. enum wc_SignatureType sig_type;
  27082. word32 key_len;
  27083. word32 idx = 0;
  27084. /* Initialize RSA Key */
  27085. RsaKey rsa_key;
  27086. byte* tmp = NULL;
  27087. size_t bytes;
  27088. #ifdef USE_CERT_BUFFERS_1024
  27089. bytes = (size_t)sizeof_client_key_der_1024;
  27090. if (bytes < (size_t)sizeof_client_key_der_1024)
  27091. bytes = (size_t)sizeof_client_cert_der_1024;
  27092. #elif defined(USE_CERT_BUFFERS_2048)
  27093. bytes = (size_t)sizeof_client_key_der_2048;
  27094. if (bytes < (size_t)sizeof_client_cert_der_2048)
  27095. bytes = (size_t)sizeof_client_cert_der_2048;
  27096. #else
  27097. bytes = FOURK_BUF;
  27098. #endif
  27099. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27100. if (tmp != NULL) {
  27101. #ifdef USE_CERT_BUFFERS_1024
  27102. XMEMCPY(tmp, client_key_der_1024,
  27103. (size_t)sizeof_client_key_der_1024);
  27104. #elif defined(USE_CERT_BUFFERS_2048)
  27105. XMEMCPY(tmp, client_key_der_2048,
  27106. (size_t)sizeof_client_key_der_2048);
  27107. #elif !defined(NO_FILESYSTEM)
  27108. file = XFOPEN(clientKey, "rb");
  27109. if (file != XBADFILE) {
  27110. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  27111. XFCLOSE(file);
  27112. }
  27113. else {
  27114. ret = WOLFSSL_FATAL_ERROR;
  27115. }
  27116. #else
  27117. ret = WOLFSSL_FATAL_ERROR;
  27118. #endif
  27119. }
  27120. else {
  27121. ret = WOLFSSL_FATAL_ERROR;
  27122. }
  27123. if (ret == 0) {
  27124. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  27125. }
  27126. if (ret == 0) {
  27127. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  27128. }
  27129. if (ret == 0) {
  27130. /* Input for signature type RSA */
  27131. sig_type = WC_SIGNATURE_TYPE_RSA;
  27132. key_len = sizeof(RsaKey);
  27133. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27134. /* Test bad args */
  27135. if (ret > 0) {
  27136. sig_type = (enum wc_SignatureType) 100;
  27137. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27138. if (ret == BAD_FUNC_ARG) {
  27139. sig_type = WC_SIGNATURE_TYPE_RSA;
  27140. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  27141. }
  27142. #ifndef HAVE_USER_RSA
  27143. if (ret == BAD_FUNC_ARG)
  27144. #else
  27145. if (ret == 0)
  27146. #endif
  27147. {
  27148. key_len = (word32)0;
  27149. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  27150. }
  27151. if (ret == BAD_FUNC_ARG) {
  27152. ret = SIG_TYPE_E;
  27153. }
  27154. }
  27155. }
  27156. else {
  27157. ret = WOLFSSL_FATAL_ERROR;
  27158. }
  27159. wc_FreeRsaKey(&rsa_key);
  27160. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27161. #else
  27162. ret = SIG_TYPE_E;
  27163. #endif
  27164. res = TEST_RES_CHECK(ret == SIG_TYPE_E);
  27165. #endif /* NO_SIG_WRAPPER */
  27166. return res;
  27167. }/* END test_wc_SignatureGetSize_rsa(void) */
  27168. /*----------------------------------------------------------------------------*
  27169. | hash.h Tests
  27170. *----------------------------------------------------------------------------*/
  27171. static int test_wc_HashInit(void)
  27172. {
  27173. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  27174. wc_HashAlg hash;
  27175. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27176. enum wc_HashType enumArray[] = {
  27177. #ifndef NO_MD5
  27178. WC_HASH_TYPE_MD5,
  27179. #endif
  27180. #ifndef NO_SHA
  27181. WC_HASH_TYPE_SHA,
  27182. #endif
  27183. #ifndef WOLFSSL_SHA224
  27184. WC_HASH_TYPE_SHA224,
  27185. #endif
  27186. #ifndef NO_SHA256
  27187. WC_HASH_TYPE_SHA256,
  27188. #endif
  27189. #ifndef WOLFSSL_SHA384
  27190. WC_HASH_TYPE_SHA384,
  27191. #endif
  27192. #ifndef WOLFSSL_SHA512
  27193. WC_HASH_TYPE_SHA512,
  27194. #endif
  27195. };
  27196. /* dynamically finds the length */
  27197. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27198. /* For loop to test various arguments... */
  27199. for (i = 0; i < enumlen; i++) {
  27200. /* check for bad args */
  27201. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  27202. ret = 1;
  27203. break;
  27204. }
  27205. wc_HashFree(&hash, enumArray[i]);
  27206. /* check for null ptr */
  27207. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  27208. ret = 1;
  27209. break;
  27210. }
  27211. } /* end of for loop */
  27212. return TEST_RES_CHECK(ret == 0);
  27213. } /* end of test_wc_HashInit */
  27214. /*
  27215. * Unit test function for wc_HashSetFlags()
  27216. */
  27217. static int test_wc_HashSetFlags(void)
  27218. {
  27219. int res = TEST_SKIPPED;
  27220. #ifdef WOLFSSL_HASH_FLAGS
  27221. wc_HashAlg hash;
  27222. int ret = 0;
  27223. word32 flags = 0;
  27224. int i, j;
  27225. int notSupportedLen;
  27226. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27227. enum wc_HashType enumArray[] = {
  27228. #ifndef NO_MD5
  27229. WC_HASH_TYPE_MD5,
  27230. #endif
  27231. #ifndef NO_SHA
  27232. WC_HASH_TYPE_SHA,
  27233. #endif
  27234. #ifdef WOLFSSL_SHA224
  27235. WC_HASH_TYPE_SHA224,
  27236. #endif
  27237. #ifndef NO_SHA256
  27238. WC_HASH_TYPE_SHA256,
  27239. #endif
  27240. #ifdef WOLFSSL_SHA384
  27241. WC_HASH_TYPE_SHA384,
  27242. #endif
  27243. #ifdef WOLFSSL_SHA512
  27244. WC_HASH_TYPE_SHA512,
  27245. #endif
  27246. #ifdef WOLFSSL_SHA3
  27247. WC_HASH_TYPE_SHA3_224,
  27248. #endif
  27249. };
  27250. enum wc_HashType notSupported[] = {
  27251. WC_HASH_TYPE_MD5_SHA,
  27252. WC_HASH_TYPE_MD2,
  27253. WC_HASH_TYPE_MD4,
  27254. WC_HASH_TYPE_BLAKE2B,
  27255. WC_HASH_TYPE_BLAKE2S,
  27256. WC_HASH_TYPE_NONE,
  27257. };
  27258. /* dynamically finds the length */
  27259. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27260. /* For loop to test various arguments... */
  27261. for (i = 0; i < enumlen; i++) {
  27262. ret = wc_HashInit(&hash, enumArray[i]);
  27263. if (ret == 0) {
  27264. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  27265. }
  27266. if (ret == 0) {
  27267. if (flags & WC_HASH_FLAG_ISCOPY) {
  27268. ret = 0;
  27269. }
  27270. }
  27271. if (ret == 0) {
  27272. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  27273. if (ret == BAD_FUNC_ARG) {
  27274. ret = 0;
  27275. }
  27276. }
  27277. wc_HashFree(&hash, enumArray[i]);
  27278. }
  27279. /* For loop to test not supported cases */
  27280. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27281. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27282. ret = wc_HashInit(&hash, notSupported[j]);
  27283. if (ret == 0) {
  27284. ret = -1;
  27285. }
  27286. else if (ret == BAD_FUNC_ARG) {
  27287. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  27288. if (ret == 0) {
  27289. ret = -1;
  27290. }
  27291. else if (ret == BAD_FUNC_ARG) {
  27292. ret = 0;
  27293. }
  27294. }
  27295. if (ret == 0) {
  27296. ret = wc_HashFree(&hash, notSupported[j]);
  27297. if (ret == 0) {
  27298. ret = -1;
  27299. }
  27300. else if (ret == BAD_FUNC_ARG) {
  27301. ret = 0;
  27302. }
  27303. }
  27304. }
  27305. res = TEST_RES_CHECK(ret == 0);
  27306. #endif
  27307. return res;
  27308. } /* END test_wc_HashSetFlags */
  27309. /*
  27310. * Unit test function for wc_HashGetFlags()
  27311. */
  27312. static int test_wc_HashGetFlags(void)
  27313. {
  27314. int res = TEST_SKIPPED;
  27315. #ifdef WOLFSSL_HASH_FLAGS
  27316. wc_HashAlg hash;
  27317. int ret = 0;
  27318. word32 flags = 0;
  27319. int i, j;
  27320. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27321. enum wc_HashType enumArray[] = {
  27322. #ifndef NO_MD5
  27323. WC_HASH_TYPE_MD5,
  27324. #endif
  27325. #ifndef NO_SHA
  27326. WC_HASH_TYPE_SHA,
  27327. #endif
  27328. #ifdef WOLFSSL_SHA224
  27329. WC_HASH_TYPE_SHA224,
  27330. #endif
  27331. #ifndef NO_SHA256
  27332. WC_HASH_TYPE_SHA256,
  27333. #endif
  27334. #ifdef WOLFSSL_SHA384
  27335. WC_HASH_TYPE_SHA384,
  27336. #endif
  27337. #ifdef WOLFSSL_SHA512
  27338. WC_HASH_TYPE_SHA512,
  27339. #endif
  27340. #ifdef WOLFSSL_SHA3
  27341. WC_HASH_TYPE_SHA3_224,
  27342. #endif
  27343. };
  27344. enum wc_HashType notSupported[] = {
  27345. WC_HASH_TYPE_MD5_SHA,
  27346. WC_HASH_TYPE_MD2,
  27347. WC_HASH_TYPE_MD4,
  27348. WC_HASH_TYPE_BLAKE2B,
  27349. WC_HASH_TYPE_BLAKE2S,
  27350. WC_HASH_TYPE_NONE,
  27351. };
  27352. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27353. int notSupportedLen;
  27354. /* For loop to test various arguments... */
  27355. for (i = 0; i < enumlen; i++) {
  27356. ret = wc_HashInit(&hash, enumArray[i]);
  27357. if (ret == 0) {
  27358. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  27359. }
  27360. if (ret == 0) {
  27361. if (flags & WC_HASH_FLAG_ISCOPY) {
  27362. ret = 0;
  27363. }
  27364. }
  27365. if (ret == 0) {
  27366. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  27367. if (ret == BAD_FUNC_ARG) {
  27368. ret = 0;
  27369. }
  27370. }
  27371. wc_HashFree(&hash, enumArray[i]);
  27372. if (ret != 0) {
  27373. break;
  27374. }
  27375. }
  27376. /* For loop to test not supported cases */
  27377. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27378. for (j = 0; ret == 0 && j < notSupportedLen; j++) {
  27379. ret = wc_HashInit(&hash, notSupported[j]);
  27380. if (ret == 0) {
  27381. ret = -1;
  27382. }
  27383. else if (ret == BAD_FUNC_ARG) {
  27384. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  27385. if (ret == 0) {
  27386. ret = -1;
  27387. }
  27388. else if (ret == BAD_FUNC_ARG) {
  27389. ret = 0;
  27390. }
  27391. }
  27392. if (ret == 0) {
  27393. ret = wc_HashFree(&hash, notSupported[j]);
  27394. if (ret == 0) {
  27395. ret = -1;
  27396. }
  27397. if (ret == BAD_FUNC_ARG) {
  27398. ret = 0;
  27399. }
  27400. }
  27401. }
  27402. res = TEST_RES_CHECK(ret == 0);
  27403. #endif
  27404. return res;
  27405. } /* END test_wc_HashGetFlags */
  27406. /*----------------------------------------------------------------------------*
  27407. | Compatibility Tests
  27408. *----------------------------------------------------------------------------*/
  27409. /*----------------------------------------------------------------------------*
  27410. | ASN.1 Tests
  27411. *----------------------------------------------------------------------------*/
  27412. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27413. {
  27414. int res = TEST_SKIPPED;
  27415. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27416. ASN1_BIT_STRING* str;
  27417. AssertNotNull(str = ASN1_BIT_STRING_new());
  27418. /* Empty data testing. */
  27419. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27420. ASN1_BIT_STRING_free(str);
  27421. AssertNotNull(str = ASN1_BIT_STRING_new());
  27422. /* Invalid parameter testing. */
  27423. AssertIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27424. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27425. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27426. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27427. /* No bit string - bit is always 0. */
  27428. AssertIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27429. AssertIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27430. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27431. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27432. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27433. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27434. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27435. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27436. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27437. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27438. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27439. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27440. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27441. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27442. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27443. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27444. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27445. ASN1_BIT_STRING_free(str);
  27446. ASN1_BIT_STRING_free(NULL);
  27447. res = TEST_RES_CHECK(1);
  27448. #endif
  27449. return res;
  27450. }
  27451. static int test_wolfSSL_ASN1_INTEGER(void)
  27452. {
  27453. int res = TEST_SKIPPED;
  27454. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27455. ASN1_INTEGER* a;
  27456. ASN1_INTEGER* dup;
  27457. const unsigned char invalidLenDer[] = {
  27458. 0x02, 0x20, 0x00
  27459. };
  27460. const unsigned char longDer[] = {
  27461. 0x02, 0x20,
  27462. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27463. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27464. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27465. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27466. };
  27467. const unsigned char* p;
  27468. /* Invalid parameter testing. */
  27469. ASN1_INTEGER_free(NULL);
  27470. AssertNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27471. AssertNotNull(a = ASN1_INTEGER_new());
  27472. AssertNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27473. ASN1_INTEGER_free(dup);
  27474. ASN1_INTEGER_free(a);
  27475. p = longDer;
  27476. AssertNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27477. p = longDer;
  27478. AssertNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27479. AssertPtrNE(p, longDer);
  27480. AssertNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27481. ASN1_INTEGER_free(dup);
  27482. ASN1_INTEGER_free(a);
  27483. res = TEST_RES_CHECK(1);
  27484. #endif
  27485. return res;
  27486. }
  27487. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27488. {
  27489. int res = TEST_SKIPPED;
  27490. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27491. ASN1_INTEGER* a;
  27492. ASN1_INTEGER* b;
  27493. AssertNotNull(a = ASN1_INTEGER_new());
  27494. AssertNotNull(b = ASN1_INTEGER_new());
  27495. AssertIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27496. AssertIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27497. /* Invalid parameter testing. */
  27498. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27499. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27500. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27501. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27502. AssertIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27503. AssertIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27504. AssertIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27505. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27506. AssertIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27507. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27508. AssertIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27509. AssertIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27510. AssertIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27511. AssertIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27512. ASN1_INTEGER_free(b);
  27513. ASN1_INTEGER_free(a);
  27514. res = TEST_RES_CHECK(1);
  27515. #endif
  27516. return res;
  27517. }
  27518. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27519. {
  27520. int res = TEST_SKIPPED;
  27521. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27522. ASN1_INTEGER* ai;
  27523. ASN1_INTEGER* ai2;
  27524. BIGNUM* bn;
  27525. BIGNUM* bn2;
  27526. ai = ASN1_INTEGER_new();
  27527. AssertNotNull(ai);
  27528. bn2 = BN_new();
  27529. AssertNotNull(bn2);
  27530. /* Invalid parameter testing. */
  27531. AssertNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27532. AssertNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27533. /* at the moment hard setting since no set function */
  27534. ai->data[0] = 0xff; /* No DER encoding. */
  27535. ai->length = 1;
  27536. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27537. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27538. BN_free(bn);
  27539. #else
  27540. AssertNull(ASN1_INTEGER_to_BN(ai, NULL));
  27541. #endif
  27542. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27543. ai->data[1] = 0x04; /* bad length of integer */
  27544. ai->data[2] = 0x03;
  27545. ai->length = 3;
  27546. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27547. /* Interpreted as a number 0x020403. */
  27548. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27549. BN_free(bn);
  27550. #else
  27551. AssertNull(ASN1_INTEGER_to_BN(ai, NULL));
  27552. #endif
  27553. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27554. ai->data[1] = 0x01; /* length of integer */
  27555. ai->data[2] = 0x03;
  27556. ai->length = 3;
  27557. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27558. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27559. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27560. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27561. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27562. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27563. ai->data[1] = 0x02; /* length of integer */
  27564. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27565. ai->data[3] = 0xff;
  27566. ai->length = 4;
  27567. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27568. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27569. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27570. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27571. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27572. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27573. ai->data[1] = 0x01; /* length of integer */
  27574. ai->data[2] = 0x00;
  27575. ai->length = 3;
  27576. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27577. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27578. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27579. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27580. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27581. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27582. ai->data[1] = 0x01; /* length of integer */
  27583. ai->data[2] = 0x01;
  27584. ai->length = 3;
  27585. ai->negative = 1;
  27586. AssertNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27587. AssertNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27588. AssertIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27589. AssertNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27590. AssertIntEQ(BN_cmp(bn, bn2), 0);
  27591. BN_free(bn2);
  27592. BN_free(bn);
  27593. ASN1_INTEGER_free(ai2);
  27594. ASN1_INTEGER_free(ai);
  27595. res = TEST_RES_CHECK(1);
  27596. #endif
  27597. return res;
  27598. }
  27599. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27600. {
  27601. int res = TEST_SKIPPED;
  27602. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27603. ASN1_INTEGER *a;
  27604. long val;
  27605. int ret;
  27606. a = ASN1_INTEGER_new();
  27607. /* Invalid parameter testing. */
  27608. AssertIntEQ(ASN1_INTEGER_get(NULL), 0);
  27609. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27610. AssertIntEQ(ASN1_INTEGER_get(a), 0);
  27611. #else
  27612. AssertIntEQ(ASN1_INTEGER_get(a), -1);
  27613. #endif
  27614. ASN1_INTEGER_free(a);
  27615. a = ASN1_INTEGER_new();
  27616. val = 0;
  27617. ret = ASN1_INTEGER_set(NULL, val);
  27618. AssertIntEQ(ret, 0);
  27619. ASN1_INTEGER_free(a);
  27620. /* 0 */
  27621. a = ASN1_INTEGER_new();
  27622. val = 0;
  27623. ret = ASN1_INTEGER_set(a, val);
  27624. AssertIntEQ(ret, 1);
  27625. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27626. ASN1_INTEGER_free(a);
  27627. /* 40 */
  27628. a = ASN1_INTEGER_new();
  27629. val = 40;
  27630. ret = ASN1_INTEGER_set(a, val);
  27631. AssertIntEQ(ret, 1);
  27632. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27633. ASN1_INTEGER_free(a);
  27634. /* -40 */
  27635. a = ASN1_INTEGER_new();
  27636. val = -40;
  27637. ret = ASN1_INTEGER_set(a, val);
  27638. AssertIntEQ(ret, 1);
  27639. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27640. ASN1_INTEGER_free(a);
  27641. /* 128 */
  27642. a = ASN1_INTEGER_new();
  27643. val = 128;
  27644. ret = ASN1_INTEGER_set(a, val);
  27645. AssertIntEQ(ret, 1);
  27646. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27647. ASN1_INTEGER_free(a);
  27648. /* -128 */
  27649. a = ASN1_INTEGER_new();
  27650. val = -128;
  27651. ret = ASN1_INTEGER_set(a, val);
  27652. AssertIntEQ(ret, 1);
  27653. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27654. ASN1_INTEGER_free(a);
  27655. /* 200 */
  27656. a = ASN1_INTEGER_new();
  27657. val = 200;
  27658. ret = ASN1_INTEGER_set(a, val);
  27659. AssertIntEQ(ret, 1);
  27660. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27661. ASN1_INTEGER_free(a);
  27662. /* int max (2147483647) */
  27663. a = ASN1_INTEGER_new();
  27664. val = 2147483647;
  27665. ret = ASN1_INTEGER_set(a, val);
  27666. AssertIntEQ(ret, 1);
  27667. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27668. ASN1_INTEGER_free(a);
  27669. /* int min (-2147483648) */
  27670. a = ASN1_INTEGER_new();
  27671. val = -2147483647 - 1;
  27672. ret = ASN1_INTEGER_set(a, val);
  27673. AssertIntEQ(ret, 1);
  27674. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27675. ASN1_INTEGER_free(a);
  27676. /* long max positive */
  27677. a = ASN1_INTEGER_new();
  27678. val = (long)(((unsigned long)-1) >> 1);
  27679. ret = ASN1_INTEGER_set(a, val);
  27680. AssertIntEQ(ret, 1);
  27681. AssertIntEQ(ASN1_INTEGER_get(a), val);
  27682. ASN1_INTEGER_free(a);
  27683. res = TEST_RES_CHECK(1);
  27684. #endif
  27685. return res;
  27686. }
  27687. #if defined(OPENSSL_EXTRA)
  27688. typedef struct ASN1IntTestVector {
  27689. const byte* der;
  27690. const size_t derSz;
  27691. const long value;
  27692. } ASN1IntTestVector;
  27693. #endif
  27694. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27695. {
  27696. int res = TEST_SKIPPED;
  27697. #if defined(OPENSSL_EXTRA)
  27698. size_t i;
  27699. WOLFSSL_ASN1_INTEGER* a = NULL;
  27700. WOLFSSL_ASN1_INTEGER* b = NULL;
  27701. WOLFSSL_ASN1_INTEGER* c = NULL;
  27702. const byte* p = NULL;
  27703. byte* reEncoded = NULL;
  27704. int reEncodedSz;
  27705. static const byte zeroDer[] = {
  27706. 0x02, 0x01, 0x00
  27707. };
  27708. static const byte oneDer[] = {
  27709. 0x02, 0x01, 0x01
  27710. };
  27711. static const byte negativeDer[] = {
  27712. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27713. };
  27714. static const byte positiveDer[] = {
  27715. 0x02, 0x03, 0x01, 0x00, 0x01
  27716. };
  27717. static const byte primeDer[] = {
  27718. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27719. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27720. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27721. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27722. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27723. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27724. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27725. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27726. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27727. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27728. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27729. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27730. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27731. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27732. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27733. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27734. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27735. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27736. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27737. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27738. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27739. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27740. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27741. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27742. };
  27743. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27744. static const ASN1IntTestVector testVectors[] = {
  27745. {zeroDer, sizeof(zeroDer), 0},
  27746. {oneDer, sizeof(oneDer), 1},
  27747. {negativeDer, sizeof(negativeDer), -4123123},
  27748. {positiveDer, sizeof(positiveDer), 65537},
  27749. {primeDer, sizeof(primeDer), 0}
  27750. };
  27751. static const size_t NUM_TEST_VECTORS =
  27752. sizeof(testVectors)/sizeof(testVectors[0]);
  27753. /* Check d2i error conditions */
  27754. /* NULL pointer to input. */
  27755. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27756. AssertNull(b);
  27757. /* NULL input. */
  27758. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27759. AssertNull(b);
  27760. /* 0 length. */
  27761. p = testVectors[0].der;
  27762. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27763. AssertNull(b);
  27764. /* Negative length. */
  27765. p = testVectors[0].der;
  27766. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27767. AssertNull(b);
  27768. /* Garbage DER input. */
  27769. p = garbageDer;
  27770. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27771. AssertNull(b);
  27772. {
  27773. /* Check i2d error conditions */
  27774. /* NULL input. */
  27775. byte* p2 = NULL;
  27776. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27777. /* 0 length input data buffer (a->length == 0). */
  27778. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27779. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27780. a->data = NULL;
  27781. /* NULL input data buffer. */
  27782. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27783. /* Reset a->data. */
  27784. a->data = a->intData;
  27785. /* Set a to valid value. */
  27786. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27787. /* NULL output buffer. */
  27788. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27789. wolfSSL_ASN1_INTEGER_free(a);
  27790. }
  27791. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27792. p = testVectors[i].der;
  27793. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  27794. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27795. if (testVectors[i].derSz <= sizeof(long)) {
  27796. c = wolfSSL_ASN1_INTEGER_new();
  27797. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  27798. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27799. wolfSSL_ASN1_INTEGER_free(c);
  27800. }
  27801. /* Convert to DER without a pre-allocated output buffer. */
  27802. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27803. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  27804. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27805. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27806. * output buffer pointer should be incremented just past the end of the
  27807. * encoded data. */
  27808. p = reEncoded;
  27809. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27810. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  27811. AssertPtrEq(p, reEncoded - reEncodedSz);
  27812. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  27813. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  27814. reEncoded = NULL;
  27815. wolfSSL_ASN1_INTEGER_free(a);
  27816. }
  27817. res = TEST_RES_CHECK(1);
  27818. #endif /* OPENSSL_EXTRA */
  27819. return res;
  27820. }
  27821. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27822. {
  27823. int res = TEST_SKIPPED;
  27824. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27825. BIO* bio;
  27826. BIO* out;
  27827. BIO* fixed;
  27828. ASN1_INTEGER* ai;
  27829. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27830. char tmp[1024];
  27831. int tmpSz;
  27832. const char expected1[] = "123456";
  27833. const char expected2[] = "112345678912345678901234567890";
  27834. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27835. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27836. AssertNotNull(out = BIO_new(BIO_s_mem()));
  27837. AssertNotNull(ai = ASN1_INTEGER_new());
  27838. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  27839. /* Invalid parameter testing. */
  27840. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27841. AssertIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27842. AssertIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27843. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27844. AssertIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27845. AssertIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27846. AssertIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27847. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27848. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27849. AssertIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27850. AssertIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27851. AssertIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27852. /* No data to read from BIO. */
  27853. AssertIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27854. /* read first line */
  27855. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27856. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27857. XMEMSET(tmp, 0, 1024);
  27858. tmpSz = BIO_read(out, tmp, 1024);
  27859. AssertIntEQ(tmpSz, 6);
  27860. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27861. /* fail on second line (not % 2) */
  27862. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27863. /* read 3rd long line */
  27864. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27865. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27866. XMEMSET(tmp, 0, 1024);
  27867. tmpSz = BIO_read(out, tmp, 1024);
  27868. AssertIntEQ(tmpSz, 30);
  27869. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27870. /* fail on empty line */
  27871. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27872. BIO_free(bio);
  27873. /* Make long integer, requiring dynamic memory, even longer. */
  27874. AssertNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27875. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27876. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27877. XMEMSET(tmp, 0, 1024);
  27878. tmpSz = BIO_read(out, tmp, 1024);
  27879. AssertIntEQ(tmpSz, 48);
  27880. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27881. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27882. XMEMSET(tmp, 0, 1024);
  27883. tmpSz = BIO_read(out, tmp, 1024);
  27884. AssertIntEQ(tmpSz, 56);
  27885. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27886. BIO_free(bio);
  27887. BIO_free(out);
  27888. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27889. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27890. /* Ensure there is 0 bytes avaialble to write into. */
  27891. AssertIntEQ(BIO_write(fixed, tmp, 1), 1);
  27892. AssertIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27893. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27894. AssertIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27895. BIO_free(fixed);
  27896. ASN1_INTEGER_free(ai);
  27897. res = TEST_RES_CHECK(1);
  27898. #endif
  27899. return res;
  27900. }
  27901. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27902. {
  27903. int res = TEST_SKIPPED;
  27904. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27905. ASN1_INTEGER *a;
  27906. unsigned char *pp,*tpp;
  27907. int ret;
  27908. a = wolfSSL_ASN1_INTEGER_new();
  27909. /* Invalid parameter testing. */
  27910. /* Set pp to an invalid value. */
  27911. pp = NULL;
  27912. AssertIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27913. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27914. AssertIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27915. /* 40 */
  27916. a->intData[0] = ASN_INTEGER;
  27917. a->intData[1] = 1;
  27918. a->intData[2] = 40;
  27919. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27920. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27921. DYNAMIC_TYPE_TMP_BUFFER));
  27922. tpp = pp;
  27923. XMEMSET(pp, 0, ret + 1);
  27924. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27925. pp--;
  27926. AssertIntEQ(*pp, 40);
  27927. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27928. /* 128 */
  27929. a->intData[0] = ASN_INTEGER;
  27930. a->intData[1] = 1;
  27931. a->intData[2] = 128;
  27932. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27933. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27934. DYNAMIC_TYPE_TMP_BUFFER));
  27935. tpp = pp;
  27936. XMEMSET(pp, 0, ret + 1);
  27937. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  27938. pp--;
  27939. AssertIntEQ(*(pp--), 128);
  27940. AssertIntEQ(*pp, 0);
  27941. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27942. /* -40 */
  27943. a->intData[0] = ASN_INTEGER;
  27944. a->intData[1] = 1;
  27945. a->intData[2] = 40;
  27946. a->negative = 1;
  27947. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27948. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27949. DYNAMIC_TYPE_TMP_BUFFER));
  27950. tpp = pp;
  27951. XMEMSET(pp, 0, ret + 1);
  27952. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27953. pp--;
  27954. AssertIntEQ(*pp, 216);
  27955. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27956. /* -128 */
  27957. a->intData[0] = ASN_INTEGER;
  27958. a->intData[1] = 1;
  27959. a->intData[2] = 128;
  27960. a->negative = 1;
  27961. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27962. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27963. DYNAMIC_TYPE_TMP_BUFFER));
  27964. tpp = pp;
  27965. XMEMSET(pp, 0, ret + 1);
  27966. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27967. pp--;
  27968. AssertIntEQ(*pp, 128);
  27969. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27970. /* -200 */
  27971. a->intData[0] = ASN_INTEGER;
  27972. a->intData[1] = 1;
  27973. a->intData[2] = 200;
  27974. a->negative = 1;
  27975. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27976. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27977. DYNAMIC_TYPE_TMP_BUFFER));
  27978. tpp = pp;
  27979. XMEMSET(pp, 0, ret + 1);
  27980. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  27981. pp--;
  27982. AssertIntEQ(*(pp--), 56);
  27983. AssertIntEQ(*pp, 255);
  27984. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27985. /* Empty */
  27986. a->intData[0] = ASN_INTEGER;
  27987. a->intData[1] = 0;
  27988. a->negative = 0;
  27989. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27990. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27991. DYNAMIC_TYPE_TMP_BUFFER));
  27992. tpp = pp;
  27993. XMEMSET(pp, 0, ret + 1);
  27994. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  27995. pp--;
  27996. AssertIntEQ(*pp, 0);
  27997. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27998. /* 0 */
  27999. a->intData[0] = ASN_INTEGER;
  28000. a->intData[1] = 1;
  28001. a->intData[2] = 0;
  28002. a->negative = 1;
  28003. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  28004. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28005. DYNAMIC_TYPE_TMP_BUFFER));
  28006. tpp = pp;
  28007. XMEMSET(pp, 0, ret + 1);
  28008. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 1);
  28009. pp--;
  28010. AssertIntEQ(*pp, 0);
  28011. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28012. /* 0x100 */
  28013. a->intData[0] = ASN_INTEGER;
  28014. a->intData[1] = 2;
  28015. a->intData[2] = 0x01;
  28016. a->intData[3] = 0x00;
  28017. a->negative = 0;
  28018. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28019. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28020. DYNAMIC_TYPE_TMP_BUFFER));
  28021. tpp = pp;
  28022. XMEMSET(pp, 0, ret + 1);
  28023. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  28024. pp -= 2;
  28025. AssertIntEQ(pp[0], 0x01);
  28026. AssertIntEQ(pp[1], 0x00);
  28027. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28028. /* -0x8000 => 0x8000 */
  28029. a->intData[0] = ASN_INTEGER;
  28030. a->intData[1] = 2;
  28031. a->intData[2] = 0x80;
  28032. a->intData[3] = 0x00;
  28033. a->negative = 1;
  28034. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  28035. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28036. DYNAMIC_TYPE_TMP_BUFFER));
  28037. tpp = pp;
  28038. XMEMSET(pp, 0, ret + 1);
  28039. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 2);
  28040. pp -= 2;
  28041. AssertIntEQ(pp[0], 0x80);
  28042. AssertIntEQ(pp[1], 0x00);
  28043. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28044. /* -0x8001 => 0xFF7FFF */
  28045. a->intData[0] = ASN_INTEGER;
  28046. a->intData[1] = 2;
  28047. a->intData[2] = 0x80;
  28048. a->intData[3] = 0x01;
  28049. a->negative = 1;
  28050. AssertIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  28051. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  28052. DYNAMIC_TYPE_TMP_BUFFER));
  28053. tpp = pp;
  28054. XMEMSET(pp, 0, ret + 1);
  28055. AssertIntEQ(i2c_ASN1_INTEGER(a, &pp), 3);
  28056. pp -= 3;
  28057. AssertIntEQ(pp[0], 0xFF);
  28058. AssertIntEQ(pp[1], 0x7F);
  28059. AssertIntEQ(pp[2], 0xFF);
  28060. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28061. wolfSSL_ASN1_INTEGER_free(a);
  28062. res = TEST_RES_CHECK(1);
  28063. #endif /* OPENSSL_EXTRA && !NO_ASN */
  28064. return res;
  28065. }
  28066. static int test_wolfSSL_ASN1_OBJECT(void)
  28067. {
  28068. int res = TEST_SKIPPED;
  28069. #if defined(OPENSSL_EXTRA)
  28070. ASN1_OBJECT* a;
  28071. ASN1_OBJECT s;
  28072. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  28073. /* Invalid parameter testing. */
  28074. ASN1_OBJECT_free(NULL);
  28075. AssertNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  28076. /* Test that a static ASN1_OBJECT can be freed. */
  28077. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28078. ASN1_OBJECT_free(&s);
  28079. AssertNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28080. ASN1_OBJECT_free(a);
  28081. s.obj = der;
  28082. s.objSz = sizeof(der);
  28083. AssertNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  28084. ASN1_OBJECT_free(a);
  28085. ASN1_OBJECT_free(&s);
  28086. res = TEST_RES_CHECK(1);
  28087. #endif /* OPENSSL_EXTRA */
  28088. return res;
  28089. }
  28090. static int test_wolfSSL_ASN1_get_object(void)
  28091. {
  28092. int res = TEST_SKIPPED;
  28093. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28094. const unsigned char* derBuf = cliecc_cert_der_256;
  28095. const unsigned char* nullPtr = NULL;
  28096. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  28097. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  28098. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  28099. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  28100. const unsigned char* p;
  28101. unsigned char objDer[8];
  28102. unsigned char* der;
  28103. unsigned char* derPtr;
  28104. int len = sizeof_cliecc_cert_der_256;
  28105. long asnLen = 0;
  28106. int tag = 0;
  28107. int cls = 0;
  28108. ASN1_OBJECT* a;
  28109. ASN1_OBJECT s;
  28110. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  28111. /* Invalid encoding at length. */
  28112. p = objDerInvalidLen;
  28113. AssertIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28114. 0x80);
  28115. p = objDerBadLen;
  28116. /* Error = 0x80, Constructed = 0x20 */
  28117. AssertIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  28118. 0x80 | 0x20);
  28119. /* Read a couple TLV triplets and make sure they match the expected values
  28120. */
  28121. /* SEQUENCE */
  28122. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  28123. AssertIntEQ(asnLen, 862);
  28124. AssertIntEQ(tag, 0x10);
  28125. AssertIntEQ(cls, 0);
  28126. /* SEQUENCE */
  28127. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28128. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28129. AssertIntEQ(asnLen, 772);
  28130. AssertIntEQ(tag, 0x10);
  28131. AssertIntEQ(cls, 0);
  28132. /* [0] */
  28133. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28134. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28135. AssertIntEQ(asnLen, 3);
  28136. AssertIntEQ(tag, 0);
  28137. AssertIntEQ(cls, 0x80);
  28138. /* INTEGER */
  28139. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28140. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28141. AssertIntEQ(asnLen, 1);
  28142. AssertIntEQ(tag, 0x2);
  28143. AssertIntEQ(cls, 0);
  28144. derBuf += asnLen;
  28145. /* INTEGER */
  28146. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28147. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28148. AssertIntEQ(asnLen, 20);
  28149. AssertIntEQ(tag, 0x2);
  28150. AssertIntEQ(cls, 0);
  28151. derBuf += asnLen;
  28152. /* SEQUENCE */
  28153. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28154. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28155. AssertIntEQ(asnLen, 10);
  28156. AssertIntEQ(tag, 0x10);
  28157. AssertIntEQ(cls, 0);
  28158. /* Found OBJECT_ID. */
  28159. /* Invalid parameter testing. */
  28160. AssertIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  28161. AssertIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  28162. AssertIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  28163. AssertIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  28164. AssertIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  28165. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  28166. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  28167. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  28168. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  28169. AssertNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  28170. AssertNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28171. AssertNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28172. AssertNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  28173. AssertNull(d2i_ASN1_OBJECT(&a, NULL, len));
  28174. AssertNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  28175. AssertNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  28176. AssertNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  28177. AssertNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28178. AssertNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28179. AssertNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  28180. AssertNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  28181. /* Invalid encoding at length. */
  28182. p = objDerInvalidLen;
  28183. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  28184. p = objDerBadLen;
  28185. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  28186. p = objDerNotObj;
  28187. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  28188. p = objDerNoData;
  28189. AssertNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  28190. /* Create an ASN OBJECT from content */
  28191. p = derBuf + 2;
  28192. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  28193. ASN1_OBJECT_free(a);
  28194. /* Create an ASN OBJECT from DER */
  28195. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  28196. /* Invalid parameter testing. */
  28197. AssertIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  28198. AssertIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  28199. AssertIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  28200. der = NULL;
  28201. AssertIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  28202. derPtr = objDer;
  28203. AssertIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  28204. AssertPtrNE(derPtr, objDer);
  28205. AssertIntEQ(XMEMCMP(der, objDer, 8), 0);
  28206. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28207. ASN1_OBJECT_free(a);
  28208. res = TEST_RES_CHECK(1);
  28209. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28210. return res;
  28211. }
  28212. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  28213. {
  28214. int res = TEST_SKIPPED;
  28215. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28216. ASN1_OBJECT* obj = NULL;
  28217. ASN1_OBJECT* a = NULL;
  28218. BIO *bio = NULL;
  28219. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  28220. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  28221. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  28222. const unsigned char* p;
  28223. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  28224. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  28225. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  28226. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  28227. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  28228. /* No DER encoding in ASN1_OBJECT. */
  28229. a = wolfSSL_ASN1_OBJECT_new();
  28230. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28231. ASN1_OBJECT_free(a);
  28232. /* DER encoding - not OBJECT_ID */
  28233. p = notObjDer;
  28234. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28235. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28236. ASN1_OBJECT_free(a);
  28237. /* Bad length encoding. */
  28238. p = badLenDer;
  28239. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28240. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28241. ASN1_OBJECT_free(a);
  28242. /* Good encoding - but unknown. */
  28243. p = goodDer;
  28244. AssertNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28245. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28246. ASN1_OBJECT_free(a);
  28247. BIO_free(bio);
  28248. ASN1_OBJECT_free(obj);
  28249. res = TEST_RES_CHECK(1);
  28250. #endif
  28251. return res;
  28252. }
  28253. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28254. {
  28255. int res = TEST_SKIPPED;
  28256. #if defined(OPENSSL_EXTRA) && \
  28257. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28258. char buf[50] = {0};
  28259. ASN1_OBJECT* obj;
  28260. const char* oid = "2.5.29.19";
  28261. const char* ln = "X509v3 Basic Constraints";
  28262. obj = NULL;
  28263. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28264. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28265. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28266. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  28267. XMEMSET(buf, 0, sizeof(buf));
  28268. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28269. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28270. ASN1_OBJECT_free(obj);
  28271. res = TEST_RES_CHECK(1);
  28272. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28273. return res;
  28274. }
  28275. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28276. {
  28277. int res = TEST_SKIPPED;
  28278. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28279. WOLFSSL_STACK* sk;
  28280. WOLFSSL_ASN1_OBJECT* obj;
  28281. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28282. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28283. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28284. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28285. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28286. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28287. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28288. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28289. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28290. /* obj freed in pop_free call. */
  28291. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28292. AssertNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28293. AssertIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28294. AssertPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28295. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28296. wolfSSL_ASN1_OBJECT_free(obj);
  28297. res = TEST_RES_CHECK(1);
  28298. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28299. return res;
  28300. }
  28301. static int test_wolfSSL_ASN1_STRING(void)
  28302. {
  28303. int res = TEST_SKIPPED;
  28304. #if defined(OPENSSL_EXTRA)
  28305. ASN1_STRING* str = NULL;
  28306. ASN1_STRING* c = NULL;
  28307. const char data[] = "hello wolfSSL";
  28308. const char data2[] = "Same len data";
  28309. const char longData[] =
  28310. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28311. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28312. ASN1_STRING_free(str);
  28313. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28314. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28315. AssertIntEQ(ASN1_STRING_type(NULL), 0);
  28316. /* Check setting to NULL works. */
  28317. AssertIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28318. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28319. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28320. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28321. AssertIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28322. AssertIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28323. AssertIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28324. AssertIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28325. AssertNull(wolfSSL_ASN1_STRING_dup(NULL));
  28326. AssertNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28327. AssertIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28328. AssertIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28329. AssertIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28330. AssertIntEQ(ASN1_STRING_cmp(str, c), 0);
  28331. AssertIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28332. AssertIntGT(ASN1_STRING_cmp(str, c), 0);
  28333. AssertIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28334. AssertIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28335. AssertIntEQ(ASN1_STRING_cmp(str, c), 0);
  28336. /* Check setting back to smaller size frees dynamic data. */
  28337. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28338. AssertIntLT(ASN1_STRING_cmp(str, c), 0);
  28339. AssertIntGT(ASN1_STRING_cmp(c, str), 0);
  28340. AssertNull(ASN1_STRING_get0_data(NULL));
  28341. AssertNotNull(ASN1_STRING_get0_data(str));
  28342. AssertNull(ASN1_STRING_data(NULL));
  28343. AssertNotNull(ASN1_STRING_data(str));
  28344. AssertIntEQ(ASN1_STRING_length(NULL), 0);
  28345. AssertIntGT(ASN1_STRING_length(str), 0);
  28346. ASN1_STRING_free(c);
  28347. ASN1_STRING_free(str);
  28348. ASN1_STRING_free(NULL);
  28349. #ifndef NO_WOLFSSL_STUB
  28350. AssertNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28351. #endif
  28352. res = TEST_RES_CHECK(1);
  28353. #endif
  28354. return res;
  28355. }
  28356. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28357. {
  28358. int res = TEST_SKIPPED;
  28359. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA)
  28360. WOLFSSL_X509* x509;
  28361. WOLFSSL_X509_NAME* subject;
  28362. WOLFSSL_X509_NAME_ENTRY* e;
  28363. WOLFSSL_ASN1_STRING* a;
  28364. FILE* file;
  28365. int idx = 0;
  28366. char targetOutput[16] = "www.wolfssl.com";
  28367. unsigned char* actual_output;
  28368. int len = 0;
  28369. int result = 0;
  28370. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28371. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28372. fclose(file);
  28373. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28374. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28375. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28376. NID_commonName, -1)), 5);
  28377. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28378. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28379. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28380. result = strncmp((const char*)actual_output, targetOutput, len);
  28381. AssertIntEQ(result, 0);
  28382. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28383. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28384. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28385. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28386. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28387. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28388. wolfSSL_X509_free(x509);
  28389. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28390. AssertNotNull(a = ASN1_STRING_new());
  28391. AssertIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28392. ASN1_STRING_free(a);
  28393. res = TEST_RES_CHECK(1);
  28394. #endif
  28395. return res;
  28396. }
  28397. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28398. {
  28399. int res = TEST_SKIPPED;
  28400. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28401. WOLFSSL_ASN1_STRING* str;
  28402. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28403. char* ret;
  28404. AssertNotNull(str = ASN1_STRING_new());
  28405. AssertNull(wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28406. /* No data. */
  28407. AssertNull(wolfSSL_i2s_ASN1_STRING(NULL, str));
  28408. AssertIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28409. AssertNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28410. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28411. AssertIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28412. /* No type. */
  28413. AssertNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28414. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28415. ASN1_STRING_free(str);
  28416. res = TEST_RES_CHECK(1);
  28417. #endif
  28418. return res;
  28419. }
  28420. static int test_wolfSSL_ASN1_STRING_canon(void)
  28421. {
  28422. int res = TEST_SKIPPED;
  28423. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28424. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28425. defined(OPENSSL_EXTRA_X509_SMALL))
  28426. WOLFSSL_ASN1_STRING* orig;
  28427. WOLFSSL_ASN1_STRING* canon;
  28428. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28429. const char* whitespaceOnly = "\t\r\n";
  28430. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28431. const char* canonData = "\x01 \x02 \xff tt";
  28432. const char longData[] =
  28433. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28434. AssertNotNull(orig = ASN1_STRING_new());
  28435. AssertNotNull(canon = ASN1_STRING_new());
  28436. /* Invalid parameter testing. */
  28437. AssertIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28438. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28439. AssertIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28440. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28441. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28442. AssertIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28443. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28444. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28445. AssertIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28446. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28447. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28448. ASN1_STRING_free(orig);
  28449. AssertNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28450. AssertIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28451. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28452. AssertIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28453. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28454. ASN1_STRING_free(orig);
  28455. AssertNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28456. AssertIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28457. AssertIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28458. ASN1_STRING_free(orig);
  28459. AssertNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28460. AssertIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28461. ASN1_STRING_free(orig);
  28462. ASN1_STRING_free(canon);
  28463. res = TEST_RES_CHECK(1);
  28464. #endif
  28465. #endif
  28466. return res;
  28467. }
  28468. static int test_wolfSSL_ASN1_STRING_print(void)
  28469. {
  28470. int res = TEST_SKIPPED;
  28471. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28472. !defined(NO_BIO)
  28473. ASN1_STRING* asnStr = NULL;
  28474. const char HELLO_DATA[]= \
  28475. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28476. #define MAX_UNPRINTABLE_CHAR 32
  28477. #define MAX_BUF 255
  28478. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28479. unsigned char expected[sizeof(unprintableData)+1];
  28480. unsigned char rbuf[MAX_BUF];
  28481. BIO *bio;
  28482. int p_len, i;
  28483. /* setup */
  28484. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28485. unprintableData[i] = HELLO_DATA[i];
  28486. expected[i] = HELLO_DATA[i];
  28487. }
  28488. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28489. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28490. if (i == (int)'\n' || i == (int)'\r')
  28491. expected[sizeof(HELLO_DATA)+i] = i;
  28492. else
  28493. expected[sizeof(HELLO_DATA)+i] = '.';
  28494. }
  28495. unprintableData[sizeof(unprintableData)-1] = '\0';
  28496. expected[sizeof(expected)-1] = '\0';
  28497. XMEMSET(rbuf, 0, MAX_BUF);
  28498. bio = BIO_new(BIO_s_mem());
  28499. BIO_set_write_buf_size(bio, MAX_BUF);
  28500. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28501. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28502. (int)sizeof(unprintableData));
  28503. /* test */
  28504. AssertIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28505. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28506. AssertIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28507. AssertIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28508. BIO_read(bio, (void*)rbuf, 46);
  28509. AssertStrEQ((char*)rbuf, (const char*)expected);
  28510. BIO_free(bio);
  28511. AssertNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28512. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28513. /* Ensure there is 0 bytes avaialble to write into. */
  28514. AssertIntEQ(BIO_write(bio, rbuf, 1), 1);
  28515. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28516. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28517. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28518. AssertIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28519. AssertIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28520. BIO_free(bio);
  28521. ASN1_STRING_free(asnStr);
  28522. res = TEST_RES_CHECK(1);
  28523. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28524. return res;
  28525. }
  28526. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28527. {
  28528. int res = TEST_SKIPPED;
  28529. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28530. ASN1_STRING* asn_str;
  28531. const char data[] = "Hello wolfSSL!";
  28532. ASN1_STRING* esc_str;
  28533. const char esc_data[] = "a+;<>";
  28534. ASN1_STRING* neg_int;
  28535. const char neg_int_data[] = "\xff";
  28536. ASN1_STRING* neg_enum;
  28537. const char neg_enum_data[] = "\xff";
  28538. BIO *bio;
  28539. BIO *fixed;
  28540. unsigned long flags;
  28541. int p_len;
  28542. unsigned char rbuf[255];
  28543. /* setup */
  28544. XMEMSET(rbuf, 0, 255);
  28545. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28546. BIO_set_write_buf_size(bio, 255);
  28547. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28548. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28549. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  28550. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  28551. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  28552. neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER);
  28553. ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28554. sizeof(neg_int_data) - 1);
  28555. neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED);
  28556. ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28557. sizeof(neg_enum_data) - 1);
  28558. /* Invalid parameter testing. */
  28559. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28560. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28561. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28562. /* no flags */
  28563. XMEMSET(rbuf, 0, 255);
  28564. flags = 0;
  28565. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28566. AssertIntEQ(p_len, 15);
  28567. BIO_read(bio, (void*)rbuf, 15);
  28568. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  28569. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28570. /* Ensure there is 0 bytes avaialble to write into. */
  28571. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28572. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28573. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28574. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28575. AssertIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28576. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28577. /* RFC2253 Escape */
  28578. XMEMSET(rbuf, 0, 255);
  28579. flags = ASN1_STRFLGS_ESC_2253;
  28580. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  28581. AssertIntEQ(p_len, 9);
  28582. BIO_read(bio, (void*)rbuf, 9);
  28583. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28584. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28585. /* Ensure there is 0 bytes avaialble to write into. */
  28586. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28587. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28588. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28589. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28590. AssertIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28591. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28592. /* Show type */
  28593. XMEMSET(rbuf, 0, 255);
  28594. flags = ASN1_STRFLGS_SHOW_TYPE;
  28595. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28596. AssertIntEQ(p_len, 28);
  28597. BIO_read(bio, (void*)rbuf, 28);
  28598. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28599. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28600. /* Ensure there is 0 bytes avaialble to write into. */
  28601. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28602. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28603. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28604. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28605. AssertIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28606. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28607. AssertIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28608. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28609. /* Dump All */
  28610. XMEMSET(rbuf, 0, 255);
  28611. flags = ASN1_STRFLGS_DUMP_ALL;
  28612. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28613. AssertIntEQ(p_len, 31);
  28614. BIO_read(bio, (void*)rbuf, 31);
  28615. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28616. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28617. /* Ensure there is 0 bytes avaialble to write into. */
  28618. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28619. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28620. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28621. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28622. AssertIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28623. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28624. /* Dump Der */
  28625. XMEMSET(rbuf, 0, 255);
  28626. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28627. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28628. AssertIntEQ(p_len, 35);
  28629. BIO_read(bio, (void*)rbuf, 35);
  28630. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28631. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28632. /* Ensure there is 0 bytes avaialble to write into. */
  28633. AssertIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28634. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28635. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28636. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28637. AssertIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28638. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28639. AssertIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28640. AssertIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28641. /* Dump All + Show type */
  28642. XMEMSET(rbuf, 0, 255);
  28643. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28644. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  28645. AssertIntEQ(p_len, 44);
  28646. BIO_read(bio, (void*)rbuf, 44);
  28647. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28648. /* Dump All + Show type - Negative Integer. */
  28649. XMEMSET(rbuf, 0, 255);
  28650. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28651. p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags);
  28652. AssertIntEQ(p_len, 11);
  28653. BIO_read(bio, (void*)rbuf, 11);
  28654. AssertStrEQ((char*)rbuf, "INTEGER:#FF");
  28655. /* Dump All + Show type - Negative Enumerated. */
  28656. XMEMSET(rbuf, 0, 255);
  28657. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28658. p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags);
  28659. AssertIntEQ(p_len, 14);
  28660. BIO_read(bio, (void*)rbuf, 14);
  28661. AssertStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28662. BIO_free(fixed);
  28663. BIO_free(bio);
  28664. ASN1_STRING_free(asn_str);
  28665. ASN1_STRING_free(esc_str);
  28666. ASN1_STRING_free(neg_int);
  28667. ASN1_STRING_free(neg_enum);
  28668. AssertStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28669. AssertStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28670. res = TEST_RES_CHECK(1);
  28671. #endif
  28672. return res;
  28673. }
  28674. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28675. {
  28676. int res = TEST_SKIPPED;
  28677. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28678. ASN1_STRING* asn1str_test;
  28679. ASN1_STRING* asn1str_answer;
  28680. /* Each character is encoded using 4 bytes */
  28681. char input[] = {
  28682. 0, 0, 0, 'T',
  28683. 0, 0, 0, 'e',
  28684. 0, 0, 0, 's',
  28685. 0, 0, 0, 't',
  28686. };
  28687. char output[] = "Test";
  28688. char badInput[] = {
  28689. 1, 0, 0, 'T',
  28690. 0, 1, 0, 'e',
  28691. 0, 0, 1, 's',
  28692. };
  28693. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28694. /* Test wrong type. */
  28695. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28696. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28697. ASN1_STRING_free(asn1str_test);
  28698. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28699. /* Test bad length. */
  28700. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28701. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28702. /* Test bad input. */
  28703. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28704. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28705. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28706. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28707. AssertIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28708. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28709. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28710. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28711. AssertNotNull(
  28712. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28713. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28714. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28715. ASN1_STRING_free(asn1str_test);
  28716. ASN1_STRING_free(asn1str_answer);
  28717. res = TEST_RES_CHECK(1);
  28718. #endif /* OPENSSL_ALL && !NO_ASN */
  28719. return res;
  28720. }
  28721. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28722. {
  28723. int res = TEST_SKIPPED;
  28724. #if defined(OPENSSL_EXTRA)
  28725. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  28726. unsigned char nullstr[32];
  28727. XMEMSET(nullstr, 0, 32);
  28728. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28729. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  28730. DYNAMIC_TYPE_TMP_BUFFER);
  28731. if (asn1_gtime) {
  28732. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28733. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28734. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28735. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28736. }
  28737. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28738. res = TEST_RES_CHECK(1);
  28739. #endif /* OPENSSL_EXTRA */
  28740. return res;
  28741. }
  28742. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28743. {
  28744. int res = TEST_SKIPPED;
  28745. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28746. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28747. BIO* bio;
  28748. unsigned char buf[24];
  28749. int i;
  28750. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28751. BIO_set_write_buf_size(bio, 24);
  28752. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28753. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28754. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28755. /* Type not set. */
  28756. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28757. gtime.type = V_ASN1_GENERALIZEDTIME;
  28758. /* Invalid parameters testing. */
  28759. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28760. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28761. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28762. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28763. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28764. AssertIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28765. BIO_free(bio);
  28766. AssertNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28767. AssertIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28768. /* Ensure there is 0 bytes avaialble to write into. */
  28769. AssertIntEQ(BIO_write(bio, buf, 1), 1);
  28770. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28771. for (i = 1; i < 20; i++) {
  28772. AssertIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28773. AssertIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28774. }
  28775. BIO_free(bio);
  28776. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28777. res = TEST_RES_CHECK(1);
  28778. #endif /* OPENSSL_EXTRA */
  28779. return res;
  28780. }
  28781. static int test_wolfSSL_ASN1_TIME(void)
  28782. {
  28783. int res = TEST_SKIPPED;
  28784. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28785. WOLFSSL_ASN1_TIME* asn_time;
  28786. unsigned char *data;
  28787. AssertNotNull(asn_time = ASN1_TIME_new());
  28788. #ifndef NO_WOLFSSL_STUB
  28789. AssertNotNull(ASN1_TIME_set(asn_time, 1));
  28790. #endif
  28791. AssertIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28792. AssertIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28793. AssertIntEQ(ASN1_TIME_set_string(NULL,
  28794. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28795. AssertIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28796. AssertIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28797. AssertIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28798. AssertIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28799. AssertNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28800. AssertNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28801. AssertIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28802. AssertIntEQ(ASN1_TIME_check(NULL), 0);
  28803. AssertIntEQ(ASN1_TIME_check(asn_time), 1);
  28804. ASN1_TIME_free(asn_time);
  28805. ASN1_TIME_free(NULL);
  28806. res = TEST_RES_CHECK(1);
  28807. #endif
  28808. return res;
  28809. }
  28810. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28811. {
  28812. int res = TEST_SKIPPED;
  28813. #ifndef NO_ASN_TIME
  28814. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28815. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28816. WOLFSSL_ASN1_TIME* t;
  28817. char buf[ASN_GENERALIZED_TIME_SIZE];
  28818. AssertNotNull((t = ASN1_TIME_new()));
  28819. AssertIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28820. /* Invalid parameter testing. */
  28821. AssertNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28822. AssertNull(ASN1_TIME_to_string(t, NULL, 4));
  28823. AssertNull(ASN1_TIME_to_string(NULL, buf, 4));
  28824. AssertNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28825. AssertNull(ASN1_TIME_to_string(NULL, buf, 5));
  28826. AssertNull(ASN1_TIME_to_string(t, NULL, 5));
  28827. AssertNull(ASN1_TIME_to_string(t, buf, 4));
  28828. /* Buffer needs to be longer than minimum of 5 characters. */
  28829. AssertNull(ASN1_TIME_to_string(t, buf, 5));
  28830. ASN1_TIME_free(t);
  28831. res = TEST_RES_CHECK(1);
  28832. #endif
  28833. #endif /* NO_ASN_TIME */
  28834. return res;
  28835. }
  28836. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28837. {
  28838. int res = TEST_SKIPPED;
  28839. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28840. ASN1_TIME* fromTime;
  28841. ASN1_TIME* closeToTime;
  28842. ASN1_TIME* toTime;
  28843. ASN1_TIME* invalidTime;
  28844. int daysDiff;
  28845. int secsDiff;
  28846. AssertNotNull((fromTime = ASN1_TIME_new()));
  28847. /* Feb 22, 2003, 21:15:15 */
  28848. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28849. AssertNotNull((closeToTime = ASN1_TIME_new()));
  28850. /* Feb 22, 2003, 21:16:15 */
  28851. AssertIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28852. AssertNotNull((toTime = ASN1_TIME_new()));
  28853. /* Dec 19, 2010, 18:10:11 */
  28854. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28855. AssertNotNull((invalidTime = ASN1_TIME_new()));
  28856. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28857. AssertIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28858. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28859. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28860. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28861. /* Error conditions. */
  28862. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28863. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28864. /* If both times are NULL, difference is 0. */
  28865. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28866. AssertIntEQ(daysDiff, 0);
  28867. AssertIntEQ(secsDiff, 0);
  28868. /* If one time is NULL, it defaults to the current time. */
  28869. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28870. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28871. /* Normal operation. Both times non-NULL. */
  28872. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28873. AssertIntEQ(daysDiff, 2856);
  28874. AssertIntEQ(secsDiff, 75296);
  28875. /* Swapping the times should return negative values. */
  28876. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28877. AssertIntEQ(daysDiff, -2856);
  28878. AssertIntEQ(secsDiff, -75296);
  28879. /* Compare with invalid time string. */
  28880. AssertIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28881. AssertIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28882. /* Compare with days difference of 0. */
  28883. AssertIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28884. AssertIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28885. /* Days and seconds differences not 0. */
  28886. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28887. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28888. /* Same time. */
  28889. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28890. /* Compare regression test: No seconds difference, just difference in days.
  28891. */
  28892. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28893. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28894. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28895. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28896. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28897. /* Edge case with Unix epoch. */
  28898. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28899. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28900. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28901. AssertIntEQ(daysDiff, 3652);
  28902. AssertIntEQ(secsDiff, 0);
  28903. /* Edge case with year > 2038 (year 2038 problem). */
  28904. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28905. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28906. AssertIntEQ(daysDiff, 2932896);
  28907. AssertIntEQ(secsDiff, 86399);
  28908. ASN1_TIME_free(fromTime);
  28909. ASN1_TIME_free(closeToTime);
  28910. ASN1_TIME_free(toTime);
  28911. ASN1_TIME_free(invalidTime);
  28912. res = TEST_RES_CHECK(1);
  28913. #endif
  28914. return res;
  28915. }
  28916. static int test_wolfSSL_ASN1_TIME_adj(void)
  28917. {
  28918. int res = TEST_SKIPPED;
  28919. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  28920. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28921. const int year = 365*24*60*60;
  28922. const int day = 24*60*60;
  28923. const int hour = 60*60;
  28924. const int mini = 60;
  28925. const byte asn_utc_time = ASN_UTC_TIME;
  28926. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28927. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28928. #endif
  28929. WOLFSSL_ASN1_TIME* asn_time;
  28930. WOLFSSL_ASN1_TIME* s;
  28931. int offset_day;
  28932. long offset_sec;
  28933. char date_str[CTC_DATE_SIZE + 1];
  28934. time_t t;
  28935. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  28936. /* UTC notation test */
  28937. /* 2000/2/15 20:30:00 */
  28938. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28939. offset_day = 7;
  28940. offset_sec = 45 * mini;
  28941. /* offset_sec = -45 * min;*/
  28942. AssertNotNull(asn_time =
  28943. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28944. AssertTrue(asn_time->type == asn_utc_time);
  28945. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28946. date_str[CTC_DATE_SIZE] = '\0';
  28947. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28948. /* negative offset */
  28949. offset_sec = -45 * mini;
  28950. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28951. AssertNotNull(asn_time);
  28952. AssertTrue(asn_time->type == asn_utc_time);
  28953. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28954. date_str[CTC_DATE_SIZE] = '\0';
  28955. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28956. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28957. XMEMSET(date_str, 0, sizeof(date_str));
  28958. /* Generalized time will overflow time_t if not long */
  28959. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28960. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28961. DYNAMIC_TYPE_OPENSSL);
  28962. /* GeneralizedTime notation test */
  28963. /* 2055/03/01 09:00:00 */
  28964. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28965. offset_day = 12;
  28966. offset_sec = 10 * mini;
  28967. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28968. AssertTrue(asn_time->type == asn_gen_time);
  28969. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28970. date_str[CTC_DATE_SIZE] = '\0';
  28971. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28972. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28973. XMEMSET(date_str, 0, sizeof(date_str));
  28974. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28975. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28976. s = NULL;
  28977. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28978. offset_day = 7;
  28979. offset_sec = 45 * mini;
  28980. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28981. AssertTrue(asn_time->type == asn_utc_time);
  28982. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28983. date_str[CTC_DATE_SIZE] = '\0';
  28984. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28985. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28986. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  28987. AssertTrue(asn_time->type == asn_utc_time);
  28988. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28989. date_str[CTC_DATE_SIZE] = '\0';
  28990. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28991. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28992. res = TEST_RES_CHECK(1);
  28993. #endif
  28994. return res;
  28995. }
  28996. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28997. {
  28998. int res = TEST_SKIPPED;
  28999. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29000. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29001. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  29002. ASN1_TIME asnTime;
  29003. struct tm tm;
  29004. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  29005. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  29006. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  29007. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29008. AssertIntEQ(tm.tm_sec, 15);
  29009. AssertIntEQ(tm.tm_min, 15);
  29010. AssertIntEQ(tm.tm_hour, 21);
  29011. AssertIntEQ(tm.tm_mday, 22);
  29012. AssertIntEQ(tm.tm_mon, 1);
  29013. AssertIntEQ(tm.tm_year, 100);
  29014. AssertIntEQ(tm.tm_isdst, 0);
  29015. #ifdef XMKTIME
  29016. AssertIntEQ(tm.tm_wday, 2);
  29017. AssertIntEQ(tm.tm_yday, 52);
  29018. #endif
  29019. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  29020. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  29021. AssertIntEQ(tm.tm_year, 50);
  29022. /* Get current time. */
  29023. AssertIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  29024. AssertIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  29025. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  29026. /* 0 length. */
  29027. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29028. /* No type. */
  29029. asnTime.length = 1;
  29030. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29031. /* Not UTCTIME length. */
  29032. asnTime.type = V_ASN1_UTCTIME;
  29033. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29034. /* Not GENERALIZEDTIME length. */
  29035. asnTime.type = V_ASN1_GENERALIZEDTIME;
  29036. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29037. /* Not Zulu timezone. */
  29038. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  29039. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29040. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  29041. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  29042. res = TEST_RES_CHECK(1);
  29043. #endif
  29044. return res;
  29045. }
  29046. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  29047. {
  29048. int res = TEST_SKIPPED;
  29049. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  29050. WOLFSSL_ASN1_TIME *t;
  29051. WOLFSSL_ASN1_TIME *out;
  29052. WOLFSSL_ASN1_TIME *gtime;
  29053. int tlen = 0;
  29054. unsigned char *data;
  29055. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  29056. AssertNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  29057. /* type not set. */
  29058. AssertNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  29059. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29060. /* UTC Time test */
  29061. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  29062. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  29063. t->type = ASN_UTC_TIME;
  29064. t->length = ASN_UTC_TIME_SIZE;
  29065. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29066. tlen = wolfSSL_ASN1_TIME_get_length(t);
  29067. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  29068. data = wolfSSL_ASN1_TIME_get_data(t);
  29069. AssertStrEQ((char*)data, "050727123456Z");
  29070. out = NULL;
  29071. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29072. wolfSSL_ASN1_TIME_free(gtime);
  29073. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  29074. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29075. AssertPtrEq(gtime, out);
  29076. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29077. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29078. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29079. /* Generalized Time test */
  29080. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29081. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  29082. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  29083. t->type = ASN_GENERALIZED_TIME;
  29084. t->length = ASN_GENERALIZED_TIME_SIZE;
  29085. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  29086. tlen = wolfSSL_ASN1_TIME_get_length(t);
  29087. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  29088. data = wolfSSL_ASN1_TIME_get_data(t);
  29089. AssertStrEQ((char*)data, "20050727123456Z");
  29090. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29091. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29092. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29093. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29094. /* UTC Time to Generalized Time 1900's test */
  29095. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29096. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  29097. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  29098. t->type = ASN_UTC_TIME;
  29099. t->length = ASN_UTC_TIME_SIZE;
  29100. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  29101. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  29102. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29103. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29104. AssertStrEQ((char*)gtime->data, "19500727123456Z");
  29105. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29106. /* Null parameter test */
  29107. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  29108. gtime = NULL;
  29109. out = NULL;
  29110. t->type = ASN_UTC_TIME;
  29111. t->length = ASN_UTC_TIME_SIZE;
  29112. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29113. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  29114. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29115. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29116. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  29117. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29118. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29119. res = TEST_RES_CHECK(1);
  29120. #endif
  29121. return res;
  29122. }
  29123. static int test_wolfSSL_ASN1_TIME_print(void)
  29124. {
  29125. int res = TEST_SKIPPED;
  29126. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  29127. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29128. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29129. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  29130. !defined(NO_ASN_TIME)
  29131. BIO* bio;
  29132. BIO* fixed;
  29133. X509* x509;
  29134. const unsigned char* der = client_cert_der_2048;
  29135. ASN1_TIME* notAfter;
  29136. ASN1_TIME* notBefore;
  29137. unsigned char buf[25];
  29138. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29139. AssertNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29140. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  29141. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  29142. AssertNotNull(notBefore = X509_get_notBefore(x509));
  29143. AssertIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  29144. AssertIntEQ(ASN1_TIME_print(bio, NULL), 0);
  29145. AssertIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  29146. AssertIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  29147. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29148. AssertIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  29149. /* Test BIO_write fails. */
  29150. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29151. /* Ensure there is 0 bytes avaialble to write into. */
  29152. AssertIntEQ(BIO_write(fixed, buf, 1), 1);
  29153. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29154. AssertIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29155. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29156. AssertIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  29157. AssertIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29158. /* create a bad time and test results */
  29159. AssertNotNull(notAfter = X509_get_notAfter(x509));
  29160. AssertIntEQ(ASN1_TIME_check(notAfter), 1);
  29161. notAfter->data[8] = 0;
  29162. notAfter->data[3] = 0;
  29163. AssertIntNE(ASN1_TIME_print(bio, notAfter), 1);
  29164. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29165. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29166. AssertIntEQ(ASN1_TIME_check(notAfter), 0);
  29167. BIO_free(bio);
  29168. BIO_free(fixed);
  29169. X509_free(x509);
  29170. res = TEST_RES_CHECK(1);
  29171. #endif
  29172. return res;
  29173. }
  29174. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  29175. {
  29176. int res = TEST_SKIPPED;
  29177. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  29178. BIO* bio;
  29179. ASN1_UTCTIME* utc = NULL;
  29180. unsigned char buf[25];
  29181. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  29182. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  29183. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  29184. /* Valid date */
  29185. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29186. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  29187. DYNAMIC_TYPE_ASN1));
  29188. utc->type = ASN_UTC_TIME;
  29189. utc->length = ASN_UTC_TIME_SIZE;
  29190. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  29191. AssertIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  29192. AssertIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  29193. AssertIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  29194. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  29195. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29196. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  29197. XMEMSET(buf, 0, sizeof(buf));
  29198. BIO_free(bio);
  29199. /* Invalid format */
  29200. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29201. utc->type = ASN_UTC_TIME;
  29202. utc->length = ASN_UTC_TIME_SIZE;
  29203. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  29204. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29205. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29206. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29207. /* Invalid type */
  29208. utc->type = ASN_GENERALIZED_TIME;
  29209. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29210. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29211. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29212. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29213. BIO_free(bio);
  29214. res = TEST_RES_CHECK(1);
  29215. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29216. return res;
  29217. }
  29218. static int test_wolfSSL_ASN1_TYPE(void)
  29219. {
  29220. int res = TEST_SKIPPED;
  29221. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29222. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29223. WOLFSSL_ASN1_TYPE* t;
  29224. WOLFSSL_ASN1_OBJECT* obj;
  29225. #ifndef NO_ASN_TIME
  29226. WOLFSSL_ASN1_TIME* time;
  29227. #endif
  29228. WOLFSSL_ASN1_STRING* str;
  29229. unsigned char data[] = { 0x00 };
  29230. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29231. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29232. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29233. wolfSSL_ASN1_TYPE_free(t);
  29234. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29235. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29236. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29237. wolfSSL_ASN1_TYPE_free(t);
  29238. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29239. AssertNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29240. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29241. wolfSSL_ASN1_TYPE_free(t);
  29242. #ifndef NO_ASN_TIME
  29243. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29244. AssertNotNull(time = wolfSSL_ASN1_TIME_new());
  29245. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29246. wolfSSL_ASN1_TYPE_free(t);
  29247. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29248. AssertNotNull(time = wolfSSL_ASN1_TIME_new());
  29249. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29250. wolfSSL_ASN1_TYPE_free(t);
  29251. #endif
  29252. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29253. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29254. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29255. wolfSSL_ASN1_TYPE_free(t);
  29256. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29257. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29258. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29259. wolfSSL_ASN1_TYPE_free(t);
  29260. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29261. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29262. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29263. wolfSSL_ASN1_TYPE_free(t);
  29264. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29265. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29266. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29267. wolfSSL_ASN1_TYPE_free(t);
  29268. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29269. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29270. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29271. wolfSSL_ASN1_TYPE_free(t);
  29272. AssertNotNull(t = wolfSSL_ASN1_TYPE_new());
  29273. AssertNotNull(str = wolfSSL_ASN1_STRING_new());
  29274. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29275. wolfSSL_ASN1_TYPE_free(t);
  29276. res = TEST_RES_CHECK(1);
  29277. #endif
  29278. return res;
  29279. }
  29280. /* Testing code used in dpp.c in hostap */
  29281. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29282. typedef struct {
  29283. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29284. * as an OID identifying the curve */
  29285. X509_ALGOR *alg;
  29286. /* Compressed format public key per ANSI X9.63 */
  29287. ASN1_BIT_STRING *pub_key;
  29288. } DPP_BOOTSTRAPPING_KEY;
  29289. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29290. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29291. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29292. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29293. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29294. typedef struct {
  29295. ASN1_INTEGER *integer;
  29296. } TEST_ASN1;
  29297. #define WOLFSSL_ASN1_INTEGER_ASN1 2
  29298. ASN1_SEQUENCE(TEST_ASN1) = {
  29299. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29300. } ASN1_SEQUENCE_END(TEST_ASN1)
  29301. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29302. #endif
  29303. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29304. {
  29305. int res = TEST_SKIPPED;
  29306. /* Testing code used in dpp.c in hostap */
  29307. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29308. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29309. EC_KEY *eckey;
  29310. EVP_PKEY *key;
  29311. size_t len;
  29312. unsigned char *der = NULL;
  29313. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29314. const unsigned char *in = ecc_clikey_der_256;
  29315. const EC_GROUP *group;
  29316. const EC_POINT *point;
  29317. int nid;
  29318. TEST_ASN1 test_asn1;
  29319. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29320. const unsigned char goodObjDer[] = {
  29321. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29322. };
  29323. WOLFSSL_ASN1_ITEM emptyTemplate;
  29324. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29325. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29326. der = NULL;
  29327. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29328. AssertIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29329. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29330. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29331. (long)sizeof_ecc_clikey_der_256));
  29332. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29333. AssertNotNull(group = EC_KEY_get0_group(eckey));
  29334. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  29335. nid = EC_GROUP_get_curve_name(group);
  29336. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  29337. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  29338. AssertIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29339. #ifdef HAVE_COMP_KEY
  29340. AssertIntGT((len = EC_POINT_point2oct(
  29341. group, point, POINT_CONVERSION_COMPRESSED,
  29342. NULL, 0, NULL)), 0);
  29343. #else
  29344. AssertIntGT((len = EC_POINT_point2oct(
  29345. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29346. NULL, 0, NULL)), 0);
  29347. #endif
  29348. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29349. #ifdef HAVE_COMP_KEY
  29350. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29351. der, len-1, NULL), 0);
  29352. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29353. der, len, NULL), len);
  29354. #else
  29355. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29356. der, len-1, NULL), 0);
  29357. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29358. der, len, NULL), len);
  29359. #endif
  29360. bootstrap->pub_key->data = der;
  29361. bootstrap->pub_key->length = (int)len;
  29362. /* Not actually used */
  29363. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29364. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29365. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29366. der = NULL;
  29367. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29368. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29369. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29370. EVP_PKEY_free(key);
  29371. EC_KEY_free(eckey);
  29372. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29373. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29374. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29375. * a NULL and an empty BIT_STRING. */
  29376. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29377. bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new();
  29378. bootstrap->alg->algorithm->obj = badObjDer;
  29379. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29380. bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new();
  29381. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29382. bootstrap->alg->parameter->value.ptr = NULL;
  29383. bootstrap->pub_key->data = NULL;
  29384. bootstrap->pub_key->length = 0;
  29385. /* Not actually used */
  29386. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29387. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29388. /* Encode with bad OBJECT_ID. */
  29389. der = NULL;
  29390. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29391. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29392. bootstrap->alg->algorithm->obj = goodObjDer;
  29393. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29394. bootstrap->alg->algorithm->grp = 2;
  29395. der = NULL;
  29396. AssertIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29397. AssertIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29398. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29399. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29400. /* Test error cases. */
  29401. AssertNull(TEST_ASN1_new());
  29402. AssertNull(wolfSSL_ASN1_item_new(NULL));
  29403. TEST_ASN1_free(NULL);
  29404. XMEMSET(&test_asn1, 0, sizeof(TEST_ASN1));
  29405. AssertIntEQ(i2d_TEST_ASN1(&test_asn1, &der), 0);
  29406. res = TEST_RES_CHECK(1);
  29407. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29408. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29409. return res;
  29410. }
  29411. static int test_wolfSSL_lhash(void)
  29412. {
  29413. int res = TEST_SKIPPED;
  29414. #ifdef OPENSSL_ALL
  29415. const char testStr[] = "Like a true nature's child\n"
  29416. "We were born\n"
  29417. "Born to be wild";
  29418. #ifdef NO_SHA
  29419. AssertIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29420. #else
  29421. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29422. #endif
  29423. res = TEST_RES_CHECK(1);
  29424. #endif
  29425. return res;
  29426. }
  29427. static int test_wolfSSL_X509_NAME(void)
  29428. {
  29429. int res = TEST_SKIPPED;
  29430. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29431. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29432. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29433. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29434. defined(OPENSSL_EXTRA))
  29435. X509* x509;
  29436. const unsigned char* c;
  29437. unsigned char buf[4096];
  29438. int bytes;
  29439. XFILE f;
  29440. const X509_NAME* a;
  29441. const X509_NAME* b;
  29442. X509_NAME* d2i_name = NULL;
  29443. int sz;
  29444. unsigned char* tmp;
  29445. char file[] = "./certs/ca-cert.der";
  29446. #ifndef OPENSSL_EXTRA_X509_SMALL
  29447. byte empty[] = { /* CN=empty emailAddress= */
  29448. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29449. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29450. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29451. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29452. 0x01, 0x16, 0x00
  29453. };
  29454. #endif
  29455. #ifndef OPENSSL_EXTRA_X509_SMALL
  29456. /* test compile of deprecated function, returns 0 */
  29457. AssertIntEQ(CRYPTO_thread_id(), 0);
  29458. #endif
  29459. AssertNotNull(a = X509_NAME_new());
  29460. X509_NAME_free((X509_NAME*)a);
  29461. f = XFOPEN(file, "rb");
  29462. AssertTrue(f != XBADFILE);
  29463. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  29464. XFCLOSE(f);
  29465. c = buf;
  29466. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  29467. /* test cmp function */
  29468. AssertNotNull(a = X509_get_issuer_name(x509));
  29469. AssertNotNull(b = X509_get_subject_name(x509));
  29470. #ifndef OPENSSL_EXTRA_X509_SMALL
  29471. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29472. #endif
  29473. tmp = buf;
  29474. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29475. if (sz > 0 && tmp == buf) {
  29476. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29477. __LINE__);
  29478. fprintf(stderr, " Expected pointer to be incremented\n");
  29479. abort();
  29480. }
  29481. #ifndef OPENSSL_EXTRA_X509_SMALL
  29482. tmp = buf;
  29483. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29484. #endif
  29485. /* if output parameter is NULL, should still return required size. */
  29486. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29487. /* retry but with the function creating a buffer */
  29488. tmp = NULL;
  29489. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29490. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29491. #ifdef WOLFSSL_CERT_NAME_ALL
  29492. /* test for givenName and name */
  29493. {
  29494. WOLFSSL_X509_NAME_ENTRY* entry;
  29495. const byte gName[] = "test-given-name";
  29496. const byte name[] = "test-name";
  29497. entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL, NID_givenName,
  29498. ASN_UTF8STRING, gName, sizeof(gName));
  29499. AssertNotNull(entry);
  29500. wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0);
  29501. wolfSSL_X509_NAME_ENTRY_free(entry);
  29502. entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL, NID_name,
  29503. ASN_UTF8STRING, name, sizeof(name));
  29504. AssertNotNull(entry);
  29505. wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0);
  29506. wolfSSL_X509_NAME_ENTRY_free(entry);
  29507. tmp = NULL;
  29508. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29509. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29510. }
  29511. #endif
  29512. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29513. #ifndef OPENSSL_EXTRA_X509_SMALL
  29514. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  29515. #endif
  29516. X509_NAME_free((X509_NAME*)b);
  29517. X509_NAME_free(d2i_name);
  29518. X509_free(x509);
  29519. #ifndef OPENSSL_EXTRA_X509_SMALL
  29520. /* test with an empty domain component */
  29521. tmp = empty;
  29522. sz = sizeof(empty);
  29523. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29524. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29525. /* size of empty emailAddress will be 0 */
  29526. tmp = buf;
  29527. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29528. (char*)tmp, sizeof(buf)), 0);
  29529. /* should contain no organization name */
  29530. tmp = buf;
  29531. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29532. (char*)tmp, sizeof(buf)), -1);
  29533. X509_NAME_free(d2i_name);
  29534. #endif
  29535. res = TEST_RES_CHECK(1);
  29536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  29537. return res;
  29538. }
  29539. static int test_wolfSSL_X509_NAME_hash(void)
  29540. {
  29541. int res = TEST_SKIPPED;
  29542. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  29543. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29544. BIO* bio;
  29545. X509* x509 = NULL;
  29546. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29547. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29548. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29549. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29550. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29551. X509_free(x509);
  29552. BIO_free(bio);
  29553. res = TEST_RES_CHECK(1);
  29554. #endif
  29555. return res;
  29556. }
  29557. static int test_wolfSSL_X509_NAME_print_ex(void)
  29558. {
  29559. int res = TEST_SKIPPED;
  29560. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29561. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29562. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29563. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29564. !defined(NO_BIO) && !defined(NO_RSA)
  29565. int memSz;
  29566. byte* mem = NULL;
  29567. BIO* bio = NULL;
  29568. BIO* membio = NULL;
  29569. X509* x509 = NULL;
  29570. X509_NAME* name = NULL;
  29571. const char* expNormal = "C=US, CN=wolfssl.com";
  29572. const char* expReverse = "CN=wolfssl.com, C=US";
  29573. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29574. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29575. const char* expRFC5523 =
  29576. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29577. /* Test with real cert (svrCertFile) first */
  29578. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29579. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29580. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29581. AssertNotNull(name = X509_get_subject_name(x509));
  29582. /* Test without flags */
  29583. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29584. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29585. BIO_free(membio);
  29586. /* Test flag: XN_FLAG_RFC2253 */
  29587. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29588. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29589. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29590. BIO_free(membio);
  29591. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29592. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29593. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29594. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29595. BIO_free(membio);
  29596. X509_free(x509);
  29597. BIO_free(bio);
  29598. /* Test normal case without escaped characters */
  29599. {
  29600. /* Create name: "/C=US/CN=wolfssl.com" */
  29601. AssertNotNull(name = X509_NAME_new());
  29602. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29603. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29604. WOLFSSL_SUCCESS);
  29605. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29606. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29607. WOLFSSL_SUCCESS);
  29608. /* Test without flags */
  29609. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29610. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29611. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29612. AssertIntEQ(memSz, XSTRLEN(expNormal));
  29613. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29614. BIO_free(membio);
  29615. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29616. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29617. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29618. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29619. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29620. AssertIntEQ(memSz, XSTRLEN(expReverse));
  29621. BIO_free(membio);
  29622. /* Test flags: XN_FLAG_DN_REV - reversed */
  29623. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29624. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29625. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29626. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29627. AssertIntEQ(memSz, XSTRLEN(expReverse));
  29628. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29629. BIO_free(membio);
  29630. X509_NAME_free(name);
  29631. }
  29632. /* Test RFC2253 characters are escaped with backslashes */
  29633. {
  29634. AssertNotNull(name = X509_NAME_new());
  29635. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29636. /* space at beginning and end, and: ,+"\ */
  29637. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29638. WOLFSSL_SUCCESS);
  29639. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29640. /* # at beginning, and: <>;*/
  29641. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29642. WOLFSSL_SUCCESS);
  29643. /* Test without flags */
  29644. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29645. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29646. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29647. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  29648. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29649. XSTRLEN(expNotEscaped)), 0);
  29650. BIO_free(membio);
  29651. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29652. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29653. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29654. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29655. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29656. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  29657. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29658. BIO_free(membio);
  29659. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29660. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  29661. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  29662. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29663. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29664. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29665. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29666. XSTRLEN(expNotEscapedRev)), 0);
  29667. BIO_free(membio);
  29668. X509_NAME_free(name);
  29669. }
  29670. res = TEST_RES_CHECK(1);
  29671. #endif
  29672. return res;
  29673. }
  29674. #ifndef NO_BIO
  29675. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29676. {
  29677. int res = TEST_SKIPPED;
  29678. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29679. STACK_OF(X509_INFO) *info_stack;
  29680. X509_INFO *info;
  29681. int len;
  29682. int i;
  29683. const char* files[] = {
  29684. cliCertFile,
  29685. cliKeyFile,
  29686. /* This needs to be the order as svrCertFile contains the
  29687. * intermediate cert as well. */
  29688. svrKeyFile,
  29689. svrCertFile,
  29690. NULL,
  29691. };
  29692. const char** curFile;
  29693. BIO *fileBIO;
  29694. BIO *concatBIO = NULL;
  29695. byte tmp[FOURK_BUF];
  29696. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29697. * to group objects together. */
  29698. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29699. for (curFile = files; *curFile != NULL; curFile++) {
  29700. int fileLen;
  29701. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29702. fileLen = wolfSSL_BIO_get_len(fileBIO);
  29703. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29704. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  29705. fileLen -= len;
  29706. }
  29707. /* Make sure we read the entire file */
  29708. AssertIntEQ(fileLen, 0);
  29709. BIO_free(fileBIO);
  29710. }
  29711. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29712. NULL));
  29713. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  29714. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29715. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  29716. AssertNotNull(info->x509);
  29717. AssertNull(info->crl);
  29718. if (i != 0) {
  29719. AssertNotNull(info->x_pkey);
  29720. AssertIntEQ(X509_check_private_key(info->x509,
  29721. info->x_pkey->dec_pkey), 1);
  29722. }
  29723. else {
  29724. AssertNull(info->x_pkey);
  29725. }
  29726. }
  29727. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29728. BIO_free(concatBIO);
  29729. res = TEST_RES_CHECK(1);
  29730. #endif
  29731. return res;
  29732. }
  29733. #endif
  29734. #ifndef NO_BIO
  29735. static int test_wolfSSL_X509_INFO(void)
  29736. {
  29737. int res = TEST_SKIPPED;
  29738. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29739. STACK_OF(X509_INFO) *info_stack;
  29740. X509_INFO *info;
  29741. BIO *cert;
  29742. int i;
  29743. /* PEM in hex format to avoid null terminator */
  29744. byte data[] = {
  29745. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29746. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29747. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29748. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29749. 0x2d, 0x2d, 0x2d
  29750. };
  29751. /* PEM in hex format to avoid null terminator */
  29752. byte data2[] = {
  29753. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29754. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29755. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29756. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29757. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29758. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29759. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29760. };
  29761. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29762. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29763. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29764. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  29765. AssertNotNull(info->x509);
  29766. AssertNull(info->crl);
  29767. AssertNull(info->x_pkey);
  29768. }
  29769. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29770. BIO_free(cert);
  29771. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29772. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29773. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29774. BIO_free(cert);
  29775. /* This case should fail due to invalid input. */
  29776. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  29777. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29778. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29779. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29780. BIO_free(cert);
  29781. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  29782. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29783. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29784. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29785. BIO_free(cert);
  29786. res = TEST_RES_CHECK(1);
  29787. #endif
  29788. return res;
  29789. }
  29790. #endif
  29791. static int test_wolfSSL_X509_subject_name_hash(void)
  29792. {
  29793. int res = TEST_SKIPPED;
  29794. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29795. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29796. X509* x509;
  29797. X509_NAME* subjectName = NULL;
  29798. unsigned long ret1 = 0;
  29799. unsigned long ret2 = 0;
  29800. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29801. SSL_FILETYPE_PEM));
  29802. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29803. /* These two
  29804. * - X509_subject_name_hash(x509)
  29805. * - X509_NAME_hash(X509_get_subject_name(x509))
  29806. * should give the same hash, if !defined(NO_SHA) is true. */
  29807. ret1 = X509_subject_name_hash(x509);
  29808. AssertIntNE(ret1, 0);
  29809. #if !defined(NO_SHA)
  29810. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29811. AssertIntNE(ret2, 0);
  29812. AssertIntEQ(ret1, ret2);
  29813. #else
  29814. (void) ret2;
  29815. #endif
  29816. X509_free(x509);
  29817. res = TEST_RES_CHECK(1);
  29818. #endif
  29819. return res;
  29820. }
  29821. static int test_wolfSSL_X509_issuer_name_hash(void)
  29822. {
  29823. int res = TEST_SKIPPED;
  29824. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29825. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29826. X509* x509;
  29827. X509_NAME* issuertName = NULL;
  29828. unsigned long ret1 = 0;
  29829. unsigned long ret2 = 0;
  29830. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29831. SSL_FILETYPE_PEM));
  29832. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29833. /* These two
  29834. * - X509_issuer_name_hash(x509)
  29835. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29836. * should give the same hash, if !defined(NO_SHA) is true. */
  29837. ret1 = X509_issuer_name_hash(x509);
  29838. AssertIntNE(ret1, 0);
  29839. #if !defined(NO_SHA)
  29840. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29841. AssertIntNE(ret2, 0);
  29842. AssertIntEQ(ret1, ret2);
  29843. #else
  29844. (void) ret2;
  29845. #endif
  29846. X509_free(x509);
  29847. res = TEST_RES_CHECK(1);
  29848. #endif
  29849. return res;
  29850. }
  29851. static int test_wolfSSL_X509_check_host(void)
  29852. {
  29853. int res = TEST_SKIPPED;
  29854. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29855. && !defined(NO_SHA) && !defined(NO_RSA)
  29856. X509* x509;
  29857. const char altName[] = "example.com";
  29858. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29859. SSL_FILETYPE_PEM));
  29860. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29861. WOLFSSL_SUCCESS);
  29862. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29863. WOLFSSL_FAILURE);
  29864. X509_free(x509);
  29865. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29866. WOLFSSL_FAILURE);
  29867. res = TEST_RES_CHECK(1);
  29868. #endif
  29869. return res;
  29870. }
  29871. static int test_wolfSSL_X509_check_email(void)
  29872. {
  29873. int res = TEST_SKIPPED;
  29874. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29875. X509* x509;
  29876. const char goodEmail[] = "info@wolfssl.com";
  29877. const char badEmail[] = "disinfo@wolfssl.com";
  29878. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29879. SSL_FILETYPE_PEM));
  29880. /* Should fail on non-matching email address */
  29881. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29882. WOLFSSL_FAILURE);
  29883. /* Should succeed on matching email address */
  29884. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29885. WOLFSSL_SUCCESS);
  29886. /* Should compute length internally when not provided */
  29887. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29888. WOLFSSL_SUCCESS);
  29889. /* Should fail when email address is NULL */
  29890. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29891. WOLFSSL_FAILURE);
  29892. X509_free(x509);
  29893. /* Should fail when x509 is NULL */
  29894. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29895. WOLFSSL_FAILURE);
  29896. res = TEST_RES_CHECK(1);
  29897. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29898. return res;
  29899. }
  29900. static int test_wolfSSL_DES(void)
  29901. {
  29902. int res = TEST_SKIPPED;
  29903. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  29904. const_DES_cblock myDes;
  29905. DES_cblock iv;
  29906. DES_key_schedule key;
  29907. word32 i;
  29908. DES_LONG dl;
  29909. unsigned char msg[] = "hello wolfssl";
  29910. DES_check_key(1);
  29911. DES_set_key(&myDes, &key);
  29912. /* check, check of odd parity */
  29913. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  29914. XMEMSET(key, 5, sizeof(DES_key_schedule));
  29915. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  29916. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  29917. /* set odd parity for success case */
  29918. DES_set_odd_parity(&myDes);
  29919. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  29920. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  29921. myDes[3]);
  29922. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  29923. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  29924. AssertIntEQ(key[i], myDes[i]);
  29925. }
  29926. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  29927. /* check weak key */
  29928. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  29929. XMEMSET(key, 5, sizeof(DES_key_schedule));
  29930. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  29931. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  29932. /* now do unchecked copy of a weak key over */
  29933. DES_set_key_unchecked(&myDes, &key);
  29934. /* compare arrays, should be the same */
  29935. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  29936. AssertIntEQ(key[i], myDes[i]);
  29937. }
  29938. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  29939. /* check DES_key_sched API */
  29940. XMEMSET(key, 1, sizeof(DES_key_schedule));
  29941. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  29942. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  29943. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  29944. /* compare arrays, should be the same */
  29945. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  29946. AssertIntEQ(key[i], myDes[i]);
  29947. }
  29948. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  29949. * DES_cbc_encrypt on the input */
  29950. XMEMSET(iv, 0, sizeof(DES_cblock));
  29951. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  29952. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  29953. AssertIntEQ(dl, 480052723);
  29954. res = TEST_RES_CHECK(1);
  29955. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  29956. return res;
  29957. }
  29958. static int test_wc_PemToDer(void)
  29959. {
  29960. int res = TEST_SKIPPED;
  29961. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29962. int ret;
  29963. DerBuffer* pDer = NULL;
  29964. const char* ca_cert = "./certs/server-cert.pem";
  29965. byte* cert_buf = NULL;
  29966. size_t cert_sz = 0;
  29967. int eccKey = 0;
  29968. EncryptedInfo info;
  29969. XMEMSET(&info, 0, sizeof(info));
  29970. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  29971. if (ret == 0) {
  29972. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  29973. &pDer, NULL, &info, &eccKey);
  29974. AssertIntEQ(ret, 0);
  29975. wc_FreeDer(&pDer);
  29976. }
  29977. if (cert_buf)
  29978. free(cert_buf);
  29979. #ifdef HAVE_ECC
  29980. {
  29981. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29982. byte key_buf[256] = {0};
  29983. /* Test fail of loading a key with cert type */
  29984. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29985. key_buf[0] = '\n';
  29986. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  29987. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29988. &pDer, NULL, &info, &eccKey)), 0);
  29989. #ifdef OPENSSL_EXTRA
  29990. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29991. &pDer, NULL, &info, &eccKey)), 0);
  29992. #endif
  29993. wc_FreeDer(&pDer);
  29994. if (cert_buf)
  29995. free(cert_buf);
  29996. }
  29997. #endif
  29998. res = TEST_RES_CHECK(1);
  29999. #endif
  30000. return res;
  30001. }
  30002. static int test_wc_AllocDer(void)
  30003. {
  30004. int res = TEST_SKIPPED;
  30005. #if !defined(NO_CERTS)
  30006. int ret;
  30007. DerBuffer* pDer = NULL;
  30008. word32 testSize = 1024;
  30009. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  30010. AssertIntEQ(ret, 0);
  30011. AssertNotNull(pDer);
  30012. wc_FreeDer(&pDer);
  30013. res = TEST_RES_CHECK(1);
  30014. #endif
  30015. return res;
  30016. }
  30017. static int test_wc_CertPemToDer(void)
  30018. {
  30019. int res = TEST_SKIPPED;
  30020. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  30021. int ret;
  30022. const char* ca_cert = "./certs/ca-cert.pem";
  30023. byte* cert_buf = NULL;
  30024. size_t cert_sz = 0, cert_dersz = 0;
  30025. byte* cert_der = NULL;
  30026. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  30027. if (ret == 0) {
  30028. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30029. cert_der = (byte*)malloc(cert_dersz);
  30030. if (cert_der) {
  30031. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  30032. cert_der, (int)cert_dersz, CERT_TYPE);
  30033. AssertIntGE(ret, 0);
  30034. }
  30035. }
  30036. if (cert_der)
  30037. free(cert_der);
  30038. if (cert_buf)
  30039. free(cert_buf);
  30040. res = TEST_RES_CHECK(1);
  30041. #endif
  30042. return res;
  30043. }
  30044. static int test_wc_KeyPemToDer(void)
  30045. {
  30046. int res = TEST_SKIPPED;
  30047. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30048. int ret;
  30049. const byte cert_buf[] = \
  30050. "-----BEGIN PRIVATE KEY-----\n"
  30051. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  30052. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  30053. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  30054. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  30055. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  30056. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  30057. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  30058. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  30059. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  30060. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  30061. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  30062. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  30063. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  30064. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  30065. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  30066. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  30067. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  30068. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  30069. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  30070. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  30071. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  30072. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  30073. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  30074. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  30075. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  30076. "AQue5o6oVfhgLiJzMdo/77gw\n"
  30077. "-----END PRIVATE KEY-----\n";
  30078. const int cert_sz = sizeof(cert_buf);
  30079. const char cert_pw[] = "password";
  30080. int cert_dersz = 0;
  30081. byte* cert_der = NULL;
  30082. /* Bad arg: Cert buffer is NULL */
  30083. ret = wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, "");
  30084. AssertIntEQ(ret, BAD_FUNC_ARG);
  30085. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  30086. ret = wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, "");
  30087. AssertIntEQ(ret, BAD_FUNC_ARG);
  30088. /* Test normal operation */
  30089. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30090. cert_der = (byte*)malloc(cert_dersz);
  30091. AssertNotNull(cert_der);
  30092. if (cert_der) {
  30093. ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz, cert_pw);
  30094. AssertIntGE(ret, 0);
  30095. AssertIntLE(ret, cert_sz);
  30096. free(cert_der);
  30097. cert_der = NULL;
  30098. ret = 0;
  30099. }
  30100. if (ret == 0) {
  30101. /* Test NULL for DER buffer to return needed DER buffer size */
  30102. ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, "");
  30103. AssertIntGT(ret, 0);
  30104. AssertIntLE(ret, cert_sz);
  30105. cert_dersz = ret;
  30106. cert_der = (byte*)malloc(cert_dersz);
  30107. AssertNotNull(cert_der);
  30108. if (cert_der) {
  30109. ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz, cert_pw);
  30110. AssertIntGE(ret, 0);
  30111. AssertIntLE(ret, cert_sz);
  30112. free(cert_der);
  30113. cert_der = NULL;
  30114. }
  30115. }
  30116. res = TEST_RES_CHECK(1);
  30117. #endif
  30118. return res;
  30119. }
  30120. static int test_wc_PubKeyPemToDer(void)
  30121. {
  30122. int res = TEST_SKIPPED;
  30123. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  30124. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30125. int ret;
  30126. const char* key = "./certs/ecc-client-keyPub.pem";
  30127. byte* cert_buf = NULL;
  30128. size_t cert_sz = 0, cert_dersz = 0;
  30129. byte* cert_der = NULL;
  30130. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  30131. cert_der, (int)cert_dersz);
  30132. AssertIntGE(ret, BAD_FUNC_ARG);
  30133. ret = load_file(key, &cert_buf, &cert_sz);
  30134. if (ret == 0) {
  30135. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30136. cert_der = (byte*)malloc(cert_dersz);
  30137. AssertNotNull(cert_der);
  30138. if (cert_der) {
  30139. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30140. (int)cert_dersz);
  30141. AssertIntGE(ret, 0);
  30142. free(cert_der);
  30143. cert_der = NULL;
  30144. ret = 0;
  30145. }
  30146. }
  30147. if (ret == 0) {
  30148. /* Test NULL for DER buffer to return needed DER buffer size */
  30149. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0);
  30150. AssertIntGT(ret, 0);
  30151. AssertIntLE(ret, cert_sz);
  30152. cert_dersz = ret;
  30153. cert_der = (byte*)malloc(cert_dersz);
  30154. AssertNotNull(cert_der);
  30155. if (cert_der) {
  30156. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30157. (int)cert_dersz);
  30158. AssertIntGE(ret, 0);
  30159. free(cert_der);
  30160. cert_der = NULL;
  30161. }
  30162. }
  30163. if (cert_buf) {
  30164. free(cert_buf);
  30165. }
  30166. res = TEST_RES_CHECK(1);
  30167. #endif
  30168. return res;
  30169. }
  30170. static int test_wc_PemPubKeyToDer(void)
  30171. {
  30172. int res = TEST_SKIPPED;
  30173. #if !defined(NO_FILESYSTEM) && \
  30174. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30175. int ret;
  30176. const char* key = "./certs/ecc-client-keyPub.pem";
  30177. size_t cert_dersz = 1024;
  30178. byte* cert_der = (byte*)malloc(cert_dersz);
  30179. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  30180. AssertIntGE(ret, BAD_FUNC_ARG);
  30181. if (cert_der) {
  30182. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  30183. AssertIntGE(ret, 0);
  30184. free(cert_der);
  30185. }
  30186. res = TEST_RES_CHECK(1);
  30187. #endif
  30188. return res;
  30189. }
  30190. static int test_wc_GetPubKeyDerFromCert(void)
  30191. {
  30192. int res = TEST_SKIPPED;
  30193. #if !defined(NO_RSA) || defined(HAVE_ECC)
  30194. int ret;
  30195. word32 idx = 0;
  30196. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30197. word32 keyDerSz = (word32)sizeof(keyDer);
  30198. DecodedCert decoded;
  30199. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30200. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  30201. word32 certBufSz = sizeof(certBuf);
  30202. #endif
  30203. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  30204. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30205. XFILE fp;
  30206. #endif
  30207. #ifndef NO_RSA
  30208. RsaKey rsaKey;
  30209. #if defined(USE_CERT_BUFFERS_2048)
  30210. byte* rsaCertDer = (byte*)client_cert_der_2048;
  30211. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  30212. #elif defined(USE_CERT_BUFFERS_1024)
  30213. byte* rsaCertDer = (byte*)client_cert_der_1024;
  30214. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  30215. #else
  30216. unsigned char rsaCertDer[TWOK_BUF];
  30217. word32 rsaCertDerSz;
  30218. #endif
  30219. #endif
  30220. #ifdef HAVE_ECC
  30221. ecc_key eccKey;
  30222. #if defined(USE_CERT_BUFFERS_256)
  30223. byte* eccCert = (byte*)cliecc_cert_der_256;
  30224. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  30225. #else
  30226. unsigned char eccCert[ONEK_BUF];
  30227. word32 eccCertSz;
  30228. XFILE fp2;
  30229. #endif
  30230. #endif
  30231. #ifndef NO_RSA
  30232. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  30233. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  30234. AssertTrue((fp != XBADFILE));
  30235. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  30236. XFCLOSE(fp);
  30237. #endif
  30238. /* good test case - RSA DER cert */
  30239. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  30240. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30241. AssertIntEQ(ret, 0);
  30242. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30243. AssertIntEQ(ret, 0);
  30244. AssertIntGT(keyDerSz, 0);
  30245. /* sanity check, verify we can import DER public key */
  30246. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30247. AssertIntEQ(ret, 0);
  30248. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  30249. AssertIntEQ(ret, 0);
  30250. wc_FreeRsaKey(&rsaKey);
  30251. /* test LENGTH_ONLY_E case */
  30252. keyDerSz = 0;
  30253. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  30254. AssertIntEQ(ret, LENGTH_ONLY_E);
  30255. AssertIntGT(keyDerSz, 0);
  30256. /* bad args: DecodedCert NULL */
  30257. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  30258. AssertIntEQ(ret, BAD_FUNC_ARG);
  30259. /* bad args: output key buff size */
  30260. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  30261. AssertIntEQ(ret, BAD_FUNC_ARG);
  30262. /* bad args: zero size output key buffer */
  30263. keyDerSz = 0;
  30264. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30265. AssertIntEQ(ret, BAD_FUNC_ARG);
  30266. wc_FreeDecodedCert(&decoded);
  30267. /* Certificate Request Tests */
  30268. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30269. {
  30270. XMEMSET(certBuf, 0, sizeof(certBuf));
  30271. fp = XFOPEN("./certs/csr.signed.der", "rb");
  30272. AssertTrue((fp != XBADFILE));
  30273. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  30274. XFCLOSE(fp);
  30275. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30276. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  30277. AssertIntEQ(ret, 0);
  30278. /* good test case - RSA DER certificate request */
  30279. keyDerSz = sizeof(keyDer);
  30280. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30281. AssertIntEQ(ret, 0);
  30282. AssertIntGT(keyDerSz, 0);
  30283. /* sanity check, verify we can import DER public key */
  30284. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30285. AssertIntEQ(ret, 0);
  30286. idx = 0;
  30287. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  30288. AssertIntEQ(ret, 0);
  30289. wc_FreeRsaKey(&rsaKey);
  30290. wc_FreeDecodedCert(&decoded);
  30291. }
  30292. #endif /* WOLFSSL_CERT_REQ */
  30293. #endif /* NO_RSA */
  30294. #ifdef HAVE_ECC
  30295. #ifndef USE_CERT_BUFFERS_256
  30296. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  30297. AssertTrue((fp2 != XBADFILE));
  30298. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  30299. XFCLOSE(fp2);
  30300. #endif
  30301. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30302. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30303. AssertIntEQ(ret, 0);
  30304. /* good test case - ECC */
  30305. XMEMSET(keyDer, 0, sizeof(keyDer));
  30306. keyDerSz = sizeof(keyDer);
  30307. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30308. AssertIntEQ(ret, 0);
  30309. AssertIntGT(keyDerSz, 0);
  30310. /* sanity check, verify we can import DER public key */
  30311. ret = wc_ecc_init(&eccKey);
  30312. AssertIntEQ(ret, 0);
  30313. idx = 0; /* reset idx to 0, used above in RSA case */
  30314. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  30315. AssertIntEQ(ret, 0);
  30316. wc_ecc_free(&eccKey);
  30317. /* test LENGTH_ONLY_E case */
  30318. keyDerSz = 0;
  30319. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  30320. AssertIntEQ(ret, LENGTH_ONLY_E);
  30321. AssertIntGT(keyDerSz, 0);
  30322. wc_FreeDecodedCert(&decoded);
  30323. #endif
  30324. res = TEST_RES_CHECK(1);
  30325. #endif /* !NO_RSA || HAVE_ECC */
  30326. return res;
  30327. }
  30328. static int test_wc_CheckCertSigPubKey(void)
  30329. {
  30330. int res = TEST_SKIPPED;
  30331. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30332. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30333. int ret;
  30334. const char* ca_cert = "./certs/ca-cert.pem";
  30335. byte* cert_buf = NULL;
  30336. size_t cert_sz = 0;
  30337. byte* cert_der = NULL;
  30338. word32 cert_dersz = 0;
  30339. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30340. word32 keyDerSz = (word32)sizeof(keyDer);
  30341. DecodedCert decoded;
  30342. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  30343. if (ret == 0) {
  30344. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30345. cert_der = (byte*)malloc(cert_dersz);
  30346. if (cert_der) {
  30347. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  30348. cert_der, (int)cert_dersz, CERT_TYPE);
  30349. AssertIntGE(ret, 0);
  30350. }
  30351. }
  30352. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30353. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  30354. AssertIntEQ(ret, 0);
  30355. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  30356. AssertIntEQ(ret, 0);
  30357. AssertIntGT(keyDerSz, 0);
  30358. /* Good test case. */
  30359. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  30360. RSAk);
  30361. AssertIntEQ(ret, 0);
  30362. /* No certificate. */
  30363. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30364. ECDSAk);
  30365. AssertIntEQ(ret, BAD_FUNC_ARG);
  30366. /* Bad cert size. */
  30367. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30368. RSAk);
  30369. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30370. /* No public key. */
  30371. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  30372. RSAk);
  30373. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  30374. /* Bad public key size. */
  30375. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30376. RSAk);
  30377. AssertIntEQ(ret, BAD_FUNC_ARG);
  30378. /* Wrong aglo. */
  30379. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  30380. ECDSAk);
  30381. AssertIntEQ(ret, ASN_PARSE_E);
  30382. wc_FreeDecodedCert(&decoded);
  30383. if (cert_der)
  30384. free(cert_der);
  30385. if (cert_buf)
  30386. free(cert_buf);
  30387. res = TEST_RES_CHECK(1);
  30388. #endif
  30389. return res;
  30390. }
  30391. static int test_wolfSSL_certs(void)
  30392. {
  30393. int res = TEST_SKIPPED;
  30394. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30395. !defined(NO_RSA)
  30396. X509* x509ext;
  30397. #ifdef OPENSSL_ALL
  30398. X509* x509;
  30399. WOLFSSL_X509_EXTENSION* ext;
  30400. ASN1_OBJECT* obj;
  30401. #endif
  30402. WOLFSSL* ssl;
  30403. WOLFSSL_CTX* ctx;
  30404. STACK_OF(ASN1_OBJECT)* sk;
  30405. ASN1_STRING* asn1_str;
  30406. AUTHORITY_KEYID* akey;
  30407. BASIC_CONSTRAINTS* bc;
  30408. int crit;
  30409. #ifndef NO_WOLFSSL_SERVER
  30410. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30411. #else
  30412. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30413. #endif
  30414. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30415. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30416. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30417. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30418. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30419. #endif
  30420. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30421. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30422. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30423. #endif
  30424. AssertNotNull(ssl = SSL_new(ctx));
  30425. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30426. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30427. #endif
  30428. #ifdef HAVE_PK_CALLBACKS
  30429. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30430. #endif /* HAVE_PK_CALLBACKS */
  30431. /* create and use x509 */
  30432. #ifdef OPENSSL_ALL
  30433. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  30434. AssertNotNull(x509);
  30435. #endif
  30436. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  30437. AssertNotNull(x509ext);
  30438. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30439. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30440. /* with loading in a new cert the check on private key should now fail */
  30441. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30442. #endif
  30443. #if defined(USE_CERT_BUFFERS_2048)
  30444. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  30445. (unsigned char*)server_cert_der_2048,
  30446. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30447. #endif
  30448. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30449. /************* Get Digest of Certificate ******************/
  30450. {
  30451. byte digest[64]; /* max digest size */
  30452. word32 digestSz;
  30453. XMEMSET(digest, 0, sizeof(digest));
  30454. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30455. WOLFSSL_SUCCESS);
  30456. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30457. WOLFSSL_SUCCESS);
  30458. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30459. WOLFSSL_FAILURE);
  30460. }
  30461. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30462. /* test and checkout X509 extensions */
  30463. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  30464. &crit, NULL);
  30465. AssertNotNull(bc);
  30466. AssertIntEQ(crit, 0);
  30467. #ifdef OPENSSL_ALL
  30468. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  30469. AssertNotNull(ext);
  30470. X509_EXTENSION_free(ext);
  30471. AssertNotNull(ext = X509_EXTENSION_new());
  30472. X509_EXTENSION_set_critical(ext, 1);
  30473. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30474. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30475. ASN1_OBJECT_free(obj);
  30476. X509_EXTENSION_free(ext);
  30477. AssertNotNull(ext = X509_EXTENSION_new());
  30478. X509_EXTENSION_set_critical(ext, 0);
  30479. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30480. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30481. NULL);
  30482. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30483. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30484. * and X509_get_ext_d2i has created new */
  30485. X509_EXTENSION_free(ext);
  30486. #endif
  30487. BASIC_CONSTRAINTS_free(bc);
  30488. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  30489. AssertNotNull(asn1_str);
  30490. AssertIntEQ(crit, 1);
  30491. AssertIntEQ(asn1_str->type, NID_key_usage);
  30492. #ifdef OPENSSL_ALL
  30493. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  30494. AssertNotNull(ext);
  30495. X509_EXTENSION_free(ext);
  30496. #endif
  30497. ASN1_STRING_free(asn1_str);
  30498. #ifdef OPENSSL_ALL
  30499. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  30500. &crit, NULL);
  30501. AssertNotNull(sk);
  30502. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  30503. AssertNotNull(ext);
  30504. X509_EXTENSION_free(ext);
  30505. sk_ASN1_OBJECT_pop_free(sk, NULL);
  30506. #else
  30507. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30508. &crit, NULL);
  30509. AssertNull(sk);
  30510. #endif
  30511. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30512. NID_authority_key_identifier, &crit, NULL);
  30513. AssertNotNull(akey);
  30514. #ifdef OPENSSL_ALL
  30515. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  30516. AssertNotNull(ext);
  30517. X509_EXTENSION_free(ext);
  30518. #endif
  30519. wolfSSL_AUTHORITY_KEYID_free(akey);
  30520. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30521. NID_private_key_usage_period, &crit, NULL);
  30522. /* AssertNotNull(sk); NID not yet supported */
  30523. AssertIntEQ(crit, -1);
  30524. sk_ASN1_OBJECT_free(sk);
  30525. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  30526. &crit, NULL);
  30527. {
  30528. int i;
  30529. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30530. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30531. AssertIntEQ(gen->type, GEN_DNS);
  30532. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30533. }
  30534. }
  30535. /* AssertNotNull(sk); no alt names set */
  30536. sk_GENERAL_NAME_free(sk);
  30537. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  30538. &crit, NULL);
  30539. /* AssertNotNull(sk); NID not yet supported */
  30540. AssertIntEQ(crit, -1);
  30541. sk_ASN1_OBJECT_free(sk);
  30542. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  30543. NULL);
  30544. /* AssertNotNull(sk); no auth info set */
  30545. sk_ASN1_OBJECT_free(sk);
  30546. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  30547. &crit, NULL);
  30548. /* AssertNotNull(sk); NID not yet supported */
  30549. AssertIntEQ(crit, -1);
  30550. sk_ASN1_OBJECT_free(sk);
  30551. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  30552. &crit, NULL);
  30553. /* AssertNotNull(sk); NID not yet supported */
  30554. AssertIntEQ(crit, -1);
  30555. sk_ASN1_OBJECT_free(sk);
  30556. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30557. NID_certificate_policies, &crit, NULL);
  30558. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  30559. AssertNull(sk);
  30560. #else
  30561. /* AssertNotNull(sk); no cert policy set */
  30562. #endif
  30563. sk_ASN1_OBJECT_free(sk);
  30564. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  30565. &crit, NULL);
  30566. /* AssertNotNull(sk); NID not yet supported */
  30567. AssertIntEQ(crit, -1);
  30568. sk_ASN1_OBJECT_free(sk);
  30569. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  30570. &crit, NULL);
  30571. /* AssertNotNull(sk); NID not yet supported */
  30572. AssertIntEQ(crit, -1);
  30573. sk_ASN1_OBJECT_free(sk);
  30574. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  30575. &crit, NULL);
  30576. /* AssertNotNull(sk); NID not yet supported */
  30577. AssertIntEQ(crit, -1);
  30578. sk_ASN1_OBJECT_free(sk);
  30579. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  30580. NULL);
  30581. /* AssertNotNull(sk); NID not yet supported */
  30582. AssertIntEQ(crit, -1);
  30583. sk_ASN1_OBJECT_free(sk);
  30584. /* test invalid cases */
  30585. crit = 0;
  30586. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  30587. AssertNull(sk);
  30588. AssertIntEQ(crit, -1);
  30589. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  30590. NULL, NULL);
  30591. AssertNull(sk);
  30592. AssertIntEQ(SSL_get_hit(ssl), 0);
  30593. #ifdef OPENSSL_ALL
  30594. X509_free(x509);
  30595. #endif
  30596. X509_free(x509ext);
  30597. SSL_free(ssl);
  30598. SSL_CTX_free(ctx);
  30599. res = TEST_RES_CHECK(1);
  30600. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30601. return res;
  30602. }
  30603. static int test_wolfSSL_X509_check_private_key(void)
  30604. {
  30605. int res = TEST_SKIPPED;
  30606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30607. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30608. X509* x509;
  30609. EVP_PKEY* pkey = NULL;
  30610. const byte* key;
  30611. /* Check with correct key */
  30612. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30613. SSL_FILETYPE_PEM)));
  30614. key = client_key_der_2048;
  30615. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30616. &key, (long)sizeof_client_key_der_2048));
  30617. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  30618. EVP_PKEY_free(pkey);
  30619. pkey = NULL;
  30620. /* Check with wrong key */
  30621. key = server_key_der_2048;
  30622. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30623. &key, (long)sizeof_server_key_der_2048));
  30624. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  30625. /* test for incorrect parameter */
  30626. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  30627. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  30628. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  30629. EVP_PKEY_free(pkey);
  30630. X509_free(x509);
  30631. res = TEST_RES_CHECK(1);
  30632. #endif
  30633. return res;
  30634. }
  30635. static int test_wolfSSL_private_keys(void)
  30636. {
  30637. int res = TEST_SKIPPED;
  30638. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30639. !defined(NO_FILESYSTEM)
  30640. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30641. WOLFSSL* ssl;
  30642. WOLFSSL_CTX* ctx;
  30643. EVP_PKEY* pkey = NULL;
  30644. OpenSSL_add_all_digests();
  30645. OpenSSL_add_all_algorithms();
  30646. #ifndef NO_RSA
  30647. #ifndef NO_WOLFSSL_SERVER
  30648. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30649. #else
  30650. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30651. #endif
  30652. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30653. /* Have to load a cert before you can check the private key against that
  30654. * certificates public key! */
  30655. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30656. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30657. #endif
  30658. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30659. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30660. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30661. #endif
  30662. AssertNotNull(ssl = SSL_new(ctx));
  30663. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30664. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30665. #endif
  30666. #ifdef USE_CERT_BUFFERS_2048
  30667. {
  30668. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30669. unsigned char buf[FOURK_BUF];
  30670. word32 bufSz;
  30671. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30672. (unsigned char*)client_key_der_2048,
  30673. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30674. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30675. /* Should mismatch now that a different private key loaded */
  30676. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30677. #endif
  30678. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30679. (unsigned char*)server_key,
  30680. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30681. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30682. /* After loading back in DER format of original key, should match */
  30683. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30684. #endif
  30685. /* test loading private key to the WOLFSSL_CTX */
  30686. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30687. (unsigned char*)client_key_der_2048,
  30688. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30689. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30690. /* Should mismatch now that a different private key loaded */
  30691. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30692. #endif
  30693. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30694. (unsigned char*)server_key,
  30695. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30696. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30697. /* After loading back in DER format of original key, should match */
  30698. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30699. #endif
  30700. /* pkey not set yet, expecting to fail */
  30701. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30702. /* set PKEY and test again */
  30703. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30704. &server_key, (long)sizeof_server_key_der_2048));
  30705. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30706. /* reuse PKEY structure and test
  30707. * this should be checked with a memory management sanity checker */
  30708. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  30709. server_key = (const unsigned char*)server_key_der_2048;
  30710. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30711. &server_key, (long)sizeof_server_key_der_2048));
  30712. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30713. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30714. bufSz = FOURK_BUF;
  30715. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30716. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30717. RSAk, NULL, 0)), 0);
  30718. server_key = (const unsigned char*)buf;
  30719. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30720. (long)bufSz));
  30721. }
  30722. #endif
  30723. EVP_PKEY_free(pkey);
  30724. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30725. SSL_CTX_free(ctx);
  30726. #endif /* end of RSA private key match tests */
  30727. #ifdef HAVE_ECC
  30728. #ifndef NO_WOLFSSL_SERVER
  30729. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30730. #else
  30731. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30732. #endif
  30733. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30734. WOLFSSL_FILETYPE_PEM));
  30735. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30736. WOLFSSL_FILETYPE_PEM));
  30737. AssertNotNull(ssl = SSL_new(ctx));
  30738. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30739. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30740. #endif
  30741. SSL_free(ssl);
  30742. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30743. WOLFSSL_FILETYPE_PEM));
  30744. AssertNotNull(ssl = SSL_new(ctx));
  30745. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30746. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30747. #endif
  30748. SSL_free(ssl);
  30749. SSL_CTX_free(ctx);
  30750. #endif /* end of ECC private key match tests */
  30751. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30752. #ifndef NO_WOLFSSL_SERVER
  30753. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30754. #else
  30755. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30756. #endif
  30757. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30758. WOLFSSL_FILETYPE_PEM));
  30759. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30760. WOLFSSL_FILETYPE_PEM));
  30761. AssertNotNull(ssl = SSL_new(ctx));
  30762. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30763. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30764. #endif
  30765. SSL_free(ssl);
  30766. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30767. WOLFSSL_FILETYPE_PEM));
  30768. AssertNotNull(ssl = SSL_new(ctx));
  30769. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30770. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30771. #endif
  30772. SSL_free(ssl);
  30773. SSL_CTX_free(ctx);
  30774. #endif /* end of Ed25519 private key match tests */
  30775. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30776. #ifndef NO_WOLFSSL_SERVER
  30777. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30778. #else
  30779. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30780. #endif
  30781. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30782. WOLFSSL_FILETYPE_PEM));
  30783. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30784. WOLFSSL_FILETYPE_PEM));
  30785. AssertNotNull(ssl = SSL_new(ctx));
  30786. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30787. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30788. #endif
  30789. SSL_free(ssl);
  30790. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30791. WOLFSSL_FILETYPE_PEM));
  30792. AssertNotNull(ssl = SSL_new(ctx));
  30793. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  30794. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30795. #endif
  30796. SSL_free(ssl);
  30797. SSL_CTX_free(ctx);
  30798. #endif /* end of Ed448 private key match tests */
  30799. EVP_cleanup();
  30800. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30801. CONF_modules_free();
  30802. ENGINE_cleanup();
  30803. CONF_modules_unload();
  30804. (void)ssl;
  30805. (void)ctx;
  30806. (void)pkey;
  30807. res = TEST_RES_CHECK(1);
  30808. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30809. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30810. return res;
  30811. }
  30812. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30813. {
  30814. int res = TEST_SKIPPED;
  30815. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30816. && !defined(NO_FILESYSTEM)
  30817. XFILE file;
  30818. const char* fname = "./certs/server-key.pem";
  30819. EVP_PKEY* pkey;
  30820. RSA* rsa;
  30821. WOLFSSL_EVP_PKEY_CTX* ctx;
  30822. unsigned char* sig;
  30823. size_t sigLen;
  30824. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30825. size_t tbsLen = sizeof(tbs);
  30826. /* Check error case. */
  30827. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30828. /* Read in an RSA key. */
  30829. file = XFOPEN(fname, "rb");
  30830. AssertTrue(file != XBADFILE);
  30831. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30832. XFCLOSE(file);
  30833. /* Make sure the key is usable by signing some data with it. */
  30834. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30835. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  30836. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30837. DYNAMIC_TYPE_TMP_BUFFER));
  30838. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30839. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30840. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30841. WOLFSSL_SUCCESS);
  30842. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30843. EVP_PKEY_CTX_free(ctx);
  30844. EVP_PKEY_free(pkey);
  30845. res = TEST_RES_CHECK(1);
  30846. #endif
  30847. return res;
  30848. }
  30849. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30850. {
  30851. int res = TEST_SKIPPED;
  30852. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30853. && !defined(NO_FILESYSTEM)
  30854. XFILE file;
  30855. const char* fname = "./certs/client-keyPub.pem";
  30856. EVP_PKEY* pkey;
  30857. /* Check error case. */
  30858. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30859. /* Read in an RSA key. */
  30860. file = XFOPEN(fname, "rb");
  30861. AssertTrue(file != XBADFILE);
  30862. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30863. EVP_PKEY_free(pkey);
  30864. XFCLOSE(file);
  30865. res = TEST_RES_CHECK(1);
  30866. #endif
  30867. return res;
  30868. }
  30869. static int test_wolfSSL_PEM_PrivateKey(void)
  30870. {
  30871. int res = TEST_SKIPPED;
  30872. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30873. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30874. #ifndef NO_BIO
  30875. BIO* bio = NULL;
  30876. #endif
  30877. EVP_PKEY* pkey = NULL;
  30878. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30879. #ifndef NO_BIO
  30880. /* test creating new EVP_PKEY with bad arg */
  30881. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30882. /* test loading RSA key using BIO */
  30883. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30884. {
  30885. XFILE file;
  30886. const char* fname = "./certs/server-key.pem";
  30887. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30888. size_t sz;
  30889. byte* buf;
  30890. EVP_PKEY* pkey2;
  30891. EVP_PKEY* pkey3;
  30892. RSA* rsa_key = NULL;
  30893. file = XFOPEN(fname, "rb");
  30894. AssertTrue((file != XBADFILE));
  30895. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30896. sz = XFTELL(file);
  30897. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30898. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30899. if (buf) {
  30900. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30901. }
  30902. XFCLOSE(file);
  30903. /* Test using BIO new mem and loading PEM private key */
  30904. bio = BIO_new_mem_buf(buf, (int)sz);
  30905. AssertNotNull(bio);
  30906. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30907. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30908. BIO_free(bio);
  30909. bio = NULL;
  30910. AssertNotNull(pkey2 = EVP_PKEY_new());
  30911. pkey2->type = EVP_PKEY_RSA;
  30912. /* Test parameter copy */
  30913. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30914. EVP_PKEY_free(pkey2);
  30915. EVP_PKEY_free(pkey);
  30916. pkey = NULL;
  30917. /* Qt unit test case : rsa pkcs8 key */
  30918. file = XFOPEN(fname_rsa_p8, "rb");
  30919. AssertTrue((file != XBADFILE));
  30920. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30921. sz = XFTELL(file);
  30922. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30923. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30924. if (buf)
  30925. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30926. XFCLOSE(file);
  30927. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30928. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30929. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30930. BIO_free(bio);
  30931. bio = NULL;
  30932. AssertNotNull(pkey3 = EVP_PKEY_new());
  30933. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30934. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30935. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30936. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30937. #else
  30938. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30939. #endif
  30940. RSA_free(rsa_key);
  30941. EVP_PKEY_free(pkey3);
  30942. EVP_PKEY_free(pkey);
  30943. pkey = NULL;
  30944. }
  30945. #endif
  30946. /* test loading ECC key using BIO */
  30947. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30948. {
  30949. XFILE file;
  30950. const char* fname = "./certs/ecc-key.pem";
  30951. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30952. size_t sz;
  30953. byte* buf;
  30954. EVP_PKEY* pkey2;
  30955. EVP_PKEY* pkey3;
  30956. EC_KEY* ec_key;
  30957. int nid = 0;
  30958. file = XFOPEN(fname, "rb");
  30959. AssertTrue((file != XBADFILE));
  30960. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30961. sz = XFTELL(file);
  30962. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30963. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30964. if (buf)
  30965. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  30966. XFCLOSE(file);
  30967. /* Test using BIO new mem and loading PEM private key */
  30968. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30969. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30970. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30971. BIO_free(bio);
  30972. bio = NULL;
  30973. AssertNotNull(pkey2 = EVP_PKEY_new());
  30974. AssertNotNull(pkey3 = EVP_PKEY_new());
  30975. pkey2->type = EVP_PKEY_EC;
  30976. /* Test parameter copy */
  30977. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30978. /* Qt unit test case 1*/
  30979. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30980. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30981. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30982. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30983. #else
  30984. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30985. #endif
  30986. /* Test default digest */
  30987. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30988. AssertIntEQ(nid, NID_sha256);
  30989. EC_KEY_free(ec_key);
  30990. EVP_PKEY_free(pkey3);
  30991. EVP_PKEY_free(pkey2);
  30992. EVP_PKEY_free(pkey);
  30993. pkey = NULL;
  30994. /* Qt unit test case ec pkcs8 key */
  30995. file = XFOPEN(fname_ecc_p8, "rb");
  30996. AssertTrue((file != XBADFILE));
  30997. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30998. sz = XFTELL(file);
  30999. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31000. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31001. if (buf)
  31002. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  31003. XFCLOSE(file);
  31004. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31005. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31006. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31007. BIO_free(bio);
  31008. bio = NULL;
  31009. AssertNotNull(pkey3 = EVP_PKEY_new());
  31010. /* Qt unit test case */
  31011. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31012. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31013. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31014. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31015. #else
  31016. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31017. #endif
  31018. EC_KEY_free(ec_key);
  31019. EVP_PKEY_free(pkey3);
  31020. EVP_PKEY_free(pkey);
  31021. pkey = NULL;
  31022. }
  31023. #endif
  31024. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  31025. defined(WOLFSSL_CERT_GEN))
  31026. {
  31027. #define BIO_PEM_TEST_CHAR 'a'
  31028. EVP_PKEY* pkey2 = NULL;
  31029. unsigned char extra[10];
  31030. int i;
  31031. BIO* pub_bio = NULL;
  31032. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  31033. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31034. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  31035. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31036. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  31037. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  31038. &server_key, (long)sizeof_server_key_der_2048));
  31039. AssertNull(pkey);
  31040. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31041. &server_key, (long)sizeof_server_key_der_2048));
  31042. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  31043. NULL), WOLFSSL_FAILURE);
  31044. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31045. NULL), WOLFSSL_FAILURE);
  31046. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31047. NULL), WOLFSSL_SUCCESS);
  31048. AssertIntGT(BIO_pending(bio), 0);
  31049. AssertIntEQ(BIO_pending(bio), 1679);
  31050. /* Check if the pubkey API writes only the public key */
  31051. #ifdef WOLFSSL_KEY_GEN
  31052. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  31053. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  31054. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  31055. AssertIntGT(BIO_pending(pub_bio), 0);
  31056. /* Previously both the private key and the pubkey calls would write
  31057. * out the private key and the PEM header was the only difference.
  31058. * The public PEM should be significantly shorter than the
  31059. * private key versison. */
  31060. AssertIntEQ(BIO_pending(pub_bio), 451);
  31061. #endif
  31062. /* test creating new EVP_PKEY with good args */
  31063. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31064. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  31065. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  31066. /* test of reuse of EVP_PKEY */
  31067. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31068. AssertIntEQ(BIO_pending(bio), 0);
  31069. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31070. SSL_SUCCESS);
  31071. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  31072. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  31073. AssertNotNull(pkey);
  31074. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  31075. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  31076. }
  31077. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  31078. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  31079. for (i = 0; i < 10; i++) {
  31080. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  31081. }
  31082. BIO_free(pub_bio);
  31083. BIO_free(bio);
  31084. bio = NULL;
  31085. EVP_PKEY_free(pkey);
  31086. pkey = NULL;
  31087. EVP_PKEY_free(pkey2);
  31088. }
  31089. #endif
  31090. /* key is DES encrypted */
  31091. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31092. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31093. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  31094. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  31095. {
  31096. XFILE f;
  31097. wc_pem_password_cb* passwd_cb;
  31098. void* passwd_cb_userdata;
  31099. SSL_CTX* ctx;
  31100. char passwd[] = "bad password";
  31101. #ifndef WOLFSSL_NO_TLS12
  31102. #ifndef NO_WOLFSSL_SERVER
  31103. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31104. #else
  31105. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31106. #endif
  31107. #else
  31108. #ifndef NO_WOLFSSL_SERVER
  31109. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31110. #else
  31111. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31112. #endif
  31113. #endif
  31114. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31115. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31116. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31117. AssertNull(passwd_cb_userdata =
  31118. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31119. /* fail case with password call back */
  31120. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31121. (void*)passwd));
  31122. BIO_free(bio);
  31123. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31124. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31125. (void*)passwd));
  31126. BIO_free(bio);
  31127. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  31128. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31129. /* use callback that works */
  31130. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31131. (void*)"yassl123"));
  31132. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31133. EVP_PKEY_free(pkey);
  31134. pkey = NULL;
  31135. BIO_free(bio);
  31136. bio = NULL;
  31137. SSL_CTX_free(ctx);
  31138. }
  31139. #endif /* !defined(NO_DES3) */
  31140. #endif /* !NO_BIO */
  31141. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31142. {
  31143. unsigned char buf[2048];
  31144. size_t bytes;
  31145. XFILE f;
  31146. SSL_CTX* ctx;
  31147. #ifndef WOLFSSL_NO_TLS12
  31148. #ifndef NO_WOLFSSL_SERVER
  31149. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31150. #else
  31151. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31152. #endif
  31153. #else
  31154. #ifndef NO_WOLFSSL_SERVER
  31155. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31156. #else
  31157. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31158. #endif
  31159. #endif
  31160. f = XFOPEN("./certs/ecc-key.der", "rb");
  31161. AssertTrue((f != XBADFILE));
  31162. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  31163. XFCLOSE(f);
  31164. server_key = buf;
  31165. pkey = NULL;
  31166. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31167. AssertNull(pkey);
  31168. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31169. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31170. EVP_PKEY_free(pkey);
  31171. pkey = NULL;
  31172. SSL_CTX_free(ctx);
  31173. }
  31174. #endif
  31175. res = TEST_RES_CHECK(1);
  31176. #ifndef NO_BIO
  31177. (void)bio;
  31178. #endif
  31179. (void)pkey;
  31180. (void)server_key;
  31181. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31182. return res;
  31183. }
  31184. static int test_wolfSSL_PEM_file_RSAKey(void)
  31185. {
  31186. int res = TEST_SKIPPED;
  31187. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31188. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31189. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31190. RSA* rsa = NULL;
  31191. XFILE fp;
  31192. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31193. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31194. XFCLOSE(fp);
  31195. AssertIntEQ(RSA_size(rsa), 256);
  31196. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31197. AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31198. AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31199. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31200. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31201. AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31202. RSA_free(rsa);
  31203. res = TEST_RES_CHECK(1);
  31204. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31205. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31206. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31207. return res;
  31208. }
  31209. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31210. {
  31211. int res = TEST_SKIPPED;
  31212. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31213. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  31214. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31215. RSA* rsa = NULL;
  31216. XFILE f = NULL;
  31217. f = XFOPEN(svrKeyFile, "r");
  31218. AssertTrue((f != XBADFILE));
  31219. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31220. AssertIntEQ(RSA_size(rsa), 256);
  31221. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31222. NULL), WOLFSSL_FAILURE);
  31223. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31224. NULL), WOLFSSL_FAILURE);
  31225. AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31226. WOLFSSL_SUCCESS);
  31227. RSA_free(rsa);
  31228. XFCLOSE(f);
  31229. #ifdef HAVE_ECC
  31230. f = XFOPEN(eccKeyFile, "r");
  31231. AssertTrue((f != XBADFILE));
  31232. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31233. XFCLOSE(f);
  31234. #endif /* HAVE_ECC */
  31235. res = TEST_RES_CHECK(1);
  31236. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31237. return res;
  31238. }
  31239. #ifndef NO_BIO
  31240. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31241. {
  31242. int res = TEST_SKIPPED;
  31243. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31244. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31245. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31246. RSA* rsa = NULL;
  31247. BIO* bio = NULL;
  31248. /* PrivateKey */
  31249. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31250. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31251. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31252. AssertNotNull(rsa);
  31253. AssertIntEQ(RSA_size(rsa), 256);
  31254. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31255. NULL), WOLFSSL_FAILURE);
  31256. BIO_free(bio);
  31257. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31258. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31259. NULL), WOLFSSL_SUCCESS);
  31260. BIO_free(bio);
  31261. RSA_free(rsa);
  31262. /* PUBKEY */
  31263. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31264. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31265. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31266. AssertIntEQ(RSA_size(rsa), 256);
  31267. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31268. BIO_free(bio);
  31269. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31270. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31271. BIO_free(bio);
  31272. RSA_free(rsa);
  31273. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31274. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31275. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31276. BIO_free(bio);
  31277. RSA_free(rsa);
  31278. #ifdef HAVE_ECC
  31279. /* ensure that non-rsa keys do not work */
  31280. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31281. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31282. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31283. BIO_free(bio);
  31284. RSA_free(rsa);
  31285. #endif /* HAVE_ECC */
  31286. res = TEST_RES_CHECK(1);
  31287. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31288. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31289. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31290. return res;
  31291. }
  31292. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31293. {
  31294. int res = TEST_SKIPPED;
  31295. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31296. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31297. RSA* rsa = NULL;
  31298. RSA* rsa_dup = NULL;
  31299. BIO* bio = NULL;
  31300. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31301. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31302. AssertIntEQ(RSA_size(rsa), 256);
  31303. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  31304. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  31305. /* Test duplicating empty key. */
  31306. rsa_dup = RSA_new();
  31307. AssertNull(RSAPublicKey_dup(rsa_dup));
  31308. RSA_free(rsa_dup);
  31309. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31310. AssertPtrNE(rsa_dup, rsa);
  31311. #endif
  31312. /* test if valgrind complains about unreleased memory */
  31313. RSA_up_ref(rsa);
  31314. RSA_free(rsa);
  31315. BIO_free(bio);
  31316. RSA_free(rsa);
  31317. RSA_free(rsa_dup);
  31318. #ifdef HAVE_ECC
  31319. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31320. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31321. BIO_free(bio);
  31322. #endif /* HAVE_ECC */
  31323. res = TEST_RES_CHECK(1);
  31324. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31325. return res;
  31326. }
  31327. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31328. {
  31329. int res = TEST_SKIPPED;
  31330. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31331. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31332. XFILE file;
  31333. const char* fname = "./certs/client-keyPub.pem";
  31334. RSA *rsa;
  31335. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31336. file = XFOPEN(fname, "rb");
  31337. AssertTrue((file != XBADFILE));
  31338. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31339. AssertIntEQ(RSA_size(rsa), 256);
  31340. RSA_free(rsa);
  31341. XFCLOSE(file);
  31342. res = TEST_RES_CHECK(1);
  31343. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31344. return res;
  31345. }
  31346. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31347. {
  31348. int res = TEST_SKIPPED;
  31349. #ifndef HAVE_SELFTEST
  31350. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31351. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31352. DSA* dsa = NULL;
  31353. BIO* bio = NULL;
  31354. /* PrivateKey */
  31355. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31356. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31357. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31358. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  31359. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  31360. WOLFSSL_FAILURE);
  31361. BIO_free(bio);
  31362. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31363. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  31364. WOLFSSL_SUCCESS);
  31365. BIO_free(bio);
  31366. DSA_free(dsa);
  31367. /* PUBKEY */
  31368. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31369. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31370. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31371. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  31372. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31373. BIO_free(bio);
  31374. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31375. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31376. BIO_free(bio);
  31377. DSA_free(dsa);
  31378. #ifdef HAVE_ECC
  31379. /* ensure that non-dsa keys do not work */
  31380. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31381. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31382. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31383. BIO_free(bio);
  31384. DSA_free(dsa);
  31385. #endif /* HAVE_ECC */
  31386. res = TEST_RES_CHECK(1);
  31387. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31388. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31389. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31390. #endif /* HAVE_SELFTEST */
  31391. return res;
  31392. }
  31393. static int test_wolfSSL_PEM_bio_ECKey(void)
  31394. {
  31395. int res = TEST_SKIPPED;
  31396. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31397. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31398. EC_KEY* ec = NULL;
  31399. EC_KEY* ec2;
  31400. BIO* bio = NULL;
  31401. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31402. unsigned char* pem = NULL;
  31403. int pLen;
  31404. #endif
  31405. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31406. "MAA=\n"
  31407. "-----END PUBLIC KEY-----";
  31408. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31409. "MAA=\n"
  31410. "-----END EC PRIVATE KEY-----";
  31411. /* PrivateKey */
  31412. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31413. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31414. ec2 = NULL;
  31415. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31416. AssertIntEQ(ec == ec2, 1);
  31417. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31418. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31419. NULL), WOLFSSL_FAILURE);
  31420. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31421. NULL), WOLFSSL_FAILURE);
  31422. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31423. WOLFSSL_FAILURE);
  31424. BIO_free(bio);
  31425. /* Public key data - fail. */
  31426. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31427. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31428. BIO_free(bio);
  31429. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31430. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31431. NULL), WOLFSSL_SUCCESS);
  31432. BIO_free(bio);
  31433. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31434. NULL),WOLFSSL_FAILURE);
  31435. AssertIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31436. WOLFSSL_FAILURE);
  31437. AssertIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31438. WOLFSSL_FAILURE);
  31439. AssertIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31440. WOLFSSL_SUCCESS);
  31441. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31442. NULL), 0);
  31443. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31444. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31445. NULL), 0);
  31446. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31447. NULL), 0);
  31448. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31449. &pLen), 0);
  31450. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31451. &pLen), 0);
  31452. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31453. &pLen), 0);
  31454. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31455. NULL), 0);
  31456. AssertIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31457. &pLen), 1);
  31458. AssertIntGT(pLen, 0);
  31459. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31460. #endif
  31461. EC_KEY_free(ec);
  31462. /* PUBKEY */
  31463. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31464. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31465. ec2 = NULL;
  31466. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31467. AssertIntEQ(ec == ec2, 1);
  31468. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31469. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31470. BIO_free(bio);
  31471. /* Test 0x30, 0x00 fails. */
  31472. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31473. sizeof(ec_key_bad_1)));
  31474. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31475. BIO_free(bio);
  31476. /* Private key data - fail. */
  31477. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31478. AssertNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31479. BIO_free(bio);
  31480. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31481. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31482. BIO_free(bio);
  31483. /* Same test as above, but with a file pointer rather than a BIO. */
  31484. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31485. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31486. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31487. AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31488. EC_KEY_free(ec);
  31489. #ifndef NO_RSA
  31490. /* ensure that non-ec keys do not work */
  31491. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31492. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31493. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31494. BIO_free(bio);
  31495. EC_KEY_free(ec);
  31496. #endif /* HAVE_ECC */
  31497. /* Test 0x30, 0x00 fails. */
  31498. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31499. sizeof(ec_priv_key_bad_1)));
  31500. AssertNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31501. BIO_free(bio);
  31502. res = TEST_RES_CHECK(1);
  31503. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31504. return res;
  31505. }
  31506. static int test_wolfSSL_PEM_PUBKEY(void)
  31507. {
  31508. int res = TEST_SKIPPED;
  31509. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31510. BIO* bio = NULL;
  31511. EVP_PKEY* pkey = NULL;
  31512. /* test creating new EVP_PKEY with bad arg */
  31513. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31514. /* test loading ECC key using BIO */
  31515. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31516. {
  31517. XFILE file;
  31518. const char* fname = "./certs/ecc-client-keyPub.pem";
  31519. size_t sz;
  31520. byte* buf;
  31521. EVP_PKEY* pkey2;
  31522. EC_KEY* ec_key;
  31523. file = XFOPEN(fname, "rb");
  31524. AssertTrue((file != XBADFILE));
  31525. AssertIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31526. sz = XFTELL(file);
  31527. AssertIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31528. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31529. if (buf)
  31530. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  31531. XFCLOSE(file);
  31532. /* Test using BIO new mem and loading PEM private key */
  31533. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31534. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31535. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31536. BIO_free(bio);
  31537. bio = NULL;
  31538. /* Qt unit test case*/
  31539. AssertNotNull(pkey2 = EVP_PKEY_new());
  31540. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31541. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31542. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31543. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31544. #else
  31545. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31546. #endif
  31547. EC_KEY_free(ec_key);
  31548. EVP_PKEY_free(pkey2);
  31549. EVP_PKEY_free(pkey);
  31550. pkey = NULL;
  31551. }
  31552. #endif
  31553. (void)bio;
  31554. (void)pkey;
  31555. res = TEST_RES_CHECK(1);
  31556. #endif
  31557. return res;
  31558. }
  31559. #endif /* !NO_BIO */
  31560. static int test_DSA_do_sign_verify(void)
  31561. {
  31562. int res = TEST_SKIPPED;
  31563. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31564. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31565. !defined(NO_DSA)
  31566. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31567. DSA_SIG* sig;
  31568. DSA* dsa;
  31569. word32 bytes;
  31570. byte sigBin[DSA_SIG_SIZE];
  31571. int dsacheck;
  31572. #ifdef USE_CERT_BUFFERS_1024
  31573. byte tmp[ONEK_BUF];
  31574. XMEMSET(tmp, 0, sizeof(tmp));
  31575. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31576. bytes = sizeof_dsa_key_der_1024;
  31577. #elif defined(USE_CERT_BUFFERS_2048)
  31578. byte tmp[TWOK_BUF];
  31579. XMEMSET(tmp, 0, sizeof(tmp));
  31580. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31581. bytes = sizeof_dsa_key_der_2048;
  31582. #else
  31583. byte tmp[TWOK_BUF];
  31584. XMEMSET(tmp, 0, sizeof(tmp));
  31585. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  31586. if (fp == XBADFILE) {
  31587. return WOLFSSL_BAD_FILE;
  31588. }
  31589. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  31590. XFCLOSE(fp);
  31591. #endif /* END USE_CERT_BUFFERS_1024 */
  31592. XMEMSET(digest, 202, sizeof(digest));
  31593. AssertNotNull(dsa = DSA_new());
  31594. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31595. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31596. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31597. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31598. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31599. DSA_SIG_free(sig);
  31600. DSA_free(dsa);
  31601. res = TEST_RES_CHECK(1);
  31602. #endif
  31603. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31604. return res;
  31605. }
  31606. static int test_wolfSSL_tmp_dh(void)
  31607. {
  31608. int res = TEST_SKIPPED;
  31609. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31610. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31611. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31612. byte buff[6000];
  31613. char file[] = "./certs/dsaparams.pem";
  31614. XFILE f;
  31615. int bytes;
  31616. DSA* dsa;
  31617. DH* dh;
  31618. #if defined(WOLFSSL_DH_EXTRA) && \
  31619. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31620. DH* dh2;
  31621. #endif
  31622. BIO* bio;
  31623. SSL* ssl;
  31624. SSL_CTX* ctx;
  31625. #ifndef NO_WOLFSSL_SERVER
  31626. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31627. #else
  31628. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31629. #endif
  31630. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  31631. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  31632. AssertNotNull(ssl = SSL_new(ctx));
  31633. f = XFOPEN(file, "rb");
  31634. AssertTrue((f != XBADFILE));
  31635. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  31636. XFCLOSE(f);
  31637. bio = BIO_new_mem_buf((void*)buff, bytes);
  31638. AssertNotNull(bio);
  31639. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31640. AssertNotNull(dsa);
  31641. dh = wolfSSL_DSA_dup_DH(dsa);
  31642. AssertNotNull(dh);
  31643. #if defined(WOLFSSL_DH_EXTRA) && \
  31644. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31645. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  31646. #endif
  31647. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31648. #ifndef NO_WOLFSSL_SERVER
  31649. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31650. #else
  31651. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31652. #endif
  31653. BIO_free(bio);
  31654. DSA_free(dsa);
  31655. DH_free(dh);
  31656. #if defined(WOLFSSL_DH_EXTRA) && \
  31657. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31658. DH_free(dh2);
  31659. #endif
  31660. SSL_free(ssl);
  31661. SSL_CTX_free(ctx);
  31662. res = TEST_RES_CHECK(1);
  31663. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31664. #endif
  31665. return res;
  31666. }
  31667. static int test_wolfSSL_ctrl(void)
  31668. {
  31669. int res = TEST_SKIPPED;
  31670. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31671. byte buff[6000];
  31672. BIO* bio;
  31673. int bytes;
  31674. BUF_MEM* ptr = NULL;
  31675. XMEMSET(buff, 0, sizeof(buff));
  31676. bytes = sizeof(buff);
  31677. bio = BIO_new_mem_buf((void*)buff, bytes);
  31678. AssertNotNull(bio);
  31679. AssertNotNull(BIO_s_socket());
  31680. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31681. /* needs tested after stubs filled out @TODO
  31682. SSL_ctrl
  31683. SSL_CTX_ctrl
  31684. */
  31685. BIO_free(bio);
  31686. res = TEST_RES_CHECK(1);
  31687. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31688. return res;
  31689. }
  31690. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31691. {
  31692. int res = TEST_SKIPPED;
  31693. #ifdef OPENSSL_EXTRA
  31694. static const unsigned char pw[] = "password";
  31695. static const int pwSz = sizeof(pw) - 1;
  31696. size_t checkPwSz = 0;
  31697. const unsigned char* checkPw = NULL;
  31698. WOLFSSL_EVP_PKEY* key = NULL;
  31699. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31700. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31701. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  31702. if (key) {
  31703. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  31704. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  31705. AssertIntEQ(key->pkey_sz, pwSz);
  31706. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31707. }
  31708. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31709. AssertIntEQ((int)checkPwSz, pwSz);
  31710. if (checkPw) {
  31711. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31712. }
  31713. wolfSSL_EVP_PKEY_free(key);
  31714. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  31715. if (key) {
  31716. AssertIntEQ(key->pkey_sz, 0);
  31717. }
  31718. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31719. (void)checkPw;
  31720. AssertIntEQ((int)checkPwSz, 0);
  31721. wolfSSL_EVP_PKEY_free(key);
  31722. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  31723. if (key) {
  31724. AssertIntEQ(key->pkey_sz, 0);
  31725. }
  31726. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31727. (void)checkPw;
  31728. AssertIntEQ((int)checkPwSz, 0);
  31729. wolfSSL_EVP_PKEY_free(key);
  31730. res = TEST_RES_CHECK(1);
  31731. #endif /* OPENSSL_EXTRA */
  31732. return res;
  31733. }
  31734. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31735. {
  31736. int res = TEST_SKIPPED;
  31737. #ifdef OPENSSL_EXTRA
  31738. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31739. const char *priv = "ABCDEFGHIJKLMNOP";
  31740. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31741. WOLFSSL_EVP_PKEY* key = NULL;
  31742. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31743. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31744. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31745. NULL, (const unsigned char *)priv, 0, cipher));
  31746. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31747. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31748. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31749. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31750. wolfSSL_EVP_PKEY_free(key);
  31751. res = TEST_RES_CHECK(1);
  31752. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  31753. #endif /* OPENSSL_EXTRA */
  31754. return res;
  31755. }
  31756. static int test_wolfSSL_EVP_Digest(void)
  31757. {
  31758. int res = TEST_SKIPPED;
  31759. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31760. const char* in = "abc";
  31761. int inLen = (int)XSTRLEN(in);
  31762. byte out[WC_SHA256_DIGEST_SIZE];
  31763. unsigned int outLen;
  31764. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31765. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31766. "\x15\xAD";
  31767. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  31768. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31769. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31770. res = TEST_RES_CHECK(1);
  31771. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31772. return res;
  31773. }
  31774. static int test_wolfSSL_EVP_Digest_all(void)
  31775. {
  31776. int res = TEST_SKIPPED;
  31777. #ifdef OPENSSL_EXTRA
  31778. const char* digests[] = {
  31779. #ifndef NO_MD5
  31780. "MD5",
  31781. #endif
  31782. #ifndef NO_SHA
  31783. "SHA",
  31784. #endif
  31785. #ifdef WOLFSSL_SHA224
  31786. "SHA224",
  31787. #endif
  31788. #ifndef NO_SHA256
  31789. "SHA256",
  31790. #endif
  31791. #ifdef WOLFSSL_SHA384
  31792. "SHA384",
  31793. #endif
  31794. #ifdef WOLFSSL_SHA512
  31795. "SHA512",
  31796. #endif
  31797. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31798. "SHA512_224",
  31799. #endif
  31800. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31801. "SHA512_256",
  31802. #endif
  31803. #ifdef WOLFSSL_SHA3
  31804. #ifndef WOLFSSL_NOSHA3_224
  31805. "SHA3_224",
  31806. #endif
  31807. #ifndef WOLFSSL_NOSHA3_256
  31808. "SHA3_256",
  31809. #endif
  31810. "SHA3_384",
  31811. #ifndef WOLFSSL_NOSHA3_512
  31812. "SHA3_512",
  31813. #endif
  31814. #endif /* WOLFSSL_SHA3 */
  31815. NULL
  31816. };
  31817. const char** d;
  31818. const unsigned char in[] = "abc";
  31819. int inLen = XSTR_SIZEOF(in);
  31820. byte out[WC_MAX_DIGEST_SIZE];
  31821. unsigned int outLen;
  31822. for (d = digests; *d != NULL; d++) {
  31823. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31824. AssertIntGT(outLen, 0);
  31825. AssertIntEQ(EVP_MD_size(*d), outLen);
  31826. }
  31827. res = TEST_RES_CHECK(1);
  31828. #endif
  31829. return res;
  31830. }
  31831. static int test_wolfSSL_EVP_MD_size(void)
  31832. {
  31833. int res = TEST_SKIPPED;
  31834. #ifdef OPENSSL_EXTRA
  31835. WOLFSSL_EVP_MD_CTX mdCtx;
  31836. #ifdef WOLFSSL_SHA3
  31837. #ifndef WOLFSSL_NOSHA3_224
  31838. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31839. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31840. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31841. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31842. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31843. #endif
  31844. #ifndef WOLFSSL_NOSHA3_256
  31845. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31846. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31847. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31848. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31849. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31850. #endif
  31851. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31852. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31853. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31854. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31855. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31856. #ifndef WOLFSSL_NOSHA3_512
  31857. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31858. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31859. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31860. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31861. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31862. #endif
  31863. #endif /* WOLFSSL_SHA3 */
  31864. #ifndef NO_SHA256
  31865. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31866. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31867. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  31868. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  31869. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31870. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31871. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31872. #endif
  31873. #ifndef NO_MD5
  31874. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31875. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31876. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  31877. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  31878. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31879. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31880. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31881. #endif
  31882. #ifdef WOLFSSL_SHA224
  31883. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31884. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31885. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  31886. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  31887. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31888. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31889. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31890. #endif
  31891. #ifdef WOLFSSL_SHA384
  31892. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31893. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31894. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  31895. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  31896. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31897. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31898. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31899. #endif
  31900. #ifdef WOLFSSL_SHA512
  31901. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31902. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31903. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  31904. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  31905. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31906. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31907. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31908. #endif
  31909. #ifndef NO_SHA
  31910. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31911. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31912. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  31913. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  31914. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31915. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31916. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31917. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31918. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31919. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  31920. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  31921. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31922. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31923. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31924. #endif
  31925. /* error case */
  31926. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31927. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31928. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  31929. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31930. /* Cleanup is valid on uninit'ed struct */
  31931. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31932. res = TEST_RES_CHECK(1);
  31933. #endif /* OPENSSL_EXTRA */
  31934. return res;
  31935. }
  31936. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31937. {
  31938. int res = TEST_SKIPPED;
  31939. #ifdef OPENSSL_EXTRA
  31940. const WOLFSSL_EVP_MD* md;
  31941. #ifndef NO_MD5
  31942. AssertNotNull(md = EVP_md5());
  31943. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31944. #endif
  31945. #ifndef NO_SHA
  31946. AssertNotNull(md = EVP_sha1());
  31947. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31948. #endif
  31949. #ifdef WOLFSSL_SHA224
  31950. AssertNotNull(md = EVP_sha224());
  31951. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31952. #endif
  31953. AssertNotNull(md = EVP_sha256());
  31954. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31955. #ifdef WOLFSSL_SHA384
  31956. AssertNotNull(md = EVP_sha384());
  31957. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31958. #endif
  31959. #ifdef WOLFSSL_SHA512
  31960. AssertNotNull(md = EVP_sha512());
  31961. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31962. #endif
  31963. res = TEST_RES_CHECK(1);
  31964. #endif
  31965. return res;
  31966. }
  31967. #ifdef OPENSSL_EXTRA
  31968. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31969. size_t testKeySz, const char* testData, size_t testDataSz,
  31970. const byte* testResult, size_t testResultSz)
  31971. {
  31972. unsigned char check[WC_MAX_DIGEST_SIZE];
  31973. size_t checkSz = -1;
  31974. WOLFSSL_EVP_PKEY* key;
  31975. WOLFSSL_EVP_MD_CTX mdCtx;
  31976. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31977. testKey, (int)testKeySz));
  31978. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31979. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31980. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31981. (unsigned int)testDataSz), 1);
  31982. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31983. AssertIntEQ((int)checkSz, (int)testResultSz);
  31984. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31985. AssertIntEQ((int)checkSz,(int)testResultSz);
  31986. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31987. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31988. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31989. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31990. (unsigned int)testDataSz), 1);
  31991. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31992. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31993. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31994. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31995. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31996. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31997. AssertIntEQ((int)checkSz, (int)testResultSz);
  31998. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31999. AssertIntEQ((int)checkSz,(int)testResultSz);
  32000. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32001. (unsigned int)testDataSz - 4), 1);
  32002. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32003. AssertIntEQ((int)checkSz,(int)testResultSz);
  32004. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32005. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32006. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32007. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32008. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32009. (unsigned int)testDataSz - 4), 1);
  32010. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32011. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32012. wolfSSL_EVP_PKEY_free(key);
  32013. }
  32014. #endif
  32015. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32016. {
  32017. int res = TEST_SKIPPED;
  32018. #ifdef OPENSSL_EXTRA
  32019. static const unsigned char testKey[] =
  32020. {
  32021. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32022. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32023. 0x0b, 0x0b, 0x0b, 0x0b
  32024. };
  32025. static const char testData[] = "Hi There";
  32026. #ifdef WOLFSSL_SHA224
  32027. static const unsigned char testResultSha224[] =
  32028. {
  32029. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32030. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32031. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32032. 0x53, 0x68, 0x4b, 0x22
  32033. };
  32034. #endif
  32035. #ifndef NO_SHA256
  32036. static const unsigned char testResultSha256[] =
  32037. {
  32038. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32039. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32040. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32041. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32042. };
  32043. #endif
  32044. #ifdef WOLFSSL_SHA384
  32045. static const unsigned char testResultSha384[] =
  32046. {
  32047. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32048. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32049. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32050. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32051. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32052. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32053. };
  32054. #endif
  32055. #ifdef WOLFSSL_SHA512
  32056. static const unsigned char testResultSha512[] =
  32057. {
  32058. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32059. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32060. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32061. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32062. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32063. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32064. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32065. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32066. };
  32067. #endif
  32068. #ifdef WOLFSSL_SHA3
  32069. #ifndef WOLFSSL_NOSHA3_224
  32070. static const unsigned char testResultSha3_224[] =
  32071. {
  32072. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32073. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32074. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32075. 0xf3, 0xc8, 0x60, 0xf7
  32076. };
  32077. #endif
  32078. #ifndef WOLFSSL_NOSHA3_256
  32079. static const unsigned char testResultSha3_256[] =
  32080. {
  32081. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32082. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32083. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32084. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32085. };
  32086. #endif
  32087. #ifndef WOLFSSL_NOSHA3_384
  32088. static const unsigned char testResultSha3_384[] =
  32089. {
  32090. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32091. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32092. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32093. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32094. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32095. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32096. };
  32097. #endif
  32098. #ifndef WOLFSSL_NOSHA3_512
  32099. static const unsigned char testResultSha3_512[] =
  32100. {
  32101. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32102. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32103. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32104. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32105. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32106. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32107. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32108. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32109. };
  32110. #endif
  32111. #endif
  32112. #ifndef NO_SHA256
  32113. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  32114. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  32115. #endif
  32116. #ifdef WOLFSSL_SHA224
  32117. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  32118. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  32119. #endif
  32120. #ifdef WOLFSSL_SHA384
  32121. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  32122. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  32123. #endif
  32124. #ifdef WOLFSSL_SHA512
  32125. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  32126. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  32127. #endif
  32128. #ifdef WOLFSSL_SHA3
  32129. #ifndef WOLFSSL_NOSHA3_224
  32130. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  32131. testData, XSTRLEN(testData), testResultSha3_224,
  32132. sizeof(testResultSha3_224));
  32133. #endif
  32134. #ifndef WOLFSSL_NOSHA3_256
  32135. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  32136. testData, XSTRLEN(testData), testResultSha3_256,
  32137. sizeof(testResultSha3_256));
  32138. #endif
  32139. #ifndef WOLFSSL_NOSHA3_384
  32140. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  32141. testData, XSTRLEN(testData), testResultSha3_384,
  32142. sizeof(testResultSha3_384));
  32143. #endif
  32144. #ifndef WOLFSSL_NOSHA3_512
  32145. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  32146. testData, XSTRLEN(testData), testResultSha3_512,
  32147. sizeof(testResultSha3_512));
  32148. #endif
  32149. #endif
  32150. res = TEST_RES_CHECK(1);
  32151. #endif /* OPENSSL_EXTRA */
  32152. return res;
  32153. }
  32154. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32155. {
  32156. int res = TEST_SKIPPED;
  32157. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  32158. defined(USE_CERT_BUFFERS_2048)
  32159. WOLFSSL_EVP_PKEY* privKey;
  32160. WOLFSSL_EVP_PKEY* pubKey;
  32161. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  32162. const char testData[] = "Hi There";
  32163. WOLFSSL_EVP_MD_CTX mdCtx;
  32164. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32165. size_t checkSz = -1;
  32166. int sz = 2048 / 8;
  32167. const unsigned char* cp;
  32168. const unsigned char* p;
  32169. unsigned char check[2048/8];
  32170. size_t i;
  32171. int paddings[] = {
  32172. RSA_PKCS1_PADDING,
  32173. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32174. RSA_PKCS1_PSS_PADDING,
  32175. #endif
  32176. };
  32177. cp = client_key_der_2048;
  32178. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32179. sizeof_client_key_der_2048)));
  32180. p = client_keypub_der_2048;
  32181. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32182. sizeof_client_keypub_der_2048)));
  32183. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32184. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32185. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32186. NULL, privKey), 1);
  32187. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32188. (unsigned int)XSTRLEN(testData)), 1);
  32189. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32190. AssertIntEQ((int)checkSz, sz);
  32191. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32192. AssertIntEQ((int)checkSz,sz);
  32193. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32194. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32195. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  32196. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32197. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32198. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32199. NULL, pubKey), 1);
  32200. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32201. (unsigned int)XSTRLEN(testData)),
  32202. 1);
  32203. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32204. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32205. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32206. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32207. NULL, privKey), 1);
  32208. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32209. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32210. AssertIntEQ((int)checkSz, sz);
  32211. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32212. AssertIntEQ((int)checkSz, sz);
  32213. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32214. (unsigned int)XSTRLEN(testData) - 4), 1);
  32215. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32216. AssertIntEQ((int)checkSz, sz);
  32217. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32218. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32219. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32220. NULL, pubKey), 1);
  32221. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32222. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32223. (unsigned int)XSTRLEN(testData) - 4),
  32224. 1);
  32225. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32226. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32227. /* Check all signing padding types */
  32228. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32229. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32230. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32231. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32232. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32233. paddings[i]), 1);
  32234. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32235. (unsigned int)XSTRLEN(testData)), 1);
  32236. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32237. AssertIntEQ((int)checkSz, sz);
  32238. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32239. AssertIntEQ((int)checkSz,sz);
  32240. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32241. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32242. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32243. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32244. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32245. paddings[i]), 1);
  32246. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32247. (unsigned int)XSTRLEN(testData)), 1);
  32248. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32249. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32250. }
  32251. wolfSSL_EVP_PKEY_free(pubKey);
  32252. wolfSSL_EVP_PKEY_free(privKey);
  32253. res = TEST_RES_CHECK(1);
  32254. #endif
  32255. return res;
  32256. }
  32257. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32258. {
  32259. int res = TEST_SKIPPED;
  32260. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32261. WOLFSSL_EVP_PKEY* privKey;
  32262. WOLFSSL_EVP_PKEY* pubKey;
  32263. const char testData[] = "Hi There";
  32264. WOLFSSL_EVP_MD_CTX mdCtx;
  32265. size_t checkSz = -1;
  32266. const unsigned char* cp;
  32267. const unsigned char* p;
  32268. unsigned char check[2048/8];
  32269. cp = ecc_clikey_der_256;
  32270. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32271. sizeof_ecc_clikey_der_256);
  32272. AssertNotNull(privKey);
  32273. p = ecc_clikeypub_der_256;
  32274. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32275. sizeof_ecc_clikeypub_der_256)));
  32276. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32277. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32278. NULL, privKey), 1);
  32279. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32280. (unsigned int)XSTRLEN(testData)), 1);
  32281. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32282. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32283. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32284. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32285. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32286. NULL, pubKey), 1);
  32287. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32288. (unsigned int)XSTRLEN(testData)),
  32289. 1);
  32290. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32291. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32292. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32293. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32294. NULL, privKey), 1);
  32295. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32296. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32297. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32298. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32299. (unsigned int)XSTRLEN(testData) - 4), 1);
  32300. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32301. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32302. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32303. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32304. NULL, pubKey), 1);
  32305. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32306. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32307. (unsigned int)XSTRLEN(testData) - 4),
  32308. 1);
  32309. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32310. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32311. wolfSSL_EVP_PKEY_free(pubKey);
  32312. wolfSSL_EVP_PKEY_free(privKey);
  32313. res = TEST_RES_CHECK(1);
  32314. #endif
  32315. return res;
  32316. }
  32317. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32318. {
  32319. int res = TEST_SKIPPED;
  32320. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32321. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32322. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32323. char caFile[] = "./certs/client-ca.pem";
  32324. char clientFile[] = "./certs/client-cert.pem";
  32325. SSL_CTX* ctx;
  32326. X509* x509;
  32327. BIO *bio = NULL;
  32328. X509 *cert = NULL;
  32329. X509 *ca;
  32330. STACK_OF(X509) *chain = NULL;
  32331. STACK_OF(X509) *chain2 = NULL;
  32332. #ifndef NO_WOLFSSL_SERVER
  32333. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32334. #else
  32335. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32336. #endif
  32337. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  32338. AssertNotNull(x509);
  32339. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32340. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  32341. AssertNotNull(x509);
  32342. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  32343. /* additional test of getting EVP_PKEY key size from X509
  32344. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32345. * allowed with user RSA */
  32346. {
  32347. EVP_PKEY* pkey;
  32348. #if defined(HAVE_ECC)
  32349. X509* ecX509;
  32350. #endif /* HAVE_ECC */
  32351. AssertNotNull(pkey = X509_get_pubkey(x509));
  32352. /* current RSA key is 2048 bit (256 bytes) */
  32353. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  32354. EVP_PKEY_free(pkey);
  32355. #if defined(HAVE_ECC)
  32356. #if defined(USE_CERT_BUFFERS_256)
  32357. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32358. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32359. SSL_FILETYPE_ASN1));
  32360. #else
  32361. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  32362. SSL_FILETYPE_PEM));
  32363. #endif
  32364. pkey = X509_get_pubkey(ecX509);
  32365. AssertNotNull(pkey);
  32366. /* current ECC key is 256 bit (32 bytes) */
  32367. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  32368. X509_free(ecX509);
  32369. EVP_PKEY_free(pkey);
  32370. #endif /* HAVE_ECC */
  32371. }
  32372. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  32373. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32374. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32375. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  32376. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32377. #endif
  32378. SSL_CTX_free(ctx);
  32379. #ifndef NO_WOLFSSL_SERVER
  32380. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32381. #else
  32382. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32383. #endif
  32384. /* Test haproxy use case */
  32385. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32386. /* Read Certificate */
  32387. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32388. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32389. AssertNotNull(chain = sk_X509_new_null());
  32390. AssertIntEQ(sk_X509_push(chain, ca), 1);
  32391. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  32392. AssertNotNull(ca = sk_X509_shift(chain2));
  32393. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32394. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32395. BIO_free(bio);
  32396. X509_free(cert);
  32397. sk_X509_pop_free(chain, X509_free);
  32398. sk_X509_pop_free(chain2, X509_free);
  32399. SSL_CTX_free(ctx);
  32400. res = TEST_RES_CHECK(1);
  32401. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32402. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32403. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32404. return res;
  32405. }
  32406. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32407. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32408. {
  32409. int res = TEST_SKIPPED;
  32410. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32411. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32412. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32413. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32414. tcp_ready ready;
  32415. func_args client_args;
  32416. func_args server_args;
  32417. #ifndef SINGLE_THREADED
  32418. THREAD_TYPE serverThread;
  32419. #endif
  32420. callback_functions client_cb;
  32421. callback_functions server_cb;
  32422. int line = 0;
  32423. int flag = ERR_TXT_STRING;
  32424. const char* file = NULL;
  32425. const char* data = NULL;
  32426. /* create a failed connection and inspect the error */
  32427. #ifdef WOLFSSL_TIRTOS
  32428. fdOpenSession(Task_self());
  32429. #endif
  32430. XMEMSET(&client_args, 0, sizeof(func_args));
  32431. XMEMSET(&server_args, 0, sizeof(func_args));
  32432. StartTCP();
  32433. InitTcpReady(&ready);
  32434. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32435. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32436. client_cb.method = wolfTLSv1_1_client_method;
  32437. server_cb.method = wolfTLSv1_2_server_method;
  32438. server_args.signal = &ready;
  32439. server_args.callbacks = &server_cb;
  32440. client_args.signal = &ready;
  32441. client_args.callbacks = &client_cb;
  32442. #ifndef SINGLE_THREADED
  32443. start_thread(test_server_nofail, &server_args, &serverThread);
  32444. wait_tcp_ready(&server_args);
  32445. test_client_nofail(&client_args, NULL);
  32446. join_thread(serverThread);
  32447. #endif
  32448. FreeTcpReady(&ready);
  32449. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32450. AssertNotNull(data);
  32451. /* check clearing error state */
  32452. ERR_remove_state(0);
  32453. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32454. ERR_peek_last_error_line(NULL, &line);
  32455. AssertIntEQ(line, 0);
  32456. ERR_peek_last_error_line(&file, NULL);
  32457. AssertNull(file);
  32458. /* retry connection to fill error queue */
  32459. XMEMSET(&client_args, 0, sizeof(func_args));
  32460. XMEMSET(&server_args, 0, sizeof(func_args));
  32461. StartTCP();
  32462. InitTcpReady(&ready);
  32463. client_cb.method = wolfTLSv1_1_client_method;
  32464. server_cb.method = wolfTLSv1_2_server_method;
  32465. server_args.signal = &ready;
  32466. server_args.callbacks = &server_cb;
  32467. client_args.signal = &ready;
  32468. client_args.callbacks = &client_cb;
  32469. start_thread(test_server_nofail, &server_args, &serverThread);
  32470. wait_tcp_ready(&server_args);
  32471. test_client_nofail(&client_args, NULL);
  32472. join_thread(serverThread);
  32473. FreeTcpReady(&ready);
  32474. /* check that error code was stored */
  32475. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32476. ERR_peek_last_error_line(NULL, &line);
  32477. AssertIntNE(line, 0);
  32478. ERR_peek_last_error_line(&file, NULL);
  32479. AssertNotNull(file);
  32480. #ifdef WOLFSSL_TIRTOS
  32481. fdOpenSession(Task_self());
  32482. #endif
  32483. fprintf(stderr, "\nTesting error print out\n");
  32484. ERR_print_errors_fp(stderr);
  32485. fprintf(stderr, "Done testing print out\n\n");
  32486. res = TEST_RES_CHECK(1);
  32487. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32488. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32489. return res;
  32490. }
  32491. #endif
  32492. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32493. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32494. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32495. {
  32496. (void) ok;
  32497. (void) ctx;
  32498. fprintf(stderr, "ENTER verify_cb\n");
  32499. return SSL_SUCCESS;
  32500. }
  32501. #endif
  32502. static int test_wolfSSL_X509_Name_canon(void)
  32503. {
  32504. int res = TEST_SKIPPED;
  32505. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32506. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32507. defined(WOLFSSL_CERT_GEN) && \
  32508. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32509. const long ex_hash1 = 0x0fdb2da4;
  32510. const long ex_hash2 = 0x9f3e8c9e;
  32511. X509_NAME *name = NULL;
  32512. X509 *x509 = NULL;
  32513. FILE* file = NULL;
  32514. unsigned long hash = 0;
  32515. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32516. byte *pbuf = NULL;
  32517. word32 len = 0;
  32518. (void) ex_hash2;
  32519. file = XFOPEN(caCertFile, "rb");
  32520. AssertNotNull(file);
  32521. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32522. AssertNotNull(name = X509_get_issuer_name(x509));
  32523. /* When output buffer is NULL, should return necessary output buffer
  32524. * length.*/
  32525. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32526. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32527. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32528. hash = (((unsigned long)digest[3] << 24) |
  32529. ((unsigned long)digest[2] << 16) |
  32530. ((unsigned long)digest[1] << 8) |
  32531. ((unsigned long)digest[0]));
  32532. AssertIntEQ(hash, ex_hash1);
  32533. XFCLOSE(file);
  32534. X509_free(x509);
  32535. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32536. pbuf = NULL;
  32537. file = XFOPEN(cliCertFile, "rb");
  32538. AssertNotNull(file);
  32539. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32540. AssertNotNull(name = X509_get_issuer_name(x509));
  32541. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32542. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32543. hash = (((unsigned long)digest[3] << 24) |
  32544. ((unsigned long)digest[2] << 16) |
  32545. ((unsigned long)digest[1] << 8) |
  32546. ((unsigned long)digest[0]));
  32547. AssertIntEQ(hash, ex_hash2);
  32548. XFCLOSE(file);
  32549. X509_free(x509);
  32550. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32551. res = TEST_RES_CHECK(1);
  32552. #endif
  32553. return res;
  32554. }
  32555. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32556. {
  32557. int res = TEST_SKIPPED;
  32558. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32559. const int MAX_DIR = 4;
  32560. const char paths[][32] = {
  32561. "./certs/ed25519",
  32562. "./certs/ecc",
  32563. "./certs/crl",
  32564. "./certs/",
  32565. };
  32566. char CertCrl_path[MAX_FILENAME_SZ];
  32567. char *p;
  32568. X509_STORE* str;
  32569. X509_LOOKUP* lookup;
  32570. WOLFSSL_STACK* sk = NULL;
  32571. int len, total_len, i;
  32572. (void) sk;
  32573. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32574. /* illegal string */
  32575. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32576. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32577. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32578. SSL_FILETYPE_PEM,NULL), 0);
  32579. /* free store */
  32580. X509_STORE_free(str);
  32581. /* short folder string */
  32582. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32583. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32584. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32585. SSL_FILETYPE_PEM,NULL), 1);
  32586. #if defined(WOLFSSL_INT_H)
  32587. /* only available when including internal.h */
  32588. AssertNotNull(sk = lookup->dirs->dir_entry);
  32589. #endif
  32590. /* free store */
  32591. X509_STORE_free(str);
  32592. /* typical function check */
  32593. p = &CertCrl_path[0];
  32594. total_len = 0;
  32595. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32596. len = (int)XSTRLEN((const char*)&paths[i]);
  32597. total_len += len;
  32598. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32599. p += len;
  32600. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32601. }
  32602. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32603. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32604. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32605. SSL_FILETYPE_PEM,NULL), 1);
  32606. #if defined(WOLFSSL_INT_H)
  32607. /* only available when including internal.h */
  32608. AssertNotNull(sk = lookup->dirs->dir_entry);
  32609. #endif
  32610. X509_STORE_free(str);
  32611. res = TEST_RES_CHECK(1);
  32612. #endif
  32613. return res;
  32614. }
  32615. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32616. {
  32617. int res = TEST_SKIPPED;
  32618. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32619. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32620. defined(WOLFSSL_SIGNER_DER_CERT)
  32621. X509_STORE_CTX* ctx;
  32622. X509_STORE* str;
  32623. X509_LOOKUP* lookup;
  32624. X509* cert1;
  32625. X509* x509Ca;
  32626. X509* x509Svr;
  32627. X509* issuer;
  32628. WOLFSSL_STACK* sk = NULL;
  32629. X509_NAME* caName;
  32630. X509_NAME* issuerName;
  32631. FILE* file1 = NULL;
  32632. int i, cert_count, cmp;
  32633. char der[] = "certs/ca-cert.der";
  32634. #ifdef HAVE_CRL
  32635. char pem[][100] = {
  32636. "./certs/crl/crl.pem",
  32637. "./certs/crl/crl2.pem",
  32638. "./certs/crl/caEccCrl.pem",
  32639. "./certs/crl/eccCliCRL.pem",
  32640. "./certs/crl/eccSrvCRL.pem",
  32641. ""
  32642. };
  32643. #endif
  32644. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  32645. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32646. fclose(file1);
  32647. AssertNotNull(ctx = X509_STORE_CTX_new());
  32648. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32649. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32650. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32651. SSL_FILETYPE_PEM,NULL), 1);
  32652. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32653. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  32654. /* check if CA cert is loaded into the store */
  32655. for (i = 0; i < cert_count; i++) {
  32656. x509Ca = sk_X509_value(sk, i);
  32657. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32658. }
  32659. AssertNotNull((x509Svr =
  32660. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32661. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32662. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32663. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32664. AssertNotNull(issuer);
  32665. caName = X509_get_subject_name(x509Ca);
  32666. AssertNotNull(caName);
  32667. issuerName = X509_get_subject_name(issuer);
  32668. AssertNotNull(issuerName);
  32669. cmp = X509_NAME_cmp(caName, issuerName);
  32670. AssertIntEQ(cmp, 0);
  32671. /* load der format */
  32672. X509_free(issuer);
  32673. X509_STORE_CTX_free(ctx);
  32674. X509_STORE_free(str);
  32675. sk_X509_pop_free(sk, NULL);
  32676. X509_free(x509Svr);
  32677. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32678. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32679. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32680. SSL_FILETYPE_ASN1,NULL), 1);
  32681. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32682. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  32683. /* check if CA cert is loaded into the store */
  32684. for (i = 0; i < cert_count; i++) {
  32685. x509Ca = sk_X509_value(sk, i);
  32686. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32687. }
  32688. X509_STORE_free(str);
  32689. sk_X509_pop_free(sk, NULL);
  32690. X509_free(cert1);
  32691. #ifdef HAVE_CRL
  32692. AssertNotNull(str = wolfSSL_X509_STORE_new());
  32693. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32694. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32695. SSL_FILETYPE_PEM,NULL), 1);
  32696. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32697. "certs/server-revoked-cert.pem",
  32698. SSL_FILETYPE_PEM,NULL), 1);
  32699. if (str) {
  32700. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32701. WOLFSSL_FILETYPE_PEM), 1);
  32702. /* since store hasn't yet known the revoked cert*/
  32703. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32704. "certs/server-revoked-cert.pem",
  32705. WOLFSSL_FILETYPE_PEM), 1);
  32706. }
  32707. for (i = 0; pem[i][0] != '\0'; i++)
  32708. {
  32709. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32710. SSL_FILETYPE_PEM, NULL), 1);
  32711. }
  32712. if (str) {
  32713. /* since store knows crl list */
  32714. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32715. "certs/server-revoked-cert.pem",
  32716. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32717. }
  32718. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32719. X509_STORE_free(str);
  32720. #endif
  32721. res = TEST_RES_CHECK(1);
  32722. #endif
  32723. return res;
  32724. }
  32725. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32726. {
  32727. int res = TEST_SKIPPED;
  32728. #if defined(OPENSSL_EXTRA)
  32729. X509_STORE_CTX_cleanup(NULL);
  32730. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32731. AssertTrue(1); /* to confirm previous call gives no harm */
  32732. res = TEST_RES_CHECK(1);
  32733. #endif
  32734. return res;
  32735. }
  32736. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32737. {
  32738. int res = TEST_SKIPPED;
  32739. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32740. #ifdef WOLFSSL_SIGNER_DER_CERT
  32741. int cmp;
  32742. #endif
  32743. X509_STORE_CTX* ctx;
  32744. X509_STORE* str;
  32745. X509* x509Ca;
  32746. X509* x509Svr;
  32747. X509* issuer;
  32748. X509_NAME* caName;
  32749. X509_NAME* issuerName;
  32750. AssertNotNull(ctx = X509_STORE_CTX_new());
  32751. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32752. AssertNotNull((x509Ca =
  32753. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32754. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32755. AssertNotNull((x509Svr =
  32756. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32757. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32758. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32759. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32760. AssertNotNull(issuer);
  32761. caName = X509_get_subject_name(x509Ca);
  32762. AssertNotNull(caName);
  32763. issuerName = X509_get_subject_name(issuer);
  32764. AssertNotNull(issuerName);
  32765. #ifdef WOLFSSL_SIGNER_DER_CERT
  32766. cmp = X509_NAME_cmp(caName, issuerName);
  32767. AssertIntEQ(cmp, 0);
  32768. #endif
  32769. X509_free(issuer);
  32770. X509_STORE_CTX_free(ctx);
  32771. X509_free(x509Svr);
  32772. X509_STORE_free(str);
  32773. X509_free(x509Ca);
  32774. res = TEST_RES_CHECK(1);
  32775. #endif
  32776. return res;
  32777. }
  32778. static int test_wolfSSL_PKCS7_certs(void)
  32779. {
  32780. int res = TEST_SKIPPED;
  32781. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32782. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32783. STACK_OF(X509)* sk = NULL;
  32784. STACK_OF(X509_INFO)* info_sk = NULL;
  32785. PKCS7 *p7 = NULL;
  32786. BIO* bio;
  32787. const byte* p = NULL;
  32788. int buflen = 0;
  32789. int i;
  32790. /* Test twice. Once with d2i and once without to test
  32791. * that everything is free'd correctly. */
  32792. for (i = 0; i < 2; i++) {
  32793. AssertNotNull(p7 = PKCS7_new());
  32794. p7->version = 1;
  32795. #ifdef NO_SHA
  32796. p7->hashOID = SHA256h;
  32797. #else
  32798. p7->hashOID = SHAh;
  32799. #endif
  32800. AssertNotNull(bio = BIO_new(BIO_s_file()));
  32801. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32802. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32803. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  32804. AssertNotNull(sk = sk_X509_new_null());
  32805. while (sk_X509_INFO_num(info_sk)) {
  32806. X509_INFO* info;
  32807. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  32808. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  32809. info->x509 = NULL;
  32810. X509_INFO_free(info);
  32811. }
  32812. sk_X509_INFO_free(info_sk);
  32813. BIO_free(bio);
  32814. bio = BIO_new(BIO_s_mem());
  32815. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32816. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32817. if (i == 0) {
  32818. PKCS7_free(p7);
  32819. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  32820. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  32821. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32822. /* PKCS7_free free's the certs */
  32823. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  32824. }
  32825. BIO_free(bio);
  32826. PKCS7_free(p7);
  32827. }
  32828. res = TEST_RES_CHECK(1);
  32829. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32830. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32831. return res;
  32832. }
  32833. static int test_wolfSSL_X509_STORE_CTX(void)
  32834. {
  32835. int res = TEST_SKIPPED;
  32836. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32837. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32838. X509_STORE_CTX* ctx;
  32839. X509_STORE* str;
  32840. X509* x509;
  32841. #ifdef OPENSSL_ALL
  32842. X509* x5092;
  32843. STACK_OF(X509) *sk, *sk2, *sk3;
  32844. #endif
  32845. AssertNotNull(ctx = X509_STORE_CTX_new());
  32846. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  32847. AssertNotNull((x509 =
  32848. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32849. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32850. #ifdef OPENSSL_ALL
  32851. /* sk_X509_new only in OPENSSL_ALL */
  32852. sk = sk_X509_new_null();
  32853. AssertNotNull(sk);
  32854. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32855. #else
  32856. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32857. #endif
  32858. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32859. X509_STORE_CTX_set_error(ctx, -5);
  32860. X509_STORE_CTX_set_error(NULL, -5);
  32861. X509_STORE_CTX_free(ctx);
  32862. #ifdef OPENSSL_ALL
  32863. sk_X509_pop_free(sk, NULL);
  32864. #endif
  32865. X509_STORE_free(str);
  32866. X509_free(x509);
  32867. AssertNotNull(ctx = X509_STORE_CTX_new());
  32868. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32869. X509_STORE_CTX_free(ctx);
  32870. #ifdef OPENSSL_ALL
  32871. /* test X509_STORE_CTX_get(1)_chain */
  32872. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32873. SSL_FILETYPE_PEM)));
  32874. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32875. SSL_FILETYPE_PEM)));
  32876. AssertNotNull((sk = sk_X509_new_null()));
  32877. AssertIntEQ(sk_X509_push(sk, x509), 1);
  32878. AssertNotNull((str = X509_STORE_new()));
  32879. AssertNotNull((ctx = X509_STORE_CTX_new()));
  32880. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32881. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32882. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32883. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32884. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32885. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32886. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32887. X509_STORE_CTX_free(ctx);
  32888. X509_STORE_free(str);
  32889. /* CTX certs not freed yet */
  32890. X509_free(x5092);
  32891. sk_X509_pop_free(sk, NULL);
  32892. /* sk3 is dup so free here */
  32893. sk_X509_pop_free(sk3, NULL);
  32894. #endif
  32895. /* test X509_STORE_CTX_get/set_ex_data */
  32896. {
  32897. int i = 0, tmpData = 5;
  32898. void* tmpDataRet;
  32899. AssertNotNull(ctx = X509_STORE_CTX_new());
  32900. #ifdef HAVE_EX_DATA
  32901. for (i = 0; i < MAX_EX_DATA; i++) {
  32902. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32903. WOLFSSL_SUCCESS);
  32904. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32905. AssertNotNull(tmpDataRet);
  32906. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  32907. }
  32908. #else
  32909. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32910. WOLFSSL_FAILURE);
  32911. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32912. AssertNull(tmpDataRet);
  32913. #endif
  32914. X509_STORE_CTX_free(ctx);
  32915. }
  32916. /* test X509_STORE_get/set_ex_data */
  32917. {
  32918. int i = 0, tmpData = 99;
  32919. void* tmpDataRet;
  32920. AssertNotNull(str = X509_STORE_new());
  32921. #ifdef HAVE_EX_DATA
  32922. for (i = 0; i < MAX_EX_DATA; i++) {
  32923. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32924. WOLFSSL_SUCCESS);
  32925. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32926. AssertNotNull(tmpDataRet);
  32927. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  32928. }
  32929. #else
  32930. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32931. WOLFSSL_FAILURE);
  32932. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32933. AssertNull(tmpDataRet);
  32934. #endif
  32935. X509_STORE_free(str);
  32936. }
  32937. res = TEST_RES_CHECK(1);
  32938. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32939. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32940. return res;
  32941. }
  32942. static int test_wolfSSL_X509_STORE_set_flags(void)
  32943. {
  32944. int res = TEST_SKIPPED;
  32945. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32946. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32947. X509_STORE* store;
  32948. X509* x509;
  32949. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  32950. AssertNotNull((x509 =
  32951. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  32952. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  32953. #ifdef HAVE_CRL
  32954. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  32955. #else
  32956. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32957. NOT_COMPILED_IN);
  32958. #endif
  32959. wolfSSL_X509_free(x509);
  32960. wolfSSL_X509_STORE_free(store);
  32961. res = TEST_RES_CHECK(1);
  32962. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32963. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32964. return res;
  32965. }
  32966. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  32967. {
  32968. int res = TEST_SKIPPED;
  32969. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32970. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32971. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  32972. WOLFSSL_X509_STORE* store;
  32973. WOLFSSL_X509_LOOKUP* lookup;
  32974. AssertNotNull(store = wolfSSL_X509_STORE_new());
  32975. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  32976. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  32977. X509_FILETYPE_PEM), 1);
  32978. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  32979. X509_FILETYPE_PEM), 1);
  32980. if (store) {
  32981. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  32982. WOLFSSL_FILETYPE_PEM), 1);
  32983. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32984. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  32985. }
  32986. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  32987. X509_FILETYPE_PEM), 1);
  32988. if (store) {
  32989. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32990. WOLFSSL_FILETYPE_PEM), 1);
  32991. }
  32992. wolfSSL_X509_STORE_free(store);
  32993. res = TEST_RES_CHECK(1);
  32994. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32995. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32996. return res;
  32997. }
  32998. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  32999. {
  33000. int res = TEST_SKIPPED;
  33001. #if defined(OPENSSL_EXTRA)
  33002. WOLFSSL_X509_STORE_CTX* ctx;
  33003. time_t c_time;
  33004. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33005. c_time = 365*24*60*60;
  33006. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33007. AssertTrue(
  33008. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  33009. AssertTrue(ctx->param->check_time == c_time);
  33010. wolfSSL_X509_STORE_CTX_free(ctx);
  33011. res = TEST_RES_CHECK(1);
  33012. #endif /* OPENSSL_EXTRA */
  33013. return res;
  33014. }
  33015. static int test_wolfSSL_CTX_get0_set1_param(void)
  33016. {
  33017. int res = TEST_SKIPPED;
  33018. #if defined(OPENSSL_EXTRA)
  33019. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33020. int ret;
  33021. SSL_CTX* ctx;
  33022. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33023. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  33024. char testIPv4[] = "127.0.0.1";
  33025. char testhostName[] = "foo.hoge.com";
  33026. #ifndef NO_WOLFSSL_SERVER
  33027. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33028. #else
  33029. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33030. #endif
  33031. AssertNull(SSL_CTX_get0_param(NULL));
  33032. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  33033. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33034. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  33035. AssertNotNull(pvpm);
  33036. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33037. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33038. (int)XSTRLEN(testhostName));
  33039. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  33040. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33041. ret = SSL_CTX_set1_param(ctx, pvpm);
  33042. AssertIntEQ(1, ret);
  33043. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33044. (int)XSTRLEN(testhostName)));
  33045. AssertIntEQ(0x01, pParam->hostFlags);
  33046. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33047. /* test for incorrect patameter */
  33048. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33049. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33050. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33051. SSL_CTX_free(ctx);
  33052. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33053. res = TEST_RES_CHECK(1);
  33054. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33055. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33056. return res;
  33057. }
  33058. static int test_wolfSSL_get0_param(void)
  33059. {
  33060. int res = TEST_SKIPPED;
  33061. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33062. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33063. SSL_CTX* ctx;
  33064. SSL* ssl;
  33065. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33066. #ifndef NO_WOLFSSL_SERVER
  33067. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33068. #else
  33069. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33070. #endif
  33071. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33072. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33073. AssertNotNull(ssl = SSL_new(ctx));
  33074. pParam = SSL_get0_param(ssl);
  33075. (void)pParam;
  33076. SSL_free(ssl);
  33077. SSL_CTX_free(ctx);
  33078. res = TEST_RES_CHECK(1);
  33079. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33080. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33081. return res;
  33082. }
  33083. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33084. {
  33085. int res = TEST_SKIPPED;
  33086. #if defined(OPENSSL_EXTRA)
  33087. const char host[] = "www.example.com";
  33088. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33089. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33090. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  33091. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33092. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33093. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33094. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33095. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33096. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33097. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33098. res = TEST_RES_CHECK(1);
  33099. #endif /* OPENSSL_EXTRA */
  33100. return res;
  33101. }
  33102. static int test_wolfSSL_set1_host(void)
  33103. {
  33104. int res = TEST_SKIPPED;
  33105. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33106. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33107. const char host[] = "www.test_wolfSSL_set1_host.com";
  33108. const char emptyStr[] = "";
  33109. SSL_CTX* ctx;
  33110. SSL* ssl;
  33111. WOLFSSL_X509_VERIFY_PARAM* pParam;
  33112. #ifndef NO_WOLFSSL_SERVER
  33113. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33114. #else
  33115. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33116. #endif
  33117. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33118. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33119. AssertNotNull(ssl = SSL_new(ctx));
  33120. pParam = SSL_get0_param(ssl);
  33121. /* we should get back host string */
  33122. SSL_set1_host(ssl, host);
  33123. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33124. /* we should get back empty string */
  33125. SSL_set1_host(ssl, emptyStr);
  33126. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33127. /* we should get back host string */
  33128. SSL_set1_host(ssl, host);
  33129. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33130. /* we should get back empty string */
  33131. SSL_set1_host(ssl, NULL);
  33132. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33133. SSL_free(ssl);
  33134. SSL_CTX_free(ctx);
  33135. res = TEST_RES_CHECK(1);
  33136. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33137. #endif /* OPENSSL_EXTRA */
  33138. return res;
  33139. }
  33140. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33141. {
  33142. int res = TEST_SKIPPED;
  33143. #if defined(OPENSSL_EXTRA)
  33144. unsigned char buf[16] = {0};
  33145. WOLFSSL_X509_VERIFY_PARAM* param;
  33146. AssertNotNull(param = X509_VERIFY_PARAM_new());
  33147. /* test 127.0.0.1 */
  33148. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33149. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33150. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33151. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33152. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33153. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33154. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33155. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33156. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33157. AssertIntEQ(XSTRNCMP(param->ipasc,
  33158. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33159. /* test 2001:db8:: */
  33160. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33161. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33162. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33163. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33164. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33165. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33166. /* test ::1234:5678 */
  33167. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33168. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33169. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33170. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33171. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33172. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33173. /* test 2001:db8::1234:5678 */
  33174. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33175. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33176. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33177. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33178. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33179. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33180. sizeof(param->ipasc)), 0);
  33181. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33182. /* 2001:db8:1::ab9:c0a8:102 */
  33183. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33184. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33185. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33186. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33187. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33188. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33189. sizeof(param->ipasc)), 0);
  33190. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33191. res = TEST_RES_CHECK(1);
  33192. #endif /* OPENSSL_EXTRA */
  33193. return res;
  33194. }
  33195. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33196. {
  33197. int res = TEST_SKIPPED;
  33198. #if defined(OPENSSL_EXTRA)
  33199. X509_STORE* store;
  33200. X509_STORE_CTX* ctx;
  33201. X509_STORE_CTX* ctx_no_init;
  33202. AssertNotNull((store = X509_STORE_new()));
  33203. AssertNotNull(ctx = X509_STORE_CTX_new());
  33204. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  33205. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33206. AssertNull(X509_STORE_CTX_get0_store(NULL));
  33207. /* should return NULL if ctx has not bee initialized */
  33208. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33209. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  33210. wolfSSL_X509_STORE_CTX_free(ctx);
  33211. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33212. X509_STORE_free(store);
  33213. res = TEST_RES_CHECK(1);
  33214. #endif /* OPENSSL_EXTRA */
  33215. return res;
  33216. }
  33217. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33218. {
  33219. int res = TEST_SKIPPED;
  33220. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33221. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33222. WOLFSSL_CTX* ctx;
  33223. WOLFSSL* ssl;
  33224. X509_NAME* name = NULL;
  33225. STACK_OF(X509_NAME)* names = NULL;
  33226. STACK_OF(X509_NAME)* ca_list = NULL;
  33227. int i, names_len;
  33228. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33229. /* Send two X501 names in cert request */
  33230. names = SSL_load_client_CA_file(cliCertFile);
  33231. AssertNotNull(names);
  33232. ca_list = SSL_load_client_CA_file(caCertFile);
  33233. AssertNotNull(ca_list);
  33234. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  33235. SSL_CTX_set_client_CA_list(ctx, names);
  33236. /* This should only free the stack structure */
  33237. sk_X509_NAME_free(ca_list);
  33238. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33239. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33240. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33241. for (i=0; i<names_len; i++) {
  33242. AssertNotNull(name = sk_X509_NAME_value(names, i));
  33243. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  33244. }
  33245. /* Needed to be able to create ssl object */
  33246. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33247. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33248. AssertNotNull(ssl = wolfSSL_new(ctx));
  33249. /* load again as old names are responsibility of ctx to free*/
  33250. names = SSL_load_client_CA_file(cliCertFile);
  33251. AssertNotNull(names);
  33252. SSL_set_client_CA_list(ssl, names);
  33253. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33254. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33255. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33256. for (i=0; i<names_len; i++) {
  33257. AssertNotNull(name = sk_X509_NAME_value(names, i));
  33258. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  33259. }
  33260. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33261. {
  33262. tcp_ready ready;
  33263. func_args server_args;
  33264. callback_functions server_cb;
  33265. THREAD_TYPE serverThread;
  33266. WOLFSSL* ssl_client;
  33267. WOLFSSL_CTX* ctx_client;
  33268. SOCKET_T sockfd = 0;
  33269. /* wolfSSL_get_client_CA_list() with handshake */
  33270. StartTCP();
  33271. InitTcpReady(&ready);
  33272. XMEMSET(&server_args, 0, sizeof(func_args));
  33273. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33274. server_args.signal = &ready;
  33275. server_args.callbacks = &server_cb;
  33276. /* we are responsible for free'ing WOLFSSL_CTX */
  33277. server_cb.ctx = ctx;
  33278. server_cb.isSharedCtx = 1;
  33279. AssertIntEQ(WOLFSSL_SUCCESS,
  33280. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33281. start_thread(test_server_nofail, &server_args, &serverThread);
  33282. wait_tcp_ready(&server_args);
  33283. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33284. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33285. AssertIntEQ(WOLFSSL_SUCCESS,
  33286. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  33287. AssertIntEQ(WOLFSSL_SUCCESS,
  33288. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33289. AssertIntEQ(WOLFSSL_SUCCESS,
  33290. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33291. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  33292. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33293. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33294. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33295. /* We are expecting two cert names to be sent */
  33296. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  33297. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33298. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33299. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  33300. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  33301. }
  33302. wolfSSL_shutdown(ssl_client);
  33303. wolfSSL_free(ssl_client);
  33304. wolfSSL_CTX_free(ctx_client);
  33305. join_thread(serverThread);
  33306. FreeTcpReady(&ready);
  33307. }
  33308. #endif
  33309. wolfSSL_free(ssl);
  33310. wolfSSL_CTX_free(ctx);
  33311. res = TEST_RES_CHECK(1);
  33312. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33313. * !NO_BIO */
  33314. return res;
  33315. }
  33316. static int test_wolfSSL_CTX_add_client_CA(void)
  33317. {
  33318. int res = TEST_SKIPPED;
  33319. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33320. !defined(NO_WOLFSSL_CLIENT)
  33321. WOLFSSL_CTX* ctx;
  33322. WOLFSSL_X509* x509;
  33323. WOLFSSL_X509* x509_a;
  33324. STACK_OF(X509_NAME)* ca_list;
  33325. int ret = 0;
  33326. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33327. /* Add client cert */
  33328. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  33329. AssertNotNull(x509);
  33330. ret = SSL_CTX_add_client_CA(ctx, x509);
  33331. AssertIntEQ(ret, SSL_SUCCESS);
  33332. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33333. /* Add another client cert */
  33334. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33335. SSL_FILETYPE_PEM));
  33336. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33337. /* test for incorrect parameter */
  33338. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33339. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33340. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33341. X509_free(x509);
  33342. X509_free(x509_a);
  33343. SSL_CTX_free(ctx);
  33344. res = TEST_RES_CHECK(1);
  33345. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33346. return res;
  33347. }
  33348. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  33349. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33350. {
  33351. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33352. WOLFSSL_CTX* ctx = callbacks->ctx;
  33353. WOLFSSL* ssl = NULL;
  33354. SOCKET_T sfd = 0;
  33355. SOCKET_T cfd = 0;
  33356. word16 port;
  33357. char input[1024];
  33358. int idx;
  33359. int ret, err = 0;
  33360. const char* privateName = "ech-private-name.com";
  33361. int privateNameLen = (int)XSTRLEN(privateName);
  33362. ((func_args*)args)->return_code = TEST_FAIL;
  33363. port = ((func_args*)args)->signal->port;
  33364. AssertIntEQ(WOLFSSL_SUCCESS,
  33365. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33366. AssertIntEQ(WOLFSSL_SUCCESS,
  33367. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33368. WOLFSSL_FILETYPE_PEM));
  33369. AssertIntEQ(WOLFSSL_SUCCESS,
  33370. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33371. WOLFSSL_FILETYPE_PEM));
  33372. if (callbacks->ctx_ready)
  33373. callbacks->ctx_ready(ctx);
  33374. ssl = wolfSSL_new(ctx);
  33375. /* set the sni for the server */
  33376. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33377. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33378. CloseSocket(sfd);
  33379. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33380. if (callbacks->ssl_ready)
  33381. callbacks->ssl_ready(ssl);
  33382. do {
  33383. err = 0; /* Reset error */
  33384. ret = wolfSSL_accept(ssl);
  33385. if (ret != WOLFSSL_SUCCESS) {
  33386. err = wolfSSL_get_error(ssl, 0);
  33387. }
  33388. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33389. if (ret != WOLFSSL_SUCCESS) {
  33390. char buff[WOLFSSL_MAX_ERROR_SZ];
  33391. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33392. }
  33393. else {
  33394. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33395. input[idx] = 0;
  33396. printf("Client message: %s\n", input);
  33397. }
  33398. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33399. privateNameLen));
  33400. ((func_args*)args)->return_code = TEST_SUCCESS;
  33401. }
  33402. if (callbacks->on_result)
  33403. callbacks->on_result(ssl);
  33404. wolfSSL_shutdown(ssl);
  33405. wolfSSL_free(ssl);
  33406. wolfSSL_CTX_free(ctx);
  33407. CloseSocket(cfd);
  33408. #ifdef FP_ECC
  33409. wc_ecc_fp_free();
  33410. #endif
  33411. return 0;
  33412. }
  33413. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33414. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33415. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  33416. {
  33417. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33418. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  33419. WOLFSSL* ssl = NULL;
  33420. SOCKET_T sfd = 0;
  33421. SOCKET_T cfd = 0;
  33422. word16 port;
  33423. char msg[] = "I hear you fa shizzle!";
  33424. int len = (int) XSTRLEN(msg);
  33425. char input[1024];
  33426. int idx;
  33427. int ret, err = 0;
  33428. #ifdef WOLFSSL_TIRTOS
  33429. fdOpenSession(Task_self());
  33430. #endif
  33431. ((func_args*)args)->return_code = TEST_FAIL;
  33432. port = ((func_args*)args)->signal->port;
  33433. AssertIntEQ(WOLFSSL_SUCCESS,
  33434. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33435. AssertIntEQ(WOLFSSL_SUCCESS,
  33436. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33437. WOLFSSL_FILETYPE_PEM));
  33438. AssertIntEQ(WOLFSSL_SUCCESS,
  33439. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33440. WOLFSSL_FILETYPE_PEM));
  33441. if (callbacks->ctx_ready)
  33442. callbacks->ctx_ready(ctx);
  33443. ssl = wolfSSL_new(ctx);
  33444. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33445. CloseSocket(sfd);
  33446. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33447. if (callbacks->ssl_ready)
  33448. callbacks->ssl_ready(ssl);
  33449. do {
  33450. err = 0; /* Reset error */
  33451. ret = wolfSSL_accept(ssl);
  33452. if (ret != WOLFSSL_SUCCESS) {
  33453. err = wolfSSL_get_error(ssl, 0);
  33454. }
  33455. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33456. if (ret != WOLFSSL_SUCCESS) {
  33457. char buff[WOLFSSL_MAX_ERROR_SZ];
  33458. fprintf(stderr, "error = %d, %s\n", err,
  33459. wolfSSL_ERR_error_string(err, buff));
  33460. }
  33461. else {
  33462. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33463. input[idx] = 0;
  33464. fprintf(stderr, "Client message: %s\n", input);
  33465. }
  33466. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  33467. #ifdef WOLFSSL_TIRTOS
  33468. Task_yield();
  33469. #endif
  33470. ((func_args*)args)->return_code = TEST_SUCCESS;
  33471. }
  33472. if (callbacks->on_result)
  33473. callbacks->on_result(ssl);
  33474. wolfSSL_shutdown(ssl);
  33475. wolfSSL_free(ssl);
  33476. wolfSSL_CTX_free(ctx);
  33477. CloseSocket(cfd);
  33478. #ifdef WOLFSSL_TIRTOS
  33479. fdCloseSession(Task_self());
  33480. #endif
  33481. #ifndef WOLFSSL_TIRTOS
  33482. return 0;
  33483. #endif
  33484. }
  33485. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33486. {
  33487. AssertNotNull(ssl);
  33488. AssertNotNull(line);
  33489. XFILE fp;
  33490. const byte lf = '\n';
  33491. fp = XFOPEN("./MyKeyLog.txt", "a");
  33492. XFWRITE( line, 1, strlen(line),fp);
  33493. XFWRITE( (void*)&lf,1,1,fp);
  33494. XFCLOSE(fp);
  33495. }
  33496. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33497. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33498. {
  33499. int res = TEST_SKIPPED;
  33500. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33501. !defined(NO_WOLFSSL_CLIENT)
  33502. SSL_CTX* ctx;
  33503. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33504. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33505. SSL_CTX_free(ctx);
  33506. SSL_CTX_set_keylog_callback(NULL, NULL);
  33507. res = TEST_RES_CHECK(1);
  33508. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33509. return res;
  33510. }
  33511. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33512. {
  33513. int res = TEST_SKIPPED;
  33514. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33515. !defined(NO_WOLFSSL_CLIENT)
  33516. SSL_CTX* ctx;
  33517. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33518. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33519. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33520. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33521. SSL_CTX_set_keylog_callback(ctx, NULL );
  33522. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33523. SSL_CTX_free(ctx);
  33524. res = TEST_RES_CHECK(1);
  33525. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33526. return res;
  33527. }
  33528. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33529. {
  33530. int res = TEST_SKIPPED;
  33531. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33532. /* This test is intended for checking whether keylog callback is called
  33533. * in client during TLS handshake between the client and a server.
  33534. */
  33535. tcp_ready ready;
  33536. func_args client_args;
  33537. func_args server_args;
  33538. THREAD_TYPE serverThread;
  33539. callback_functions server_cbf;
  33540. callback_functions client_cbf;
  33541. SOCKET_T sockfd = 0;
  33542. WOLFSSL_CTX* ctx;
  33543. WOLFSSL* ssl;
  33544. XFILE fp;
  33545. char msg[64] = "hello wolfssl!";
  33546. char reply[1024];
  33547. int msgSz = (int)XSTRLEN(msg);
  33548. #ifdef WOLFSSL_TIRTOS
  33549. fdOpenSession(Task_self());
  33550. #endif
  33551. InitTcpReady(&ready);
  33552. ready.port = 22222;
  33553. XMEMSET(&client_args, 0, sizeof(func_args));
  33554. XMEMSET(&server_args, 0, sizeof(func_args));
  33555. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33556. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33557. server_cbf.method = wolfTLSv1_2_server_method;
  33558. server_args.callbacks = &server_cbf;
  33559. server_args.signal = &ready;
  33560. /* clean up keylog file */
  33561. fp = XFOPEN("./MyKeyLog.txt", "w");
  33562. XFCLOSE(fp);
  33563. /* start server task */
  33564. start_thread(server_task, &server_args, &serverThread);
  33565. wait_tcp_ready(&server_args);
  33566. /* run as a TLS1.2 client */
  33567. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33568. AssertIntEQ(WOLFSSL_SUCCESS,
  33569. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33570. AssertIntEQ(WOLFSSL_SUCCESS,
  33571. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33572. AssertIntEQ(WOLFSSL_SUCCESS,
  33573. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33574. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33575. /* set keylog callback */
  33576. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  33577. /* get connected the server task */
  33578. AssertNotNull(ssl = wolfSSL_new(ctx));
  33579. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33580. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33581. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  33582. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  33583. wolfSSL_shutdown(ssl);
  33584. wolfSSL_free(ssl);
  33585. wolfSSL_CTX_free(ctx);
  33586. CloseSocket(sockfd);
  33587. join_thread(serverThread);
  33588. FreeTcpReady(&ready);
  33589. #ifdef WOLFSSL_TIRTOS
  33590. fdOpenSession(Task_self());
  33591. #endif
  33592. /* check if the keylog file exists */
  33593. char buff[300] = {0};
  33594. int found = 0;
  33595. fp = XFOPEN("./MyKeyLog.txt", "r");
  33596. AssertNotNull(fp);
  33597. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  33598. if (0 == strncmp(buff,"CLIENT_RANDOM ",
  33599. sizeof("CLIENT_RANDOM ")-1)) {
  33600. found = 1;
  33601. break;
  33602. }
  33603. }
  33604. XFCLOSE(fp);
  33605. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33606. AssertNotNull( found );
  33607. res = TEST_RES_CHECK(1);
  33608. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33609. return res;
  33610. }
  33611. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33612. {
  33613. int res = TEST_SKIPPED;
  33614. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33615. defined(HAVE_SECRET_CALLBACK)
  33616. /* This test is intended for checking whether keylog callback is called
  33617. * in client during TLS handshake between the client and a server.
  33618. */
  33619. tcp_ready ready;
  33620. func_args client_args;
  33621. func_args server_args;
  33622. THREAD_TYPE serverThread;
  33623. callback_functions server_cbf;
  33624. callback_functions client_cbf;
  33625. SOCKET_T sockfd = 0;
  33626. WOLFSSL_CTX* ctx;
  33627. WOLFSSL* ssl;
  33628. XFILE fp;
  33629. char msg[64] = "hello wolfssl!";
  33630. char reply[1024];
  33631. int msgSz = (int)XSTRLEN(msg);
  33632. #ifdef WOLFSSL_TIRTOS
  33633. fdOpenSession(Task_self());
  33634. #endif
  33635. InitTcpReady(&ready);
  33636. ready.port = 22222;
  33637. XMEMSET(&client_args, 0, sizeof(func_args));
  33638. XMEMSET(&server_args, 0, sizeof(func_args));
  33639. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33640. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33641. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33642. server_args.callbacks = &server_cbf;
  33643. server_args.signal = &ready;
  33644. /* clean up keylog file */
  33645. fp = XFOPEN("./MyKeyLog.txt", "w");
  33646. XFCLOSE(fp);
  33647. /* start server task */
  33648. start_thread(server_task, &server_args, &serverThread);
  33649. wait_tcp_ready(&server_args);
  33650. /* run as a TLS1.3 client */
  33651. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33652. AssertIntEQ(WOLFSSL_SUCCESS,
  33653. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33654. AssertIntEQ(WOLFSSL_SUCCESS,
  33655. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33656. AssertIntEQ(WOLFSSL_SUCCESS,
  33657. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33658. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33659. /* set keylog callback */
  33660. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  33661. /* get connected the server task */
  33662. AssertNotNull(ssl = wolfSSL_new(ctx));
  33663. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33664. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33665. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  33666. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  33667. wolfSSL_free(ssl);
  33668. wolfSSL_CTX_free(ctx);
  33669. join_thread(serverThread);
  33670. FreeTcpReady(&ready);
  33671. #ifdef WOLFSSL_TIRTOS
  33672. fdOpenSession(Task_self());
  33673. #endif
  33674. /* check if the keylog file exists */
  33675. {
  33676. char buff[300] = {0};
  33677. int found[4] = {0};
  33678. int numfnd = 0;
  33679. int i;
  33680. fp = XFOPEN("./MyKeyLog.txt", "r");
  33681. AssertNotNull(fp);
  33682. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  33683. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33684. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33685. found[0] = 1;
  33686. continue;
  33687. }
  33688. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33689. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33690. found[1] = 1;
  33691. continue;
  33692. }
  33693. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  33694. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33695. found[2] = 1;
  33696. continue;
  33697. }
  33698. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  33699. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33700. found[3] = 1;
  33701. continue;
  33702. }
  33703. }
  33704. XFCLOSE(fp);
  33705. for (i = 0; i < 4; i++) {
  33706. if (found[i] != 0)
  33707. numfnd++;
  33708. }
  33709. AssertIntEQ(numfnd, 4);
  33710. }
  33711. res = TEST_RES_CHECK(1);
  33712. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33713. return res;
  33714. }
  33715. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  33716. static int test_wolfSSL_Tls13_ECH_params(void)
  33717. {
  33718. #if !defined(NO_WOLFSSL_CLIENT)
  33719. word32 outputLen = 0;
  33720. byte testBuf[72];
  33721. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33722. WOLFSSL *ssl = wolfSSL_new(ctx);
  33723. AssertNotNull(ctx);
  33724. AssertNotNull(ssl);
  33725. /* invalid ctx */
  33726. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33727. "ech-public-name.com", 0, 0, 0));
  33728. /* invalid public name */
  33729. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33730. 0, 0));
  33731. /* invalid algorithms */
  33732. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33733. "ech-public-name.com", 1000, 1000, 1000));
  33734. /* invalid ctx */
  33735. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33736. &outputLen));
  33737. /* invalid output len */
  33738. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33739. /* invalid ssl */
  33740. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33741. (char*)testBuf, sizeof(testBuf)));
  33742. /* invalid configs64 */
  33743. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33744. sizeof(testBuf)));
  33745. /* invalid size */
  33746. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33747. (char*)testBuf, 0));
  33748. /* invalid ssl */
  33749. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33750. sizeof(testBuf)));
  33751. /* invalid configs */
  33752. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33753. sizeof(testBuf)));
  33754. /* invalid size */
  33755. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33756. /* invalid ssl */
  33757. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33758. /* invalid size */
  33759. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33760. wolfSSL_free(ssl);
  33761. wolfSSL_CTX_free(ctx);
  33762. #endif /* !NO_WOLFSSL_CLIENT */
  33763. return TEST_SUCCESS;
  33764. }
  33765. static int test_wolfSSL_Tls13_ECH(void)
  33766. {
  33767. tcp_ready ready;
  33768. func_args client_args;
  33769. func_args server_args;
  33770. THREAD_TYPE serverThread;
  33771. callback_functions server_cbf;
  33772. callback_functions client_cbf;
  33773. SOCKET_T sockfd = 0;
  33774. WOLFSSL_CTX* ctx;
  33775. WOLFSSL* ssl;
  33776. const char* publicName = "ech-public-name.com";
  33777. const char* privateName = "ech-private-name.com";
  33778. int privateNameLen = 20;
  33779. char reply[1024];
  33780. int replyLen = 0;
  33781. byte rawEchConfig[128];
  33782. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33783. InitTcpReady(&ready);
  33784. ready.port = 22222;
  33785. XMEMSET(&client_args, 0, sizeof(func_args));
  33786. XMEMSET(&server_args, 0, sizeof(func_args));
  33787. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33788. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33789. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33790. /* create the server context here so we can get the ech config */
  33791. AssertNotNull(server_cbf.ctx =
  33792. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33793. /* generate ech config */
  33794. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33795. publicName, 0, 0, 0));
  33796. /* get the config for the client to use */
  33797. AssertIntEQ(WOLFSSL_SUCCESS,
  33798. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33799. &rawEchConfigLen));
  33800. server_args.callbacks = &server_cbf;
  33801. server_args.signal = &ready;
  33802. /* start server task */
  33803. start_thread(server_task_ech, &server_args, &serverThread);
  33804. wait_tcp_ready(&server_args);
  33805. /* run as a TLS1.3 client */
  33806. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33807. AssertIntEQ(WOLFSSL_SUCCESS,
  33808. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33809. AssertIntEQ(WOLFSSL_SUCCESS,
  33810. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33811. AssertIntEQ(WOLFSSL_SUCCESS,
  33812. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33813. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33814. /* get connected the server task */
  33815. AssertNotNull(ssl = wolfSSL_new(ctx));
  33816. /* set the ech configs for the client */
  33817. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33818. rawEchConfigLen));
  33819. /* set the sni for the client */
  33820. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33821. privateName, privateNameLen));
  33822. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33823. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33824. AssertIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33825. privateNameLen);
  33826. AssertIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33827. /* add th null terminator for string compare */
  33828. reply[replyLen] = 0;
  33829. /* check that the server replied with the private name */
  33830. AssertStrEQ(privateName, reply);
  33831. wolfSSL_free(ssl);
  33832. wolfSSL_CTX_free(ctx);
  33833. join_thread(serverThread);
  33834. FreeTcpReady(&ready);
  33835. return TEST_SUCCESS;
  33836. }
  33837. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33838. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33839. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33840. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33841. static void post_auth_version_cb(WOLFSSL* ssl)
  33842. {
  33843. /* do handshake and then test version error */
  33844. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33845. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33846. }
  33847. static void post_auth_version_client_cb(WOLFSSL* ssl)
  33848. {
  33849. /* do handshake and then test version error */
  33850. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33851. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33852. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33853. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33854. /* check was added to error queue */
  33855. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33856. /* check the string matches expected string */
  33857. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33858. "WRONG_SSL_VERSION");
  33859. #endif
  33860. }
  33861. static void post_auth_cb(WOLFSSL* ssl)
  33862. {
  33863. WOLFSSL_X509* x509;
  33864. /* do handshake and then test version error */
  33865. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33866. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33867. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33868. wolfSSL_X509_free(x509);
  33869. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33870. }
  33871. static void set_post_auth_cb(WOLFSSL* ssl)
  33872. {
  33873. if (!wolfSSL_is_server(ssl)) {
  33874. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33875. }
  33876. else {
  33877. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33878. }
  33879. }
  33880. #endif
  33881. static int test_wolfSSL_Tls13_postauth(void)
  33882. {
  33883. int res = TEST_SKIPPED;
  33884. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33885. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33886. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33887. tcp_ready ready;
  33888. func_args client_args;
  33889. func_args server_args;
  33890. callback_functions server_cbf;
  33891. callback_functions client_cbf;
  33892. THREAD_TYPE serverThread;
  33893. XMEMSET(&client_args, 0, sizeof(func_args));
  33894. XMEMSET(&server_args, 0, sizeof(func_args));
  33895. StartTCP();
  33896. InitTcpReady(&ready);
  33897. #if defined(USE_WINDOWS_API)
  33898. /* use RNG to get random port if using windows */
  33899. ready.port = GetRandomPort();
  33900. #endif
  33901. server_args.signal = &ready;
  33902. client_args.signal = &ready;
  33903. /* test version failure doing post auth with TLS 1.2 connection */
  33904. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33905. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33906. server_cbf.method = wolfTLSv1_2_server_method;
  33907. server_cbf.ssl_ready = set_post_auth_cb;
  33908. server_cbf.on_result = post_auth_version_cb;
  33909. client_cbf.ssl_ready = set_post_auth_cb;
  33910. client_cbf.on_result = post_auth_version_client_cb;
  33911. server_args.callbacks = &server_cbf;
  33912. client_args.callbacks = &client_cbf;
  33913. start_thread(test_server_nofail, &server_args, &serverThread);
  33914. wait_tcp_ready(&server_args);
  33915. test_client_nofail(&client_args, NULL);
  33916. join_thread(serverThread);
  33917. /* tests on post auth with TLS 1.3 */
  33918. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33919. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33920. server_cbf.method = wolfTLSv1_3_server_method;
  33921. server_cbf.ssl_ready = set_post_auth_cb;
  33922. client_cbf.ssl_ready = set_post_auth_cb;
  33923. server_cbf.on_result = post_auth_cb;
  33924. client_cbf.on_result = NULL;
  33925. server_args.callbacks = &server_cbf;
  33926. client_args.callbacks = &client_cbf;
  33927. start_thread(test_server_nofail, &server_args, &serverThread);
  33928. wait_tcp_ready(&server_args);
  33929. test_client_nofail(&client_args, NULL);
  33930. join_thread(serverThread);
  33931. FreeTcpReady(&ready);
  33932. res = TEST_RES_CHECK(1);
  33933. #endif
  33934. return res;
  33935. }
  33936. static int test_wolfSSL_X509_NID(void)
  33937. {
  33938. int res = TEST_SKIPPED;
  33939. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33940. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33941. int sigType;
  33942. int nameSz;
  33943. X509* cert;
  33944. EVP_PKEY* pubKeyTmp;
  33945. X509_NAME* name;
  33946. char commonName[80];
  33947. char countryName[80];
  33948. char localityName[80];
  33949. char stateName[80];
  33950. char orgName[80];
  33951. char orgUnit[80];
  33952. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33953. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33954. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  33955. sizeof_client_cert_der_2048));
  33956. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33957. /* extract PUBLIC KEY from cert */
  33958. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33959. /* extract signatureType */
  33960. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33961. /* extract subjectName info */
  33962. AssertNotNull(name = X509_get_subject_name(cert));
  33963. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33964. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33965. NULL, 0)), 0);
  33966. AssertIntEQ(nameSz, 15);
  33967. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33968. commonName, sizeof(commonName))), 0);
  33969. AssertIntEQ(nameSz, 15);
  33970. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33971. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33972. commonName, 9)), 0);
  33973. AssertIntEQ(nameSz, 8);
  33974. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33975. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33976. countryName, sizeof(countryName))), 0);
  33977. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33978. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33979. localityName, sizeof(localityName))), 0);
  33980. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33981. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  33982. stateName, sizeof(stateName))), 0);
  33983. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33984. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33985. orgName, sizeof(orgName))), 0);
  33986. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33987. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  33988. orgUnit, sizeof(orgUnit))), 0);
  33989. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33990. EVP_PKEY_free(pubKeyTmp);
  33991. X509_free(cert);
  33992. res = TEST_RES_CHECK(1);
  33993. #endif
  33994. return res;
  33995. }
  33996. static int test_wolfSSL_CTX_set_srp_username(void)
  33997. {
  33998. int res = TEST_SKIPPED;
  33999. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34000. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34001. WOLFSSL_CTX* ctx;
  34002. WOLFSSL* ssl;
  34003. const char *username = "TESTUSER";
  34004. const char *password = "TESTPASSWORD";
  34005. int r;
  34006. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34007. AssertNotNull(ctx);
  34008. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  34009. AssertIntEQ(r,SSL_SUCCESS);
  34010. wolfSSL_CTX_free(ctx);
  34011. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34012. AssertNotNull(ctx);
  34013. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34014. AssertIntEQ(r,SSL_SUCCESS);
  34015. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  34016. AssertIntEQ(r,SSL_SUCCESS);
  34017. AssertNotNull(ssl = SSL_new(ctx));
  34018. AssertNotNull(SSL_get_srp_username(ssl));
  34019. AssertStrEQ(SSL_get_srp_username(ssl), username);
  34020. wolfSSL_free(ssl);
  34021. wolfSSL_CTX_free(ctx);
  34022. res = TEST_RES_CHECK(1);
  34023. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34024. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34025. return res;
  34026. }
  34027. static int test_wolfSSL_CTX_set_srp_password(void)
  34028. {
  34029. int res = TEST_SKIPPED;
  34030. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34031. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34032. WOLFSSL_CTX* ctx;
  34033. const char *username = "TESTUSER";
  34034. const char *password = "TESTPASSWORD";
  34035. int r;
  34036. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34037. AssertNotNull(ctx);
  34038. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34039. AssertIntEQ(r,SSL_SUCCESS);
  34040. wolfSSL_CTX_free(ctx);
  34041. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  34042. AssertNotNull(ctx);
  34043. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  34044. AssertIntEQ(r,SSL_SUCCESS);
  34045. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  34046. AssertIntEQ(r,SSL_SUCCESS);
  34047. wolfSSL_CTX_free(ctx);
  34048. res = TEST_RES_CHECK(1);
  34049. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34050. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34051. return res;
  34052. }
  34053. static int test_wolfSSL_X509_STORE(void)
  34054. {
  34055. int res = TEST_SKIPPED;
  34056. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34057. X509_STORE *store;
  34058. #ifdef HAVE_CRL
  34059. X509_STORE_CTX *storeCtx;
  34060. X509_CRL *crl;
  34061. X509 *ca, *cert;
  34062. const char crlPem[] = "./certs/crl/crl.revoked";
  34063. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34064. const char caCert[] = "./certs/ca-cert.pem";
  34065. XFILE fp;
  34066. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34067. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34068. SSL_FILETYPE_PEM)));
  34069. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34070. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34071. SSL_FILETYPE_PEM)));
  34072. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  34073. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34074. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34075. X509_STORE_free(store);
  34076. X509_STORE_CTX_free(storeCtx);
  34077. X509_free(cert);
  34078. X509_free(ca);
  34079. /* should fail to verify now after adding in CRL */
  34080. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34081. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34082. SSL_FILETYPE_PEM)));
  34083. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34084. fp = XFOPEN(crlPem, "rb");
  34085. AssertTrue((fp != XBADFILE));
  34086. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34087. NULL, NULL));
  34088. XFCLOSE(fp);
  34089. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34090. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34091. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  34092. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34093. SSL_FILETYPE_PEM)));
  34094. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34095. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34096. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  34097. X509_CRL_free(crl);
  34098. X509_STORE_free(store);
  34099. X509_STORE_CTX_free(storeCtx);
  34100. X509_free(cert);
  34101. X509_free(ca);
  34102. #endif /* HAVE_CRL */
  34103. #ifndef WOLFCRYPT_ONLY
  34104. {
  34105. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34106. SSL_CTX* ctx;
  34107. SSL* ssl;
  34108. int i;
  34109. for (i = 0; i < 2; i++) {
  34110. #ifndef NO_WOLFSSL_SERVER
  34111. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34112. #else
  34113. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34114. #endif
  34115. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34116. SSL_CTX_set_cert_store(ctx, store);
  34117. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34118. SSL_CTX_set_cert_store(ctx, store);
  34119. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  34120. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34121. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34122. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34123. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34124. AssertNotNull(ssl = SSL_new(ctx));
  34125. if (i == 0) {
  34126. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  34127. }
  34128. else {
  34129. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34130. X509_STORE_free(store);
  34131. }
  34132. SSL_free(ssl);
  34133. SSL_CTX_free(ctx);
  34134. }
  34135. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34136. }
  34137. #endif
  34138. res = TEST_RES_CHECK(1);
  34139. #endif
  34140. return res;
  34141. }
  34142. static int test_wolfSSL_X509_STORE_load_locations(void)
  34143. {
  34144. int res = TEST_SKIPPED;
  34145. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34146. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34147. SSL_CTX *ctx;
  34148. X509_STORE *store;
  34149. const char ca_file[] = "./certs/ca-cert.pem";
  34150. const char client_pem_file[] = "./certs/client-cert.pem";
  34151. const char client_der_file[] = "./certs/client-cert.der";
  34152. const char ecc_file[] = "./certs/ecc-key.pem";
  34153. const char certs_path[] = "./certs/";
  34154. const char bad_path[] = "./bad-path/";
  34155. #ifdef HAVE_CRL
  34156. const char crl_path[] = "./certs/crl/";
  34157. const char crl_file[] = "./certs/crl/crl.pem";
  34158. #endif
  34159. #ifndef NO_WOLFSSL_SERVER
  34160. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34161. #else
  34162. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34163. #endif
  34164. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  34165. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  34166. /* Test bad arguments */
  34167. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  34168. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34169. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  34170. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  34171. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  34172. #ifdef HAVE_CRL
  34173. /* Test with CRL */
  34174. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  34175. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  34176. #endif
  34177. /* Test with CA */
  34178. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  34179. /* Test with client_cert and certs path */
  34180. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  34181. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  34182. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34183. /* Clear nodes */
  34184. ERR_clear_error();
  34185. #endif
  34186. SSL_CTX_free(ctx);
  34187. res = TEST_RES_CHECK(1);
  34188. #endif
  34189. return res;
  34190. }
  34191. static int test_X509_STORE_get0_objects(void)
  34192. {
  34193. int res = TEST_SKIPPED;
  34194. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34195. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34196. X509_STORE *store;
  34197. X509_STORE *store_cpy;
  34198. SSL_CTX *ctx;
  34199. X509_OBJECT *obj;
  34200. STACK_OF(X509_OBJECT) *objs;
  34201. int i;
  34202. /* Setup store */
  34203. #ifndef NO_WOLFSSL_SERVER
  34204. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34205. #else
  34206. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34207. #endif
  34208. AssertNotNull(store_cpy = X509_STORE_new());
  34209. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  34210. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  34211. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  34212. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  34213. #ifdef HAVE_CRL
  34214. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  34215. #endif
  34216. /* Store ready */
  34217. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34218. AssertNotNull(objs = X509_STORE_get0_objects(store));
  34219. #ifdef HAVE_CRL
  34220. #ifdef WOLFSSL_SIGNER_DER_CERT
  34221. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  34222. #else
  34223. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  34224. #endif
  34225. #else
  34226. #ifdef WOLFSSL_SIGNER_DER_CERT
  34227. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  34228. #else
  34229. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  34230. #endif
  34231. #endif
  34232. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34233. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34234. switch (X509_OBJECT_get_type(obj)) {
  34235. case X509_LU_X509:
  34236. AssertNotNull(X509_OBJECT_get0_X509(obj));
  34237. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  34238. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  34239. break;
  34240. case X509_LU_CRL:
  34241. #ifdef HAVE_CRL
  34242. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  34243. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  34244. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  34245. break;
  34246. #endif
  34247. case X509_LU_NONE:
  34248. default:
  34249. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34250. "(when built with crl support)"),
  34251. ("Unrecognized X509_OBJECT type or none"));
  34252. }
  34253. }
  34254. X509_STORE_free(store_cpy);
  34255. SSL_CTX_free(ctx);
  34256. res = TEST_RES_CHECK(1);
  34257. #endif
  34258. return res;
  34259. }
  34260. static int test_wolfSSL_BN_CTX(void)
  34261. {
  34262. int res = TEST_SKIPPED;
  34263. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34264. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34265. WOLFSSL_BN_CTX* bn_ctx;
  34266. WOLFSSL_BIGNUM* t;
  34267. AssertNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34268. /* No implementation. */
  34269. BN_CTX_init(NULL);
  34270. AssertNotNull(t = BN_CTX_get(NULL));
  34271. BN_free(t);
  34272. AssertNotNull(t = BN_CTX_get(bn_ctx));
  34273. BN_free(t);
  34274. #ifndef NO_WOLFSSL_STUB
  34275. /* No implementation. */
  34276. BN_CTX_start(NULL);
  34277. BN_CTX_start(bn_ctx);
  34278. #endif
  34279. BN_CTX_free(NULL);
  34280. BN_CTX_free(bn_ctx);
  34281. res = TEST_RES_CHECK(1);
  34282. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34283. return res;
  34284. }
  34285. static int test_wolfSSL_BN(void)
  34286. {
  34287. int res = TEST_SKIPPED;
  34288. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34289. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34290. BIGNUM* a;
  34291. BIGNUM* b;
  34292. BIGNUM* c;
  34293. BIGNUM* d;
  34294. BIGNUM emptyBN;
  34295. /* Setup */
  34296. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34297. /* internal not set emptyBN. */
  34298. AssertNotNull(a = BN_new());
  34299. AssertNotNull(b = BN_new());
  34300. AssertNotNull(c = BN_dup(b));
  34301. AssertNotNull(d = BN_new());
  34302. /* Invalid parameter testing. */
  34303. BN_free(NULL);
  34304. AssertNull(BN_dup(NULL));
  34305. AssertNull(BN_dup(&emptyBN));
  34306. AssertNull(BN_copy(NULL, NULL));
  34307. AssertNull(BN_copy(b, NULL));
  34308. AssertNull(BN_copy(NULL, c));
  34309. AssertNull(BN_copy(b, &emptyBN));
  34310. AssertNull(BN_copy(&emptyBN, c));
  34311. BN_clear(NULL);
  34312. BN_clear(&emptyBN);
  34313. AssertIntEQ(BN_num_bytes(NULL), 0);
  34314. AssertIntEQ(BN_num_bytes(&emptyBN), 0);
  34315. AssertIntEQ(BN_num_bits(NULL), 0);
  34316. AssertIntEQ(BN_num_bits(&emptyBN), 0);
  34317. AssertIntEQ(BN_is_negative(NULL), 0);
  34318. AssertIntEQ(BN_is_negative(&emptyBN), 0);
  34319. /* END Invalid Parameters */
  34320. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34321. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34322. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34323. AssertIntEQ(BN_num_bits(a), 2);
  34324. AssertIntEQ(BN_num_bytes(a), 1);
  34325. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34326. defined(WOLFSSL_SP_INT_NEGATIVE))
  34327. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34328. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34329. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34330. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34331. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34332. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34333. {
  34334. /* Do additional tests on negative BN conversions. */
  34335. char * ret;
  34336. ASN1_INTEGER * asn1;
  34337. BIGNUM * tmp;
  34338. /* Sanity check we have a negative BN. */
  34339. AssertIntEQ(BN_is_negative(c), 1);
  34340. AssertNotNull(ret = BN_bn2dec(c));
  34341. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34342. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34343. /* Convert to ASN1_INTEGER and back to BN. */
  34344. AssertNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34345. AssertNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34346. /* After converting back BN should be negative and correct. */
  34347. AssertIntEQ(BN_is_negative(tmp), 1);
  34348. AssertNotNull(ret = BN_bn2dec(tmp));
  34349. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34350. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34351. ASN1_INTEGER_free(asn1);
  34352. BN_free(tmp);
  34353. }
  34354. #endif
  34355. AssertIntEQ(BN_get_word(c), 4);
  34356. #endif
  34357. AssertIntEQ(BN_set_word(a, 3), 1);
  34358. AssertIntEQ(BN_set_word(b, 3), 1);
  34359. AssertIntEQ(BN_set_word(c, 4), 1);
  34360. /* NULL == NULL, NULL < num, num > NULL */
  34361. AssertIntEQ(BN_cmp(NULL, NULL), 0);
  34362. AssertIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34363. AssertIntLT(BN_cmp(NULL, b), 0);
  34364. AssertIntLT(BN_cmp(&emptyBN, b), 0);
  34365. AssertIntGT(BN_cmp(a, NULL), 0);
  34366. AssertIntGT(BN_cmp(a, &emptyBN), 0);
  34367. AssertIntEQ(BN_cmp(a, b), 0);
  34368. AssertIntLT(BN_cmp(a, c), 0);
  34369. AssertIntGT(BN_cmp(c, b), 0);
  34370. AssertIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34371. AssertIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34372. AssertIntEQ(BN_print_fp(stderr, NULL), 0);
  34373. AssertIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34374. AssertIntEQ(BN_print_fp(XBADFILE, a), 0);
  34375. AssertIntEQ(BN_print_fp(stderr, a), 1);
  34376. BN_clear(a);
  34377. BN_free(a);
  34378. BN_free(b);
  34379. BN_free(c);
  34380. BN_clear_free(d);
  34381. res = TEST_RES_CHECK(1);
  34382. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34383. return res;
  34384. }
  34385. static int test_wolfSSL_BN_init(void)
  34386. {
  34387. int res = TEST_SKIPPED;
  34388. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34389. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34390. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34391. BIGNUM* ap;
  34392. BIGNUM bv;
  34393. BIGNUM cv;
  34394. BIGNUM dv;
  34395. AssertNotNull(ap = BN_new());
  34396. BN_init(NULL);
  34397. XMEMSET(&bv, 0, sizeof(bv));
  34398. AssertNull(BN_dup(&bv));
  34399. BN_init(&bv);
  34400. BN_init(&cv);
  34401. BN_init(&dv);
  34402. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34403. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34404. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34405. /* a^b mod c = */
  34406. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34407. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34408. /* check result 3^2 mod 5 */
  34409. AssertIntEQ(BN_get_word(&dv), 4);
  34410. /* a*b mod c = */
  34411. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34412. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34413. /* check result 3*2 mod 5 */
  34414. AssertIntEQ(BN_get_word(&dv), 1);
  34415. BN_free(ap);
  34416. res = TEST_RES_CHECK(1);
  34417. #endif
  34418. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34419. return res;
  34420. }
  34421. static int test_wolfSSL_BN_enc_dec(void)
  34422. {
  34423. int res = TEST_SKIPPED;
  34424. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34425. BIGNUM* a;
  34426. BIGNUM* b;
  34427. BIGNUM* c = NULL;
  34428. BIGNUM emptyBN;
  34429. char* str;
  34430. const char* emptyStr = "";
  34431. const char* numberStr = "12345";
  34432. const char* badStr = "g12345";
  34433. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34434. const char* twoStr = "2";
  34435. #endif
  34436. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34437. unsigned char outNum[5];
  34438. /* Setup */
  34439. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34440. AssertNotNull(a = BN_new());
  34441. AssertNotNull(b = BN_new());
  34442. AssertIntEQ(BN_set_word(a, 2), 1);
  34443. /* Invalid parameters */
  34444. AssertIntEQ(BN_bn2bin(NULL, NULL), -1);
  34445. AssertIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34446. AssertIntEQ(BN_bn2bin(NULL, outNum), -1);
  34447. AssertIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34448. AssertNull(BN_bn2hex(NULL));
  34449. AssertNull(BN_bn2hex(&emptyBN));
  34450. AssertNull(BN_bn2dec(NULL));
  34451. AssertNull(BN_bn2dec(&emptyBN));
  34452. AssertNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34453. AssertNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34454. AssertNull(BN_bin2bn(binNum, -1, a));
  34455. AssertNull(BN_bin2bn(binNum, -1, NULL));
  34456. AssertNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34457. AssertIntEQ(BN_hex2bn(NULL, NULL), 0);
  34458. AssertIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34459. AssertIntEQ(BN_hex2bn(&a, NULL), 0);
  34460. AssertIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34461. AssertIntEQ(BN_hex2bn(&a, badStr), 0);
  34462. AssertIntEQ(BN_hex2bn(&c, badStr), 0);
  34463. AssertIntEQ(BN_dec2bn(NULL, NULL), 0);
  34464. AssertIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34465. AssertIntEQ(BN_dec2bn(&a, NULL), 0);
  34466. AssertIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34467. AssertIntEQ(BN_dec2bn(&a, badStr), 0);
  34468. AssertIntEQ(BN_dec2bn(&c, badStr), 0);
  34469. AssertIntEQ(BN_set_word(a, 2), 1);
  34470. AssertIntEQ(BN_bn2bin(a, NULL), 1);
  34471. AssertIntEQ(BN_bn2bin(a, outNum), 1);
  34472. AssertNotNull(BN_bin2bn(outNum, 1, b));
  34473. AssertIntEQ(BN_cmp(a, b), 0);
  34474. AssertNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34475. AssertIntEQ(BN_cmp(a, b), -1);
  34476. AssertNotNull(str = BN_bn2hex(a));
  34477. AssertNotNull(BN_hex2bn(&b, str));
  34478. AssertIntEQ(BN_cmp(a, b), 0);
  34479. AssertNotNull(BN_hex2bn(&b, numberStr));
  34480. AssertIntEQ(BN_cmp(a, b), -1);
  34481. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34482. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34483. AssertNotNull(str = BN_bn2dec(a));
  34484. AssertStrEQ(str, twoStr);
  34485. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34486. #ifndef NO_RSA
  34487. AssertNotNull(str = BN_bn2dec(a));
  34488. AssertNotNull(BN_dec2bn(&b, str));
  34489. AssertIntEQ(BN_cmp(a, b), 0);
  34490. AssertNotNull(BN_dec2bn(&b, numberStr));
  34491. AssertIntEQ(BN_cmp(a, b), -1);
  34492. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34493. #else
  34494. /* No implementation - fail with good parameters. */
  34495. AssertIntEQ(BN_dec2bn(&a, numberStr), 0);
  34496. #endif
  34497. #endif
  34498. BN_free(b);
  34499. BN_free(a);
  34500. res = TEST_RES_CHECK(1);
  34501. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34502. return res;
  34503. }
  34504. static int test_wolfSSL_BN_word(void)
  34505. {
  34506. int res = TEST_SKIPPED;
  34507. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34508. BIGNUM* a;
  34509. BIGNUM* b;
  34510. BIGNUM* c;
  34511. BIGNUM av;
  34512. AssertNotNull(a = BN_new());
  34513. AssertNotNull(b = BN_new());
  34514. AssertNotNull(c = BN_new());
  34515. XMEMSET(&av, 0, sizeof(av));
  34516. /* Invalid parameter. */
  34517. AssertIntEQ(BN_add_word(NULL, 3), 0);
  34518. AssertIntEQ(BN_add_word(&av, 3), 0);
  34519. AssertIntEQ(BN_sub_word(NULL, 3), 0);
  34520. AssertIntEQ(BN_sub_word(&av, 3), 0);
  34521. AssertIntEQ(BN_set_word(NULL, 3), 0);
  34522. AssertIntEQ(BN_set_word(&av, 3), 0);
  34523. AssertIntEQ(BN_get_word(NULL), 0);
  34524. AssertIntEQ(BN_get_word(&av), 0);
  34525. AssertIntEQ(BN_is_word(NULL, 3), 0);
  34526. AssertIntEQ(BN_is_word(&av, 3), 0);
  34527. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34528. !defined(NO_DSA))
  34529. AssertIntEQ(BN_mod_word(NULL, 3), -1);
  34530. AssertIntEQ(BN_mod_word(&av, 3), -1);
  34531. #endif
  34532. AssertIntEQ(BN_one(NULL), 0);
  34533. AssertIntEQ(BN_one(&av), 0);
  34534. BN_zero(NULL);
  34535. BN_zero(&av);
  34536. AssertIntEQ(BN_is_one(NULL), 0);
  34537. AssertIntEQ(BN_is_one(&av), 0);
  34538. AssertIntEQ(BN_is_zero(NULL), 0);
  34539. AssertIntEQ(BN_is_zero(&av), 0);
  34540. AssertIntEQ(BN_set_word(a, 3), 1);
  34541. AssertIntEQ(BN_set_word(b, 2), 1);
  34542. AssertIntEQ(BN_set_word(c, 5), 1);
  34543. /* a + 3 = */
  34544. AssertIntEQ(BN_add_word(a, 3), 1);
  34545. /* check result 3 + 3*/
  34546. AssertIntEQ(BN_get_word(a), 6);
  34547. AssertIntEQ(BN_is_word(a, 6), 1);
  34548. AssertIntEQ(BN_is_word(a, 5), 0);
  34549. /* set a back to 3 */
  34550. AssertIntEQ(BN_set_word(a, 3), 1);
  34551. /* a - 3 = */
  34552. AssertIntEQ(BN_sub_word(a, 3), 1);
  34553. /* check result 3 - 3*/
  34554. AssertIntEQ(BN_get_word(a), 0);
  34555. AssertIntEQ(BN_one(a), 1);
  34556. AssertIntEQ(BN_is_word(a, 1), 1);
  34557. AssertIntEQ(BN_is_word(a, 0), 0);
  34558. AssertIntEQ(BN_is_one(a), 1);
  34559. AssertIntEQ(BN_is_zero(a), 0);
  34560. BN_zero(a);
  34561. AssertIntEQ(BN_is_word(a, 0), 1);
  34562. AssertIntEQ(BN_is_word(a, 1), 0);
  34563. AssertIntEQ(BN_is_zero(a), 1);
  34564. AssertIntEQ(BN_is_one(a), 0);
  34565. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34566. !defined(NO_DSA))
  34567. AssertIntEQ(BN_set_word(a, 5), 1);
  34568. AssertIntEQ(BN_mod_word(a, 3), 2);
  34569. AssertIntEQ(BN_mod_word(a, 0), -1);
  34570. #endif
  34571. BN_free(c);
  34572. BN_free(b);
  34573. BN_free(a);
  34574. res = TEST_RES_CHECK(1);
  34575. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34576. return res;
  34577. }
  34578. static int test_wolfSSL_BN_bits(void)
  34579. {
  34580. int res = TEST_SKIPPED;
  34581. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34582. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34583. BIGNUM* a;
  34584. BIGNUM emptyBN;
  34585. /* Setup */
  34586. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34587. AssertNotNull(a = BN_new());
  34588. /* Invalid parameters. */
  34589. AssertIntEQ(BN_set_bit(NULL, 1), 0);
  34590. AssertIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34591. AssertIntEQ(BN_set_bit(a, -1), 0);
  34592. AssertIntEQ(BN_clear_bit(NULL, 1), 0);
  34593. AssertIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34594. AssertIntEQ(BN_clear_bit(a, -1), 0);
  34595. AssertIntEQ(BN_is_bit_set(NULL, 1), 0);
  34596. AssertIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34597. AssertIntEQ(BN_is_bit_set(a, -1), 0);
  34598. AssertIntEQ(BN_is_odd(NULL), 0);
  34599. AssertIntEQ(BN_is_odd(&emptyBN), 0);
  34600. AssertIntEQ(BN_set_word(a, 0), 1);
  34601. AssertIntEQ(BN_is_zero(a), 1);
  34602. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  34603. AssertIntEQ(BN_is_zero(a), 0);
  34604. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  34605. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  34606. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  34607. AssertIntEQ(BN_is_zero(a), 1);
  34608. AssertIntEQ(BN_set_bit(a, 0), 1);
  34609. AssertIntEQ(BN_is_odd(a), 1);
  34610. AssertIntEQ(BN_clear_bit(a, 0), 1);
  34611. AssertIntEQ(BN_is_odd(a), 0);
  34612. AssertIntEQ(BN_set_bit(a, 1), 1);
  34613. AssertIntEQ(BN_is_odd(a), 0);
  34614. AssertIntEQ(BN_set_bit(a, 129), 1);
  34615. AssertIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34616. #ifndef NO_WOLFSSL_STUB
  34617. AssertIntEQ(BN_mask_bits(a, 1), 0);
  34618. #endif
  34619. BN_free(a);
  34620. res = TEST_RES_CHECK(1);
  34621. #endif
  34622. return res;
  34623. }
  34624. static int test_wolfSSL_BN_shift(void)
  34625. {
  34626. int res = TEST_SKIPPED;
  34627. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34628. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34629. BIGNUM* a;
  34630. BIGNUM* b;
  34631. BIGNUM emptyBN;
  34632. /* Setup */
  34633. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34634. AssertNotNull(a = BN_new());
  34635. AssertNotNull(b = BN_new());
  34636. /* Invalid parameters. */
  34637. AssertIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34638. AssertIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34639. AssertIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34640. AssertIntEQ(BN_lshift(b, NULL, 1), 0);
  34641. AssertIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34642. AssertIntEQ(BN_lshift(NULL, a, 1), 0);
  34643. AssertIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34644. AssertIntEQ(BN_lshift(b, a, -1), 0);
  34645. AssertIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34646. AssertIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34647. AssertIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34648. AssertIntEQ(BN_rshift(b, NULL, 1), 0);
  34649. AssertIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34650. AssertIntEQ(BN_rshift(NULL, a, 1), 0);
  34651. AssertIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34652. AssertIntEQ(BN_rshift(b, a, -1), 0);
  34653. AssertIntEQ(BN_set_word(a, 1), 1);
  34654. AssertIntEQ(BN_lshift(b, a, 1), 1);
  34655. AssertIntEQ(BN_is_word(b, 2), 1);
  34656. AssertIntEQ(BN_lshift(a, a, 1), 1);
  34657. AssertIntEQ(BN_is_word(a, 2), 1);
  34658. AssertIntEQ(BN_rshift(b, a, 1), 1);
  34659. AssertIntEQ(BN_is_word(b, 1), 1);
  34660. AssertIntEQ(BN_rshift(a, a, 1), 1);
  34661. AssertIntEQ(BN_is_word(a, 1), 1);
  34662. BN_free(b);
  34663. BN_free(a);
  34664. res = TEST_RES_CHECK(1);
  34665. #endif
  34666. return res;
  34667. }
  34668. static int test_wolfSSL_BN_math(void)
  34669. {
  34670. int res = TEST_SKIPPED;
  34671. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34672. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34673. BIGNUM* a;
  34674. BIGNUM* b;
  34675. BIGNUM* r;
  34676. BIGNUM* rem;
  34677. BIGNUM emptyBN;
  34678. BN_ULONG val1;
  34679. BN_ULONG val2;
  34680. /* Setup */
  34681. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34682. AssertNotNull(a = BN_new());
  34683. AssertNotNull(b = BN_new());
  34684. AssertNotNull(r = BN_new());
  34685. AssertNotNull(rem = BN_new());
  34686. /* Invalid parameters. */
  34687. AssertIntEQ(BN_add(NULL, NULL, NULL), 0);
  34688. AssertIntEQ(BN_add(r, NULL, NULL), 0);
  34689. AssertIntEQ(BN_add(NULL, a, NULL), 0);
  34690. AssertIntEQ(BN_add(NULL, NULL, b), 0);
  34691. AssertIntEQ(BN_add(r, a, NULL), 0);
  34692. AssertIntEQ(BN_add(r, NULL, b), 0);
  34693. AssertIntEQ(BN_add(NULL, a, b), 0);
  34694. AssertIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34695. AssertIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34696. AssertIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34697. AssertIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34698. AssertIntEQ(BN_add(r, a, &emptyBN), 0);
  34699. AssertIntEQ(BN_add(r, &emptyBN, b), 0);
  34700. AssertIntEQ(BN_add(&emptyBN, a, b), 0);
  34701. AssertIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34702. AssertIntEQ(BN_sub(r, NULL, NULL), 0);
  34703. AssertIntEQ(BN_sub(NULL, a, NULL), 0);
  34704. AssertIntEQ(BN_sub(NULL, NULL, b), 0);
  34705. AssertIntEQ(BN_sub(r, a, NULL), 0);
  34706. AssertIntEQ(BN_sub(r, NULL, b), 0);
  34707. AssertIntEQ(BN_sub(NULL, a, b), 0);
  34708. AssertIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34709. AssertIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34710. AssertIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34711. AssertIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34712. AssertIntEQ(BN_sub(r, a, &emptyBN), 0);
  34713. AssertIntEQ(BN_sub(r, &emptyBN, b), 0);
  34714. AssertIntEQ(BN_sub(&emptyBN, a, b), 0);
  34715. AssertIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34716. AssertIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34717. AssertIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34718. AssertIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34719. AssertIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34720. AssertIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34721. AssertIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34722. AssertIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34723. AssertIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34724. AssertIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34725. AssertIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34726. AssertIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34727. AssertIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34728. AssertIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34729. AssertIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34730. AssertIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34731. AssertIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34732. AssertIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34733. AssertIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34734. AssertIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34735. AssertIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34736. AssertIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34737. AssertIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34738. AssertIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34739. AssertIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34740. AssertIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34741. AssertIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34742. AssertIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34743. AssertIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34744. AssertIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34745. AssertIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34746. AssertIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34747. AssertIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34748. AssertIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34749. AssertIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34750. AssertIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34751. AssertIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34752. AssertIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34753. AssertIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34754. AssertIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34755. AssertIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34756. AssertIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34757. AssertIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34758. AssertIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34759. AssertIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34760. AssertIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34761. /* END Invalid parameters. */
  34762. val1 = 8;
  34763. val2 = 3;
  34764. AssertIntEQ(BN_set_word(a, val1), 1);
  34765. AssertIntEQ(BN_set_word(b, val2), 1);
  34766. AssertIntEQ(BN_add(r, a, b), 1);
  34767. AssertIntEQ(BN_is_word(r, val1 + val2), 1);
  34768. AssertIntEQ(BN_sub(r, a, b), 1);
  34769. AssertIntEQ(BN_is_word(r, val1 - val2), 1);
  34770. AssertIntEQ(BN_mul(r, a, b, NULL), 1);
  34771. AssertIntEQ(BN_is_word(r, val1 * val2), 1);
  34772. AssertIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34773. AssertIntEQ(BN_is_word(r, val1 / val2), 1);
  34774. AssertIntEQ(BN_is_word(rem, val1 % val2), 1);
  34775. AssertIntEQ(BN_mod(r, a, b, NULL), 1);
  34776. AssertIntEQ(BN_is_word(r, val1 % val2), 1);
  34777. BN_free(rem);
  34778. BN_free(r);
  34779. BN_free(b);
  34780. BN_free(a);
  34781. res = TEST_RES_CHECK(1);
  34782. #endif
  34783. return res;
  34784. }
  34785. static int test_wolfSSL_BN_math_mod(void)
  34786. {
  34787. int res = TEST_SKIPPED;
  34788. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34789. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34790. BIGNUM* a;
  34791. BIGNUM* b;
  34792. BIGNUM* m;
  34793. BIGNUM* r;
  34794. BIGNUM* t;
  34795. BIGNUM emptyBN;
  34796. BN_ULONG val1;
  34797. BN_ULONG val2;
  34798. BN_ULONG val3;
  34799. /* Setup */
  34800. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34801. AssertNotNull(a = BN_new());
  34802. AssertNotNull(b = BN_new());
  34803. AssertNotNull(m = BN_new());
  34804. AssertNotNull(r = BN_new());
  34805. /* Invalid parameters. */
  34806. AssertIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34807. AssertIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34808. AssertIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34809. AssertIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34810. AssertIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34811. AssertIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34812. AssertIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34813. AssertIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34814. AssertIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34815. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34816. AssertIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34817. AssertIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34818. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34819. AssertIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34820. AssertIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34821. AssertIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34822. AssertIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34823. AssertIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34824. AssertIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34825. AssertIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34826. AssertIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34827. AssertIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34828. AssertIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34829. AssertIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34830. AssertIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34831. AssertIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34832. AssertIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34833. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34834. AssertIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34835. AssertIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34836. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34837. AssertIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34838. AssertIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34839. AssertIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34840. AssertIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34841. AssertIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34842. AssertIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34843. AssertIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34844. AssertIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34845. AssertIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34846. AssertIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34847. AssertIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34848. AssertIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34849. AssertIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34850. AssertIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34851. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34852. AssertIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34853. AssertIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34854. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34855. AssertIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34856. AssertIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34857. AssertIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34858. AssertIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34859. AssertIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34860. AssertNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34861. AssertNull(BN_mod_inverse(r, a, NULL, NULL));
  34862. AssertNull(BN_mod_inverse(r, NULL, m, NULL));
  34863. AssertNull(BN_mod_inverse(r, NULL, m, NULL));
  34864. AssertNull(BN_mod_inverse(r, a, NULL, NULL));
  34865. AssertNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34866. AssertNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34867. AssertNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34868. AssertNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34869. AssertNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34870. AssertNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34871. AssertNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34872. /* END Invalid parameters. */
  34873. val1 = 9;
  34874. val2 = 13;
  34875. val3 = 5;
  34876. AssertIntEQ(BN_set_word(a, val1), 1);
  34877. AssertIntEQ(BN_set_word(b, val2), 1);
  34878. AssertIntEQ(BN_set_word(m, val3), 1);
  34879. AssertIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34880. AssertIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34881. AssertIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34882. AssertIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34883. AssertIntEQ(BN_set_word(a, 2), 1);
  34884. AssertIntEQ(BN_set_word(b, 3), 1);
  34885. AssertIntEQ(BN_set_word(m, 5), 1);
  34886. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34887. AssertIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34888. AssertIntEQ(BN_is_word(r, 3), 1);
  34889. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34890. AssertNotNull(BN_mod_inverse(r, a, m, NULL));
  34891. AssertIntEQ(BN_is_word(r, 3), 1);
  34892. AssertNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34893. AssertIntEQ(BN_is_word(t, 3), 1);
  34894. BN_free(t);
  34895. /* No inverse case. No inverse when a divides b. */
  34896. AssertIntEQ(BN_set_word(a, 3), 1);
  34897. AssertIntEQ(BN_set_word(m, 9), 1);
  34898. AssertNull(BN_mod_inverse(r, a, m, NULL));
  34899. BN_free(r);
  34900. BN_free(m);
  34901. BN_free(b);
  34902. BN_free(a);
  34903. res = TEST_RES_CHECK(1);
  34904. #endif
  34905. return res;
  34906. }
  34907. static int test_wolfSSL_BN_math_other(void)
  34908. {
  34909. int res = TEST_SKIPPED;
  34910. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34911. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34912. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34913. BIGNUM* a;
  34914. BIGNUM* b;
  34915. BIGNUM* r;
  34916. BIGNUM emptyBN;
  34917. /* Setup */
  34918. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34919. AssertNotNull(a = BN_new());
  34920. AssertNotNull(b = BN_new());
  34921. AssertNotNull(r = BN_new());
  34922. /* Invalid parameters. */
  34923. AssertIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34924. AssertIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34925. AssertIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34926. AssertIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34927. AssertIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34928. AssertIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34929. AssertIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34930. AssertIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34931. AssertIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34932. AssertIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34933. AssertIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34934. AssertIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34935. AssertIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34936. AssertIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34937. /* END Invalid parameters. */
  34938. /* No comman factors between 2 and 3. */
  34939. AssertIntEQ(BN_set_word(a, 2), 1);
  34940. AssertIntEQ(BN_set_word(b, 3), 1);
  34941. AssertIntEQ(BN_gcd(r, a, b, NULL), 1);
  34942. AssertIntEQ(BN_is_word(r, 1), 1);
  34943. /* 3 is largest value that divides both 6 and 9. */
  34944. AssertIntEQ(BN_set_word(a, 6), 1);
  34945. AssertIntEQ(BN_set_word(b, 9), 1);
  34946. AssertIntEQ(BN_gcd(r, a, b, NULL), 1);
  34947. AssertIntEQ(BN_is_word(r, 3), 1);
  34948. /* GCD of 0 and 0 is undefined. */
  34949. AssertIntEQ(BN_set_word(a, 0), 1);
  34950. AssertIntEQ(BN_set_word(b, 0), 1);
  34951. AssertIntEQ(BN_gcd(r, a, b, NULL), 0);
  34952. /* Teardown */
  34953. BN_free(r);
  34954. BN_free(b);
  34955. BN_free(a);
  34956. res = TEST_RES_CHECK(1);
  34957. #endif
  34958. #endif
  34959. return res;
  34960. }
  34961. static int test_wolfSSL_BN_rand(void)
  34962. {
  34963. int res = TEST_SKIPPED;
  34964. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34965. BIGNUM* bn;
  34966. BIGNUM* range;
  34967. BIGNUM emptyBN;
  34968. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34969. AssertNotNull(bn = BN_new());
  34970. AssertNotNull(range = BN_new());
  34971. /* Invalid parameters. */
  34972. AssertIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34973. AssertIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34974. AssertIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34975. AssertIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34976. AssertIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34977. AssertIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34978. AssertIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34979. AssertIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34980. AssertIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34981. AssertIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34982. AssertIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34983. AssertIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34984. AssertIntEQ(BN_rand_range(NULL, NULL), 0);
  34985. AssertIntEQ(BN_rand_range(bn, NULL), 0);
  34986. AssertIntEQ(BN_rand_range(NULL, range), 0);
  34987. AssertIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34988. AssertIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34989. AssertIntEQ(BN_rand_range(&emptyBN, range), 0);
  34990. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34991. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34992. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34993. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34994. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34995. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34996. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34997. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34998. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34999. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35000. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35001. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35002. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35003. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35004. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35005. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35006. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35007. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35008. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35009. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35010. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35011. /* 1 bit random value must have no more than one top bit set. */
  35012. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35013. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35014. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35015. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35016. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35017. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35018. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35019. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35020. /* END Invalid parameters. */
  35021. /* 0 bit random: 0. */
  35022. AssertIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35023. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35024. AssertIntEQ(BN_is_zero(bn), 1);
  35025. AssertIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35026. AssertIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35027. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35028. AssertIntEQ(BN_is_zero(bn), 1);
  35029. /* 1 bit random: 0 or 1. */
  35030. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35031. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35032. AssertIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35033. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35034. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35035. AssertIntEQ(BN_get_word(bn), 1);
  35036. AssertIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35037. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35038. AssertIntEQ(BN_get_word(bn), 1);
  35039. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35040. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35041. AssertIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35042. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35043. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35044. AssertIntEQ(BN_get_word(bn), 1);
  35045. AssertIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35046. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35047. AssertIntEQ(BN_get_word(bn), 1);
  35048. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35049. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35050. AssertIntEQ(BN_num_bits(bn), 8);
  35051. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35052. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35053. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35054. AssertIntEQ(BN_num_bits(bn), 8);
  35055. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35056. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35057. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35058. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35059. AssertIntEQ(BN_is_bit_set(bn, 6), 1);
  35060. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35061. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35062. AssertIntEQ(BN_is_bit_set(bn, 7), 1);
  35063. AssertIntEQ(BN_is_bit_set(bn, 6), 1);
  35064. AssertIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35065. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35066. AssertIntEQ(BN_is_bit_set(bn, 0), 1);
  35067. AssertIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35068. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35069. AssertIntEQ(BN_is_bit_set(bn, 0), 1);
  35070. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35071. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35072. * case, requesting a 13-bit random number would actually return a 16-bit
  35073. * random number. */
  35074. AssertIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35075. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35076. AssertIntEQ(BN_num_bits(bn), 13);
  35077. AssertIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35078. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35079. AssertIntEQ(BN_rand_range(bn, range), 1);
  35080. AssertIntEQ(BN_set_word(range, 0), 1);
  35081. AssertIntEQ(BN_rand_range(bn, range), 1);
  35082. AssertIntEQ(BN_set_word(range, 1), 1);
  35083. AssertIntEQ(BN_rand_range(bn, range), 1);
  35084. BN_free(bn);
  35085. BN_free(range);
  35086. res = TEST_RES_CHECK(1);
  35087. #endif
  35088. return res;
  35089. }
  35090. static int test_wolfSSL_BN_prime(void)
  35091. {
  35092. int res = TEST_SKIPPED;
  35093. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35094. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35095. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35096. BIGNUM* a;
  35097. BIGNUM* add;
  35098. BIGNUM* rem;
  35099. BIGNUM emptyBN;
  35100. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35101. AssertNotNull(a = BN_new());
  35102. AssertNotNull(add = BN_new());
  35103. AssertNotNull(rem = BN_new());
  35104. /* Invalid parameters. */
  35105. /* BN_generate_prime_ex()
  35106. * prime - must have valid BIGNUM
  35107. * bits - Greater then 0
  35108. * safe - not supported, must be 0
  35109. * add - not supported, must be NULL
  35110. * rem - not supported, must be NULL
  35111. * cb - anything
  35112. */
  35113. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35114. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35115. AssertIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35116. AssertIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35117. AssertIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35118. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35119. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35120. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35121. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35122. AssertIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35123. AssertIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35124. AssertIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35125. AssertIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35126. AssertIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35127. AssertIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35128. AssertIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35129. AssertIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35130. AssertIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35131. AssertIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35132. AssertIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35133. AssertIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35134. AssertIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35135. AssertIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35136. AssertIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35137. /* END Invalid parameters. */
  35138. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35139. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35140. AssertIntEQ(BN_clear_bit(a, 0), 1);
  35141. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35142. BN_free(rem);
  35143. BN_free(add);
  35144. BN_free(a);
  35145. res = TEST_RES_CHECK(1);
  35146. #endif
  35147. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35148. return res;
  35149. }
  35150. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35151. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35152. #define TEST_ARG 0x1234
  35153. static void msg_cb(int write_p, int version, int content_type,
  35154. const void *buf, size_t len, SSL *ssl, void *arg)
  35155. {
  35156. (void)write_p;
  35157. (void)version;
  35158. (void)content_type;
  35159. (void)buf;
  35160. (void)len;
  35161. (void)ssl;
  35162. AssertTrue(arg == (void*)TEST_ARG);
  35163. }
  35164. #endif
  35165. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35166. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  35167. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  35168. !defined(NO_WOLFSSL_SERVER)
  35169. #ifndef SINGLE_THREADED
  35170. #if defined(SESSION_CERTS)
  35171. #include "wolfssl/internal.h"
  35172. #endif
  35173. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35174. {
  35175. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35176. STACK_OF(X509)* sk;
  35177. X509* x509;
  35178. int i, num;
  35179. BIO* bio;
  35180. #endif
  35181. (void) ctx;
  35182. fprintf(stderr, "\n===== msgcb called ====\n");
  35183. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35184. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35185. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35186. AssertNotNull(SSL_get0_verified_chain(ssl));
  35187. #else
  35188. (void) ssl;
  35189. #endif
  35190. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35191. bio = BIO_new(BIO_s_file());
  35192. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  35193. sk = SSL_get_peer_cert_chain(ssl);
  35194. AssertNotNull(sk);
  35195. if (!sk) {
  35196. BIO_free(bio);
  35197. return SSL_FAILURE;
  35198. }
  35199. num = sk_X509_num(sk);
  35200. AssertTrue(num > 0);
  35201. for (i = 0; i < num; i++) {
  35202. x509 = sk_X509_value(sk,i);
  35203. AssertNotNull(x509);
  35204. if (!x509)
  35205. break;
  35206. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35207. X509_print(bio,x509);
  35208. fprintf(stderr, "\n\n");
  35209. }
  35210. BIO_free(bio);
  35211. #endif
  35212. return SSL_SUCCESS;
  35213. }
  35214. #endif
  35215. #endif
  35216. static int test_wolfSSL_msgCb(void)
  35217. {
  35218. int res = TEST_SKIPPED;
  35219. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35220. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  35221. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  35222. !defined(NO_WOLFSSL_SERVER)
  35223. tcp_ready ready;
  35224. func_args client_args;
  35225. func_args server_args;
  35226. #ifndef SINGLE_THREADED
  35227. THREAD_TYPE serverThread;
  35228. #endif
  35229. callback_functions client_cb;
  35230. callback_functions server_cb;
  35231. /* create a failed connection and inspect the error */
  35232. #ifdef WOLFSSL_TIRTOS
  35233. fdOpenSession(Task_self());
  35234. #endif
  35235. XMEMSET(&client_args, 0, sizeof(func_args));
  35236. XMEMSET(&server_args, 0, sizeof(func_args));
  35237. StartTCP();
  35238. InitTcpReady(&ready);
  35239. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35240. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35241. #ifndef WOLFSSL_NO_TLS12
  35242. client_cb.method = wolfTLSv1_2_client_method;
  35243. server_cb.method = wolfTLSv1_2_server_method;
  35244. #else
  35245. client_cb.method = wolfTLSv1_3_client_method;
  35246. server_cb.method = wolfTLSv1_3_server_method;
  35247. #endif
  35248. server_args.signal = &ready;
  35249. server_args.callbacks = &server_cb;
  35250. client_args.signal = &ready;
  35251. client_args.callbacks = &client_cb;
  35252. client_args.return_code = TEST_FAIL;
  35253. #ifndef SINGLE_THREADED
  35254. start_thread(test_server_nofail, &server_args, &serverThread);
  35255. wait_tcp_ready(&server_args);
  35256. test_client_nofail(&client_args, msgCb);
  35257. join_thread(serverThread);
  35258. #endif
  35259. FreeTcpReady(&ready);
  35260. #ifndef SINGLE_THREADED
  35261. AssertTrue(client_args.return_code);
  35262. AssertTrue(server_args.return_code);
  35263. #endif
  35264. #ifdef WOLFSSL_TIRTOS
  35265. fdOpenSession(Task_self());
  35266. #endif
  35267. res = TEST_RES_CHECK(1);
  35268. #endif
  35269. return res;
  35270. }
  35271. static int test_wolfSSL_either_side(void)
  35272. {
  35273. int res = TEST_SKIPPED;
  35274. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35275. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35276. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  35277. tcp_ready ready;
  35278. func_args client_args;
  35279. func_args server_args;
  35280. #ifndef SINGLE_THREADED
  35281. THREAD_TYPE serverThread;
  35282. #endif
  35283. callback_functions client_cb;
  35284. callback_functions server_cb;
  35285. /* create a failed connection and inspect the error */
  35286. #ifdef WOLFSSL_TIRTOS
  35287. fdOpenSession(Task_self());
  35288. #endif
  35289. XMEMSET(&client_args, 0, sizeof(func_args));
  35290. XMEMSET(&server_args, 0, sizeof(func_args));
  35291. StartTCP();
  35292. InitTcpReady(&ready);
  35293. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35294. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35295. /* Use different CTX for client and server */
  35296. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35297. AssertNotNull(client_cb.ctx);
  35298. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35299. AssertNotNull(server_cb.ctx);
  35300. /* we are responsible for free'ing WOLFSSL_CTX */
  35301. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35302. server_args.signal = &ready;
  35303. server_args.callbacks = &server_cb;
  35304. client_args.signal = &ready;
  35305. client_args.callbacks = &client_cb;
  35306. client_args.return_code = TEST_FAIL;
  35307. #ifndef SINGLE_THREADED
  35308. start_thread(test_server_nofail, &server_args, &serverThread);
  35309. wait_tcp_ready(&server_args);
  35310. test_client_nofail(&client_args, NULL);
  35311. join_thread(serverThread);
  35312. #endif
  35313. wolfSSL_CTX_free(client_cb.ctx);
  35314. wolfSSL_CTX_free(server_cb.ctx);
  35315. FreeTcpReady(&ready);
  35316. #ifndef SINGLE_THREADED
  35317. AssertTrue(client_args.return_code);
  35318. AssertTrue(server_args.return_code);
  35319. #endif
  35320. #ifdef WOLFSSL_TIRTOS
  35321. fdOpenSession(Task_self());
  35322. #endif
  35323. res = TEST_RES_CHECK(1);
  35324. #endif
  35325. return res;
  35326. }
  35327. static int test_wolfSSL_DTLS_either_side(void)
  35328. {
  35329. int res = TEST_SKIPPED;
  35330. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35331. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35332. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  35333. defined(WOLFSSL_DTLS)
  35334. tcp_ready ready;
  35335. func_args client_args;
  35336. func_args server_args;
  35337. #ifndef SINGLE_THREADED
  35338. THREAD_TYPE serverThread;
  35339. #endif
  35340. callback_functions client_cb;
  35341. callback_functions server_cb;
  35342. #ifdef WOLFSSL_TIRTOS
  35343. fdOpenSession(Task_self());
  35344. #endif
  35345. XMEMSET(&client_args, 0, sizeof(func_args));
  35346. XMEMSET(&server_args, 0, sizeof(func_args));
  35347. StartTCP();
  35348. InitTcpReady(&ready);
  35349. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  35350. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35351. /* Use different CTX for client and server */
  35352. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35353. AssertNotNull(client_cb.ctx);
  35354. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35355. AssertNotNull(server_cb.ctx);
  35356. /* we are responsible for free'ing WOLFSSL_CTX */
  35357. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35358. server_args.signal = &ready;
  35359. server_args.callbacks = &server_cb;
  35360. client_args.signal = &ready;
  35361. client_args.callbacks = &client_cb;
  35362. client_args.return_code = TEST_FAIL;
  35363. #ifndef SINGLE_THREADED
  35364. start_thread(test_server_nofail, &server_args, &serverThread);
  35365. wait_tcp_ready(&server_args);
  35366. test_client_nofail(&client_args, NULL);
  35367. join_thread(serverThread);
  35368. #endif
  35369. wolfSSL_CTX_free(client_cb.ctx);
  35370. wolfSSL_CTX_free(server_cb.ctx);
  35371. FreeTcpReady(&ready);
  35372. #ifndef SINGLE_THREADED
  35373. AssertTrue(client_args.return_code);
  35374. AssertTrue(server_args.return_code);
  35375. #endif
  35376. #ifdef WOLFSSL_TIRTOS
  35377. fdOpenSession(Task_self());
  35378. #endif
  35379. res = TEST_RES_CHECK(1);
  35380. #endif
  35381. return res;
  35382. }
  35383. static int test_generate_cookie(void)
  35384. {
  35385. int res = TEST_SKIPPED;
  35386. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35387. SSL_CTX* ctx;
  35388. SSL* ssl;
  35389. byte buf[FOURK_BUF] = {0};
  35390. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35391. AssertNotNull(ssl = SSL_new(ctx));
  35392. /* Test unconnected */
  35393. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35394. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35395. wolfSSL_SetCookieCtx(ssl, ctx);
  35396. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  35397. AssertNull(wolfSSL_GetCookieCtx(NULL));
  35398. SSL_free(ssl);
  35399. SSL_CTX_free(ctx);
  35400. res = TEST_RES_CHECK(1);
  35401. #endif
  35402. return res;
  35403. }
  35404. static int test_wolfSSL_set_options(void)
  35405. {
  35406. int res = TEST_SKIPPED;
  35407. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35408. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35409. WOLFSSL* ssl;
  35410. WOLFSSL_CTX* ctx;
  35411. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35412. char appData[] = "extra msg";
  35413. #endif
  35414. #ifdef OPENSSL_EXTRA
  35415. unsigned char protos[] = {
  35416. 7, 't', 'l', 's', '/', '1', '.', '2',
  35417. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35418. };
  35419. unsigned int len = sizeof(protos);
  35420. void *arg = (void *)TEST_ARG;
  35421. #endif
  35422. #ifndef NO_WOLFSSL_SERVER
  35423. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35424. #else
  35425. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35426. #endif
  35427. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35428. WOLFSSL_FILETYPE_PEM));
  35429. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35430. WOLFSSL_FILETYPE_PEM));
  35431. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35432. == WOLFSSL_OP_NO_TLSv1);
  35433. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35434. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35435. WOLFSSL_OP_NO_SSLv2)), 0);
  35436. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35437. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35438. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35439. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35440. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35441. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35442. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35443. WOLFSSL_OP_NO_COMPRESSION));
  35444. wolfSSL_CTX_free(ctx);
  35445. #ifndef NO_WOLFSSL_SERVER
  35446. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  35447. AssertNotNull(ctx);
  35448. #else
  35449. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  35450. AssertNotNull(ctx);
  35451. #endif
  35452. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35453. WOLFSSL_FILETYPE_PEM));
  35454. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35455. WOLFSSL_FILETYPE_PEM));
  35456. #ifdef OPENSSL_EXTRA
  35457. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35458. #endif
  35459. AssertNotNull(ssl = wolfSSL_new(ctx));
  35460. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35461. #ifdef HAVE_EX_DATA
  35462. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35463. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35464. if (ssl) {
  35465. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35466. appData, sizeof(appData)), 0);
  35467. }
  35468. #else
  35469. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35470. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35471. #endif
  35472. #endif
  35473. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35474. WOLFSSL_OP_NO_TLSv1);
  35475. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35476. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35477. WOLFSSL_OP_NO_SSLv2)), 0);
  35478. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35479. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35480. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35481. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35482. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35483. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35484. #ifdef OPENSSL_EXTRA
  35485. AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35486. WOLFSSL_OP_NO_COMPRESSION));
  35487. #endif
  35488. #ifdef OPENSSL_EXTRA
  35489. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35490. wolfSSL_set_msg_callback_arg(ssl, arg);
  35491. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35492. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35493. #else
  35494. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35495. #endif
  35496. #endif
  35497. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35498. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35499. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35500. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35501. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35502. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35503. #else
  35504. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35505. #endif
  35506. #endif /* HAVE_ALPN && !NO_BIO */
  35507. #endif
  35508. wolfSSL_free(ssl);
  35509. wolfSSL_CTX_free(ctx);
  35510. res = TEST_RES_CHECK(1);
  35511. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35512. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35513. return res;
  35514. }
  35515. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35516. {
  35517. int res = TEST_SKIPPED;
  35518. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35519. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35520. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35521. SSL* ssl;
  35522. SSL_CTX* ctx;
  35523. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  35524. #ifndef NO_WOLFSSL_SERVER
  35525. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35526. #else
  35527. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35528. #endif
  35529. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35530. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35531. AssertNotNull(ssl = SSL_new(ctx));
  35532. AssertNotNull(sk = SSL_get_ciphers(ssl));
  35533. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35534. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  35535. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35536. /* error case because connection has not been established yet */
  35537. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35538. sk_SSL_CIPHER_free(dupSk);
  35539. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35540. SSL_free(ssl);
  35541. SSL_CTX_free(ctx);
  35542. res = TEST_RES_CHECK(1);
  35543. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35544. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35545. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35546. return res;
  35547. }
  35548. static int test_wolfSSL_set1_curves_list(void)
  35549. {
  35550. int res = TEST_SKIPPED;
  35551. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35552. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35553. SSL* ssl = NULL;
  35554. SSL_CTX* ctx = NULL;
  35555. #ifndef NO_WOLFSSL_SERVER
  35556. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35557. #else
  35558. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35559. #endif
  35560. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35561. SSL_FILETYPE_PEM));
  35562. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35563. AssertNotNull(ssl = SSL_new(ctx));
  35564. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35565. #ifdef HAVE_ECC
  35566. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35567. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35568. #endif
  35569. #ifdef HAVE_CURVE25519
  35570. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35571. #else
  35572. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35573. #endif
  35574. #ifdef HAVE_CURVE448
  35575. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35576. #else
  35577. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35578. #endif
  35579. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35580. #ifdef HAVE_ECC
  35581. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35582. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35583. #endif
  35584. #ifdef HAVE_CURVE25519
  35585. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35586. #else
  35587. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35588. #endif
  35589. #ifdef HAVE_CURVE448
  35590. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35591. #else
  35592. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35593. #endif
  35594. SSL_free(ssl);
  35595. SSL_CTX_free(ctx);
  35596. res = TEST_RES_CHECK(1);
  35597. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35598. #endif
  35599. return res;
  35600. }
  35601. static int test_wolfSSL_set1_sigalgs_list(void)
  35602. {
  35603. int res = TEST_SKIPPED;
  35604. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35605. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35606. SSL* ssl;
  35607. SSL_CTX* ctx;
  35608. #ifndef NO_WOLFSSL_SERVER
  35609. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35610. #else
  35611. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35612. #endif
  35613. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35614. SSL_FILETYPE_PEM));
  35615. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35616. AssertNotNull(ssl = SSL_new(ctx));
  35617. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35618. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35619. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35620. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35621. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35622. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35623. #ifndef NO_RSA
  35624. #ifndef NO_SHA256
  35625. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35626. WOLFSSL_FAILURE);
  35627. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35628. WOLFSSL_FAILURE);
  35629. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35630. WOLFSSL_SUCCESS);
  35631. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35632. WOLFSSL_SUCCESS);
  35633. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35634. WOLFSSL_FAILURE);
  35635. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35636. WOLFSSL_FAILURE);
  35637. #ifdef WC_RSA_PSS
  35638. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35639. WOLFSSL_SUCCESS);
  35640. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35641. WOLFSSL_SUCCESS);
  35642. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35643. WOLFSSL_SUCCESS);
  35644. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35645. WOLFSSL_SUCCESS);
  35646. #endif
  35647. #ifdef WOLFSSL_SHA512
  35648. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35649. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35650. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35651. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35652. #elif defined(WOLFSSL_SHA384)
  35653. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35654. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35655. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35656. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35657. #endif
  35658. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35659. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35660. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35661. WOLFSSL_FAILURE);
  35662. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35663. WOLFSSL_FAILURE);
  35664. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35665. WOLFSSL_FAILURE);
  35666. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35667. WOLFSSL_FAILURE);
  35668. #endif
  35669. #endif
  35670. #ifdef HAVE_ECC
  35671. #ifndef NO_SHA256
  35672. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35673. WOLFSSL_SUCCESS);
  35674. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  35675. #ifdef WOLFSSL_SHA512
  35676. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35677. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35678. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35679. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35680. #elif defined(WOLFSSL_SHA384)
  35681. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35682. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35683. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35684. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35685. #endif
  35686. #endif
  35687. #endif
  35688. #ifdef HAVE_ED25519
  35689. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35690. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35691. #endif
  35692. #ifdef HAVE_ED448
  35693. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35694. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35695. #endif
  35696. #ifndef NO_DSA
  35697. #ifndef NO_SHA256
  35698. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35699. WOLFSSL_SUCCESS);
  35700. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35701. WOLFSSL_SUCCESS);
  35702. #endif
  35703. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35704. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35705. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35706. WOLFSSL_SUCCESS);
  35707. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35708. WOLFSSL_SUCCESS);
  35709. #endif
  35710. #endif
  35711. SSL_free(ssl);
  35712. SSL_CTX_free(ctx);
  35713. res = TEST_RES_CHECK(1);
  35714. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35715. #endif
  35716. return res;
  35717. }
  35718. /* Testing wolfSSL_set_tlsext_status_type function.
  35719. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35720. */
  35721. static int test_wolfSSL_set_tlsext_status_type(void)
  35722. {
  35723. int res = TEST_SKIPPED;
  35724. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35725. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35726. SSL* ssl;
  35727. SSL_CTX* ctx;
  35728. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35729. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35730. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35731. AssertNotNull(ssl = SSL_new(ctx));
  35732. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35733. SSL_SUCCESS);
  35734. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35735. SSL_free(ssl);
  35736. SSL_CTX_free(ctx);
  35737. res = TEST_RES_CHECK(1);
  35738. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35739. return res;
  35740. }
  35741. #ifndef NO_BIO
  35742. static int test_wolfSSL_PEM_read_bio(void)
  35743. {
  35744. int res = TEST_SKIPPED;
  35745. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35746. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35747. byte buff[6000];
  35748. XFILE f;
  35749. int bytes;
  35750. X509* x509;
  35751. BIO* bio = NULL;
  35752. BUF_MEM* buf;
  35753. f = XFOPEN(cliCertFile, "rb");
  35754. AssertTrue((f != XBADFILE));
  35755. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  35756. XFCLOSE(f);
  35757. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35758. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35759. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35760. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35761. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35762. /* BIO should return the set EOF value */
  35763. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35764. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35765. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35766. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35767. BIO_free(bio);
  35768. BUF_MEM_free(buf);
  35769. X509_free(x509);
  35770. res = TEST_RES_CHECK(1);
  35771. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35772. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35773. return res;
  35774. }
  35775. #if defined(OPENSSL_EXTRA)
  35776. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35777. long argl, long ret)
  35778. {
  35779. (void)bio;
  35780. (void)cmd;
  35781. (void)argp;
  35782. (void)argi;
  35783. (void)argl;
  35784. return ret;
  35785. }
  35786. #endif
  35787. static int test_wolfSSL_BIO(void)
  35788. {
  35789. int res = TEST_SKIPPED;
  35790. #if defined(OPENSSL_EXTRA)
  35791. const unsigned char* p;
  35792. byte buff[20];
  35793. BIO* bio1;
  35794. BIO* bio2;
  35795. BIO* bio3;
  35796. char* bufPt;
  35797. int i;
  35798. for (i = 0; i < 20; i++) {
  35799. buff[i] = i;
  35800. }
  35801. /* test BIO_free with NULL */
  35802. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35803. /* Creating and testing type BIO_s_bio */
  35804. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  35805. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  35806. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  35807. /* read/write before set up */
  35808. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35809. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35810. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  35811. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35812. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35813. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35814. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35815. XMEMCPY(bufPt, buff, 10);
  35816. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35817. /* write buffer full */
  35818. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35819. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35820. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35821. /* write the other direction with pair */
  35822. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35823. XMEMCPY(bufPt, buff, 8);
  35824. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35825. /* try read */
  35826. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35827. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35828. /* try read using ctrl function */
  35829. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35830. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35831. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35832. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35833. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35834. for (i = 0; i < 20; i++) {
  35835. AssertIntEQ((int)bufPt[i], i);
  35836. }
  35837. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  35838. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35839. for (i = 0; i < 8; i++) {
  35840. AssertIntEQ((int)bufPt[i], i);
  35841. }
  35842. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  35843. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35844. /* new pair */
  35845. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35846. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35847. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35848. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35849. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  35850. /* test wrap around... */
  35851. AssertIntEQ(BIO_reset(bio1), 0);
  35852. AssertIntEQ(BIO_reset(bio3), 0);
  35853. /* fill write buffer, read only small amount then write again */
  35854. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35855. XMEMCPY(bufPt, buff, 20);
  35856. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35857. for (i = 0; i < 4; i++) {
  35858. AssertIntEQ(bufPt[i], i);
  35859. }
  35860. /* try writing over read index */
  35861. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35862. XMEMSET(bufPt, 0, 4);
  35863. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35864. /* read and write 0 bytes */
  35865. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35866. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35867. /* should read only to end of write buffer then need to read again */
  35868. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35869. for (i = 0; i < 16; i++) {
  35870. AssertIntEQ(bufPt[i], buff[4 + i]);
  35871. }
  35872. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35873. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35874. for (i = 0; i < 4; i++) {
  35875. AssertIntEQ(bufPt[i], 0);
  35876. }
  35877. /* read index should not have advanced with nread0 */
  35878. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35879. for (i = 0; i < 4; i++) {
  35880. AssertIntEQ(bufPt[i], 0);
  35881. }
  35882. /* write and fill up buffer checking reset of index state */
  35883. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35884. XMEMCPY(bufPt, buff, 20);
  35885. /* test reset on data in bio1 write buffer */
  35886. AssertIntEQ(BIO_reset(bio1), 0);
  35887. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35888. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  35889. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35890. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35891. AssertNotNull(p);
  35892. XMEMCPY(bufPt, buff, 20);
  35893. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35894. for (i = 0; i < 6; i++) {
  35895. AssertIntEQ(bufPt[i], i);
  35896. }
  35897. /* test case of writing twice with offset read index */
  35898. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35899. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35900. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35901. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35902. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35903. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35904. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35905. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35906. BIO_free(bio1);
  35907. BIO_free(bio3);
  35908. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35909. {
  35910. BIO* bioA = NULL;
  35911. BIO* bioB = NULL;
  35912. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35913. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35914. BIO_free(bioA);
  35915. bioA = NULL;
  35916. BIO_free(bioB);
  35917. bioB = NULL;
  35918. }
  35919. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35920. /* BIOs with file pointers */
  35921. #if !defined(NO_FILESYSTEM)
  35922. {
  35923. XFILE f1;
  35924. XFILE f2;
  35925. BIO* f_bio1;
  35926. BIO* f_bio2;
  35927. unsigned char cert[300];
  35928. char testFile[] = "tests/bio_write_test.txt";
  35929. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35930. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35931. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35932. /* Failure due to wrong BIO type */
  35933. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35934. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35935. f1 = XFOPEN(svrCertFile, "rwb");
  35936. AssertTrue((f1 != XBADFILE));
  35937. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35938. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  35939. WOLFSSL_SUCCESS);
  35940. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35941. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35942. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35943. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35944. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35945. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35946. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35947. AssertIntEQ(BIO_reset(f_bio2), 0);
  35948. AssertIntEQ(BIO_tell(NULL),-1);
  35949. AssertIntEQ(BIO_tell(f_bio2),0);
  35950. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  35951. AssertIntEQ(BIO_tell(f_bio2),4);
  35952. BIO_free(f_bio1);
  35953. BIO_free(f_bio2);
  35954. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35955. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35956. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35957. BIO_free(f_bio1);
  35958. }
  35959. #endif /* !defined(NO_FILESYSTEM) */
  35960. /* BIO info callback */
  35961. {
  35962. const char* testArg = "test";
  35963. BIO* cb_bio;
  35964. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35965. BIO_set_callback(cb_bio, bioCallback);
  35966. AssertNotNull(BIO_get_callback(cb_bio));
  35967. BIO_set_callback(cb_bio, NULL);
  35968. AssertNull(BIO_get_callback(cb_bio));
  35969. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35970. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35971. AssertNull(BIO_get_callback_arg(NULL));
  35972. BIO_free(cb_bio);
  35973. }
  35974. /* BIO_vfree */
  35975. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  35976. BIO_vfree(NULL);
  35977. BIO_vfree(bio1);
  35978. res = TEST_RES_CHECK(1);
  35979. #endif
  35980. return res;
  35981. }
  35982. #endif /* !NO_BIO */
  35983. static int test_wolfSSL_a2i_IPADDRESS(void)
  35984. {
  35985. int res = TEST_SKIPPED;
  35986. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35987. const unsigned char* data;
  35988. int dataSz = 0;
  35989. ASN1_OCTET_STRING *st;
  35990. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35991. const unsigned char ipv6_exp[] = {
  35992. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35993. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35994. };
  35995. const unsigned char ipv6_home[] = {
  35996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35997. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35998. };
  35999. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  36000. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  36001. data = ASN1_STRING_get0_data(st);
  36002. dataSz = ASN1_STRING_length(st);
  36003. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  36004. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  36005. ASN1_STRING_free(st);
  36006. AssertNotNull(st = a2i_IPADDRESS("::1"));
  36007. data = ASN1_STRING_get0_data(st);
  36008. dataSz = ASN1_STRING_length(st);
  36009. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  36010. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36011. ASN1_STRING_free(st);
  36012. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36013. data = ASN1_STRING_get0_data(st);
  36014. dataSz = ASN1_STRING_length(st);
  36015. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  36016. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36017. ASN1_STRING_free(st);
  36018. res = TEST_RES_CHECK(1);
  36019. #endif
  36020. return res;
  36021. }
  36022. static int test_wolfSSL_DES_ecb_encrypt(void)
  36023. {
  36024. int res = TEST_SKIPPED;
  36025. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  36026. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  36027. WOLFSSL_DES_key_schedule key;
  36028. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  36029. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  36030. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  36031. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  36032. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  36033. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  36034. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  36035. /* Encrypt messages */
  36036. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  36037. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  36038. {
  36039. /* Decrypt messages */
  36040. int ret1 = 0;
  36041. int ret2 = 0;
  36042. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  36043. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  36044. AssertIntEQ(ret1,0);
  36045. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  36046. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  36047. AssertIntEQ(ret2,0);
  36048. }
  36049. res = TEST_RES_CHECK(1);
  36050. #endif
  36051. return res;
  36052. }
  36053. static int test_wolfSSL_X509_cmp_time(void)
  36054. {
  36055. int res = TEST_SKIPPED;
  36056. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36057. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36058. WOLFSSL_ASN1_TIME asn_time;
  36059. time_t t;
  36060. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36061. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36062. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36063. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36064. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36065. res = TEST_RES_CHECK(1);
  36066. #endif
  36067. return res;
  36068. }
  36069. static int test_wolfSSL_X509_time_adj(void)
  36070. {
  36071. int res = TEST_SKIPPED;
  36072. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36073. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36074. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36075. !defined(NO_ASN_TIME)
  36076. X509* x509;
  36077. time_t t, not_before, not_after;
  36078. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36079. client_cert_der_2048, sizeof_client_cert_der_2048,
  36080. WOLFSSL_FILETYPE_ASN1));
  36081. t = 0;
  36082. not_before = wc_Time(0);
  36083. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36084. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36085. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36086. /* Check X509_gmtime_adj, too. */
  36087. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36088. X509_free(x509);
  36089. res = TEST_RES_CHECK(1);
  36090. #endif
  36091. return res;
  36092. }
  36093. static int test_wolfSSL_X509(void)
  36094. {
  36095. int res = TEST_SKIPPED;
  36096. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  36097. && !defined(NO_RSA)
  36098. X509* x509;
  36099. #ifndef NO_BIO
  36100. BIO* bio;
  36101. X509_STORE_CTX* ctx;
  36102. X509_STORE* store;
  36103. #endif
  36104. char der[] = "certs/ca-cert.der";
  36105. XFILE fp;
  36106. AssertNotNull(x509 = X509_new());
  36107. X509_free(x509);
  36108. #ifndef NO_BIO
  36109. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  36110. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36111. #ifdef WOLFSSL_CERT_GEN
  36112. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36113. #endif
  36114. AssertNotNull(ctx = X509_STORE_CTX_new());
  36115. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36116. AssertNotNull(store = X509_STORE_new());
  36117. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36118. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36119. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36120. X509_STORE_CTX_free(ctx);
  36121. X509_STORE_free(store);
  36122. X509_free(x509);
  36123. BIO_free(bio);
  36124. #endif
  36125. /** d2i_X509_fp test **/
  36126. fp = XFOPEN(der, "rb");
  36127. AssertTrue((fp != XBADFILE));
  36128. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36129. AssertNotNull(x509);
  36130. X509_free(x509);
  36131. XFCLOSE(fp);
  36132. fp = XFOPEN(der, "rb");
  36133. AssertTrue((fp != XBADFILE));
  36134. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36135. AssertNotNull(x509);
  36136. X509_free(x509);
  36137. XFCLOSE(fp);
  36138. /* X509_up_ref test */
  36139. AssertIntEQ(X509_up_ref(NULL), 0);
  36140. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  36141. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36142. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36143. X509_free(x509); /* refCount = 2 */
  36144. X509_free(x509); /* refCount = 1 */
  36145. X509_free(x509); /* refCount = 0, free */
  36146. res = TEST_RES_CHECK(1);
  36147. #endif
  36148. return res;
  36149. }
  36150. static int test_wolfSSL_X509_get_ext_count(void)
  36151. {
  36152. int res = TEST_SKIPPED;
  36153. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36154. !defined(NO_RSA)
  36155. int ret = 0;
  36156. WOLFSSL_X509* x509;
  36157. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36158. FILE* f;
  36159. /* NULL parameter check */
  36160. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36161. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36162. SSL_FILETYPE_PEM));
  36163. AssertIntEQ(X509_get_ext_count(x509), 5);
  36164. wolfSSL_X509_free(x509);
  36165. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36166. SSL_FILETYPE_PEM));
  36167. AssertIntEQ(X509_get_ext_count(x509), 5);
  36168. wolfSSL_X509_free(x509);
  36169. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  36170. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36171. fclose(f);
  36172. /* wolfSSL_X509_get_ext_count() valid input */
  36173. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36174. /* wolfSSL_X509_get_ext_count() NULL argument */
  36175. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36176. wolfSSL_X509_free(x509);
  36177. res = TEST_RES_CHECK(1);
  36178. #endif
  36179. return res;
  36180. }
  36181. static int test_wolfSSL_X509_sign2(void)
  36182. {
  36183. int res = TEST_SKIPPED;
  36184. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36185. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36186. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36187. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36188. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36189. WOLFSSL_X509 *x509, *ca;
  36190. const unsigned char *der;
  36191. const unsigned char *pt;
  36192. WOLFSSL_EVP_PKEY *priv;
  36193. WOLFSSL_X509_NAME *name;
  36194. int derSz;
  36195. #ifndef NO_ASN_TIME
  36196. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  36197. const int year = 365*24*60*60;
  36198. const int day = 24*60*60;
  36199. const int hour = 60*60;
  36200. const int mini = 60;
  36201. time_t t;
  36202. #endif
  36203. const unsigned char expected[] = {
  36204. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36205. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  36206. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  36207. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36208. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36209. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36210. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36211. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36212. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36213. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36214. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36215. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36216. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36217. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36218. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36219. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36220. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36221. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36222. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36223. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36224. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36225. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36226. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36227. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36228. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36229. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36230. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36231. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36232. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36233. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36234. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36235. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36236. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36237. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36238. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36239. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36240. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36241. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36242. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36243. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36244. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36245. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36246. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36247. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36248. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36249. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36250. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36251. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36252. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36253. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36254. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36255. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36256. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36257. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36258. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36259. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36260. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36261. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36262. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36263. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36264. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36265. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36266. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36267. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36268. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36269. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36270. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36271. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36272. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36273. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36274. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36275. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36276. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36277. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36278. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36279. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36280. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36281. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36282. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36283. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36284. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36285. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  36286. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  36287. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36288. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36289. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36290. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36291. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  36292. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  36293. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  36294. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  36295. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  36296. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  36297. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  36298. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  36299. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  36300. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  36301. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  36302. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  36303. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  36304. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  36305. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  36306. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  36307. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  36308. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  36309. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  36310. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  36311. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  36312. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  36313. };
  36314. pt = ca_key_der_2048;
  36315. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36316. sizeof_ca_key_der_2048));
  36317. pt = client_cert_der_2048;
  36318. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36319. sizeof_client_cert_der_2048));
  36320. pt = ca_cert_der_2048;
  36321. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36322. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36323. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36324. #ifndef NO_ASN_TIME
  36325. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36326. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36327. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36328. AssertIntEQ(notAfter->length, 13);
  36329. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36330. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36331. #endif
  36332. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  36333. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36334. AssertIntEQ(derSz, sizeof(expected));
  36335. #ifndef NO_ASN_TIME
  36336. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  36337. #endif
  36338. wolfSSL_X509_free(ca);
  36339. wolfSSL_X509_free(x509);
  36340. wolfSSL_EVP_PKEY_free(priv);
  36341. #ifndef NO_ASN_TIME
  36342. wolfSSL_ASN1_TIME_free(notBefore);
  36343. wolfSSL_ASN1_TIME_free(notAfter);
  36344. #endif
  36345. res = TEST_RES_CHECK(1);
  36346. #endif
  36347. return res;
  36348. }
  36349. static int test_wolfSSL_X509_sign(void)
  36350. {
  36351. int res = TEST_SKIPPED;
  36352. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36353. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36354. int ret;
  36355. char *cn;
  36356. word32 cnSz;
  36357. X509_NAME *name;
  36358. X509 *x509, *ca;
  36359. DecodedCert dCert;
  36360. EVP_PKEY *pub;
  36361. EVP_PKEY *priv;
  36362. EVP_MD_CTX *mctx;
  36363. #if defined(USE_CERT_BUFFERS_1024)
  36364. const unsigned char* rsaPriv = client_key_der_1024;
  36365. const unsigned char* rsaPub = client_keypub_der_1024;
  36366. const unsigned char* certIssuer = client_cert_der_1024;
  36367. long clientKeySz = (long)sizeof_client_key_der_1024;
  36368. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36369. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36370. #elif defined(USE_CERT_BUFFERS_2048)
  36371. const unsigned char* rsaPriv = client_key_der_2048;
  36372. const unsigned char* rsaPub = client_keypub_der_2048;
  36373. const unsigned char* certIssuer = client_cert_der_2048;
  36374. long clientKeySz = (long)sizeof_client_key_der_2048;
  36375. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36376. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36377. #endif
  36378. byte sn[16];
  36379. int snSz = sizeof(sn);
  36380. /* Set X509_NAME fields */
  36381. AssertNotNull(name = X509_NAME_new());
  36382. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36383. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36384. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36385. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36386. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36387. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36388. /* Get private and public keys */
  36389. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36390. clientKeySz));
  36391. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36392. AssertNotNull(x509 = X509_new());
  36393. /* Set version 3 */
  36394. AssertIntNE(X509_set_version(x509, 2L), 0);
  36395. /* Set subject name, add pubkey, and sign certificate */
  36396. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36397. X509_NAME_free(name);
  36398. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36399. #ifdef WOLFSSL_ALT_NAMES
  36400. /* Add some subject alt names */
  36401. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  36402. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36403. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36404. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36405. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36406. "sphygmomanometer",
  36407. ASN_DNS_TYPE), SSL_SUCCESS);
  36408. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36409. "supercalifragilisticexpialidocious",
  36410. ASN_DNS_TYPE), SSL_SUCCESS);
  36411. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  36412. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36413. ASN_DNS_TYPE), SSL_SUCCESS);
  36414. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36415. {
  36416. unsigned char ip4_type[] = {127,128,0,255};
  36417. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36418. 0xff, 0xee, 0x99, 0x88,
  36419. 0x77, 0x66, 0x55, 0x44,
  36420. 0x00, 0x33, 0x22, 0x11};
  36421. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36422. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36423. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36424. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36425. }
  36426. #endif
  36427. #endif /* WOLFSSL_ALT_NAMES */
  36428. /* test valid sign case */
  36429. AssertIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36430. /* test valid X509_sign_ctx case */
  36431. AssertNotNull(mctx = EVP_MD_CTX_new());
  36432. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36433. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  36434. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36435. AssertIntEQ(X509_get_ext_count(x509), 1);
  36436. #endif
  36437. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36438. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36439. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36440. #endif
  36441. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36442. WOLFSSL_SUCCESS);
  36443. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36444. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36445. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36446. * with the MSB set. See GenerateInteger in asn.c */
  36447. #ifndef USE_CERT_BUFFERS_1024
  36448. #ifndef WOLFSSL_ALT_NAMES
  36449. /* Valid case - size should be 781-786 with 16 byte serial number */
  36450. AssertTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36451. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36452. /* Valid case - size should be 955-960 with 16 byte serial number */
  36453. AssertTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36454. #else
  36455. /* Valid case - size should be 926-931 with 16 byte serial number */
  36456. AssertTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36457. #endif
  36458. #else
  36459. #ifndef WOLFSSL_ALT_NAMES
  36460. /* Valid case - size should be 537-542 with 16 byte serial number */
  36461. AssertTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36462. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36463. /* Valid case - size should be 695-670 with 16 byte serial number */
  36464. AssertTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36465. #else
  36466. /* Valid case - size should be 666-671 with 16 byte serial number */
  36467. AssertTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36468. #endif
  36469. #endif
  36470. /* check that issuer name is as expected after signature */
  36471. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36472. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36473. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36474. AssertNotNull(name = X509_get_subject_name(ca));
  36475. cnSz = X509_NAME_get_sz(name);
  36476. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36477. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36478. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36479. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36480. #ifdef WOLFSSL_MULTI_ATTRIB
  36481. /* test adding multiple OU's to the signer */
  36482. AssertNotNull(name = X509_get_subject_name(ca));
  36483. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36484. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36485. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36486. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36487. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36488. #endif
  36489. AssertNotNull(name = X509_get_subject_name(ca));
  36490. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36491. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36492. AssertNotNull(name = X509_get_issuer_name(x509));
  36493. cnSz = X509_NAME_get_sz(name);
  36494. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36495. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36496. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36497. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36498. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36499. FreeDecodedCert(&dCert);
  36500. /* Test invalid parameters */
  36501. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36502. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36503. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  36504. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36505. EVP_MD_CTX_free(mctx);
  36506. AssertNotNull(mctx = EVP_MD_CTX_new());
  36507. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  36508. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  36509. /* test invalid version number */
  36510. #if defined(OPENSSL_ALL)
  36511. AssertIntNE(X509_set_version(x509, 6L), 0);
  36512. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36513. /* uses ParseCert which fails on bad version number */
  36514. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36515. #endif
  36516. EVP_MD_CTX_free(mctx);
  36517. EVP_PKEY_free(priv);
  36518. EVP_PKEY_free(pub);
  36519. X509_free(x509);
  36520. X509_free(ca);
  36521. res = TEST_RES_CHECK(1);
  36522. #endif
  36523. return res;
  36524. }
  36525. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36526. {
  36527. int res = TEST_SKIPPED;
  36528. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36529. X509* x509 = NULL;
  36530. const X509_ALGOR* alg;
  36531. AssertNotNull(x509 = X509_new());
  36532. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  36533. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  36534. X509_free(x509);
  36535. res = TEST_RES_CHECK(1);
  36536. #endif
  36537. return res;
  36538. }
  36539. static int test_wolfSSL_X509_ALGOR_get0(void)
  36540. {
  36541. int res = TEST_SKIPPED;
  36542. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36543. !defined(NO_SHA256) && !defined(NO_RSA)
  36544. X509* x509 = NULL;
  36545. const ASN1_OBJECT* obj = NULL;
  36546. const X509_ALGOR* alg;
  36547. int pptype = 0;
  36548. const void *ppval = NULL;
  36549. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36550. SSL_FILETYPE_PEM));
  36551. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  36552. /* Invalid case */
  36553. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36554. AssertNull(obj);
  36555. /* Valid case */
  36556. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36557. AssertNotNull(obj);
  36558. AssertNull(ppval);
  36559. AssertIntNE(pptype, 0);
  36560. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36561. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36562. X509_free(x509);
  36563. res = TEST_RES_CHECK(1);
  36564. #endif
  36565. return res;
  36566. }
  36567. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36568. {
  36569. int res = TEST_SKIPPED;
  36570. #if defined(OPENSSL_EXTRA)
  36571. X509_VERIFY_PARAM *paramTo;
  36572. X509_VERIFY_PARAM *paramFrom;
  36573. int ret;
  36574. char testIPv4[] = "127.0.0.1";
  36575. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36576. char testhostName1[] = "foo.hoge.com";
  36577. char testhostName2[] = "foobar.hoge.com";
  36578. paramTo = X509_VERIFY_PARAM_new();
  36579. AssertNotNull(paramTo);
  36580. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  36581. paramFrom = X509_VERIFY_PARAM_new();
  36582. AssertNotNull(paramFrom);
  36583. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  36584. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36585. (int)XSTRLEN(testhostName1));
  36586. AssertIntEQ(1, ret);
  36587. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36588. (int)XSTRLEN(testhostName1)));
  36589. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36590. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36591. AssertIntEQ(0x01, paramFrom->hostFlags);
  36592. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  36593. AssertIntEQ(0, ret);
  36594. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  36595. AssertIntEQ(1, ret);
  36596. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36597. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  36598. AssertIntEQ(1, ret);
  36599. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  36600. AssertIntEQ(1, ret);
  36601. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36602. /* null pointer */
  36603. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  36604. AssertIntEQ(WOLFSSL_FAILURE, ret);
  36605. /* in the case of "from" null, returns success */
  36606. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  36607. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  36608. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  36609. AssertIntEQ(WOLFSSL_FAILURE, ret);
  36610. /* inherit flags test : VPARAM_DEFAULT */
  36611. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36612. AssertIntEQ(1, ret);
  36613. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36614. (int)XSTRLEN(testhostName1)));
  36615. AssertIntEQ(0x01, paramTo->hostFlags);
  36616. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36617. /* inherit flags test : VPARAM OVERWRITE */
  36618. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36619. (int)XSTRLEN(testhostName2));
  36620. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36621. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36622. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36623. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36624. AssertIntEQ(1, ret);
  36625. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36626. (int)XSTRLEN(testhostName1)));
  36627. AssertIntEQ(0x01, paramTo->hostFlags);
  36628. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36629. /* inherit flags test : VPARAM_RESET_FLAGS */
  36630. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36631. (int)XSTRLEN(testhostName2));
  36632. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36633. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36634. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36635. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36636. AssertIntEQ(1, ret);
  36637. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36638. (int)XSTRLEN(testhostName1)));
  36639. AssertIntEQ(0x01, paramTo->hostFlags);
  36640. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36641. /* inherit flags test : VPARAM_LOCKED */
  36642. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36643. (int)XSTRLEN(testhostName2));
  36644. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  36645. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36646. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36647. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  36648. AssertIntEQ(1, ret);
  36649. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36650. (int)XSTRLEN(testhostName2)));
  36651. AssertIntEQ(0x00, paramTo->hostFlags);
  36652. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36653. /* test for incorrect parameters */
  36654. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  36655. AssertIntEQ(0, ret);
  36656. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  36657. AssertIntEQ(0, ret);
  36658. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36659. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  36660. AssertIntEQ(1, ret);
  36661. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  36662. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  36663. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  36664. AssertIntEQ(1, ret);
  36665. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  36666. AssertIntEQ(0, ret);
  36667. X509_VERIFY_PARAM_free(paramTo);
  36668. X509_VERIFY_PARAM_free(paramFrom);
  36669. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36670. res = TEST_RES_CHECK(1);
  36671. #endif
  36672. return res;
  36673. }
  36674. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  36675. static int test_wolfSSL_check_domain_verify_count = 0;
  36676. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36677. WOLFSSL_X509_STORE_CTX* store)
  36678. {
  36679. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  36680. AssertIntEQ(preverify, 1);
  36681. test_wolfSSL_check_domain_verify_count++;
  36682. return 1;
  36683. }
  36684. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36685. {
  36686. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  36687. /* Domain check should only be done on the leaf cert */
  36688. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36689. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36690. "wolfSSL Server Chain", 0), 1);
  36691. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36692. test_wolfSSL_check_domain_verify_cb);
  36693. }
  36694. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36695. {
  36696. /* Use a cert with different domains in chain */
  36697. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36698. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36699. }
  36700. static int test_wolfSSL_check_domain(void)
  36701. {
  36702. tcp_ready ready;
  36703. func_args client_args;
  36704. func_args server_args;
  36705. THREAD_TYPE serverThread;
  36706. callback_functions func_cb_client;
  36707. callback_functions func_cb_server;
  36708. XMEMSET(&client_args, 0, sizeof(func_args));
  36709. XMEMSET(&server_args, 0, sizeof(func_args));
  36710. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  36711. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  36712. #ifdef WOLFSSL_TIRTOS
  36713. fdOpenSession(Task_self());
  36714. #endif
  36715. StartTCP();
  36716. InitTcpReady(&ready);
  36717. #if defined(USE_WINDOWS_API)
  36718. /* use RNG to get random port if using windows */
  36719. ready.port = GetRandomPort();
  36720. #endif
  36721. server_args.signal = &ready;
  36722. client_args.signal = &ready;
  36723. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36724. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36725. client_args.callbacks = &func_cb_client;
  36726. server_args.callbacks = &func_cb_server;
  36727. start_thread(test_server_nofail, &server_args, &serverThread);
  36728. wait_tcp_ready(&server_args);
  36729. test_client_nofail(&client_args, NULL);
  36730. join_thread(serverThread);
  36731. AssertTrue(client_args.return_code);
  36732. AssertTrue(server_args.return_code);
  36733. FreeTcpReady(&ready);
  36734. /* Should have been called once for each cert in sent chain */
  36735. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36736. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36737. #else
  36738. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36739. #endif
  36740. return TEST_RES_CHECK(1);
  36741. }
  36742. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  36743. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36744. {
  36745. int res = TEST_SKIPPED;
  36746. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36747. X509* x509 = NULL;
  36748. X509_PUBKEY* pubKey;
  36749. AssertNotNull(x509 = X509_new());
  36750. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36751. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36752. X509_free(x509);
  36753. res = TEST_RES_CHECK(1);
  36754. #endif
  36755. return res;
  36756. }
  36757. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36758. {
  36759. int res = TEST_SKIPPED;
  36760. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36761. !defined(NO_SHA256) && !defined(NO_RSA)
  36762. X509* x509 = NULL;
  36763. ASN1_OBJECT* obj = NULL;
  36764. const ASN1_OBJECT* pa_oid = NULL;
  36765. X509_PUBKEY* pubKey;
  36766. X509_PUBKEY* pubKey2;
  36767. EVP_PKEY* evpKey;
  36768. const unsigned char *pk;
  36769. int ppklen, pptype;
  36770. X509_ALGOR *pa;
  36771. const void *pval;
  36772. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36773. SSL_FILETYPE_PEM));
  36774. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36775. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36776. AssertNotNull(pk);
  36777. AssertNotNull(pa);
  36778. AssertNotNull(pubKey);
  36779. AssertIntGT(ppklen, 0);
  36780. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36781. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36782. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  36783. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36784. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36785. AssertNotNull(pk);
  36786. AssertNotNull(pa);
  36787. AssertIntGT(ppklen, 0);
  36788. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36789. AssertNotNull(pa_oid);
  36790. AssertNull(pval);
  36791. AssertIntEQ(pptype, V_ASN1_NULL);
  36792. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36793. X509_PUBKEY_free(pubKey2);
  36794. X509_free(x509);
  36795. EVP_PKEY_free(evpKey);
  36796. res = TEST_RES_CHECK(1);
  36797. #endif
  36798. return res;
  36799. }
  36800. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36801. {
  36802. int res = TEST_SKIPPED;
  36803. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36804. X509* x509 = NULL;
  36805. ASN1_OBJECT* obj = NULL;
  36806. ASN1_OBJECT* poid;
  36807. const ASN1_OBJECT* pa_oid = NULL;
  36808. X509_PUBKEY* pubKey;
  36809. X509_PUBKEY* pubKey2;
  36810. EVP_PKEY* evpKey;
  36811. const unsigned char *pk;
  36812. int ppklen, pptype;
  36813. X509_ALGOR *pa;
  36814. const void *pval;
  36815. char buf[50];
  36816. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36817. SSL_FILETYPE_PEM));
  36818. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36819. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36820. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  36821. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36822. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36823. AssertNotNull(pk);
  36824. AssertNotNull(pa);
  36825. AssertIntGT(ppklen, 0);
  36826. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36827. AssertNotNull(pa_oid);
  36828. AssertNotNull(pval);
  36829. AssertIntEQ(pptype, V_ASN1_OBJECT);
  36830. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36831. poid = (ASN1_OBJECT *)pval;
  36832. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36833. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36834. X509_PUBKEY_free(pubKey2);
  36835. X509_free(x509);
  36836. EVP_PKEY_free(evpKey);
  36837. res = TEST_RES_CHECK(1);
  36838. #endif
  36839. return res;
  36840. }
  36841. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36842. {
  36843. int res = TEST_SKIPPED;
  36844. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36845. word32 bytes;
  36846. #ifdef USE_CERT_BUFFERS_1024
  36847. byte tmp[ONEK_BUF];
  36848. #elif defined(USE_CERT_BUFFERS_2048)
  36849. byte tmp[TWOK_BUF];
  36850. #else
  36851. byte tmp[TWOK_BUF];
  36852. #endif /* END USE_CERT_BUFFERS_1024 */
  36853. const unsigned char* dsaKeyDer = tmp;
  36854. ASN1_OBJECT* obj = NULL;
  36855. ASN1_STRING* str;
  36856. const ASN1_OBJECT* pa_oid = NULL;
  36857. X509_PUBKEY* pubKey = NULL;
  36858. EVP_PKEY* evpKey = NULL;
  36859. const unsigned char *pk;
  36860. int ppklen, pptype;
  36861. X509_ALGOR *pa;
  36862. const void *pval;
  36863. #ifdef USE_CERT_BUFFERS_1024
  36864. XMEMSET(tmp, 0, sizeof(tmp));
  36865. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36866. bytes = sizeof_dsa_key_der_1024;
  36867. #elif defined(USE_CERT_BUFFERS_2048)
  36868. XMEMSET(tmp, 0, sizeof(tmp));
  36869. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36870. bytes = sizeof_dsa_key_der_2048;
  36871. #else
  36872. {
  36873. XFILE fp;
  36874. XMEMSET(tmp, 0, sizeof(tmp));
  36875. fp = XFOPEN("./certs/dsa2048.der", "rb");
  36876. if (fp == XBADFILE) {
  36877. return WOLFSSL_BAD_FILE;
  36878. }
  36879. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  36880. XFCLOSE(fp);
  36881. }
  36882. #endif
  36883. /* Initialize pkey with der format dsa key */
  36884. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36885. AssertNotNull(pubKey = X509_PUBKEY_new());
  36886. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36887. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36888. AssertNotNull(pk);
  36889. AssertNotNull(pa);
  36890. AssertIntGT(ppklen, 0);
  36891. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36892. AssertNotNull(pa_oid);
  36893. AssertNotNull(pval);
  36894. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  36895. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36896. str = (ASN1_STRING *)pval;
  36897. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36898. #ifdef USE_CERT_BUFFERS_1024
  36899. AssertIntEQ(ASN1_STRING_length(str), 291);
  36900. #else
  36901. AssertIntEQ(ASN1_STRING_length(str), 549);
  36902. #endif /* END USE_CERT_BUFFERS_1024 */
  36903. X509_PUBKEY_free(pubKey);
  36904. EVP_PKEY_free(evpKey);
  36905. res = TEST_RES_CHECK(1);
  36906. #endif
  36907. return res;
  36908. }
  36909. static int test_wolfSSL_RAND(void)
  36910. {
  36911. int res = TEST_SKIPPED;
  36912. #if defined(OPENSSL_EXTRA)
  36913. byte seed[16];
  36914. XMEMSET(seed, 0, sizeof(seed));
  36915. RAND_seed(seed, sizeof(seed));
  36916. AssertIntEQ(RAND_poll(), 1);
  36917. RAND_cleanup();
  36918. AssertIntEQ(RAND_egd(NULL), -1);
  36919. #ifndef NO_FILESYSTEM
  36920. {
  36921. char fname[100];
  36922. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36923. AssertIntEQ(RAND_write_file(NULL), 0);
  36924. }
  36925. #endif
  36926. res = TEST_RES_CHECK(1);
  36927. #endif
  36928. return res;
  36929. }
  36930. static int test_wolfSSL_BUF(void)
  36931. {
  36932. int res = TEST_SKIPPED;
  36933. #if defined(OPENSSL_EXTRA)
  36934. BUF_MEM* buf;
  36935. AssertNotNull(buf = BUF_MEM_new());
  36936. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  36937. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  36938. BUF_MEM_free(buf);
  36939. res = TEST_RES_CHECK(1);
  36940. #endif
  36941. return res;
  36942. }
  36943. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36944. static int stub_rand_seed(const void *buf, int num)
  36945. {
  36946. (void)buf;
  36947. (void)num;
  36948. return 123;
  36949. }
  36950. static int stub_rand_bytes(unsigned char *buf, int num)
  36951. {
  36952. (void)buf;
  36953. (void)num;
  36954. return 456;
  36955. }
  36956. static byte* was_stub_rand_cleanup_called(void)
  36957. {
  36958. static byte was_called = 0;
  36959. return &was_called;
  36960. }
  36961. static void stub_rand_cleanup(void)
  36962. {
  36963. byte* was_called = was_stub_rand_cleanup_called();
  36964. *was_called = 1;
  36965. return;
  36966. }
  36967. static byte* was_stub_rand_add_called(void)
  36968. {
  36969. static byte was_called = 0;
  36970. return &was_called;
  36971. }
  36972. static int stub_rand_add(const void *buf, int num, double entropy)
  36973. {
  36974. byte* was_called = was_stub_rand_add_called();
  36975. (void)buf;
  36976. (void)num;
  36977. (void)entropy;
  36978. *was_called = 1;
  36979. return 0;
  36980. }
  36981. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36982. {
  36983. (void)buf;
  36984. (void)num;
  36985. return 9876;
  36986. }
  36987. static int stub_rand_status(void)
  36988. {
  36989. return 5432;
  36990. }
  36991. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36992. static int test_wolfSSL_RAND_set_rand_method(void)
  36993. {
  36994. int res = TEST_SKIPPED;
  36995. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36996. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36997. unsigned char* buf = NULL;
  36998. int num = 0;
  36999. double entropy = 0;
  37000. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  37001. byte* was_add_called = was_stub_rand_add_called();
  37002. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  37003. DYNAMIC_TYPE_TMP_BUFFER);
  37004. AssertIntNE(wolfSSL_RAND_status(), 5432);
  37005. AssertIntEQ(*was_cleanup_called, 0);
  37006. RAND_cleanup();
  37007. AssertIntEQ(*was_cleanup_called, 0);
  37008. rand_methods.seed = &stub_rand_seed;
  37009. rand_methods.bytes = &stub_rand_bytes;
  37010. rand_methods.cleanup = &stub_rand_cleanup;
  37011. rand_methods.add = &stub_rand_add;
  37012. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37013. rand_methods.status = &stub_rand_status;
  37014. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37015. AssertIntEQ(RAND_seed(buf, num), 123);
  37016. AssertIntEQ(RAND_bytes(buf, num), 456);
  37017. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37018. AssertIntEQ(RAND_status(), 5432);
  37019. AssertIntEQ(*was_add_called, 0);
  37020. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  37021. RAND_add(buf, num, entropy);
  37022. AssertIntEQ(*was_add_called, 1);
  37023. was_add_called = 0;
  37024. AssertIntEQ(*was_cleanup_called, 0);
  37025. RAND_cleanup();
  37026. AssertIntEQ(*was_cleanup_called, 1);
  37027. *was_cleanup_called = 0;
  37028. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  37029. AssertIntNE(RAND_status(), 5432);
  37030. AssertIntEQ(*was_cleanup_called, 0);
  37031. RAND_cleanup();
  37032. AssertIntEQ(*was_cleanup_called, 0);
  37033. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37034. res = TEST_RES_CHECK(1);
  37035. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37036. return res;
  37037. }
  37038. static int test_wolfSSL_RAND_bytes(void)
  37039. {
  37040. int res = TEST_SKIPPED;
  37041. #if defined(OPENSSL_EXTRA)
  37042. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37043. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37044. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37045. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37046. int max_bufsize;
  37047. byte *my_buf;
  37048. /* sanity check */
  37049. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  37050. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  37051. max_bufsize = size4;
  37052. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37053. DYNAMIC_TYPE_TMP_BUFFER);
  37054. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  37055. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  37056. AssertNotNull(my_buf);
  37057. XMEMSET(my_buf, 0, max_bufsize);
  37058. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  37059. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  37060. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  37061. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  37062. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37063. res = TEST_RES_CHECK(1);
  37064. #endif
  37065. return res;
  37066. }
  37067. static int test_wolfSSL_PKCS8_Compat(void)
  37068. {
  37069. int res = TEST_SKIPPED;
  37070. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  37071. #ifndef NO_BIO
  37072. PKCS8_PRIV_KEY_INFO* pt;
  37073. BIO* bio;
  37074. XFILE f;
  37075. int bytes;
  37076. char pkcs8_buffer[512];
  37077. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37078. EVP_PKEY *pkey = NULL;
  37079. #endif
  37080. /* file from wolfssl/certs/ directory */
  37081. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  37082. AssertTrue(f != XBADFILE);
  37083. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  37084. XFCLOSE(f);
  37085. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37086. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37087. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37088. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  37089. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37090. /* gets PKCS8 pointer to pkey */
  37091. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  37092. EVP_PKEY_free(pkey);
  37093. #endif
  37094. BIO_free(bio);
  37095. PKCS8_PRIV_KEY_INFO_free(pt);
  37096. res = TEST_RES_CHECK(1);
  37097. #endif
  37098. #endif
  37099. return res;
  37100. }
  37101. static int test_wolfSSL_PKCS8_d2i(void)
  37102. {
  37103. int res = TEST_SKIPPED;
  37104. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37105. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37106. * requires a 12 byte password in FIPS mode. This test ends up
  37107. * trying to use an 8 byte password. */
  37108. #ifndef NO_FILESYSTEM
  37109. unsigned char pkcs8_buffer[2048];
  37110. const unsigned char* p;
  37111. int bytes;
  37112. XFILE file;
  37113. WOLFSSL_EVP_PKEY* pkey = NULL;
  37114. #ifndef NO_BIO
  37115. BIO* bio;
  37116. #if defined(OPENSSL_ALL) && \
  37117. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37118. defined(HAVE_ECC)) && \
  37119. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37120. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37121. #endif
  37122. #endif
  37123. #ifndef NO_RSA
  37124. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37125. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37126. #ifndef NO_DES3
  37127. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37128. #endif
  37129. #endif /* NO_RSA */
  37130. #ifdef HAVE_ECC
  37131. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37132. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37133. #ifndef NO_DES3
  37134. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37135. #endif
  37136. #endif /* HAVE_ECC */
  37137. #endif /* !NO_FILESYSTEM */
  37138. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37139. #ifndef NO_RSA
  37140. #ifdef USE_CERT_BUFFERS_1024
  37141. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37142. int rsaSz = sizeof_server_key_der_1024;
  37143. #else
  37144. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37145. int rsaSz = sizeof_server_key_der_2048;
  37146. #endif
  37147. #endif
  37148. #ifdef HAVE_ECC
  37149. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37150. int ecSz = sizeof_ecc_key_der_256;
  37151. #endif
  37152. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37153. #ifndef NO_FILESYSTEM
  37154. (void)pkcs8_buffer;
  37155. (void)p;
  37156. (void)bytes;
  37157. (void)file;
  37158. #ifndef NO_BIO
  37159. (void)bio;
  37160. #endif
  37161. #endif
  37162. #ifdef OPENSSL_ALL
  37163. #ifndef NO_RSA
  37164. /* Try to auto-detect normal RSA private key */
  37165. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37166. EVP_PKEY_free(pkey);
  37167. #endif
  37168. #ifdef HAVE_ECC
  37169. /* Try to auto-detect normal EC private key */
  37170. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37171. EVP_PKEY_free(pkey);
  37172. #endif
  37173. #endif /* OPENSSL_ALL */
  37174. #ifndef NO_FILESYSTEM
  37175. #ifndef NO_RSA
  37176. /* Get DER encoded RSA PKCS#8 data. */
  37177. file = XFOPEN(rsaDerPkcs8File, "rb");
  37178. AssertTrue(file != XBADFILE);
  37179. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37180. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37181. file)), 0);
  37182. XFCLOSE(file);
  37183. p = pkcs8_buffer;
  37184. #ifdef OPENSSL_ALL
  37185. /* Try to decode - auto-detect key type. */
  37186. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37187. #else
  37188. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37189. #endif
  37190. /* Get PEM encoded RSA PKCS#8 data. */
  37191. file = XFOPEN(rsaPemPkcs8File, "rb");
  37192. AssertTrue(file != XBADFILE);
  37193. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37194. file)), 0);
  37195. XFCLOSE(file);
  37196. #if defined(OPENSSL_ALL) && \
  37197. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37198. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37199. /* Write PKCS#8 PEM to BIO. */
  37200. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37201. NULL), bytes);
  37202. /* Compare file and written data */
  37203. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37204. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37205. BIO_free(bio);
  37206. #if !defined(NO_DES3) && !defined(NO_SHA)
  37207. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37208. /* Write Encrypted PKCS#8 PEM to BIO. */
  37209. bytes = 1834;
  37210. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37211. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37212. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37213. (void*)"yassl123"));
  37214. EVP_PKEY_free(evpPkey);
  37215. BIO_free(bio);
  37216. #endif /* !NO_DES3 && !NO_SHA */
  37217. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37218. EVP_PKEY_free(pkey);
  37219. /* PKCS#8 encrypted RSA key */
  37220. #ifndef NO_DES3
  37221. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  37222. AssertTrue(file != XBADFILE);
  37223. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37224. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37225. file)), 0);
  37226. XFCLOSE(file);
  37227. #if defined(OPENSSL_ALL) && \
  37228. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37229. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37230. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37231. (void*)"yassl123"));
  37232. EVP_PKEY_free(pkey);
  37233. BIO_free(bio);
  37234. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37235. #endif /* !NO_DES3 */
  37236. #endif /* NO_RSA */
  37237. #ifdef HAVE_ECC
  37238. /* PKCS#8 encode EC key */
  37239. file = XFOPEN(ecDerPkcs8File, "rb");
  37240. AssertTrue(file != XBADFILE);
  37241. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37242. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37243. file)), 0);
  37244. XFCLOSE(file);
  37245. p = pkcs8_buffer;
  37246. #ifdef OPENSSL_ALL
  37247. /* Try to decode - auto-detect key type. */
  37248. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37249. #else
  37250. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37251. #endif
  37252. /* Get PEM encoded RSA PKCS#8 data. */
  37253. file = XFOPEN(ecPemPkcs8File, "rb");
  37254. AssertTrue(file != XBADFILE);
  37255. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37256. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37257. file)), 0);
  37258. XFCLOSE(file);
  37259. #if defined(OPENSSL_ALL) && \
  37260. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37261. defined(HAVE_AES_CBC)
  37262. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37263. /* Write PKCS#8 PEM to BIO. */
  37264. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37265. NULL), bytes);
  37266. /* Compare file and written data */
  37267. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37268. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37269. BIO_free(bio);
  37270. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37271. /* Write Encrypted PKCS#8 PEM to BIO. */
  37272. bytes = 379;
  37273. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37274. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37275. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37276. (void*)"yassl123"));
  37277. EVP_PKEY_free(evpPkey);
  37278. BIO_free(bio);
  37279. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37280. EVP_PKEY_free(pkey);
  37281. /* PKCS#8 encrypted EC key */
  37282. #ifndef NO_DES3
  37283. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  37284. AssertTrue(file != XBADFILE);
  37285. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  37286. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37287. file)), 0);
  37288. XFCLOSE(file);
  37289. #if defined(OPENSSL_ALL) && \
  37290. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37291. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37292. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37293. (void*)"yassl123"));
  37294. EVP_PKEY_free(pkey);
  37295. BIO_free(bio);
  37296. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37297. #endif /* !NO_DES3 */
  37298. #endif /* HAVE_ECC */
  37299. #endif /* !NO_FILESYSTEM */
  37300. res = TEST_RES_CHECK(1);
  37301. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37302. return res;
  37303. }
  37304. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37305. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37306. #define LOGGING_THREADS 5
  37307. #define ERROR_COUNT 10
  37308. /* copied from logging.c since this is not exposed otherwise */
  37309. #ifndef ERROR_QUEUE_MAX
  37310. #ifdef ERROR_QUEUE_PER_THREAD
  37311. #define ERROR_QUEUE_MAX 16
  37312. #else
  37313. /* this breaks from compat of unlimited error queue size */
  37314. #define ERROR_QUEUE_MAX 100
  37315. #endif
  37316. #endif
  37317. static volatile int loggingThreadsReady;
  37318. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37319. {
  37320. const char* file;
  37321. int line;
  37322. unsigned long err;
  37323. int errorCount = 0;
  37324. int i;
  37325. (void)args;
  37326. while (!loggingThreadsReady);
  37327. for (i = 0; i < ERROR_COUNT; i++)
  37328. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37329. while ((err = ERR_get_error_line(&file, &line))) {
  37330. AssertIntEQ(err, 990 + errorCount);
  37331. errorCount++;
  37332. }
  37333. AssertIntEQ(errorCount, ERROR_COUNT);
  37334. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37335. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37336. errorCount = 0;
  37337. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37338. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37339. while ((err = ERR_get_error_line(&file, &line))) {
  37340. AssertIntEQ(err, 990 + errorCount);
  37341. errorCount++;
  37342. }
  37343. /* test that the 3 errors over the max were dropped */
  37344. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37345. return 0;
  37346. }
  37347. #endif
  37348. static int test_error_queue_per_thread(void)
  37349. {
  37350. int res = TEST_SKIPPED;
  37351. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37352. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37353. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37354. int i;
  37355. ERR_clear_error(); /* clear out any error nodes */
  37356. loggingThreadsReady = 0;
  37357. for (i = 0; i < LOGGING_THREADS; i++)
  37358. start_thread(test_logging, NULL, &loggingThreads[i]);
  37359. loggingThreadsReady = 1;
  37360. for (i = 0; i < LOGGING_THREADS; i++)
  37361. join_thread(loggingThreads[i]);
  37362. res = TEST_RES_CHECK(1);
  37363. #endif
  37364. return res;
  37365. }
  37366. static int test_wolfSSL_ERR_put_error(void)
  37367. {
  37368. int res = TEST_SKIPPED;
  37369. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37370. defined(DEBUG_WOLFSSL)
  37371. const char* file;
  37372. int line;
  37373. ERR_clear_error(); /* clear out any error nodes */
  37374. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37375. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37376. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37377. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  37378. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37379. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  37380. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37381. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  37382. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37383. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  37384. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37385. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  37386. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37387. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  37388. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37389. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  37390. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37391. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  37392. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37393. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  37394. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37395. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  37396. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37397. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  37398. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37399. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  37400. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37401. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  37402. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37403. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  37404. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37405. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  37406. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37407. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37408. "this file", 100);
  37409. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37410. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37411. AssertIntEQ(line, 100);
  37412. AssertIntEQ(wolfSSL_ERR_peek_error(),
  37413. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37414. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37415. #endif
  37416. /* try reading past end of error queue */
  37417. file = NULL;
  37418. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37419. AssertNull(file);
  37420. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37421. PEMerr(4,4);
  37422. AssertIntEQ(ERR_get_error(), 4);
  37423. /* Empty and free up all error nodes */
  37424. ERR_clear_error();
  37425. /* Verify all nodes are cleared */
  37426. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37427. ERR_clear_error();
  37428. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  37429. res = TEST_RES_CHECK(1);
  37430. #endif
  37431. return res;
  37432. }
  37433. /*
  37434. * This is a regression test for a bug where the peek/get error functions were
  37435. * drawing from the end of the queue rather than the front.
  37436. */
  37437. static int test_wolfSSL_ERR_get_error_order(void)
  37438. {
  37439. int res = TEST_SKIPPED;
  37440. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  37441. /* Empty the queue. */
  37442. wolfSSL_ERR_clear_error();
  37443. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37444. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37445. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37446. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37447. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37448. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37449. res = TEST_RES_CHECK(1);
  37450. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  37451. return res;
  37452. }
  37453. #ifndef NO_BIO
  37454. static int test_wolfSSL_ERR_print_errors(void)
  37455. {
  37456. int res = TEST_SKIPPED;
  37457. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37458. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37459. BIO* bio;
  37460. char buf[1024];
  37461. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37462. ERR_clear_error(); /* clear out any error nodes */
  37463. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37464. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  37465. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  37466. ERR_print_errors(bio);
  37467. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37468. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37469. buf, 55), 0);
  37470. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37471. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  37472. buf, 56), 0);
  37473. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37474. AssertIntEQ(buf[0], '\0');
  37475. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37476. BIO_free(bio);
  37477. res = TEST_RES_CHECK(1);
  37478. #endif
  37479. return res;
  37480. }
  37481. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37482. defined(DEBUG_WOLFSSL)
  37483. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37484. {
  37485. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37486. return 0;
  37487. }
  37488. #endif
  37489. static int test_wolfSSL_ERR_print_errors_cb(void)
  37490. {
  37491. int res = TEST_SKIPPED;
  37492. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37493. defined(DEBUG_WOLFSSL)
  37494. BIO* bio;
  37495. char buf[1024];
  37496. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37497. ERR_clear_error(); /* clear out any error nodes */
  37498. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37499. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37500. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37501. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37502. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37503. buf, 53), 0);
  37504. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37505. buf + 53, 55), 0);
  37506. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37507. BIO_free(bio);
  37508. res = TEST_RES_CHECK(1);
  37509. #endif
  37510. return res;
  37511. }
  37512. /*
  37513. * Testing WOLFSSL_ERROR_MSG
  37514. */
  37515. static int test_WOLFSSL_ERROR_MSG(void)
  37516. {
  37517. int res = TEST_SKIPPED;
  37518. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37519. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37520. const char* msg = TEST_STRING;
  37521. WOLFSSL_ERROR_MSG(msg);
  37522. res = TEST_RES_CHECK(1);
  37523. #endif
  37524. return res;
  37525. }/*End test_WOLFSSL_ERROR_MSG*/
  37526. /*
  37527. * Testing wc_ERR_remove_state
  37528. */
  37529. static int test_wc_ERR_remove_state(void)
  37530. {
  37531. int res = TEST_SKIPPED;
  37532. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37533. wc_ERR_remove_state();
  37534. res = TEST_RES_CHECK(1);
  37535. #endif
  37536. return res;
  37537. }/*End test_wc_ERR_remove_state*/
  37538. /*
  37539. * Testing wc_ERR_print_errors_fp
  37540. */
  37541. static int test_wc_ERR_print_errors_fp(void)
  37542. {
  37543. int res = TEST_SKIPPED;
  37544. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37545. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37546. long sz;
  37547. XFILE fp;
  37548. int ret = 0;
  37549. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37550. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  37551. wc_ERR_print_errors_fp(fp);
  37552. #if defined(DEBUG_WOLFSSL)
  37553. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37554. sz = XFTELL(fp);
  37555. #ifdef NO_ERROR_QUEUE
  37556. /* File should be empty when NO_ERROR_QUEUE is defined */
  37557. if (sz != 0) {
  37558. ret = BAD_FUNC_ARG;
  37559. }
  37560. #else
  37561. if (sz == 0) {
  37562. ret = BAD_FUNC_ARG;
  37563. }
  37564. #endif
  37565. #endif
  37566. XFCLOSE(fp);
  37567. (void)sz;
  37568. res = TEST_RES_CHECK(ret == 0);
  37569. #endif
  37570. return res;
  37571. }/*End test_wc_ERR_print_errors_fp*/
  37572. #ifdef DEBUG_WOLFSSL
  37573. static void Logging_cb(const int logLevel, const char *const logMessage)
  37574. {
  37575. (void)logLevel;
  37576. (void)logMessage;
  37577. }
  37578. #endif
  37579. /*
  37580. * Testing wolfSSL_GetLoggingCb
  37581. */
  37582. static int test_wolfSSL_GetLoggingCb(void)
  37583. {
  37584. int ret = 0;
  37585. #ifdef DEBUG_WOLFSSL
  37586. /* Testing without wolfSSL_SetLoggingCb() */
  37587. if (ret == 0) {
  37588. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  37589. ret = 0;
  37590. }
  37591. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  37592. ret = -1;
  37593. }
  37594. }
  37595. /* Testing with wolfSSL_SetLoggingCb() */
  37596. if (ret == 0) {
  37597. ret = wolfSSL_SetLoggingCb(Logging_cb);
  37598. if (ret == 0) {
  37599. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  37600. ret = -1;
  37601. }
  37602. if (ret == 0) {
  37603. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  37604. ret = 0;
  37605. }
  37606. }
  37607. /* reset logging callback */
  37608. wolfSSL_SetLoggingCb(NULL);
  37609. }
  37610. }
  37611. #endif
  37612. if (ret == 0) {
  37613. if (wolfSSL_GetLoggingCb() != NULL) {
  37614. ret = -1;
  37615. }
  37616. }
  37617. return TEST_RES_CHECK(ret == 0);
  37618. }/*End test_wolfSSL_GetLoggingCb*/
  37619. #endif /* !NO_BIO */
  37620. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37621. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37622. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37623. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  37624. {
  37625. static const unsigned char key[] = "simple test key";
  37626. HMAC_CTX* hmac;
  37627. ENGINE* e = NULL;
  37628. unsigned char hash[WC_MAX_DIGEST_SIZE];
  37629. unsigned int len;
  37630. AssertNotNull(hmac = HMAC_CTX_new());
  37631. HMAC_CTX_init(hmac);
  37632. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  37633. SSL_SUCCESS);
  37634. /* re-using test key as data to hash */
  37635. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  37636. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  37637. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  37638. AssertIntEQ(len, md_len);
  37639. AssertIntEQ(HMAC_size(hmac), md_len);
  37640. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  37641. HMAC_cleanup(hmac);
  37642. HMAC_CTX_free(hmac);
  37643. len = 0;
  37644. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  37645. AssertIntEQ(len, md_len);
  37646. return 0;
  37647. }
  37648. #endif
  37649. static int test_wolfSSL_HMAC(void)
  37650. {
  37651. int res = TEST_SKIPPED;
  37652. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37653. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37654. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37655. #ifndef NO_SHA256
  37656. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  37657. #endif
  37658. #ifdef WOLFSSL_SHA224
  37659. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  37660. #endif
  37661. #ifdef WOLFSSL_SHA384
  37662. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  37663. #endif
  37664. #ifdef WOLFSSL_SHA512
  37665. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  37666. #endif
  37667. #ifdef WOLFSSL_SHA3
  37668. #ifndef WOLFSSL_NOSHA3_224
  37669. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  37670. #endif
  37671. #ifndef WOLFSSL_NOSHA3_256
  37672. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  37673. #endif
  37674. #ifndef WOLFSSL_NOSHA3_384
  37675. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  37676. #endif
  37677. #ifndef WOLFSSL_NOSHA3_512
  37678. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  37679. #endif
  37680. #endif
  37681. #ifndef NO_SHA
  37682. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  37683. #endif
  37684. res = TEST_RES_CHECK(1);
  37685. #endif
  37686. return res;
  37687. }
  37688. static int test_wolfSSL_CMAC(void)
  37689. {
  37690. int res = TEST_SKIPPED;
  37691. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  37692. defined(WOLFSSL_AES_DIRECT)
  37693. int i;
  37694. byte key[AES_128_KEY_SIZE];
  37695. CMAC_CTX* cmacCtx = NULL;
  37696. byte out[AES_BLOCK_SIZE];
  37697. size_t outLen = AES_BLOCK_SIZE;
  37698. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  37699. key[i] = i;
  37700. }
  37701. AssertNotNull(cmacCtx = CMAC_CTX_new());
  37702. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  37703. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37704. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37705. NULL), SSL_SUCCESS);
  37706. /* re-using test key as data to hash */
  37707. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  37708. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  37709. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  37710. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  37711. CMAC_CTX_free(cmacCtx);
  37712. /* give a key too small for the cipher, verify we get failure */
  37713. cmacCtx = NULL;
  37714. AssertNotNull(cmacCtx = CMAC_CTX_new());
  37715. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37716. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  37717. NULL), SSL_FAILURE);
  37718. CMAC_CTX_free(cmacCtx);
  37719. res = TEST_RES_CHECK(1);
  37720. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37721. return res;
  37722. }
  37723. static int test_wolfSSL_OBJ(void)
  37724. {
  37725. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  37726. * mode
  37727. */
  37728. int res = TEST_SKIPPED;
  37729. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  37730. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  37731. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  37732. ASN1_OBJECT *obj = NULL;
  37733. ASN1_OBJECT *obj2 = NULL;
  37734. char buf[50];
  37735. XFILE fp;
  37736. X509 *x509 = NULL;
  37737. X509_NAME *x509Name;
  37738. X509_NAME_ENTRY *x509NameEntry;
  37739. ASN1_OBJECT *asn1Name = NULL;
  37740. int numNames;
  37741. BIO *bio = NULL;
  37742. int nid;
  37743. int i, j;
  37744. const char *f[] = {
  37745. #ifndef NO_RSA
  37746. "./certs/ca-cert.der",
  37747. #endif
  37748. #ifdef HAVE_ECC
  37749. "./certs/ca-ecc-cert.der",
  37750. "./certs/ca-ecc384-cert.der",
  37751. #endif
  37752. NULL};
  37753. ASN1_OBJECT *field_name_obj = NULL;
  37754. int lastpos = -1;
  37755. int tmp = -1;
  37756. ASN1_STRING *asn1 = NULL;
  37757. unsigned char *buf_dyn = NULL;
  37758. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  37759. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37760. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  37761. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  37762. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37763. ASN1_OBJECT_free(obj);
  37764. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  37765. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  37766. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  37767. #ifdef WOLFSSL_CERT_EXT
  37768. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  37769. #endif
  37770. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37771. AssertNotNull(obj2 = OBJ_dup(obj));
  37772. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  37773. ASN1_OBJECT_free(obj);
  37774. ASN1_OBJECT_free(obj2);
  37775. for (i = 0; f[i] != NULL; i++)
  37776. {
  37777. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  37778. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  37779. XFCLOSE(fp);
  37780. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  37781. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37782. /* Get the Common Name by using OBJ_txt2obj */
  37783. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  37784. do
  37785. {
  37786. lastpos = tmp;
  37787. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  37788. } while (tmp > -1);
  37789. AssertIntNE(lastpos, -1);
  37790. ASN1_OBJECT_free(field_name_obj);
  37791. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  37792. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  37793. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  37794. /*
  37795. * All Common Names should be www.wolfssl.com
  37796. * This makes testing easier as we can test for the expected value.
  37797. */
  37798. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  37799. OPENSSL_free(buf_dyn);
  37800. bio = BIO_new(BIO_s_mem());
  37801. AssertTrue(bio != NULL);
  37802. for (j = 0; j < numNames; j++)
  37803. {
  37804. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37805. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  37806. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37807. }
  37808. BIO_free(bio);
  37809. X509_free(x509);
  37810. }
  37811. #ifdef HAVE_PKCS12
  37812. {
  37813. PKCS12 *p12;
  37814. int boolRet;
  37815. EVP_PKEY *pkey = NULL;
  37816. const char *p12_f[] = {
  37817. #if !defined(NO_DES3) && !defined(NO_RSA)
  37818. "./certs/test-servercert.p12",
  37819. #endif
  37820. NULL};
  37821. for (i = 0; p12_f[i] != NULL; i++)
  37822. {
  37823. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  37824. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  37825. XFCLOSE(fp);
  37826. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  37827. &pkey, &x509, NULL)) > 0);
  37828. wc_PKCS12_free(p12);
  37829. EVP_PKEY_free(pkey);
  37830. x509Name = X509_get_issuer_name(x509);
  37831. AssertNotNull(x509Name);
  37832. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37833. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  37834. for (j = 0; j < numNames; j++)
  37835. {
  37836. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37837. AssertNotNull(asn1Name =
  37838. X509_NAME_ENTRY_get_object(x509NameEntry));
  37839. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37840. }
  37841. BIO_free(bio);
  37842. X509_free(x509);
  37843. }
  37844. }
  37845. #endif /* HAVE_PKCS12 */
  37846. res = TEST_RES_CHECK(1);
  37847. #endif
  37848. return res;
  37849. }
  37850. static int test_wolfSSL_OBJ_cmp(void)
  37851. {
  37852. int res = TEST_SKIPPED;
  37853. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37854. ASN1_OBJECT *obj = NULL;
  37855. ASN1_OBJECT *obj2 = NULL;
  37856. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37857. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  37858. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  37859. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  37860. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  37861. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  37862. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  37863. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  37864. ASN1_OBJECT_free(obj);
  37865. ASN1_OBJECT_free(obj2);
  37866. res = TEST_RES_CHECK(1);
  37867. #endif
  37868. return res;
  37869. }
  37870. static int test_wolfSSL_OBJ_txt2nid(void)
  37871. {
  37872. int res = TEST_SKIPPED;
  37873. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  37874. int i;
  37875. static const struct {
  37876. const char* sn;
  37877. const char* ln;
  37878. const char* oid;
  37879. int nid;
  37880. } testVals[] = {
  37881. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  37882. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  37883. NID_id_on_dnsSRV },
  37884. { "msUPN", "Microsoft User Principal Name",
  37885. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  37886. { NULL, NULL, NULL, NID_undef }
  37887. };
  37888. /* Invalid cases */
  37889. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  37890. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  37891. /* Valid cases */
  37892. for (i = 0; testVals[i].sn != NULL; i++) {
  37893. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  37894. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  37895. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  37896. }
  37897. res = TEST_RES_CHECK(1);
  37898. #endif
  37899. return res;
  37900. }
  37901. static int test_wolfSSL_OBJ_txt2obj(void)
  37902. {
  37903. int res = TEST_SKIPPED;
  37904. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  37905. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  37906. int i;
  37907. char buf[50];
  37908. ASN1_OBJECT* obj;
  37909. static const struct {
  37910. const char* oidStr;
  37911. const char* sn;
  37912. const char* ln;
  37913. } objs_list[] = {
  37914. #if defined(WOLFSSL_APACHE_HTTPD)
  37915. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  37916. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  37917. #endif
  37918. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  37919. { NULL, NULL, NULL }
  37920. };
  37921. static const struct {
  37922. const char* numeric;
  37923. const char* name;
  37924. } objs_named[] = {
  37925. /* In dictionary but not in normal list. */
  37926. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  37927. /* Made up OID. */
  37928. { "1.3.5.7", "1.3.5.7" },
  37929. { NULL, NULL }
  37930. };
  37931. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  37932. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  37933. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  37934. /* Test numerical value of oid (oidStr) */
  37935. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  37936. /* Convert object back to text to confirm oid is correct */
  37937. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37938. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37939. ASN1_OBJECT_free(obj);
  37940. XMEMSET(buf, 0, sizeof(buf));
  37941. /* Test short name (sn) */
  37942. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  37943. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  37944. /* Convert object back to text to confirm oid is correct */
  37945. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37946. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37947. ASN1_OBJECT_free(obj);
  37948. XMEMSET(buf, 0, sizeof(buf));
  37949. /* Test long name (ln) - should fail when no_name = 1 */
  37950. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  37951. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  37952. /* Convert object back to text to confirm oid is correct */
  37953. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37954. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  37955. ASN1_OBJECT_free(obj);
  37956. XMEMSET(buf, 0, sizeof(buf));
  37957. }
  37958. for (i = 0; objs_named[i].numeric != NULL; i++) {
  37959. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  37960. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  37961. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  37962. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  37963. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  37964. ASN1_OBJECT_free(obj);
  37965. }
  37966. res = TEST_RES_CHECK(1);
  37967. #endif
  37968. return res;
  37969. }
  37970. static int test_wolfSSL_PEM_write_bio_X509(void)
  37971. {
  37972. int res = TEST_SKIPPED;
  37973. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  37974. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  37975. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  37976. !defined(NO_FILESYSTEM)
  37977. /* This test contains the hard coded expected
  37978. * lengths. Update if necessary */
  37979. FILE* fp = NULL;
  37980. WOLFSSL_EVP_PKEY *priv = NULL;
  37981. BIO* input = NULL;
  37982. BIO* output = NULL;
  37983. X509* x509a = NULL;
  37984. X509* x509b = NULL;
  37985. ASN1_TIME* notBeforeA = NULL;
  37986. ASN1_TIME* notAfterA = NULL;
  37987. #ifndef NO_ASN_TIME
  37988. ASN1_TIME* notBeforeB = NULL;
  37989. ASN1_TIME* notAfterB = NULL;
  37990. #endif
  37991. int expectedLen;
  37992. fp = XFOPEN("certs/server-key.pem", "rb");
  37993. AssertNotNull(fp);
  37994. priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  37995. XFCLOSE(fp);
  37996. fp = NULL;
  37997. AssertNotNull(priv);
  37998. AssertNotNull(input = BIO_new_file(
  37999. "certs/test/cert-ext-multiple.pem", "rb"));
  38000. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  38001. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  38002. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38003. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  38004. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  38005. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  38006. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38007. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38008. /* compare length against expected */
  38009. expectedLen = 2000;
  38010. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38011. #ifndef NO_ASN_TIME
  38012. /* read exported X509 PEM back into struct, sanity check on export,
  38013. * make sure notBefore/notAfter are the same and certs are identical. */
  38014. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38015. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  38016. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  38017. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38018. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38019. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38020. X509_free(x509b);
  38021. #endif
  38022. /* Reset output buffer */
  38023. BIO_free(output);
  38024. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38025. /* Test forcing the AKID to be generated just from KeyIdentifier */
  38026. if (x509a->authKeyIdSrc != NULL) {
  38027. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  38028. x509a->authKeyId = x509a->authKeyIdSrc;
  38029. x509a->authKeyIdSrc = NULL;
  38030. x509a->authKeyIdSrcSz = 0;
  38031. }
  38032. /* Resign to re-generate the der */
  38033. AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  38034. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38035. /* Check that we generate a smaller output since the AKID will
  38036. * only contain the KeyIdentifier without any additional
  38037. * information */
  38038. /* Here we copy the validity struct from the original */
  38039. expectedLen = 1688;
  38040. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38041. /* Reset buffers and x509 */
  38042. BIO_free(input);
  38043. BIO_free(output);
  38044. X509_free(x509a);
  38045. /* test CA and basicConstSet values are encoded when
  38046. * the cert is a CA */
  38047. AssertNotNull(input = BIO_new_file(
  38048. "certs/server-cert.pem", "rb"));
  38049. /* read PEM into X509 struct */
  38050. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38051. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38052. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38053. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38054. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  38055. * values are maintained and certs are identical.*/
  38056. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38057. AssertIntEQ(x509b->isCa, 1);
  38058. AssertIntEQ(x509b->basicConstSet, 1);
  38059. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38060. X509_free(x509a);
  38061. X509_free(x509b);
  38062. BIO_free(input);
  38063. BIO_free(output);
  38064. /* test CA and basicConstSet values are encoded when
  38065. * the cert is not CA */
  38066. AssertNotNull(input = BIO_new_file(
  38067. "certs/client-uri-cert.pem", "rb"));
  38068. /* read PEM into X509 struct */
  38069. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38070. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38071. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38072. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38073. /* read exported X509 PEM back into struct, ensure isCa and
  38074. * basicConstSet values are maintained and certs are identical */
  38075. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38076. AssertIntEQ(x509b->isCa, 0);
  38077. AssertIntEQ(x509b->basicConstSet, 1);
  38078. AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38079. wolfSSL_EVP_PKEY_free(priv);
  38080. X509_free(x509a);
  38081. X509_free(x509b);
  38082. BIO_free(input);
  38083. BIO_free(output);
  38084. res = TEST_RES_CHECK(1);
  38085. #endif
  38086. return res;
  38087. }
  38088. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38089. {
  38090. int res = TEST_SKIPPED;
  38091. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38092. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38093. X509* x509;
  38094. #ifndef NO_BIO
  38095. BIO* bio;
  38096. #endif
  38097. X509_NAME* nm;
  38098. X509_NAME_ENTRY* entry;
  38099. unsigned char cn[] = "another name to add";
  38100. #ifdef OPENSSL_ALL
  38101. int i, names_len;
  38102. #endif
  38103. AssertNotNull(x509 =
  38104. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38105. #ifndef NO_BIO
  38106. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38107. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38108. #endif
  38109. #ifdef WOLFSSL_CERT_REQ
  38110. {
  38111. X509_REQ* req;
  38112. #ifndef NO_BIO
  38113. BIO* bReq;
  38114. #endif
  38115. AssertNotNull(req =
  38116. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38117. #ifndef NO_BIO
  38118. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  38119. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38120. BIO_free(bReq);
  38121. #endif
  38122. X509_free(req);
  38123. }
  38124. #endif
  38125. AssertNotNull(nm = X509_get_subject_name(x509));
  38126. /* Test add entry */
  38127. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38128. 0x0c, cn, (int)sizeof(cn)));
  38129. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38130. #ifdef WOLFSSL_CERT_EXT
  38131. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38132. (byte*)"support@wolfssl.com", 19, -1,
  38133. 1), WOLFSSL_SUCCESS);
  38134. #endif
  38135. X509_NAME_ENTRY_free(entry);
  38136. #ifdef WOLFSSL_CERT_REQ
  38137. {
  38138. unsigned char srv_pkcs9p[] = "Server";
  38139. unsigned char fvrtDrnk[] = "tequila";
  38140. unsigned char* der = NULL;
  38141. char* subject;
  38142. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38143. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38144. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38145. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38146. AssertIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38147. AssertNotNull(der);
  38148. subject = X509_NAME_oneline(nm, 0, 0);
  38149. AssertNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38150. #ifdef DEBUG_WOLFSSL
  38151. fprintf(stderr, "\n\t%s\n", subject);
  38152. #endif
  38153. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38154. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38155. }
  38156. #endif
  38157. /* Test add entry by text */
  38158. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38159. 0x0c, cn, (int)sizeof(cn)));
  38160. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38161. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38162. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38163. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38164. #endif
  38165. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38166. X509_NAME_ENTRY_free(entry);
  38167. /* Test add entry by NID */
  38168. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38169. cn, -1, -1, 0), SSL_SUCCESS);
  38170. #ifdef OPENSSL_ALL
  38171. /* stack of name entry */
  38172. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38173. for (i=0; i<names_len; i++) {
  38174. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38175. }
  38176. #endif
  38177. #ifndef NO_BIO
  38178. BIO_free(bio);
  38179. #endif
  38180. X509_free(x509); /* free's nm */
  38181. res = TEST_RES_CHECK(1);
  38182. #endif
  38183. return res;
  38184. }
  38185. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  38186. static int test_GENERAL_NAME_set0_othername(void) {
  38187. int res = TEST_SKIPPED;
  38188. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38189. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38190. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38191. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM)
  38192. const char * cert_fname = "./certs/server-cert.der";
  38193. const char * key_fname = "./certs/server-key.der";
  38194. X509* x509 = NULL;
  38195. GENERAL_NAME* gn = NULL;
  38196. GENERAL_NAMES* gns = NULL;
  38197. ASN1_OBJECT* upn_oid = NULL;
  38198. ASN1_UTF8STRING *utf8str = NULL;
  38199. ASN1_TYPE *value = NULL;
  38200. X509_EXTENSION * ext = NULL;
  38201. byte* pt = NULL;
  38202. byte der[4096];
  38203. int derSz = 0;
  38204. EVP_PKEY* priv = NULL;
  38205. FILE* f = NULL;
  38206. AssertNotNull(f = fopen(cert_fname, "rb"));
  38207. AssertNotNull(x509 = d2i_X509_fp(f, NULL));
  38208. fclose(f);
  38209. AssertNotNull(gn = GENERAL_NAME_new());
  38210. AssertNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38211. AssertNotNull(utf8str = ASN1_UTF8STRING_new());
  38212. AssertIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38213. AssertNotNull(value = ASN1_TYPE_new());
  38214. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38215. AssertIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38216. AssertNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38217. AssertIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38218. AssertNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38219. AssertIntEQ(X509_add_ext(x509, ext, -1), 1);
  38220. AssertNotNull(f = fopen(key_fname, "rb"));
  38221. AssertIntGT(derSz = (int)fread(der, 1, sizeof(der), f), 0);
  38222. fclose(f);
  38223. pt = der;
  38224. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38225. (const unsigned char**)&pt, derSz));
  38226. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38227. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38228. ASN1_OBJECT_free(upn_oid);
  38229. X509_EXTENSION_free(ext);
  38230. X509_free(x509);
  38231. EVP_PKEY_free(priv);
  38232. res = TEST_RES_CHECK(1);
  38233. #endif
  38234. return res;
  38235. }
  38236. /* Note the lack of wolfSSL_ prefix...this is a compatability layer test. */
  38237. static int test_othername_and_SID_ext(void) {
  38238. int res = TEST_SKIPPED;
  38239. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38240. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38241. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38242. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM)
  38243. const char* csr_fname = "./certs/csr.signed.der";
  38244. const char* key_fname = "./certs/server-key.der";
  38245. byte der[4096];
  38246. int derSz = 0;
  38247. X509_REQ* x509 = NULL;
  38248. STACK_OF(X509_EXTENSION) *exts = NULL;
  38249. X509_EXTENSION * san_ext = NULL;
  38250. GENERAL_NAME* gn = NULL;
  38251. GENERAL_NAMES* gns = NULL;
  38252. ASN1_OBJECT* upn_oid = NULL;
  38253. ASN1_UTF8STRING *utf8str = NULL;
  38254. ASN1_TYPE *value = NULL;
  38255. /* SID extension. SID data format explained here:
  38256. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38257. */
  38258. uint8_t SidExtension[] = {
  38259. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38260. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38261. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38262. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38263. X509_EXTENSION *sid_ext = NULL;
  38264. ASN1_OBJECT* sid_oid = NULL;
  38265. ASN1_OCTET_STRING *sid_data = NULL;
  38266. EVP_PKEY* priv = NULL;
  38267. FILE* f = NULL;
  38268. byte* pt = NULL;
  38269. AssertNotNull(f = fopen(csr_fname, "rb"));
  38270. AssertNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38271. fclose(f);
  38272. AssertIntEQ(X509_REQ_set_version(x509, 2), 1);
  38273. AssertNotNull(gn = GENERAL_NAME_new());
  38274. AssertNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38275. AssertNotNull(utf8str = ASN1_UTF8STRING_new());
  38276. AssertIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38277. AssertNotNull(value = ASN1_TYPE_new());
  38278. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38279. AssertIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38280. AssertNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38281. AssertIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38282. AssertNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38283. AssertNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38284. AssertNotNull(sid_data = ASN1_OCTET_STRING_new());
  38285. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38286. AssertNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38287. sid_data));
  38288. AssertNotNull(exts = sk_X509_EXTENSION_new_null());
  38289. /* Ensure an empty stack doesn't raise an error. */
  38290. AssertIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38291. AssertIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38292. AssertIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38293. AssertIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38294. AssertNotNull(f = fopen(key_fname, "rb"));
  38295. AssertIntGT(derSz = (int)fread(der, 1, sizeof(der), f), 0);
  38296. fclose(f);
  38297. pt = der;
  38298. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38299. (const unsigned char**)&pt, derSz));
  38300. AssertIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38301. pt = der;
  38302. AssertIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38303. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38304. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38305. ASN1_OBJECT_free(upn_oid);
  38306. ASN1_OBJECT_free(sid_oid);
  38307. ASN1_OCTET_STRING_free(sid_data);
  38308. X509_REQ_free(x509);
  38309. EVP_PKEY_free(priv);
  38310. res = TEST_RES_CHECK(1);
  38311. #endif
  38312. return res;
  38313. }
  38314. static int test_wolfSSL_X509_set_name(void)
  38315. {
  38316. int res = TEST_SKIPPED;
  38317. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38318. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38319. X509* x509;
  38320. X509_NAME* name;
  38321. AssertNotNull(name = X509_NAME_new());
  38322. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38323. (byte*)"wolfssl.com", 11, 0, 1),
  38324. WOLFSSL_SUCCESS);
  38325. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38326. (byte*)"support@wolfssl.com", 19, -1,
  38327. 1), WOLFSSL_SUCCESS);
  38328. AssertNotNull(x509 = X509_new());
  38329. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38330. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38331. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38332. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38333. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38334. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38335. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38336. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38337. X509_free(x509);
  38338. X509_NAME_free(name);
  38339. res = TEST_RES_CHECK(1);
  38340. #endif /* OPENSSL_ALL && !NO_CERTS */
  38341. return res;
  38342. }
  38343. static int test_wolfSSL_X509_set_notAfter(void)
  38344. {
  38345. int res = TEST_SKIPPED;
  38346. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38347. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38348. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38349. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38350. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38351. /* Generalized time will overflow time_t if not long */
  38352. X509* x;
  38353. BIO* bio;
  38354. ASN1_TIME *asn_time, *time_check;
  38355. const int year = 365*24*60*60;
  38356. const int day = 24*60*60;
  38357. const int hour = 60*60;
  38358. const int mini = 60;
  38359. int offset_day;
  38360. unsigned char buf[25];
  38361. time_t t;
  38362. /*
  38363. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38364. */
  38365. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38366. offset_day = 7;
  38367. /*
  38368. * Free these.
  38369. */
  38370. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38371. AssertNotNull(asn_time);
  38372. AssertNotNull(x = X509_new());
  38373. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38374. /*
  38375. * Tests
  38376. */
  38377. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38378. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38379. AssertNotNull(time_check = X509_get_notAfter(x));
  38380. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38381. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38382. /* Convert to human readable format and compare to intended date */
  38383. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38384. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38385. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38386. /*
  38387. * Cleanup
  38388. */
  38389. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  38390. X509_free(x);
  38391. BIO_free(bio);
  38392. res = TEST_RES_CHECK(1);
  38393. #endif
  38394. return res;
  38395. }
  38396. static int test_wolfSSL_X509_set_notBefore(void)
  38397. {
  38398. int res = TEST_SKIPPED;
  38399. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38400. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38401. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38402. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38403. X509* x;
  38404. BIO* bio;
  38405. ASN1_TIME *asn_time, *time_check;
  38406. const int year = 365*24*60*60;
  38407. const int day = 24*60*60;
  38408. const int hour = 60*60;
  38409. const int mini = 60;
  38410. int offset_day;
  38411. unsigned char buf[25];
  38412. time_t t;
  38413. /*
  38414. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38415. */
  38416. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38417. offset_day = 7;
  38418. /*
  38419. * Free these.
  38420. */
  38421. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38422. AssertNotNull(asn_time);
  38423. AssertNotNull(x = X509_new());
  38424. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38425. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38426. /*
  38427. * Main Tests
  38428. */
  38429. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38430. /* time_check == (ANS1_TIME*)x->notBefore */
  38431. AssertNotNull(time_check = X509_get_notBefore(x));
  38432. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38433. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38434. /* Convert to human readable format and compare to intended date */
  38435. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38436. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38437. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38438. /*
  38439. * Cleanup
  38440. */
  38441. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  38442. X509_free(x);
  38443. BIO_free(bio);
  38444. res = TEST_RES_CHECK(1);
  38445. #endif
  38446. return res;
  38447. }
  38448. static int test_wolfSSL_X509_set_version(void)
  38449. {
  38450. int res = TEST_SKIPPED;
  38451. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38452. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38453. X509* x509;
  38454. long v = 2L;
  38455. long maxInt = INT_MAX;
  38456. AssertNotNull(x509 = X509_new());
  38457. /* These should pass. */
  38458. AssertTrue(wolfSSL_X509_set_version(x509, v));
  38459. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  38460. /* Fail Case: When v(long) is greater than x509->version(int). */
  38461. v = maxInt+1;
  38462. AssertFalse(wolfSSL_X509_set_version(x509, v));
  38463. /* Cleanup */
  38464. X509_free(x509);
  38465. res = TEST_RES_CHECK(1);
  38466. #endif
  38467. return res;
  38468. }
  38469. #ifndef NO_BIO
  38470. static int test_wolfSSL_BIO_gets(void)
  38471. {
  38472. int res = TEST_SKIPPED;
  38473. #if defined(OPENSSL_EXTRA)
  38474. BIO* bio;
  38475. BIO* bio2;
  38476. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  38477. char emp[] = "";
  38478. char bio_buffer[20];
  38479. int bufferSz = 20;
  38480. /* try with bad args */
  38481. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  38482. /* try with real msg */
  38483. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38484. XMEMSET(bio_buffer, 0, bufferSz);
  38485. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  38486. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  38487. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  38488. AssertFalse(bio2 != BIO_next(bio));
  38489. /* make buffer filled with no terminating characters */
  38490. XMEMSET(bio_buffer, 1, bufferSz);
  38491. /* BIO_gets reads a line of data */
  38492. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38493. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38494. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38495. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  38496. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38497. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38498. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38499. /* check not null terminated string */
  38500. BIO_free(bio);
  38501. msg[0] = 0x33;
  38502. msg[1] = 0x33;
  38503. msg[2] = 0x33;
  38504. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38505. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  38506. AssertIntEQ(bio_buffer[0], msg[0]);
  38507. AssertIntEQ(bio_buffer[1], msg[1]);
  38508. AssertIntNE(bio_buffer[2], msg[2]);
  38509. BIO_free(bio);
  38510. msg[3] = 0x33;
  38511. bio_buffer[3] = 0x33;
  38512. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38513. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  38514. AssertIntEQ(bio_buffer[0], msg[0]);
  38515. AssertIntEQ(bio_buffer[1], msg[1]);
  38516. AssertIntEQ(bio_buffer[2], msg[2]);
  38517. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  38518. /* check reading an empty string */
  38519. BIO_free(bio);
  38520. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  38521. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38522. AssertStrEQ(emp, bio_buffer);
  38523. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38524. /* check error cases */
  38525. BIO_free(bio);
  38526. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  38527. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38528. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38529. #if !defined(NO_FILESYSTEM)
  38530. {
  38531. BIO* f_bio;
  38532. XFILE f;
  38533. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  38534. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38535. f = XFOPEN(svrCertFile, "rb");
  38536. AssertTrue((f != XBADFILE));
  38537. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  38538. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38539. BIO_free(f_bio);
  38540. }
  38541. #endif /* NO_FILESYSTEM */
  38542. BIO_free(bio);
  38543. BIO_free(bio2);
  38544. /* try with type BIO */
  38545. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  38546. sizeof(msg));
  38547. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  38548. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38549. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  38550. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  38551. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  38552. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  38553. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  38554. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38555. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38556. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38557. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  38558. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38559. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38560. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38561. BIO_free(bio);
  38562. BIO_free(bio2);
  38563. /* check reading an empty string */
  38564. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  38565. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  38566. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38567. AssertStrEQ(emp, bio_buffer);
  38568. BIO_free(bio);
  38569. res = TEST_RES_CHECK(1);
  38570. #endif
  38571. return res;
  38572. }
  38573. static int test_wolfSSL_BIO_puts(void)
  38574. {
  38575. int res = TEST_SKIPPED;
  38576. #if defined(OPENSSL_EXTRA)
  38577. BIO* bio;
  38578. char input[] = "hello\0world\n.....ok\n\0";
  38579. char output[128];
  38580. XMEMSET(output, 0, sizeof(output));
  38581. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38582. AssertIntEQ(BIO_puts(bio, input), 5);
  38583. AssertIntEQ(BIO_pending(bio), 5);
  38584. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  38585. AssertIntEQ(BIO_pending(bio), 19);
  38586. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  38587. AssertStrEQ(output, "helloworld\n");
  38588. AssertIntEQ(BIO_pending(bio), 8);
  38589. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  38590. AssertStrEQ(output, ".....ok\n");
  38591. AssertIntEQ(BIO_pending(bio), 0);
  38592. AssertIntEQ(BIO_puts(bio, ""), -1);
  38593. BIO_free(bio);
  38594. res = TEST_RES_CHECK(1);
  38595. #endif
  38596. return res;
  38597. }
  38598. static int test_wolfSSL_BIO_dump(void)
  38599. {
  38600. int res = TEST_SKIPPED;
  38601. #if defined(OPENSSL_EXTRA)
  38602. BIO* bio;
  38603. static const unsigned char data[] = {
  38604. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  38605. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  38606. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  38607. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  38608. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  38609. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  38610. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  38611. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  38612. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  38613. 0xB4
  38614. };
  38615. /* Generated with OpenSSL. */
  38616. static const char expected[] =
  38617. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  38618. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  38619. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  38620. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  38621. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  38622. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  38623. static const char expectedAll[] =
  38624. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  38625. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  38626. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  38627. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  38628. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  38629. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  38630. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  38631. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  38632. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  38633. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  38634. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  38635. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  38636. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  38637. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  38638. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  38639. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  38640. char output[16 * 80];
  38641. int i;
  38642. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  38643. /* Example key dumped. */
  38644. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  38645. sizeof(expected) - 1);
  38646. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  38647. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  38648. /* Try every possible value for a character. */
  38649. for (i = 0; i < 256; i++)
  38650. output[i] = i;
  38651. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  38652. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  38653. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  38654. BIO_free(bio);
  38655. res = TEST_RES_CHECK(1);
  38656. #endif
  38657. return res;
  38658. }
  38659. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38660. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38661. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38662. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  38663. {
  38664. (void)ssl;
  38665. (void)buf;
  38666. (void)sz;
  38667. (void)ctx;
  38668. return WOLFSSL_CBIO_ERR_WANT_READ;
  38669. }
  38670. #endif
  38671. static int test_wolfSSL_BIO_should_retry(void)
  38672. {
  38673. int res = TEST_SKIPPED;
  38674. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38675. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38676. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38677. tcp_ready ready;
  38678. func_args server_args;
  38679. THREAD_TYPE serverThread;
  38680. SOCKET_T sockfd = 0;
  38681. WOLFSSL_CTX* ctx;
  38682. WOLFSSL* ssl;
  38683. char msg[64] = "hello wolfssl!";
  38684. char reply[1024];
  38685. int msgSz = (int)XSTRLEN(msg);
  38686. int ret;
  38687. BIO* bio;
  38688. XMEMSET(&server_args, 0, sizeof(func_args));
  38689. #ifdef WOLFSSL_TIRTOS
  38690. fdOpenSession(Task_self());
  38691. #endif
  38692. StartTCP();
  38693. InitTcpReady(&ready);
  38694. #if defined(USE_WINDOWS_API)
  38695. /* use RNG to get random port if using windows */
  38696. ready.port = GetRandomPort();
  38697. #endif
  38698. server_args.signal = &ready;
  38699. start_thread(test_server_nofail, &server_args, &serverThread);
  38700. wait_tcp_ready(&server_args);
  38701. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38702. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  38703. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  38704. #endif
  38705. AssertIntEQ(WOLFSSL_SUCCESS,
  38706. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38707. AssertIntEQ(WOLFSSL_SUCCESS,
  38708. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38709. AssertIntEQ(WOLFSSL_SUCCESS,
  38710. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38711. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  38712. /* force retry */
  38713. ssl = wolfSSL_new(ctx);
  38714. AssertNotNull(ssl);
  38715. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  38716. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  38717. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  38718. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  38719. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  38720. AssertIntNE(BIO_should_retry(bio), 0);
  38721. /* now perform successful connection */
  38722. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  38723. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  38724. BIO_read(bio, reply, sizeof(reply));
  38725. ret = wolfSSL_get_error(ssl, -1);
  38726. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  38727. AssertIntNE(BIO_should_retry(bio), 0);
  38728. }
  38729. else {
  38730. AssertIntEQ(BIO_should_retry(bio), 0);
  38731. }
  38732. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  38733. XSTRLEN("I hear you fa shizzle!")), 0);
  38734. BIO_free(bio);
  38735. wolfSSL_CTX_free(ctx);
  38736. join_thread(serverThread);
  38737. FreeTcpReady(&ready);
  38738. #ifdef WOLFSSL_TIRTOS
  38739. fdOpenSession(Task_self());
  38740. #endif
  38741. res = TEST_RES_CHECK(1);
  38742. #endif
  38743. return res;
  38744. }
  38745. static int test_wolfSSL_BIO_connect(void)
  38746. {
  38747. int res = TEST_SKIPPED;
  38748. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  38749. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  38750. tcp_ready ready;
  38751. func_args server_args;
  38752. THREAD_TYPE serverThread;
  38753. BIO *tcpBio;
  38754. BIO *sslBio;
  38755. SSL_CTX* ctx;
  38756. SSL *ssl;
  38757. SSL *sslPtr;
  38758. char msg[] = "hello wolfssl!";
  38759. char reply[30];
  38760. char buff[10] = {0};
  38761. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38762. AssertIntEQ(WOLFSSL_SUCCESS,
  38763. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38764. AssertIntEQ(WOLFSSL_SUCCESS,
  38765. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38766. AssertIntEQ(WOLFSSL_SUCCESS,
  38767. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38768. /* Setup server */
  38769. XMEMSET(&server_args, 0, sizeof(func_args));
  38770. StartTCP();
  38771. InitTcpReady(&ready);
  38772. #if defined(USE_WINDOWS_API)
  38773. /* use RNG to get random port if using windows */
  38774. ready.port = GetRandomPort();
  38775. #endif
  38776. server_args.signal = &ready;
  38777. start_thread(test_server_nofail, &server_args, &serverThread);
  38778. wait_tcp_ready(&server_args);
  38779. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38780. /* Start the test proper */
  38781. /* Setup the TCP BIO */
  38782. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  38783. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  38784. /* Setup the SSL object */
  38785. AssertNotNull(ssl = SSL_new(ctx));
  38786. SSL_set_connect_state(ssl);
  38787. /* Setup the SSL BIO */
  38788. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  38789. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  38790. /* Verify that BIO_get_ssl works. */
  38791. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  38792. AssertPtrEq(ssl, sslPtr);
  38793. /* Link BIO's so that sslBio uses tcpBio for IO */
  38794. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  38795. /* Do TCP connect */
  38796. AssertIntEQ(BIO_do_connect(sslBio), 1);
  38797. /* Do TLS handshake */
  38798. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  38799. /* Test writing */
  38800. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38801. /* Expect length of default wolfSSL reply */
  38802. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38803. /* Clean it all up */
  38804. BIO_free_all(sslBio);
  38805. /* Server clean up */
  38806. join_thread(serverThread);
  38807. FreeTcpReady(&ready);
  38808. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  38809. * after. */
  38810. XMEMSET(&server_args, 0, sizeof(func_args));
  38811. StartTCP();
  38812. InitTcpReady(&ready);
  38813. #if defined(USE_WINDOWS_API)
  38814. /* use RNG to get random port if using windows */
  38815. ready.port = GetRandomPort();
  38816. #endif
  38817. server_args.signal = &ready;
  38818. start_thread(test_server_nofail, &server_args, &serverThread);
  38819. wait_tcp_ready(&server_args);
  38820. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  38821. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  38822. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  38823. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  38824. AssertIntEQ(BIO_do_connect(sslBio), 1);
  38825. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  38826. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  38827. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  38828. /* Attempt to close the TLS connection gracefully. */
  38829. BIO_ssl_shutdown(sslBio);
  38830. BIO_free_all(sslBio);
  38831. join_thread(serverThread);
  38832. FreeTcpReady(&ready);
  38833. SSL_CTX_free(ctx);
  38834. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38835. wc_ecc_fp_free(); /* free per thread cache */
  38836. #endif
  38837. res = TEST_RES_CHECK(1);
  38838. #endif
  38839. return res;
  38840. }
  38841. static int test_wolfSSL_BIO_tls(void)
  38842. {
  38843. int res = TEST_SKIPPED;
  38844. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  38845. SSL_CTX* ctx;
  38846. SSL *ssl;
  38847. BIO *readBio;
  38848. BIO *writeBio;
  38849. int ret, err = 0;
  38850. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38851. AssertNotNull(ssl = SSL_new(ctx));
  38852. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  38853. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  38854. /* Qt reads data from write-bio,
  38855. * then writes the read data into plain packet.
  38856. * Qt reads data from plain packet,
  38857. * then writes the read data into read-bio.
  38858. */
  38859. SSL_set_bio(ssl, readBio, writeBio);
  38860. do {
  38861. #ifdef WOLFSSL_ASYNC_CRYPT
  38862. if (err == WC_PENDING_E) {
  38863. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  38864. if (ret < 0) { break; } else if (ret == 0) { continue; }
  38865. }
  38866. #endif
  38867. ret = SSL_connect(ssl);
  38868. err = SSL_get_error(ssl, 0);
  38869. } while (err == WC_PENDING_E);
  38870. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  38871. /* in this use case, should return WANT READ
  38872. * so that Qt will read the data from plain packet for next state.
  38873. */
  38874. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  38875. SSL_free(ssl);
  38876. SSL_CTX_free(ctx);
  38877. res = TEST_RES_CHECK(1);
  38878. #endif
  38879. return res;
  38880. }
  38881. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  38882. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  38883. {
  38884. BIO* clientBio;
  38885. SSL* sslClient;
  38886. SSL_CTX* ctx;
  38887. char connectAddr[20]; /* IP + port */;
  38888. (void)args;
  38889. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  38890. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  38891. AssertIntEQ(BIO_do_connect(clientBio), 1);
  38892. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38893. AssertNotNull(sslClient = SSL_new(ctx));
  38894. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  38895. SSL_set_bio(sslClient, clientBio, clientBio);
  38896. AssertIntEQ(SSL_connect(sslClient), 1);
  38897. SSL_free(sslClient);
  38898. SSL_CTX_free(ctx);
  38899. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38900. wc_ecc_fp_free(); /* free per thread cache */
  38901. #endif
  38902. return 0;
  38903. }
  38904. #endif
  38905. static int test_wolfSSL_BIO_accept(void)
  38906. {
  38907. int res = TEST_SKIPPED;
  38908. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  38909. BIO* serverBindBio;
  38910. BIO* serverAcceptBio;
  38911. SSL* sslServer;
  38912. SSL_CTX* ctx;
  38913. func_args args;
  38914. THREAD_TYPE thread;
  38915. char port[10]; /* 10 bytes should be enough to store the string
  38916. * representation of the port */
  38917. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  38918. AssertNotNull(serverBindBio = BIO_new_accept(port));
  38919. /* First BIO_do_accept binds the port */
  38920. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  38921. XMEMSET(&args, 0, sizeof(func_args));
  38922. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  38923. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  38924. /* Let's plug it into SSL to test */
  38925. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  38926. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  38927. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  38928. AssertNotNull(sslServer = SSL_new(ctx));
  38929. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  38930. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  38931. AssertIntEQ(SSL_accept(sslServer), 1);
  38932. join_thread(thread);
  38933. BIO_free(serverBindBio);
  38934. SSL_free(sslServer);
  38935. SSL_CTX_free(ctx);
  38936. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  38937. wc_ecc_fp_free(); /* free per thread cache */
  38938. #endif
  38939. res = TEST_RES_CHECK(1);
  38940. #endif
  38941. return res;
  38942. }
  38943. static int test_wolfSSL_BIO_write(void)
  38944. {
  38945. int res = TEST_SKIPPED;
  38946. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  38947. BIO* bio;
  38948. BIO* bio64;
  38949. BIO* ptr;
  38950. int sz;
  38951. char msg[] = "conversion test";
  38952. char out[40];
  38953. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  38954. void* bufPtr = NULL;
  38955. BUF_MEM* buf = NULL;
  38956. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  38957. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  38958. /* now should convert to base64 then write to memory */
  38959. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38960. BIO_flush(bio);
  38961. /* test BIO chain */
  38962. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  38963. AssertNotNull(buf);
  38964. AssertIntEQ(buf->length, 25);
  38965. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  38966. AssertPtrEq(buf->data, bufPtr);
  38967. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  38968. sz = sizeof(out);
  38969. XMEMSET(out, 0, sz);
  38970. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  38971. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  38972. /* write then read should return the same message */
  38973. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38974. sz = sizeof(out);
  38975. XMEMSET(out, 0, sz);
  38976. AssertIntEQ(BIO_read(bio, out, sz), 16);
  38977. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  38978. /* now try encoding with no line ending */
  38979. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  38980. #ifdef HAVE_EX_DATA
  38981. BIO_set_ex_data(bio64, 0, (void*) "data");
  38982. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  38983. #endif
  38984. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  38985. BIO_flush(bio);
  38986. sz = sizeof(out);
  38987. XMEMSET(out, 0, sz);
  38988. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  38989. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  38990. BIO_free_all(bio); /* frees bio64 also */
  38991. /* test with more than one bio64 in list */
  38992. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  38993. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  38994. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  38995. /* now should convert to base64 when stored and then decode with read */
  38996. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  38997. BIO_flush(bio);
  38998. sz = sizeof(out);
  38999. XMEMSET(out, 0, sz);
  39000. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  39001. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  39002. BIO_clear_flags(bio64, ~0);
  39003. BIO_set_retry_read(bio);
  39004. BIO_free_all(bio); /* frees bio64s also */
  39005. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  39006. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39007. BIO_free(bio);
  39008. res = TEST_RES_CHECK(1);
  39009. #endif
  39010. return res;
  39011. }
  39012. static int test_wolfSSL_BIO_printf(void)
  39013. {
  39014. int res = TEST_SKIPPED;
  39015. #if defined(OPENSSL_ALL)
  39016. BIO* bio;
  39017. int sz = 7;
  39018. char msg[] = "TLS 1.3 for the world";
  39019. char out[60];
  39020. char expected[] = "TLS 1.3 for the world : sz = 7";
  39021. XMEMSET(out, 0, sizeof(out));
  39022. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39023. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39024. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39025. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39026. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39027. BIO_free(bio);
  39028. res = TEST_RES_CHECK(1);
  39029. #endif
  39030. return res;
  39031. }
  39032. static int test_wolfSSL_BIO_f_md(void)
  39033. {
  39034. int res = TEST_SKIPPED;
  39035. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39036. BIO *bio, *mem;
  39037. char msg[] = "message to hash";
  39038. char out[60];
  39039. EVP_MD_CTX* ctx;
  39040. const unsigned char testKey[] =
  39041. {
  39042. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39043. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39044. 0x0b, 0x0b, 0x0b, 0x0b
  39045. };
  39046. const char testData[] = "Hi There";
  39047. const unsigned char testResult[] =
  39048. {
  39049. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39050. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39051. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39052. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39053. };
  39054. const unsigned char expectedHash[] =
  39055. {
  39056. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39057. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39058. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39059. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39060. };
  39061. const unsigned char emptyHash[] =
  39062. {
  39063. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39064. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39065. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39066. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39067. };
  39068. unsigned char check[sizeof(testResult) + 1];
  39069. size_t checkSz = -1;
  39070. EVP_PKEY* key;
  39071. XMEMSET(out, 0, sizeof(out));
  39072. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39073. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  39074. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39075. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39076. /* should not be able to write/read yet since just digest wrapper and no
  39077. * data is passing through the bio */
  39078. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  39079. AssertIntEQ(BIO_pending(bio), 0);
  39080. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39081. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  39082. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39083. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39084. BIO_reset(bio);
  39085. /* append BIO mem to bio in order to read/write */
  39086. AssertNotNull(bio = BIO_push(bio, mem));
  39087. XMEMSET(out, 0, sizeof(out));
  39088. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39089. AssertIntEQ(BIO_pending(bio), 16);
  39090. /* this just reads the message and does not hash it (gets calls final) */
  39091. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39092. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39093. /* create a message digest using BIO */
  39094. XMEMSET(out, 0, sizeof(out));
  39095. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39096. AssertIntEQ(BIO_pending(mem), 16);
  39097. AssertIntEQ(BIO_pending(bio), 16);
  39098. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39099. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39100. BIO_free(bio);
  39101. BIO_free(mem);
  39102. /* test with HMAC */
  39103. XMEMSET(out, 0, sizeof(out));
  39104. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39105. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  39106. BIO_get_md_ctx(bio, &ctx);
  39107. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  39108. testKey, (int)sizeof(testKey)));
  39109. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39110. AssertNotNull(bio = BIO_push(bio, mem));
  39111. BIO_write(bio, testData, (int)strlen(testData));
  39112. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  39113. EVP_DigestSignFinal(ctx, check, &checkSz);
  39114. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39115. EVP_PKEY_free(key);
  39116. BIO_free(bio);
  39117. BIO_free(mem);
  39118. res = TEST_RES_CHECK(1);
  39119. #endif
  39120. return res;
  39121. }
  39122. static int test_wolfSSL_BIO_up_ref(void)
  39123. {
  39124. int res = TEST_SKIPPED;
  39125. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39126. BIO* bio;
  39127. AssertNotNull(bio = BIO_new(BIO_f_md()));
  39128. AssertIntEQ(BIO_up_ref(NULL), 0);
  39129. AssertIntEQ(BIO_up_ref(bio), 1);
  39130. BIO_free(bio);
  39131. AssertIntEQ(BIO_up_ref(bio), 1);
  39132. BIO_free(bio);
  39133. BIO_free(bio);
  39134. res = TEST_RES_CHECK(1);
  39135. #endif
  39136. return res;
  39137. }
  39138. static int test_wolfSSL_BIO_reset(void)
  39139. {
  39140. int res = TEST_SKIPPED;
  39141. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39142. BIO* bio;
  39143. byte buf[16];
  39144. AssertNotNull(bio = BIO_new_mem_buf("secure your data",
  39145. (word32)XSTRLEN("secure your data")));
  39146. AssertIntEQ(BIO_read(bio, buf, 6), 6);
  39147. AssertIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39148. XMEMSET(buf, 0, 16);
  39149. AssertIntEQ(BIO_read(bio, buf, 16), 10);
  39150. AssertIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39151. /* You cannot write to MEM BIO with read-only mode. */
  39152. AssertIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39153. AssertIntEQ(BIO_read(bio, buf, 16), -1);
  39154. XMEMSET(buf, 0, 16);
  39155. AssertIntEQ(BIO_reset(bio), 0);
  39156. AssertIntEQ(BIO_read(bio, buf, 16), 16);
  39157. AssertIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39158. BIO_free(bio);
  39159. res = TEST_RES_CHECK(1);
  39160. #endif
  39161. return res;
  39162. }
  39163. #endif /* !NO_BIO */
  39164. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  39165. /* test that the callback arg is correct */
  39166. static int certCbArg = 0;
  39167. static int clientCertCb(WOLFSSL* ssl, void* arg)
  39168. {
  39169. if (ssl == NULL || arg != &certCbArg)
  39170. return 0;
  39171. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39172. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39173. return 0;
  39174. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39175. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39176. return 0;
  39177. return 1;
  39178. }
  39179. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  39180. {
  39181. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  39182. }
  39183. /**
  39184. * This is only done because test_client_nofail has no way to stop
  39185. * certificate and key loading
  39186. */
  39187. static void clientCertClearCb(WOLFSSL* ssl)
  39188. {
  39189. /* Clear the loaded certs to force the callbacks to set them up */
  39190. SSL_certs_clear(ssl);
  39191. }
  39192. static int serverCertCb(WOLFSSL* ssl, void* arg)
  39193. {
  39194. if (ssl == NULL || arg != &certCbArg)
  39195. return 0;
  39196. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39197. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39198. return 0;
  39199. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39200. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39201. return 0;
  39202. return 1;
  39203. }
  39204. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  39205. {
  39206. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  39207. }
  39208. /**
  39209. * This is only done because test_server_nofail has no way to stop
  39210. * certificate and key loading
  39211. */
  39212. static void serverCertClearCb(WOLFSSL* ssl)
  39213. {
  39214. /* Clear the loaded certs to force the callbacks to set them up */
  39215. SSL_certs_clear(ssl);
  39216. }
  39217. #endif
  39218. static int test_wolfSSL_cert_cb(void)
  39219. {
  39220. int res = TEST_SKIPPED;
  39221. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  39222. callback_functions func_cb_client;
  39223. callback_functions func_cb_server;
  39224. tcp_ready ready;
  39225. func_args client_args;
  39226. func_args server_args;
  39227. THREAD_TYPE serverThread;
  39228. XMEMSET(&client_args, 0, sizeof(func_args));
  39229. XMEMSET(&server_args, 0, sizeof(func_args));
  39230. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  39231. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  39232. #ifdef WOLFSSL_TIRTOS
  39233. fdOpenSession(Task_self());
  39234. #endif
  39235. StartTCP();
  39236. InitTcpReady(&ready);
  39237. #if defined(USE_WINDOWS_API)
  39238. /* use RNG to get random port if using windows */
  39239. ready.port = GetRandomPort();
  39240. #endif
  39241. server_args.signal = &ready;
  39242. client_args.signal = &ready;
  39243. client_args.callbacks = &func_cb_client;
  39244. server_args.callbacks = &func_cb_server;
  39245. func_cb_client.ctx_ready = clientCertSetupCb;
  39246. func_cb_client.ssl_ready = clientCertClearCb;
  39247. func_cb_server.ctx_ready = serverCertSetupCb;
  39248. func_cb_server.ssl_ready = serverCertClearCb;
  39249. start_thread(test_server_nofail, &server_args, &serverThread);
  39250. wait_tcp_ready(&server_args);
  39251. test_client_nofail(&client_args, NULL);
  39252. join_thread(serverThread);
  39253. AssertTrue(client_args.return_code);
  39254. AssertTrue(server_args.return_code);
  39255. FreeTcpReady(&ready);
  39256. #ifdef WOLFSSL_TIRTOS
  39257. fdOpenSession(Task_self());
  39258. #endif
  39259. res = TEST_RES_CHECK(1);
  39260. #endif
  39261. return res;
  39262. }
  39263. static int test_wolfSSL_SESSION(void)
  39264. {
  39265. int res = TEST_SKIPPED;
  39266. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39267. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39268. !defined(NO_SESSION_CACHE)
  39269. WOLFSSL* ssl;
  39270. WOLFSSL_CTX* ctx;
  39271. WOLFSSL_SESSION* sess;
  39272. WOLFSSL_SESSION* sess_copy;
  39273. #ifdef OPENSSL_EXTRA
  39274. #ifdef HAVE_EXT_CACHE
  39275. unsigned char* sessDer = NULL;
  39276. unsigned char* ptr = NULL;
  39277. int sz;
  39278. #endif
  39279. const unsigned char context[] = "user app context";
  39280. unsigned int contextSz = (unsigned int)sizeof(context);
  39281. #endif
  39282. int ret, err;
  39283. SOCKET_T sockfd;
  39284. tcp_ready ready;
  39285. func_args server_args;
  39286. THREAD_TYPE serverThread;
  39287. char msg[80];
  39288. const char* sendGET = "GET";
  39289. /* TLS v1.3 requires session tickets */
  39290. /* CHACHA and POLY1305 required for myTicketEncCb */
  39291. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  39292. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  39293. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  39294. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  39295. #else
  39296. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39297. #endif
  39298. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  39299. WOLFSSL_FILETYPE_PEM));
  39300. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  39301. WOLFSSL_FILETYPE_PEM));
  39302. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  39303. WOLFSSL_SUCCESS);
  39304. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39305. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  39306. #endif
  39307. #ifdef HAVE_SESSION_TICKET
  39308. /* Use session tickets, for ticket tests below */
  39309. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  39310. #endif
  39311. XMEMSET(&server_args, 0, sizeof(func_args));
  39312. #ifdef WOLFSSL_TIRTOS
  39313. fdOpenSession(Task_self());
  39314. #endif
  39315. StartTCP();
  39316. InitTcpReady(&ready);
  39317. #if defined(USE_WINDOWS_API)
  39318. /* use RNG to get random port if using windows */
  39319. ready.port = GetRandomPort();
  39320. #endif
  39321. server_args.signal = &ready;
  39322. start_thread(test_server_nofail, &server_args, &serverThread);
  39323. wait_tcp_ready(&server_args);
  39324. /* client connection */
  39325. ssl = wolfSSL_new(ctx);
  39326. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  39327. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39328. #ifdef WOLFSSL_ASYNC_CRYPT
  39329. err = 0; /* Reset error */
  39330. #endif
  39331. do {
  39332. #ifdef WOLFSSL_ASYNC_CRYPT
  39333. if (err == WC_PENDING_E) {
  39334. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39335. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39336. }
  39337. #endif
  39338. ret = wolfSSL_connect(ssl);
  39339. err = wolfSSL_get_error(ssl, 0);
  39340. } while (err == WC_PENDING_E);
  39341. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  39342. #ifdef WOLFSSL_ASYNC_CRYPT
  39343. err = 0; /* Reset error */
  39344. #endif
  39345. do {
  39346. #ifdef WOLFSSL_ASYNC_CRYPT
  39347. if (err == WC_PENDING_E) {
  39348. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39349. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39350. }
  39351. #endif
  39352. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  39353. err = wolfSSL_get_error(ssl, 0);
  39354. } while (err == WC_PENDING_E);
  39355. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  39356. #ifdef WOLFSSL_ASYNC_CRYPT
  39357. err = 0; /* Reset error */
  39358. #endif
  39359. do {
  39360. #ifdef WOLFSSL_ASYNC_CRYPT
  39361. if (err == WC_PENDING_E) {
  39362. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39363. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39364. }
  39365. #endif
  39366. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  39367. err = wolfSSL_get_error(ssl, 0);
  39368. } while (err == WC_PENDING_E);
  39369. AssertIntEQ(ret, 23);
  39370. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  39371. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  39372. #ifdef HAVE_EXT_CACHE
  39373. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  39374. * HAVE_EXT_CACHE we get new objects each time */
  39375. #endif
  39376. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39377. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  39378. sess = wolfSSL_get_session(ssl);
  39379. #ifdef OPENSSL_EXTRA
  39380. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  39381. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  39382. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  39383. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  39384. #ifdef HAVE_SESSION_TICKET
  39385. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  39386. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  39387. SESSION_TICKET_HINT_DEFAULT);
  39388. #else
  39389. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  39390. #endif
  39391. #else
  39392. (void)sess;
  39393. #endif /* OPENSSL_EXTRA */
  39394. /* Retain copy of the session for later testing */
  39395. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  39396. wolfSSL_shutdown(ssl);
  39397. wolfSSL_free(ssl);
  39398. join_thread(serverThread);
  39399. FreeTcpReady(&ready);
  39400. #ifdef WOLFSSL_TIRTOS
  39401. fdOpenSession(Task_self());
  39402. #endif
  39403. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  39404. {
  39405. X509 *x509;
  39406. char buf[30];
  39407. int bufSz;
  39408. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  39409. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  39410. X509_get_subject_name(x509), NID_organizationalUnitName,
  39411. buf, sizeof(buf))), 0);
  39412. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  39413. if (bufSz == 7) {
  39414. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  39415. }
  39416. if (bufSz == 16) {
  39417. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  39418. }
  39419. }
  39420. #endif
  39421. #ifdef HAVE_EXT_CACHE
  39422. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  39423. wolfSSL_SESSION_free(sess_copy);
  39424. sess_copy = NULL;
  39425. #endif
  39426. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  39427. /* get session from DER and update the timeout */
  39428. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  39429. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  39430. wolfSSL_SESSION_free(sess);
  39431. sess = NULL;
  39432. ptr = sessDer;
  39433. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  39434. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  39435. (const unsigned char**)&ptr, sz));
  39436. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  39437. sessDer = NULL;
  39438. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  39439. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39440. #endif
  39441. /* successful set session test */
  39442. AssertNotNull(ssl = wolfSSL_new(ctx));
  39443. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  39444. #ifdef HAVE_SESSION_TICKET
  39445. /* Test set/get session ticket */
  39446. {
  39447. const char* ticket = "This is a session ticket";
  39448. char buf[64] = {0};
  39449. word32 bufSz = (word32)sizeof(buf);
  39450. AssertIntEQ(SSL_SUCCESS,
  39451. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  39452. (word32)XSTRLEN(ticket)));
  39453. AssertIntEQ(SSL_SUCCESS,
  39454. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  39455. AssertStrEQ(ticket, buf);
  39456. }
  39457. #endif
  39458. #ifdef OPENSSL_EXTRA
  39459. /* session timeout case */
  39460. /* make the session to be expired */
  39461. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  39462. XSLEEP_MS(1200);
  39463. /* SSL_set_session should reject specified session but return success
  39464. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  39465. */
  39466. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39467. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  39468. #else
  39469. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  39470. #endif
  39471. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39472. /* fail case with miss match session context IDs (use compatibility API) */
  39473. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  39474. SSL_SUCCESS);
  39475. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39476. wolfSSL_free(ssl);
  39477. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  39478. SSL_FAILURE);
  39479. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  39480. SSL_SUCCESS);
  39481. AssertNotNull(ssl = wolfSSL_new(ctx));
  39482. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39483. #endif /* OPENSSL_EXTRA */
  39484. wolfSSL_free(ssl);
  39485. wolfSSL_SESSION_free(sess);
  39486. wolfSSL_CTX_free(ctx);
  39487. res = TEST_RES_CHECK(1);
  39488. #endif
  39489. return res;
  39490. }
  39491. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39492. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39493. static int clientSessRemCountMalloc = 0;
  39494. static int serverSessRemCountMalloc = 0;
  39495. static int clientSessRemCountFree = 0;
  39496. static int serverSessRemCountFree = 0;
  39497. static WOLFSSL_CTX* serverSessCtx = NULL;
  39498. static WOLFSSL_SESSION* serverSess = NULL;
  39499. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39500. !defined(NO_SESSION_CACHE_REF)
  39501. static WOLFSSL_CTX* clientSessCtx = NULL;
  39502. static WOLFSSL_SESSION* clientSess = NULL;
  39503. #endif
  39504. static int serverSessRemIdx = 3;
  39505. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  39506. {
  39507. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  39508. (void)ctx;
  39509. AssertNotNull(mallocedData);
  39510. if (!*mallocedData)
  39511. clientSessRemCountFree++;
  39512. else
  39513. serverSessRemCountFree++;
  39514. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  39515. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  39516. }
  39517. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  39518. {
  39519. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  39520. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  39521. !defined(NO_SESSION_CACHE_REF)
  39522. /* Allow downgrade, set min version, and disable TLS 1.3.
  39523. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  39524. * reference to the session cache. But with WOLFSSL_TLS13 and without
  39525. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  39526. * session in the cache. In this case we need to downgrade to previous
  39527. * versions to just use the legacy session ID field. */
  39528. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  39529. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  39530. #endif
  39531. }
  39532. static void SessRemSslSetupCb(WOLFSSL* ssl)
  39533. {
  39534. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  39535. AssertNotNull(mallocedData);
  39536. *mallocedData = SSL_is_server(ssl);
  39537. if (!*mallocedData) {
  39538. clientSessRemCountMalloc++;
  39539. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39540. !defined(NO_SESSION_CACHE_REF)
  39541. AssertNotNull(clientSess = SSL_get1_session(ssl));
  39542. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  39543. SSL_SUCCESS);
  39544. #endif
  39545. }
  39546. else {
  39547. serverSessRemCountMalloc++;
  39548. AssertNotNull(serverSess = SSL_get1_session(ssl));
  39549. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  39550. SSL_SUCCESS);
  39551. }
  39552. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  39553. mallocedData), SSL_SUCCESS);
  39554. }
  39555. #endif
  39556. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  39557. {
  39558. int res = TEST_SKIPPED;
  39559. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39560. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  39561. /* Check that the remove callback gets called for external data in a
  39562. * session object */
  39563. callback_functions func_cb;
  39564. tcp_ready ready;
  39565. func_args client_args;
  39566. func_args server_args;
  39567. THREAD_TYPE serverThread;
  39568. XMEMSET(&client_args, 0, sizeof(func_args));
  39569. XMEMSET(&server_args, 0, sizeof(func_args));
  39570. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  39571. #ifdef WOLFSSL_TIRTOS
  39572. fdOpenSession(Task_self());
  39573. #endif
  39574. StartTCP();
  39575. InitTcpReady(&ready);
  39576. #if defined(USE_WINDOWS_API)
  39577. /* use RNG to get random port if using windows */
  39578. ready.port = GetRandomPort();
  39579. #endif
  39580. server_args.signal = &ready;
  39581. client_args.signal = &ready;
  39582. client_args.callbacks = &func_cb;
  39583. server_args.callbacks = &func_cb;
  39584. func_cb.ctx_ready = SessRemCtxSetupCb;
  39585. func_cb.on_result = SessRemSslSetupCb;
  39586. start_thread(test_server_nofail, &server_args, &serverThread);
  39587. wait_tcp_ready(&server_args);
  39588. test_client_nofail(&client_args, NULL);
  39589. join_thread(serverThread);
  39590. AssertTrue(client_args.return_code);
  39591. AssertTrue(server_args.return_code);
  39592. FreeTcpReady(&ready);
  39593. #ifdef WOLFSSL_TIRTOS
  39594. fdOpenSession(Task_self());
  39595. #endif
  39596. /* Both should have been allocated */
  39597. AssertIntEQ(clientSessRemCountMalloc, 1);
  39598. AssertIntEQ(serverSessRemCountMalloc, 1);
  39599. /* This should not be called yet. Session wasn't evicted from cache yet. */
  39600. AssertIntEQ(clientSessRemCountFree, 0);
  39601. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39602. !defined(NO_SESSION_CACHE_REF)
  39603. /* Force a cache lookup */
  39604. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39605. /* Force a cache update */
  39606. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  39607. /* This should set the timeout to 0 and call the remove callback from within
  39608. * the session cache. */
  39609. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  39610. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  39611. AssertIntEQ(clientSessRemCountFree, 1);
  39612. #endif
  39613. /* Server session is in the cache so ex_data isn't free'd with the SSL
  39614. * object */
  39615. AssertIntEQ(serverSessRemCountFree, 0);
  39616. /* Force a cache lookup */
  39617. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39618. /* Force a cache update */
  39619. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  39620. /* This should set the timeout to 0 and call the remove callback from within
  39621. * the session cache. */
  39622. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  39623. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  39624. AssertIntEQ(serverSessRemCountFree, 1);
  39625. /* Need to free the references that we kept */
  39626. SSL_CTX_free(serverSessCtx);
  39627. SSL_SESSION_free(serverSess);
  39628. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  39629. !defined(NO_SESSION_CACHE_REF)
  39630. SSL_CTX_free(clientSessCtx);
  39631. SSL_SESSION_free(clientSess);
  39632. #endif
  39633. res = TEST_RES_CHECK(1);
  39634. #endif
  39635. return res;
  39636. }
  39637. static int test_wolfSSL_ticket_keys(void)
  39638. {
  39639. int res = TEST_SKIPPED;
  39640. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  39641. !defined(NO_WOLFSSL_SERVER)
  39642. WOLFSSL_CTX* ctx;
  39643. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  39644. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39645. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  39646. WOLFSSL_FAILURE);
  39647. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  39648. WOLFSSL_FAILURE);
  39649. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  39650. WOLFSSL_FAILURE);
  39651. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  39652. WOLFSSL_FAILURE);
  39653. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39654. WOLFSSL_FAILURE);
  39655. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39656. WOLFSSL_FAILURE);
  39657. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39658. WOLFSSL_FAILURE);
  39659. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  39660. WOLFSSL_FAILURE);
  39661. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  39662. WOLFSSL_FAILURE);
  39663. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  39664. WOLFSSL_FAILURE);
  39665. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  39666. WOLFSSL_FAILURE);
  39667. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  39668. WOLFSSL_FAILURE);
  39669. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  39670. WOLFSSL_FAILURE);
  39671. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  39672. WOLFSSL_FAILURE);
  39673. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39674. WOLFSSL_SUCCESS);
  39675. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  39676. WOLFSSL_SUCCESS);
  39677. wolfSSL_CTX_free(ctx);
  39678. res = TEST_RES_CHECK(1);
  39679. #endif
  39680. return res;
  39681. }
  39682. #ifndef NO_BIO
  39683. static int test_wolfSSL_d2i_PUBKEY(void)
  39684. {
  39685. int res = TEST_SKIPPED;
  39686. #if defined(OPENSSL_EXTRA)
  39687. BIO* bio;
  39688. EVP_PKEY* pkey;
  39689. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39690. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  39691. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  39692. /* RSA PUBKEY test */
  39693. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  39694. sizeof_client_keypub_der_2048), 0);
  39695. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39696. EVP_PKEY_free(pkey);
  39697. #endif
  39698. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  39699. /* ECC PUBKEY test */
  39700. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  39701. sizeof_ecc_clikeypub_der_256), 0);
  39702. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39703. EVP_PKEY_free(pkey);
  39704. #endif
  39705. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  39706. /* DSA PUBKEY test */
  39707. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  39708. sizeof_dsa_pub_key_der_2048), 0);
  39709. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39710. EVP_PKEY_free(pkey);
  39711. #endif
  39712. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  39713. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  39714. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39715. (HAVE_FIPS_VERSION > 2))
  39716. /* DH PUBKEY test */
  39717. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  39718. sizeof_dh_pub_key_der_2048), 0);
  39719. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  39720. EVP_PKEY_free(pkey);
  39721. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39722. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  39723. BIO_free(bio);
  39724. (void)pkey;
  39725. res = TEST_RES_CHECK(1);
  39726. #endif
  39727. return res;
  39728. }
  39729. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  39730. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  39731. {
  39732. BIO* bio = NULL;
  39733. EVP_PKEY* pkey = NULL;
  39734. #ifndef NO_RSA
  39735. #endif
  39736. WOLFSSL_CTX* ctx;
  39737. #if defined(WOLFSSL_KEY_GEN)
  39738. unsigned char buff[4096];
  39739. unsigned char* bufPtr = buff;
  39740. #endif
  39741. /* test creating new EVP_PKEY with bad arg */
  39742. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  39743. /* test loading RSA key using BIO */
  39744. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39745. {
  39746. XFILE file;
  39747. const char* fname = "./certs/server-key.der";
  39748. size_t sz;
  39749. byte* buf;
  39750. file = XFOPEN(fname, "rb");
  39751. AssertTrue((file != XBADFILE));
  39752. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39753. sz = XFTELL(file);
  39754. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39755. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39756. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  39757. XFCLOSE(file);
  39758. /* Test using BIO new mem and loading DER private key */
  39759. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39760. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39761. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39762. BIO_free(bio);
  39763. bio = NULL;
  39764. EVP_PKEY_free(pkey);
  39765. pkey = NULL;
  39766. }
  39767. #endif
  39768. /* test loading ECC key using BIO */
  39769. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  39770. {
  39771. XFILE file;
  39772. const char* fname = "./certs/ecc-key.der";
  39773. size_t sz;
  39774. byte* buf;
  39775. file = XFOPEN(fname, "rb");
  39776. AssertTrue((file != XBADFILE));
  39777. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  39778. sz = XFTELL(file);
  39779. AssertTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  39780. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  39781. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  39782. XFCLOSE(file);
  39783. /* Test using BIO new mem and loading DER private key */
  39784. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39785. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  39786. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  39787. BIO_free(bio);
  39788. bio = NULL;
  39789. EVP_PKEY_free(pkey);
  39790. pkey = NULL;
  39791. }
  39792. #endif
  39793. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39794. #ifndef NO_WOLFSSL_SERVER
  39795. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  39796. #else
  39797. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  39798. #endif
  39799. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  39800. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  39801. {
  39802. RSA* rsa = NULL;
  39803. /* Tests bad parameters */
  39804. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  39805. /* RSA not set yet, expecting to fail*/
  39806. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  39807. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  39808. /* set RSA using bio*/
  39809. AssertIntGT(BIO_write(bio, client_key_der_2048,
  39810. sizeof_client_key_der_2048), 0);
  39811. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  39812. AssertNotNull(rsa);
  39813. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  39814. /*i2d RSAprivate key tests */
  39815. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  39816. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  39817. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39818. sizeof_client_key_der_2048);
  39819. bufPtr -= sizeof_client_key_der_2048;
  39820. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39821. sizeof_client_key_der_2048), 0);
  39822. bufPtr = NULL;
  39823. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  39824. sizeof_client_key_der_2048);
  39825. AssertNotNull(bufPtr);
  39826. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  39827. sizeof_client_key_der_2048), 0);
  39828. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  39829. RSA_free(rsa);
  39830. rsa = RSA_new();
  39831. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  39832. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  39833. RSA_free(rsa);
  39834. }
  39835. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  39836. SSL_CTX_free(ctx);
  39837. ctx = NULL;
  39838. BIO_free(bio);
  39839. bio = NULL;
  39840. return TEST_RES_CHECK(1);
  39841. }
  39842. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  39843. #endif /* !NO_BIO */
  39844. static int test_wolfSSL_sk_GENERAL_NAME(void)
  39845. {
  39846. int res = TEST_SKIPPED;
  39847. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39848. !defined(NO_RSA)
  39849. X509* x509;
  39850. GENERAL_NAME* gn;
  39851. unsigned char buf[4096];
  39852. const unsigned char* bufPt;
  39853. int bytes, i;
  39854. int j;
  39855. XFILE f;
  39856. STACK_OF(GENERAL_NAME)* sk;
  39857. f = XFOPEN(cliCertDerFileExt, "rb");
  39858. AssertTrue((f != XBADFILE));
  39859. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  39860. XFCLOSE(f);
  39861. for (j = 0; j < 2; ++j) {
  39862. bufPt = buf;
  39863. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  39864. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  39865. NID_subject_alt_name, NULL, NULL));
  39866. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  39867. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  39868. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  39869. switch (gn->type) {
  39870. case GEN_DNS:
  39871. fprintf(stderr, "found type GEN_DNS\n");
  39872. break;
  39873. case GEN_EMAIL:
  39874. fprintf(stderr, "found type GEN_EMAIL\n");
  39875. break;
  39876. case GEN_URI:
  39877. fprintf(stderr, "found type GEN_URI\n");
  39878. break;
  39879. }
  39880. }
  39881. X509_free(x509);
  39882. if (j == 0) {
  39883. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  39884. }
  39885. else {
  39886. /*
  39887. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  39888. * it was supposed to. This is a regression test for that bug.
  39889. */
  39890. GENERAL_NAMES_free(sk);
  39891. }
  39892. }
  39893. res = TEST_RES_CHECK(1);
  39894. #endif
  39895. return res;
  39896. }
  39897. static int test_wolfSSL_GENERAL_NAME_print(void)
  39898. {
  39899. int res = TEST_SKIPPED;
  39900. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  39901. X509* x509;
  39902. GENERAL_NAME* gn;
  39903. unsigned char buf[4096];
  39904. const unsigned char* bufPt;
  39905. int bytes;
  39906. XFILE f;
  39907. STACK_OF(GENERAL_NAME)* sk;
  39908. BIO* out;
  39909. unsigned char outbuf[128];
  39910. X509_EXTENSION* ext;
  39911. AUTHORITY_INFO_ACCESS* aia;
  39912. ACCESS_DESCRIPTION* ad;
  39913. const unsigned char v4Addr[] = {192,168,53,1};
  39914. const unsigned char v6Addr[] =
  39915. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  39916. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  39917. const unsigned char email[] =
  39918. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  39919. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  39920. const char* dnsStr = "DNS:example.com";
  39921. const char* uriStr = "URI:http://127.0.0.1:22220";
  39922. const char* v4addStr = "IP Address:192.168.53.1";
  39923. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  39924. const char* emailStr = "email:info@wolfssl.com";
  39925. const char* othrStr = "othername:<unsupported>";
  39926. const char* x400Str = "X400Name:<unsupported>";
  39927. const char* ediStr = "EdiPartyName:<unsupported>";
  39928. /* BIO to output */
  39929. AssertNotNull(out = BIO_new(BIO_s_mem()));
  39930. /* test for NULL param */
  39931. gn = NULL;
  39932. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  39933. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  39934. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  39935. /* test for GEN_DNS */
  39936. f = XFOPEN(cliCertDerFileExt, "rb");
  39937. AssertTrue((f != XBADFILE));
  39938. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  39939. XFCLOSE(f);
  39940. bufPt = buf;
  39941. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  39942. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  39943. NID_subject_alt_name, NULL, NULL));
  39944. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  39945. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39946. XMEMSET(outbuf,0,sizeof(outbuf));
  39947. BIO_read(out, outbuf, sizeof(outbuf));
  39948. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  39949. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  39950. X509_free(x509);
  39951. /* test for GEN_URI */
  39952. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  39953. AssertTrue((f != XBADFILE));
  39954. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39955. XFCLOSE(f);
  39956. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  39957. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  39958. AssertNotNull(aia);
  39959. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  39960. gn = ad->location;
  39961. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39962. XMEMSET(outbuf,0,sizeof(outbuf));
  39963. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39964. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  39965. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  39966. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  39967. AssertNotNull(aia);
  39968. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  39969. X509_free(x509);
  39970. /* test for GEN_IPADD */
  39971. /* ip v4 address */
  39972. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39973. gn->type = GEN_IPADD;
  39974. gn->d.iPAddress->length = sizeof(v4Addr);
  39975. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  39976. sizeof(v4Addr)), 1);
  39977. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39978. XMEMSET(outbuf,0,sizeof(outbuf));
  39979. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39980. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  39981. GENERAL_NAME_free(gn);
  39982. /* ip v6 address */
  39983. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39984. gn->type = GEN_IPADD;
  39985. gn->d.iPAddress->length = sizeof(v6Addr);
  39986. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  39987. sizeof(v6Addr)), 1);
  39988. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  39989. XMEMSET(outbuf,0,sizeof(outbuf));
  39990. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  39991. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  39992. GENERAL_NAME_free(gn);
  39993. /* test for GEN_EMAIL */
  39994. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  39995. gn->type = GEN_EMAIL;
  39996. gn->d.rfc822Name->length = sizeof(email);
  39997. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  39998. sizeof(email)), 1);
  39999. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40000. XMEMSET(outbuf,0,sizeof(outbuf));
  40001. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40002. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  40003. GENERAL_NAME_free(gn);
  40004. /* test for GEN_OTHERNAME */
  40005. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40006. gn->type = GEN_OTHERNAME;
  40007. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40008. XMEMSET(outbuf,0,sizeof(outbuf));
  40009. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40010. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  40011. GENERAL_NAME_free(gn);
  40012. /* test for GEN_X400 */
  40013. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40014. gn->type = GEN_X400;
  40015. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40016. XMEMSET(outbuf,0,sizeof(outbuf));
  40017. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40018. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  40019. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40020. gn->type = GEN_IA5;
  40021. GENERAL_NAME_free(gn);
  40022. /* test for GEN_EDIPARTY */
  40023. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40024. gn->type = GEN_EDIPARTY;
  40025. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  40026. XMEMSET(outbuf,0,sizeof(outbuf));
  40027. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40028. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  40029. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40030. gn->type = GEN_IA5;
  40031. GENERAL_NAME_free(gn);
  40032. BIO_free(out);
  40033. res = TEST_RES_CHECK(1);
  40034. #endif /* OPENSSL_ALL */
  40035. return res;
  40036. }
  40037. static int test_wolfSSL_sk_DIST_POINT(void)
  40038. {
  40039. int res = TEST_SKIPPED;
  40040. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40041. !defined(NO_RSA)
  40042. X509* x509;
  40043. unsigned char buf[4096];
  40044. const unsigned char* bufPt;
  40045. int bytes, i, j;
  40046. XFILE f;
  40047. DIST_POINT* dp;
  40048. DIST_POINT_NAME* dpn;
  40049. GENERAL_NAME* gn;
  40050. ASN1_IA5STRING* uri;
  40051. STACK_OF(DIST_POINT)* dps;
  40052. STACK_OF(GENERAL_NAME)* gns;
  40053. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  40054. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  40055. AssertTrue((f != XBADFILE));
  40056. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40057. XFCLOSE(f);
  40058. bufPt = buf;
  40059. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40060. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  40061. NID_crl_distribution_points, NULL, NULL));
  40062. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  40063. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  40064. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  40065. AssertNotNull(dpn = dp->distpoint);
  40066. /* this should be type 0, fullname */
  40067. AssertIntEQ(dpn->type, 0);
  40068. gns = dp->distpoint->name.fullname;
  40069. AssertNotNull(gns);
  40070. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40071. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  40072. gn = sk_GENERAL_NAME_value(gns, j);
  40073. AssertIntEQ(gn->type, GEN_URI);
  40074. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  40075. AssertNotNull(uri->data);
  40076. AssertIntGT(uri->length, 0);
  40077. }
  40078. }
  40079. X509_free(x509);
  40080. CRL_DIST_POINTS_free(dps);
  40081. res = TEST_RES_CHECK(1);
  40082. #endif
  40083. return res;
  40084. }
  40085. static int test_wolfSSL_MD4(void)
  40086. {
  40087. int res = TEST_SKIPPED;
  40088. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  40089. MD4_CTX md4;
  40090. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  40091. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  40092. "789012345678901234567890";
  40093. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  40094. "\xcc\x05\x36";
  40095. int msgSz = (int)XSTRLEN(msg);
  40096. XMEMSET(out, 0, sizeof(out));
  40097. MD4_Init(&md4);
  40098. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  40099. MD4_Final(out, &md4);
  40100. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  40101. res = TEST_RES_CHECK(1);
  40102. #endif
  40103. return res;
  40104. }
  40105. static int test_wolfSSL_verify_mode(void)
  40106. {
  40107. int res = TEST_SKIPPED;
  40108. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40109. WOLFSSL* ssl;
  40110. WOLFSSL_CTX* ctx;
  40111. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40112. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40113. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40114. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  40115. AssertNotNull(ssl = SSL_new(ctx));
  40116. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40117. SSL_free(ssl);
  40118. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40119. AssertNotNull(ssl = SSL_new(ctx));
  40120. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40121. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40122. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40123. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  40124. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40125. SSL_free(ssl);
  40126. wolfSSL_CTX_set_verify(ctx,
  40127. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40128. AssertNotNull(ssl = SSL_new(ctx));
  40129. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40130. AssertIntEQ(SSL_get_verify_mode(ssl),
  40131. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40132. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  40133. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  40134. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40135. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40136. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40137. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40138. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40139. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40140. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  40141. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  40142. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  40143. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  40144. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  40145. #endif
  40146. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  40147. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40148. SSL_free(ssl);
  40149. SSL_CTX_free(ctx);
  40150. res = TEST_RES_CHECK(1);
  40151. #endif
  40152. return res;
  40153. }
  40154. static int test_wolfSSL_verify_depth(void)
  40155. {
  40156. int res = TEST_SKIPPED;
  40157. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40158. WOLFSSL* ssl;
  40159. WOLFSSL_CTX* ctx;
  40160. long depth;
  40161. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40162. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40163. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40164. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  40165. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  40166. AssertNotNull(ssl = SSL_new(ctx));
  40167. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  40168. SSL_free(ssl);
  40169. SSL_CTX_set_verify_depth(ctx, -1);
  40170. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  40171. SSL_CTX_set_verify_depth(ctx, 2);
  40172. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  40173. AssertNotNull(ssl = SSL_new(ctx));
  40174. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  40175. SSL_free(ssl);
  40176. SSL_CTX_free(ctx);
  40177. res = TEST_RES_CHECK(1);
  40178. #endif
  40179. return res;
  40180. }
  40181. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40182. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  40183. * buffer of 64 bytes.
  40184. *
  40185. * returns the size of the digest buffer on success and a negative value on
  40186. * failure.
  40187. */
  40188. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  40189. {
  40190. HMAC_CTX ctx1;
  40191. HMAC_CTX ctx2;
  40192. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  40193. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  40194. unsigned char long_key[] =
  40195. "0123456789012345678901234567890123456789"
  40196. "0123456789012345678901234567890123456789"
  40197. "0123456789012345678901234567890123456789"
  40198. "0123456789012345678901234567890123456789";
  40199. unsigned char msg[] = "message to hash";
  40200. unsigned int digestSz = 64;
  40201. int keySz = sizeof(key);
  40202. int long_keySz = sizeof(long_key);
  40203. int msgSz = sizeof(msg);
  40204. unsigned char digest2[64];
  40205. unsigned int digestSz2 = 64;
  40206. HMAC_CTX_init(&ctx1);
  40207. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40208. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40209. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40210. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40211. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40212. HMAC_CTX_cleanup(&ctx1);
  40213. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40214. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  40215. HMAC_CTX_cleanup(&ctx2);
  40216. AssertIntEQ(digestSz, digestSz2);
  40217. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40218. /* test HMAC_Init with NULL key */
  40219. /* init after copy */
  40220. HMAC_CTX_init(&ctx1);
  40221. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40222. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40223. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40224. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40225. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40226. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40227. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40228. HMAC_CTX_cleanup(&ctx1);
  40229. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40230. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40231. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40232. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40233. HMAC_CTX_cleanup(&ctx2);
  40234. AssertIntEQ(digestSz, digestSz2);
  40235. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40236. /* long key */
  40237. HMAC_CTX_init(&ctx1);
  40238. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  40239. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40240. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40241. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40242. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40243. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40244. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40245. HMAC_CTX_cleanup(&ctx1);
  40246. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40247. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40248. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40249. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40250. HMAC_CTX_cleanup(&ctx2);
  40251. AssertIntEQ(digestSz, digestSz2);
  40252. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40253. /* init before copy */
  40254. HMAC_CTX_init(&ctx1);
  40255. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40256. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40257. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40258. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40259. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40260. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40261. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40262. HMAC_CTX_cleanup(&ctx1);
  40263. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40264. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40265. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40266. HMAC_CTX_cleanup(&ctx2);
  40267. AssertIntEQ(digestSz, digestSz2);
  40268. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40269. return digestSz;
  40270. }
  40271. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  40272. static int test_wolfSSL_HMAC_CTX(void)
  40273. {
  40274. int res = TEST_SKIPPED;
  40275. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40276. unsigned char digest[64];
  40277. int digestSz;
  40278. #ifndef NO_SHA
  40279. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  40280. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  40281. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  40282. #endif /* !NO_SHA */
  40283. #ifdef WOLFSSL_SHA224
  40284. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  40285. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  40286. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  40287. "\x02\x0E", digest, digestSz), 0);
  40288. #endif /* WOLFSSL_SHA224 */
  40289. #ifndef NO_SHA256
  40290. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  40291. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  40292. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  40293. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  40294. #endif /* !NO_SHA256 */
  40295. #ifdef WOLFSSL_SHA384
  40296. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  40297. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  40298. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  40299. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  40300. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  40301. digest, digestSz), 0);
  40302. #endif /* WOLFSSL_SHA384 */
  40303. #ifdef WOLFSSL_SHA512
  40304. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  40305. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  40306. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  40307. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  40308. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  40309. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  40310. digest, digestSz), 0);
  40311. #endif /* WOLFSSL_SHA512 */
  40312. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  40313. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  40314. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  40315. "\xE4\x98\xDD", digest, digestSz), 0);
  40316. #endif /* !NO_MD5 */
  40317. res = TEST_RES_CHECK(1);
  40318. #endif
  40319. return res;
  40320. }
  40321. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40322. static void sslMsgCb(int w, int version, int type, const void* buf,
  40323. size_t sz, SSL* ssl, void* arg)
  40324. {
  40325. int i;
  40326. unsigned char* pt = (unsigned char*)buf;
  40327. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  40328. (w)?"Writing":"Reading", (int)sz, version, type);
  40329. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  40330. fprintf(stderr, "\n");
  40331. (void)ssl;
  40332. (void)arg;
  40333. }
  40334. #endif /* OPENSSL_EXTRA */
  40335. static int test_wolfSSL_msg_callback(void)
  40336. {
  40337. int res = TEST_SKIPPED;
  40338. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40339. WOLFSSL* ssl;
  40340. WOLFSSL_CTX* ctx;
  40341. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40342. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40343. SSL_FILETYPE_PEM));
  40344. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40345. SSL_FILETYPE_PEM));
  40346. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40347. SSL_SUCCESS);
  40348. AssertNotNull(ssl = SSL_new(ctx));
  40349. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  40350. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  40351. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  40352. SSL_free(ssl);
  40353. SSL_CTX_free(ctx);
  40354. res = TEST_RES_CHECK(1);
  40355. #endif
  40356. return res;
  40357. }
  40358. static int test_wolfSSL_SHA(void)
  40359. {
  40360. int res = TEST_SKIPPED;
  40361. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  40362. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  40363. (!defined(HAVE_FIPS) || \
  40364. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40365. {
  40366. const unsigned char in[] = "abc";
  40367. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  40368. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  40369. unsigned char out[WC_SHA_DIGEST_SIZE];
  40370. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  40371. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  40372. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  40373. /* SHA interface test */
  40374. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  40375. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  40376. AssertNotNull(SHA(in, 0, out));
  40377. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  40378. AssertNotNull(SHA(NULL, 0, out));
  40379. AssertNotNull(SHA(NULL, 0, NULL));
  40380. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  40381. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  40382. }
  40383. #endif
  40384. #if !defined(NO_SHA256)
  40385. {
  40386. const unsigned char in[] = "abc";
  40387. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  40388. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  40389. "\x15\xAD";
  40390. unsigned char out[WC_SHA256_DIGEST_SIZE];
  40391. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  40392. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40393. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  40394. #else
  40395. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  40396. #endif
  40397. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  40398. }
  40399. #endif
  40400. #if defined(WOLFSSL_SHA384)
  40401. {
  40402. const unsigned char in[] = "abc";
  40403. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  40404. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  40405. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  40406. "\xc8\x25\xa7";
  40407. unsigned char out[WC_SHA384_DIGEST_SIZE];
  40408. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  40409. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40410. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  40411. #else
  40412. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  40413. #endif
  40414. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  40415. }
  40416. #endif
  40417. #if defined(WOLFSSL_SHA512)
  40418. {
  40419. const unsigned char in[] = "abc";
  40420. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  40421. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  40422. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  40423. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  40424. "\xa5\x4c\xa4\x9f";
  40425. unsigned char out[WC_SHA512_DIGEST_SIZE];
  40426. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  40427. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  40428. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  40429. #else
  40430. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  40431. #endif
  40432. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  40433. }
  40434. #endif
  40435. res = TEST_RES_CHECK(1);
  40436. #endif
  40437. return res;
  40438. }
  40439. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  40440. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40441. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40442. static void binary_dump(void *ptr, int size)
  40443. {
  40444. #ifdef WOLFSSL_EVP_PRINT
  40445. int i = 0;
  40446. unsigned char *p = (unsigned char *) ptr;
  40447. fprintf(stderr, "{");
  40448. while ((p != NULL) && (i < size)) {
  40449. if ((i % 8) == 0) {
  40450. fprintf(stderr, "\n");
  40451. fprintf(stderr, " ");
  40452. }
  40453. fprintf(stderr, "0x%02x, ", p[i]);
  40454. i++;
  40455. }
  40456. fprintf(stderr, "\n};\n");
  40457. #else
  40458. (void) ptr;
  40459. (void) size;
  40460. #endif
  40461. }
  40462. static int last_val = 0x0f;
  40463. static int check_result(unsigned char *data, int len)
  40464. {
  40465. int i;
  40466. for ( ; len; ) {
  40467. last_val = (last_val + 1) % 16;
  40468. for (i = 0; i < 16; len--, i++, data++)
  40469. if (*data != last_val) {
  40470. return -1;
  40471. }
  40472. }
  40473. return 0;
  40474. }
  40475. static int r_offset;
  40476. static int w_offset;
  40477. static void init_offset(void)
  40478. {
  40479. r_offset = 0;
  40480. w_offset = 0;
  40481. }
  40482. static void get_record(unsigned char *data, unsigned char *buf, int len)
  40483. {
  40484. XMEMCPY(buf, data+r_offset, len);
  40485. r_offset += len;
  40486. }
  40487. static void set_record(unsigned char *data, unsigned char *buf, int len)
  40488. {
  40489. XMEMCPY(data+w_offset, buf, len);
  40490. w_offset += len;
  40491. }
  40492. static void set_plain(unsigned char *plain, int rec)
  40493. {
  40494. int i, j;
  40495. unsigned char *p = plain;
  40496. #define BLOCKSZ 16
  40497. for (i=0; i<(rec/BLOCKSZ); i++) {
  40498. for (j=0; j<BLOCKSZ; j++)
  40499. *p++ = (i % 16);
  40500. }
  40501. }
  40502. #endif
  40503. static int test_wolfSSL_EVP_Cipher_extra(void)
  40504. {
  40505. int res = TEST_SKIPPED;
  40506. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40507. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40508. /* aes128-cbc, keylen=16, ivlen=16 */
  40509. byte aes128_cbc_key[] = {
  40510. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40511. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40512. };
  40513. byte aes128_cbc_iv[] = {
  40514. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  40515. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  40516. };
  40517. /* teset data size table */
  40518. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  40519. int test_drive2[] = {8, 3, 8, 512, 0};
  40520. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  40521. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  40522. int test_drive_len[100];
  40523. int ret = 0;
  40524. EVP_CIPHER_CTX *evp = NULL;
  40525. int ilen = 0;
  40526. int klen = 0;
  40527. int i, j;
  40528. const EVP_CIPHER *type;
  40529. byte *iv;
  40530. byte *key;
  40531. int ivlen;
  40532. int keylen;
  40533. #define RECORDS 16
  40534. #define BUFFSZ 512
  40535. byte plain [BUFFSZ * RECORDS];
  40536. byte cipher[BUFFSZ * RECORDS];
  40537. byte inb[BUFFSZ];
  40538. byte outb[BUFFSZ+16];
  40539. int outl, inl;
  40540. iv = aes128_cbc_iv;
  40541. ivlen = sizeof(aes128_cbc_iv);
  40542. key = aes128_cbc_key;
  40543. keylen = sizeof(aes128_cbc_key);
  40544. type = EVP_aes_128_cbc();
  40545. set_plain(plain, BUFFSZ * RECORDS);
  40546. SSL_library_init();
  40547. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  40548. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40549. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40550. klen = EVP_CIPHER_CTX_key_length(evp);
  40551. if (klen > 0 && keylen != klen) {
  40552. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40553. }
  40554. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40555. if (ilen > 0 && ivlen != ilen) {
  40556. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40557. }
  40558. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40559. for (j = 0; j<RECORDS; j++)
  40560. {
  40561. inl = BUFFSZ;
  40562. get_record(plain, inb, inl);
  40563. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  40564. set_record(cipher, outb, outl);
  40565. }
  40566. for (i = 0; test_drive[i]; i++) {
  40567. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40568. init_offset();
  40569. test_drive_len[i] = 0;
  40570. for (j = 0; test_drive[i][j]; j++)
  40571. {
  40572. inl = test_drive[i][j];
  40573. test_drive_len[i] += inl;
  40574. get_record(plain, inb, inl);
  40575. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  40576. /* output to cipher buffer, so that following Dec test can detect
  40577. if any error */
  40578. set_record(cipher, outb, outl);
  40579. }
  40580. EVP_CipherFinal(evp, outb, &outl);
  40581. if (outl > 0)
  40582. set_record(cipher, outb, outl);
  40583. }
  40584. for (i = 0; test_drive[i]; i++) {
  40585. last_val = 0x0f;
  40586. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  40587. init_offset();
  40588. for (j = 0; test_drive[i][j]; j++) {
  40589. inl = test_drive[i][j];
  40590. get_record(cipher, inb, inl);
  40591. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  40592. binary_dump(outb, outl);
  40593. AssertIntEQ((ret = check_result(outb, outl)), 0);
  40594. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  40595. }
  40596. ret = EVP_CipherFinal(evp, outb, &outl);
  40597. binary_dump(outb, outl);
  40598. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  40599. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  40600. AssertTrue(ret);
  40601. }
  40602. EVP_CIPHER_CTX_free(evp);
  40603. /* Do an extra test to verify correct behavior with empty input. */
  40604. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  40605. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40606. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40607. klen = EVP_CIPHER_CTX_key_length(evp);
  40608. if (klen > 0 && keylen != klen) {
  40609. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40610. }
  40611. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40612. if (ilen > 0 && ivlen != ilen) {
  40613. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40614. }
  40615. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40616. /* outl should be set to 0 after passing NULL, 0 for input args. */
  40617. outl = -1;
  40618. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  40619. AssertIntEQ(outl, 0);
  40620. EVP_CIPHER_CTX_free(evp);
  40621. res = TEST_RES_CHECK(1);
  40622. #endif /* test_EVP_Cipher */
  40623. return res;
  40624. }
  40625. static int test_wolfSSL_PEM_read_DHparams(void)
  40626. {
  40627. int res = TEST_SKIPPED;
  40628. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  40629. !defined(NO_FILESYSTEM)
  40630. DH* dh;
  40631. XFILE fp;
  40632. unsigned char derOut[300];
  40633. unsigned char* derOutBuf = derOut;
  40634. int derOutSz = 0;
  40635. unsigned char derExpected[300];
  40636. int derExpectedSz = 0;
  40637. XMEMSET(derOut, 0, sizeof(derOut));
  40638. XMEMSET(derExpected, 0, sizeof(derExpected));
  40639. /* open DH param file, read into DH struct */
  40640. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  40641. /* bad args */
  40642. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  40643. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  40644. /* good args */
  40645. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40646. XFCLOSE(fp);
  40647. /* read in certs/dh2048.der for comparison against exported params */
  40648. fp = XFOPEN("./certs/dh2048.der", "rb");
  40649. AssertTrue(fp != XBADFILE);
  40650. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  40651. XFCLOSE(fp);
  40652. /* export DH back to DER and compare */
  40653. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  40654. AssertIntEQ(derOutSz, derExpectedSz);
  40655. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  40656. DH_free(dh);
  40657. dh = NULL;
  40658. /* Test parsing with X9.42 header */
  40659. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  40660. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  40661. XFCLOSE(fp);
  40662. DH_free(dh);
  40663. res = TEST_RES_CHECK(1);
  40664. #endif
  40665. return res;
  40666. }
  40667. static int test_wolfSSL_AES_ecb_encrypt(void)
  40668. {
  40669. int res = TEST_SKIPPED;
  40670. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  40671. AES_KEY aes;
  40672. const byte msg[] =
  40673. {
  40674. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40675. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  40676. };
  40677. const byte verify[] =
  40678. {
  40679. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  40680. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  40681. };
  40682. const byte key[] =
  40683. {
  40684. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  40685. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  40686. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  40687. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  40688. };
  40689. byte out[AES_BLOCK_SIZE];
  40690. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  40691. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40692. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  40693. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  40694. #ifdef HAVE_AES_DECRYPT
  40695. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  40696. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40697. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  40698. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  40699. #endif
  40700. /* test bad arguments */
  40701. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  40702. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  40703. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  40704. res = TEST_RES_CHECK(1);
  40705. #endif
  40706. return res;
  40707. }
  40708. static int test_wolfSSL_MD5(void)
  40709. {
  40710. int res = TEST_SKIPPED;
  40711. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  40712. byte input1[] = "";
  40713. byte input2[] = "message digest";
  40714. byte hash[WC_MD5_DIGEST_SIZE];
  40715. unsigned char output1[] =
  40716. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  40717. unsigned char output2[] =
  40718. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  40719. WOLFSSL_MD5_CTX md5;
  40720. XMEMSET(&md5, 0, sizeof(md5));
  40721. /* Test cases for illegal parameters */
  40722. AssertIntEQ(MD5_Init(NULL), 0);
  40723. AssertIntEQ(MD5_Init(&md5), 1);
  40724. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  40725. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  40726. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  40727. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  40728. AssertIntEQ(MD5_Final(hash, NULL), 0);
  40729. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  40730. /* Init MD5 CTX */
  40731. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  40732. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  40733. XSTRLEN((const char*)&input1)), 1);
  40734. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  40735. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  40736. /* Init MD5 CTX */
  40737. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  40738. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  40739. (int)XSTRLEN((const char*)input2)), 1);
  40740. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  40741. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  40742. #if !defined(NO_OLD_NAMES) && \
  40743. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  40744. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  40745. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  40746. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  40747. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  40748. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  40749. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  40750. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  40751. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  40752. {
  40753. byte data[] = "Data to be hashed.";
  40754. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  40755. AssertNotNull(MD5(data, sizeof(data), NULL));
  40756. AssertNotNull(MD5(data, sizeof(data), hash));
  40757. AssertNotNull(MD5(NULL, 0, hash));
  40758. AssertNull(MD5(NULL, sizeof(data), hash));
  40759. }
  40760. #endif
  40761. res = TEST_RES_CHECK(1);
  40762. #endif
  40763. return res;
  40764. }
  40765. static int test_wolfSSL_MD5_Transform(void)
  40766. {
  40767. int res = TEST_SKIPPED;
  40768. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  40769. byte input1[] = "";
  40770. byte input2[] = "abc";
  40771. byte local[WC_MD5_BLOCK_SIZE];
  40772. word32 sLen = 0;
  40773. #ifdef BIG_ENDIAN_ORDER
  40774. unsigned char output1[] =
  40775. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  40776. unsigned char output2[] =
  40777. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  40778. #else
  40779. unsigned char output1[] =
  40780. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  40781. unsigned char output2[] =
  40782. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  40783. #endif
  40784. union {
  40785. wc_Md5 native;
  40786. MD5_CTX compat;
  40787. } md5;
  40788. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  40789. XMEMSET(&local, 0, sizeof(local));
  40790. /* sanity check */
  40791. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  40792. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  40793. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  40794. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  40795. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40796. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  40797. /* Init MD5 CTX */
  40798. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  40799. /* Do Transform*/
  40800. sLen = (word32)XSTRLEN((char*)input1);
  40801. XMEMCPY(local, input1, sLen);
  40802. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  40803. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  40804. WC_MD5_DIGEST_SIZE), 0);
  40805. /* Init MD5 CTX */
  40806. AssertIntEQ(MD5_Init(&md5.compat), 1);
  40807. sLen = (word32)XSTRLEN((char*)input2);
  40808. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  40809. XMEMCPY(local, input2, sLen);
  40810. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  40811. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  40812. WC_MD5_DIGEST_SIZE), 0);
  40813. res = TEST_RES_CHECK(1);
  40814. #endif
  40815. return res;
  40816. }
  40817. static int test_wolfSSL_SHA224(void)
  40818. {
  40819. int res = TEST_SKIPPED;
  40820. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  40821. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40822. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40823. unsigned char input[] =
  40824. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40825. unsigned char output[] =
  40826. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  40827. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  40828. size_t inLen;
  40829. byte hash[WC_SHA224_DIGEST_SIZE];
  40830. inLen = XSTRLEN((char*)input);
  40831. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  40832. AssertNull(SHA224(NULL, inLen, hash));
  40833. AssertNotNull(SHA224(input, 0, hash));
  40834. AssertNotNull(SHA224(input, inLen, NULL));
  40835. AssertNotNull(SHA224(NULL, 0, hash));
  40836. AssertNotNull(SHA224(NULL, 0, NULL));
  40837. AssertNotNull(SHA224(input, inLen, hash));
  40838. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  40839. res = TEST_RES_CHECK(1);
  40840. #endif
  40841. return res;
  40842. }
  40843. static int test_wolfSSL_SHA_Transform(void)
  40844. {
  40845. int res = TEST_SKIPPED;
  40846. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  40847. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40848. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  40849. byte input1[] = "";
  40850. byte input2[] = "abc";
  40851. byte local[WC_SHA_BLOCK_SIZE];
  40852. word32 sLen = 0;
  40853. #ifdef BIG_ENDIAN_ORDER
  40854. unsigned char output1[] =
  40855. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  40856. "\x09\xf7\x3a\x93";
  40857. unsigned char output2[] =
  40858. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  40859. "\x7c\x6e\x08\xb8";
  40860. #else
  40861. unsigned char output1[] =
  40862. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  40863. "\x93\x3a\xf7\x09";
  40864. unsigned char output2[] =
  40865. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  40866. "\xb8\x08\x6e\x7c";
  40867. #endif
  40868. union {
  40869. wc_Sha native;
  40870. SHA_CTX compat;
  40871. } sha;
  40872. union {
  40873. wc_Sha native;
  40874. SHA_CTX compat;
  40875. } sha1;
  40876. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  40877. XMEMSET(&local, 0, sizeof(local));
  40878. /* sanity check */
  40879. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  40880. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  40881. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  40882. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  40883. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  40884. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  40885. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  40886. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40887. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  40888. /* Init SHA CTX */
  40889. AssertIntEQ(SHA_Init(&sha.compat), 1);
  40890. /* Do Transform*/
  40891. sLen = (word32)XSTRLEN((char*)input1);
  40892. XMEMCPY(local, input1, sLen);
  40893. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40894. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  40895. WC_SHA_DIGEST_SIZE), 0);
  40896. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40897. /* Init SHA CTX */
  40898. AssertIntEQ(SHA_Init(&sha.compat), 1);
  40899. sLen = (word32)XSTRLEN((char*)input2);
  40900. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40901. XMEMCPY(local, input2, sLen);
  40902. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  40903. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  40904. WC_SHA_DIGEST_SIZE), 0);
  40905. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  40906. /* SHA1 */
  40907. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40908. /* Init SHA CTX */
  40909. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  40910. /* Do Transform*/
  40911. sLen = (word32)XSTRLEN((char*)input1);
  40912. XMEMCPY(local, input1, sLen);
  40913. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40914. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  40915. WC_SHA_DIGEST_SIZE), 0);
  40916. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40917. /* Init SHA CTX */
  40918. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  40919. sLen = (word32)XSTRLEN((char*)input2);
  40920. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  40921. XMEMCPY(local, input2, sLen);
  40922. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  40923. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  40924. WC_SHA_DIGEST_SIZE), 0);
  40925. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40926. res = TEST_RES_CHECK(1);
  40927. #endif
  40928. #endif
  40929. return res;
  40930. }
  40931. static int test_wolfSSL_SHA256_Transform(void)
  40932. {
  40933. int res = TEST_SKIPPED;
  40934. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40935. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40936. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40937. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  40938. !defined(WOLFSSL_KCAPI_HASH)
  40939. byte input1[] = "";
  40940. byte input2[] = "abc";
  40941. byte local[WC_SHA256_BLOCK_SIZE];
  40942. word32 sLen = 0;
  40943. #ifdef BIG_ENDIAN_ORDER
  40944. unsigned char output1[] =
  40945. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  40946. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  40947. unsigned char output2[] =
  40948. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  40949. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  40950. #else
  40951. unsigned char output1[] =
  40952. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  40953. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  40954. unsigned char output2[] =
  40955. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  40956. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  40957. #endif
  40958. union {
  40959. wc_Sha256 native;
  40960. SHA256_CTX compat;
  40961. } sha256;
  40962. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  40963. XMEMSET(&local, 0, sizeof(local));
  40964. /* sanity check */
  40965. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  40966. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  40967. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  40968. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  40969. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40970. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  40971. /* Init SHA256 CTX */
  40972. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  40973. /* Do Transform*/
  40974. sLen = (word32)XSTRLEN((char*)input1);
  40975. XMEMCPY(local, input1, sLen);
  40976. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40977. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  40978. WC_SHA256_DIGEST_SIZE), 0);
  40979. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40980. /* Init SHA256 CTX */
  40981. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  40982. sLen = (word32)XSTRLEN((char*)input2);
  40983. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  40984. XMEMCPY(local, input2, sLen);
  40985. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40986. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  40987. WC_SHA256_DIGEST_SIZE), 0);
  40988. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40989. res = TEST_RES_CHECK(1);
  40990. #endif
  40991. #endif
  40992. return res;
  40993. }
  40994. static int test_wolfSSL_SHA256(void)
  40995. {
  40996. int res = TEST_SKIPPED;
  40997. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  40998. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40999. unsigned char input[] =
  41000. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  41001. unsigned char output[] =
  41002. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  41003. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  41004. "\x06\xC1";
  41005. size_t inLen;
  41006. byte hash[WC_SHA256_DIGEST_SIZE];
  41007. inLen = XSTRLEN((char*)input);
  41008. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  41009. AssertNotNull(SHA256(input, inLen, hash));
  41010. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  41011. res = TEST_RES_CHECK(1);
  41012. #endif
  41013. return res;
  41014. }
  41015. static int test_wolfSSL_SHA512_Transform(void)
  41016. {
  41017. int res = TEST_SKIPPED;
  41018. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  41019. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  41020. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  41021. !defined(WOLFSSL_KCAPI_HASH)
  41022. byte input1[] = "";
  41023. byte input2[] = "abc";
  41024. byte local[WC_SHA512_BLOCK_SIZE];
  41025. word32 sLen = 0;
  41026. #ifdef BIG_ENDIAN_ORDER
  41027. unsigned char output1[] =
  41028. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  41029. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  41030. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  41031. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  41032. unsigned char output2[] =
  41033. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  41034. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  41035. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  41036. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  41037. #else
  41038. unsigned char output1[] =
  41039. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  41040. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  41041. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  41042. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  41043. unsigned char output2[] =
  41044. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  41045. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  41046. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  41047. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  41048. #endif
  41049. union {
  41050. wc_Sha512 native;
  41051. SHA512_CTX compat;
  41052. } sha512;
  41053. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  41054. XMEMSET(&local, 0, sizeof(local));
  41055. /* sanity check */
  41056. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  41057. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  41058. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  41059. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  41060. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  41061. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  41062. /* Init SHA512 CTX */
  41063. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  41064. /* Do Transform*/
  41065. sLen = (word32)XSTRLEN((char*)input1);
  41066. XMEMCPY(local, input1, sLen);
  41067. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  41068. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  41069. WC_SHA512_DIGEST_SIZE), 0);
  41070. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  41071. /* Init SHA512 CTX */
  41072. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  41073. sLen = (word32)XSTRLEN((char*)input2);
  41074. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  41075. XMEMCPY(local, input2, sLen);
  41076. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  41077. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  41078. WC_SHA512_DIGEST_SIZE), 0);
  41079. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  41080. (void)input1;
  41081. res = TEST_RES_CHECK(1);
  41082. #endif
  41083. #endif
  41084. return res;
  41085. }
  41086. static int test_wolfSSL_X509_get_serialNumber(void)
  41087. {
  41088. int res = TEST_SKIPPED;
  41089. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  41090. ASN1_INTEGER* a;
  41091. BIGNUM* bn;
  41092. X509* x509;
  41093. char *serialHex;
  41094. byte serial[3];
  41095. int serialSz;
  41096. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41097. SSL_FILETYPE_PEM));
  41098. AssertNotNull(a = X509_get_serialNumber(x509));
  41099. /* check on value of ASN1 Integer */
  41100. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  41101. /* test setting serial number and then retrieving it */
  41102. AssertNotNull(a = ASN1_INTEGER_new());
  41103. ASN1_INTEGER_set(a, 3);
  41104. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41105. serialSz = sizeof(serial);
  41106. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41107. WOLFSSL_SUCCESS);
  41108. AssertIntEQ(serialSz, 1);
  41109. AssertIntEQ(serial[0], 3);
  41110. ASN1_INTEGER_free(a);
  41111. /* test setting serial number with 0's in it */
  41112. serial[0] = 0x01;
  41113. serial[1] = 0x00;
  41114. serial[2] = 0x02;
  41115. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  41116. a->data[0] = ASN_INTEGER;
  41117. a->data[1] = sizeof(serial);
  41118. XMEMCPY(&a->data[2], serial, sizeof(serial));
  41119. a->length = sizeof(serial) + 2;
  41120. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41121. XMEMSET(serial, 0, sizeof(serial));
  41122. serialSz = sizeof(serial);
  41123. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41124. WOLFSSL_SUCCESS);
  41125. AssertIntEQ(serialSz, 3);
  41126. AssertIntEQ(serial[0], 0x01);
  41127. AssertIntEQ(serial[1], 0x00);
  41128. AssertIntEQ(serial[2], 0x02);
  41129. ASN1_INTEGER_free(a);
  41130. X509_free(x509); /* free's a */
  41131. AssertNotNull(serialHex = BN_bn2hex(bn));
  41132. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  41133. AssertStrEQ(serialHex, "01");
  41134. #else
  41135. AssertStrEQ(serialHex, "1");
  41136. #endif
  41137. OPENSSL_free(serialHex);
  41138. AssertIntEQ(BN_get_word(bn), 1);
  41139. BN_free(bn);
  41140. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  41141. a = ASN1_INTEGER_new();
  41142. if (a) {
  41143. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  41144. DYNAMIC_TYPE_OPENSSL));
  41145. a->isDynamic = 1;
  41146. ASN1_INTEGER_free(a);
  41147. }
  41148. res = TEST_RES_CHECK(1);
  41149. #endif
  41150. return res;
  41151. }
  41152. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  41153. {
  41154. int res = TEST_SKIPPED;
  41155. #if defined(OPENSSL_EXTRA)
  41156. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  41157. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  41158. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  41159. res = TEST_RES_CHECK(1);
  41160. #endif
  41161. return res;
  41162. }
  41163. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  41164. {
  41165. int res = TEST_SKIPPED;
  41166. #if defined(OPENSSL_EXTRA)
  41167. #define MAX_HEXSTR_BUFSZ 9
  41168. #define NUM_CASES 5
  41169. struct Output {
  41170. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  41171. long ret;
  41172. };
  41173. int i;
  41174. int j;
  41175. const char* inputs[NUM_CASES] = {
  41176. "aabcd1357e",
  41177. "01:12:23:34:a5:b6:c7:d8:e9",
  41178. ":01:02",
  41179. "012",
  41180. ":ab:ac:d"
  41181. };
  41182. struct Output expectedOutputs[NUM_CASES] = {
  41183. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  41184. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  41185. {{0x01, 0x02}, 2},
  41186. {{0x00}, 0},
  41187. {{0x00}, 0}
  41188. };
  41189. long len = 0;
  41190. unsigned char* returnedBuf = NULL;
  41191. for (i = 0; i < NUM_CASES; ++i) {
  41192. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  41193. if (returnedBuf == NULL) {
  41194. AssertIntEQ(expectedOutputs[i].ret, 0);
  41195. continue;
  41196. }
  41197. AssertIntEQ(expectedOutputs[i].ret, len);
  41198. for (j = 0; j < len; ++j) {
  41199. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  41200. }
  41201. OPENSSL_free(returnedBuf);
  41202. }
  41203. res = TEST_RES_CHECK(1);
  41204. #endif
  41205. return res;
  41206. }
  41207. static int test_wolfSSL_X509_CA_num(void)
  41208. {
  41209. int res = TEST_SKIPPED;
  41210. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41211. defined(HAVE_ECC) && !defined(NO_RSA)
  41212. WOLFSSL_X509_STORE *store;
  41213. WOLFSSL_X509 *x509_1, *x509_2;
  41214. int ca_num = 0;
  41215. store = wolfSSL_X509_STORE_new();
  41216. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41217. wolfSSL_X509_STORE_add_cert(store, x509_1);
  41218. ca_num = wolfSSL_X509_CA_num(store);
  41219. AssertIntEQ(ca_num, 1);
  41220. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  41221. wolfSSL_X509_STORE_add_cert(store, x509_2);
  41222. ca_num = wolfSSL_X509_CA_num(store);
  41223. AssertIntEQ(ca_num, 2);
  41224. wolfSSL_X509_free(x509_1);
  41225. wolfSSL_X509_free(x509_2);
  41226. wolfSSL_X509_STORE_free(store);
  41227. res = TEST_RES_CHECK(1);
  41228. #endif
  41229. return res;
  41230. }
  41231. static int test_wolfSSL_X509_check_ca(void)
  41232. {
  41233. int res = TEST_SKIPPED;
  41234. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41235. WOLFSSL_X509 *x509;
  41236. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41237. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  41238. wolfSSL_X509_free(x509);
  41239. res = TEST_RES_CHECK(1);
  41240. #endif
  41241. return res;
  41242. }
  41243. static int test_wolfSSL_X509_check_ip_asc(void)
  41244. {
  41245. int res = TEST_SKIPPED;
  41246. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41247. WOLFSSL_X509 *x509;
  41248. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  41249. #if 0
  41250. /* TODO: add cert gen for testing positive case */
  41251. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  41252. #endif
  41253. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  41254. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  41255. wolfSSL_X509_free(x509);
  41256. res = TEST_RES_CHECK(1);
  41257. #endif
  41258. return res;
  41259. }
  41260. static int test_wolfSSL_make_cert(void)
  41261. {
  41262. int res = TEST_SKIPPED;
  41263. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  41264. defined(WOLFSSL_CERT_EXT)
  41265. int ret;
  41266. Cert cert;
  41267. CertName name;
  41268. RsaKey key;
  41269. WC_RNG rng;
  41270. byte der[FOURK_BUF];
  41271. word32 idx;
  41272. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  41273. #ifdef OPENSSL_EXTRA
  41274. const unsigned char* pt;
  41275. int certSz;
  41276. X509* x509;
  41277. X509_NAME* x509name;
  41278. X509_NAME_ENTRY* entry;
  41279. ASN1_STRING* entryValue;
  41280. #endif
  41281. XMEMSET(&name, 0, sizeof(CertName));
  41282. /* set up cert name */
  41283. XMEMCPY(name.country, "US", sizeof("US"));
  41284. name.countryEnc = CTC_PRINTABLE;
  41285. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  41286. name.stateEnc = CTC_UTF8;
  41287. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  41288. name.localityEnc = CTC_UTF8;
  41289. XMEMCPY(name.sur, "Test", sizeof("Test"));
  41290. name.surEnc = CTC_UTF8;
  41291. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  41292. name.orgEnc = CTC_UTF8;
  41293. XMEMCPY(name.unit, "Development", sizeof("Development"));
  41294. name.unitEnc = CTC_UTF8;
  41295. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  41296. name.commonNameEnc = CTC_UTF8;
  41297. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  41298. name.serialDevEnc = CTC_PRINTABLE;
  41299. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  41300. name.userIdEnc = CTC_PRINTABLE;
  41301. #ifdef WOLFSSL_MULTI_ATTRIB
  41302. #if CTC_MAX_ATTRIB > 2
  41303. {
  41304. NameAttrib* n;
  41305. n = &name.name[0];
  41306. n->id = ASN_DOMAIN_COMPONENT;
  41307. n->type = CTC_UTF8;
  41308. n->sz = sizeof("com");
  41309. XMEMCPY(n->value, "com", sizeof("com"));
  41310. n = &name.name[1];
  41311. n->id = ASN_DOMAIN_COMPONENT;
  41312. n->type = CTC_UTF8;
  41313. n->sz = sizeof("wolfssl");
  41314. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  41315. }
  41316. #endif
  41317. #endif /* WOLFSSL_MULTI_ATTRIB */
  41318. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  41319. #ifndef HAVE_FIPS
  41320. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  41321. #else
  41322. AssertIntEQ(wc_InitRng(&rng), 0);
  41323. #endif
  41324. /* load test RSA key */
  41325. idx = 0;
  41326. #if defined(USE_CERT_BUFFERS_1024)
  41327. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  41328. sizeof_server_key_der_1024), 0);
  41329. #elif defined(USE_CERT_BUFFERS_2048)
  41330. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  41331. sizeof_server_key_der_2048), 0);
  41332. #else
  41333. /* error case, no RSA key loaded, happens later */
  41334. (void)idx;
  41335. #endif
  41336. XMEMSET(&cert, 0 , sizeof(Cert));
  41337. AssertIntEQ(wc_InitCert(&cert), 0);
  41338. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  41339. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  41340. cert.serialSz = (int)sizeof(mySerial);
  41341. cert.isCA = 1;
  41342. #ifndef NO_SHA256
  41343. cert.sigType = CTC_SHA256wRSA;
  41344. #else
  41345. cert.sigType = CTC_SHAwRSA;
  41346. #endif
  41347. /* add SKID from the Public Key */
  41348. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41349. /* add AKID from the Public Key */
  41350. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41351. ret = 0;
  41352. do {
  41353. #if defined(WOLFSSL_ASYNC_CRYPT)
  41354. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  41355. #endif
  41356. if (ret >= 0) {
  41357. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  41358. }
  41359. } while (ret == WC_PENDING_E);
  41360. AssertIntGT(ret, 0);
  41361. #ifdef OPENSSL_EXTRA
  41362. /* der holds a certificate with DC's now check X509 parsing of it */
  41363. certSz = ret;
  41364. pt = der;
  41365. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  41366. AssertNotNull(x509name = X509_get_subject_name(x509));
  41367. #ifdef WOLFSSL_MULTI_ATTRIB
  41368. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41369. -1)), 5);
  41370. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41371. idx)), 6);
  41372. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41373. idx)), -1);
  41374. #endif /* WOLFSSL_MULTI_ATTRIB */
  41375. /* compare DN at index 0 */
  41376. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  41377. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41378. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  41379. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  41380. #ifndef WOLFSSL_MULTI_ATTRIB
  41381. /* compare Serial Number */
  41382. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  41383. -1)), 7);
  41384. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41385. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41386. AssertIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  41387. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  41388. #endif
  41389. #ifdef WOLFSSL_MULTI_ATTRIB
  41390. /* get first and second DC and compare result */
  41391. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41392. -1)), 5);
  41393. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41394. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41395. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  41396. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41397. idx)), 6);
  41398. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41399. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41400. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  41401. #endif /* WOLFSSL_MULTI_ATTRIB */
  41402. /* try invalid index locations for regression test and sanity check */
  41403. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  41404. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  41405. X509_free(x509);
  41406. #endif /* OPENSSL_EXTRA */
  41407. wc_FreeRsaKey(&key);
  41408. wc_FreeRng(&rng);
  41409. res = TEST_RES_CHECK(1);
  41410. #endif
  41411. return res;
  41412. }
  41413. static int test_wolfSSL_X509_get_version(void)
  41414. {
  41415. int res = TEST_SKIPPED;
  41416. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41417. WOLFSSL_X509 *x509;
  41418. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41419. AssertNotNull(x509);
  41420. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  41421. wolfSSL_X509_free(x509);
  41422. res = TEST_RES_CHECK(1);
  41423. #endif
  41424. return res;
  41425. }
  41426. static int test_wolfSSL_DES_ncbc(void)
  41427. {
  41428. int res = TEST_SKIPPED;
  41429. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  41430. const_DES_cblock myDes;
  41431. DES_cblock iv = {1};
  41432. DES_key_schedule key = {0};
  41433. unsigned char msg[] = "hello wolfssl";
  41434. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  41435. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  41436. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  41437. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  41438. /* partial block test */
  41439. DES_set_key(&key, &myDes);
  41440. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  41441. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  41442. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  41443. DES_set_key(&key, &myDes);
  41444. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41445. *((byte*)&iv) = 1;
  41446. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  41447. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  41448. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  41449. /* full block test */
  41450. DES_set_key(&key, &myDes);
  41451. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  41452. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41453. *((byte*)&iv) = 1;
  41454. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  41455. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  41456. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  41457. DES_set_key(&key, &myDes);
  41458. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  41459. *((byte*)&iv) = 1;
  41460. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  41461. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  41462. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  41463. res = TEST_RES_CHECK(1);
  41464. #endif
  41465. return res;
  41466. }
  41467. static int test_wolfSSL_AES_cbc_encrypt(void)
  41468. {
  41469. int res = TEST_SKIPPED;
  41470. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  41471. AES_KEY aes;
  41472. AES_KEY* aesN = NULL;
  41473. size_t len = 0;
  41474. size_t lenB = 0;
  41475. int keySz0 = 0;
  41476. int keySzN = -1;
  41477. byte out[AES_BLOCK_SIZE] = {0};
  41478. byte* outN = NULL;
  41479. /* Test vectors retrieved from:
  41480. * <begin URL>
  41481. * https://csrc.nist.gov/
  41482. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  41483. * documents/aes/KAT_AES.zip
  41484. * </end URL>
  41485. */
  41486. const byte* pt128N = NULL;
  41487. byte* key128N = NULL;
  41488. byte* iv128N = NULL;
  41489. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  41490. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41491. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41492. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  41493. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  41494. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41495. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41496. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  41497. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  41498. len = sizeof(pt128);
  41499. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  41500. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  41501. AssertIntNE(XMEMCMP(b, g, h), i)
  41502. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  41503. /* Stressing wolfSSL_AES_cbc_encrypt() */
  41504. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41505. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  41506. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  41507. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41508. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  41509. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41510. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41511. /* Stressing wolfSSL_AES_set_encrypt_key */
  41512. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41513. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  41514. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  41515. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  41516. /* Stressing wolfSSL_AES_set_decrypt_key */
  41517. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41518. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  41519. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  41520. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  41521. #ifdef WOLFSSL_AES_128
  41522. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  41523. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41524. RESET_IV(iv128tmp, iv128);
  41525. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41526. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  41527. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41528. wc_AesFree((Aes*)&aes);
  41529. #ifdef HAVE_AES_DECRYPT
  41530. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  41531. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41532. RESET_IV(iv128tmp, iv128);
  41533. len = sizeof(ct128);
  41534. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41535. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  41536. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  41537. wc_AesFree((Aes*)&aes);
  41538. #endif
  41539. #endif /* WOLFSSL_AES_128 */
  41540. #ifdef WOLFSSL_AES_192
  41541. {
  41542. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  41543. * Appendix F.2.3 */
  41544. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  41545. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41546. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41547. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  41548. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  41549. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41550. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41551. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  41552. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  41553. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  41554. len = sizeof(pt192);
  41555. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  41556. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41557. RESET_IV(iv192tmp, iv192);
  41558. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41559. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  41560. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  41561. wc_AesFree((Aes*)&aes);
  41562. #ifdef HAVE_AES_DECRYPT
  41563. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  41564. len = sizeof(ct192);
  41565. RESET_IV(iv192tmp, iv192);
  41566. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41567. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41568. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  41569. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  41570. wc_AesFree((Aes*)&aes);
  41571. #endif
  41572. }
  41573. #endif /* WOLFSSL_AES_192 */
  41574. #ifdef WOLFSSL_AES_256
  41575. {
  41576. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  41577. * Appendix F.2.5 */
  41578. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  41579. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41580. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41581. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  41582. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  41583. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41584. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41585. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41586. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41587. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41588. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  41589. len = sizeof(pt256);
  41590. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  41591. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41592. RESET_IV(iv256tmp, iv256);
  41593. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41594. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  41595. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  41596. wc_AesFree((Aes*)&aes);
  41597. #ifdef HAVE_AES_DECRYPT
  41598. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  41599. len = sizeof(ct256);
  41600. RESET_IV(iv256tmp, iv256);
  41601. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41602. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41603. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  41604. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  41605. wc_AesFree((Aes*)&aes);
  41606. #endif
  41607. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  41608. {
  41609. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  41610. byte wrapPlain[sizeof(key256)] = { 0 };
  41611. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  41612. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  41613. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41614. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41615. 15), WOLFSSL_FAILURE);
  41616. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41617. sizeof(key256)), sizeof(wrapCipher));
  41618. wc_AesFree((Aes*)&aes);
  41619. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  41620. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41621. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41622. 23), WOLFSSL_FAILURE);
  41623. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41624. sizeof(wrapCipher)), sizeof(wrapPlain));
  41625. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41626. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  41627. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  41628. wc_AesFree((Aes*)&aes);
  41629. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  41630. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41631. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  41632. sizeof(key256)), sizeof(wrapCipher));
  41633. wc_AesFree((Aes*)&aes);
  41634. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  41635. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41636. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  41637. sizeof(wrapCipher)), sizeof(wrapPlain));
  41638. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41639. wc_AesFree((Aes*)&aes);
  41640. }
  41641. #endif /* HAVE_AES_KEYWRAP */
  41642. }
  41643. #endif /* WOLFSSL_AES_256 */
  41644. res = TEST_RES_CHECK(1);
  41645. #endif
  41646. return res;
  41647. }
  41648. static int test_wolfSSL_CRYPTO_cts128(void)
  41649. {
  41650. int res = TEST_SKIPPED;
  41651. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  41652. && defined(HAVE_CTS)
  41653. byte tmp[64]; /* Largest vector size */
  41654. /* Test vectors taken form RFC3962 Appendix B */
  41655. const testVector vects[] = {
  41656. {
  41657. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41658. "\x20",
  41659. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  41660. "\x97",
  41661. 17, 17
  41662. },
  41663. {
  41664. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41665. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  41666. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  41667. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  41668. 31, 31
  41669. },
  41670. {
  41671. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41672. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  41673. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41674. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  41675. 32, 32
  41676. },
  41677. {
  41678. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41679. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41680. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  41681. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41682. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  41683. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  41684. 47, 47
  41685. },
  41686. {
  41687. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41688. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41689. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  41690. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41691. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  41692. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  41693. 48, 48
  41694. },
  41695. {
  41696. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41697. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41698. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  41699. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  41700. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41701. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41702. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  41703. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  41704. 64, 64
  41705. }
  41706. };
  41707. byte keyBytes[AES_128_KEY_SIZE] = {
  41708. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  41709. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  41710. };
  41711. size_t i;
  41712. XMEMSET(tmp, 0, sizeof(tmp));
  41713. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  41714. AES_KEY encKey;
  41715. AES_KEY decKey;
  41716. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  41717. XMEMSET(iv, 0, sizeof(iv));
  41718. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  41719. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  41720. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  41721. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  41722. vects[i].outLen);
  41723. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  41724. XMEMSET(iv, 0, sizeof(iv));
  41725. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  41726. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  41727. vects[i].inLen);
  41728. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  41729. }
  41730. res = TEST_RES_CHECK(1);
  41731. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  41732. return res;
  41733. }
  41734. #if defined(OPENSSL_ALL)
  41735. static int test_wolfSSL_sk_CIPHER_description(void)
  41736. {
  41737. int res = TEST_SKIPPED;
  41738. #if !defined(NO_RSA)
  41739. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41740. int i,j,k;
  41741. int numCiphers = 0;
  41742. const SSL_METHOD *method = NULL;
  41743. const SSL_CIPHER *cipher = NULL;
  41744. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41745. SSL_CTX *ctx = NULL;
  41746. SSL *ssl = NULL;
  41747. char buf[256];
  41748. char test_str[9] = "0000000";
  41749. const char badStr[] = "unknown";
  41750. const char certPath[] = "./certs/client-cert.pem";
  41751. XMEMSET(buf, 0, sizeof(buf));
  41752. AssertNotNull(method = TLSv1_2_client_method());
  41753. AssertNotNull(ctx = SSL_CTX_new(method));
  41754. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41755. SSL_CTX_set_verify_depth(ctx, 4);
  41756. SSL_CTX_set_options(ctx, flags);
  41757. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41758. WOLFSSL_SUCCESS);
  41759. AssertNotNull(ssl = SSL_new(ctx));
  41760. /* SSL_get_ciphers returns a stack of all configured ciphers
  41761. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41762. */
  41763. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41764. /* loop through the amount of supportedCiphers */
  41765. numCiphers = sk_num(supportedCiphers);
  41766. for (i = 0; i < numCiphers; ++i) {
  41767. /* sk_value increments "sk->data.cipher->cipherOffset".
  41768. * wolfSSL_sk_CIPHER_description sets the description for
  41769. * the cipher based on the provided offset.
  41770. */
  41771. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41772. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41773. }
  41774. /* Search cipher description string for "unknown" descriptor */
  41775. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41776. k = 0;
  41777. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41778. test_str[k] = badStr[k];
  41779. j++;
  41780. k++;
  41781. }
  41782. }
  41783. /* Fail if test_str == badStr == "unknown" */
  41784. AssertStrNE(test_str,badStr);
  41785. }
  41786. SSL_free(ssl);
  41787. SSL_CTX_free(ctx);
  41788. res = TEST_RES_CHECK(1);
  41789. #endif
  41790. return res;
  41791. }
  41792. static int test_wolfSSL_get_ciphers_compat(void)
  41793. {
  41794. int res = TEST_SKIPPED;
  41795. #if !defined(NO_RSA)
  41796. const SSL_METHOD *method = NULL;
  41797. const char certPath[] = "./certs/client-cert.pem";
  41798. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41799. SSL_CTX *ctx = NULL;
  41800. WOLFSSL *ssl = NULL;
  41801. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41802. method = SSLv23_client_method();
  41803. AssertNotNull(method);
  41804. ctx = SSL_CTX_new(method);
  41805. AssertNotNull(ctx);
  41806. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41807. SSL_CTX_set_verify_depth(ctx, 4);
  41808. SSL_CTX_set_options(ctx, flags);
  41809. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41810. WOLFSSL_SUCCESS);
  41811. AssertNotNull(ssl = SSL_new(ctx));
  41812. /* Test Bad NULL input */
  41813. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  41814. /* Test for Good input */
  41815. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41816. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41817. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41818. SSL_free(ssl);
  41819. SSL_CTX_free(ctx);
  41820. res = TEST_RES_CHECK(1);
  41821. #endif
  41822. return res;
  41823. }
  41824. static int test_wolfSSL_X509_PUBKEY_get(void)
  41825. {
  41826. WOLFSSL_X509_PUBKEY pubkey;
  41827. WOLFSSL_X509_PUBKEY* key;
  41828. WOLFSSL_EVP_PKEY evpkey ;
  41829. WOLFSSL_EVP_PKEY* evpPkey;
  41830. WOLFSSL_EVP_PKEY* retEvpPkey;
  41831. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41832. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41833. key = &pubkey;
  41834. evpPkey = &evpkey;
  41835. evpPkey->type = WOLFSSL_SUCCESS;
  41836. key->pkey = evpPkey;
  41837. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41838. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41839. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41840. key->pkey = NULL;
  41841. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41842. return TEST_RES_CHECK(retEvpPkey == NULL);
  41843. }
  41844. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41845. {
  41846. int res = TEST_SKIPPED;
  41847. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41848. DSA *dsa = NULL;
  41849. DSA *setDsa = NULL;
  41850. EVP_PKEY *pkey = NULL;
  41851. EVP_PKEY *set1Pkey = NULL;
  41852. SHA_CTX sha;
  41853. byte signature[DSA_SIG_SIZE];
  41854. byte hash[WC_SHA_DIGEST_SIZE];
  41855. word32 bytes;
  41856. int answer;
  41857. #ifdef USE_CERT_BUFFERS_1024
  41858. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41859. int dsaKeySz = sizeof_dsa_key_der_1024;
  41860. byte tmp[ONEK_BUF];
  41861. XMEMSET(tmp, 0, sizeof(tmp));
  41862. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41863. bytes = dsaKeySz;
  41864. #elif defined(USE_CERT_BUFFERS_2048)
  41865. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41866. int dsaKeySz = sizeof_dsa_key_der_2048;
  41867. byte tmp[TWOK_BUF];
  41868. XMEMSET(tmp, 0, sizeof(tmp));
  41869. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41870. bytes = dsaKeySz;
  41871. #else
  41872. byte tmp[TWOK_BUF];
  41873. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41874. int dsaKeySz;
  41875. XMEMSET(tmp, 0, sizeof(tmp));
  41876. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  41877. if (fp == XBADFILE) {
  41878. return WOLFSSL_BAD_FILE;
  41879. }
  41880. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  41881. XFCLOSE(fp);
  41882. #endif /* END USE_CERT_BUFFERS_1024 */
  41883. /* Create hash to later Sign and Verify */
  41884. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41885. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41886. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41887. /* Initialize pkey with der format dsa key */
  41888. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41889. (long)dsaKeySz));
  41890. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41891. /* Should Fail: NULL argument */
  41892. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41893. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41894. /* Should Pass: Initialized pkey argument */
  41895. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41896. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41897. #ifdef USE_CERT_BUFFERS_1024
  41898. AssertIntEQ(DSA_bits(dsa), 1024);
  41899. #else
  41900. AssertIntEQ(DSA_bits(dsa), 2048);
  41901. #endif
  41902. /* Sign */
  41903. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41904. /* Verify. */
  41905. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41906. WOLFSSL_SUCCESS);
  41907. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41908. /* Should Fail: set1Pkey not initialized */
  41909. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41910. /* Initialize set1Pkey */
  41911. set1Pkey = EVP_PKEY_new();
  41912. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41913. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41914. WOLFSSL_SUCCESS);
  41915. /* Should Pass: set dsa into set1Pkey */
  41916. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41917. DSA_free(dsa);
  41918. DSA_free(setDsa);
  41919. EVP_PKEY_free(pkey);
  41920. EVP_PKEY_free(set1Pkey);
  41921. res = TEST_RES_CHECK(1);
  41922. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41923. return res;
  41924. } /* END test_EVP_PKEY_set1_get1_DSA */
  41925. static int test_wolfSSL_DSA_SIG(void)
  41926. {
  41927. int res = TEST_SKIPPED;
  41928. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41929. !defined(HAVE_FIPS)
  41930. DSA *dsa = NULL;
  41931. DSA *dsa2 = NULL;
  41932. DSA_SIG *sig = NULL;
  41933. const BIGNUM *p = NULL;
  41934. const BIGNUM *q = NULL;
  41935. const BIGNUM *g = NULL;
  41936. const BIGNUM *pub = NULL;
  41937. const BIGNUM *priv = NULL;
  41938. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  41939. AssertNotNull(dsa = DSA_generate_parameters(2048,
  41940. NULL, 0, NULL, NULL, NULL, NULL));
  41941. DSA_free(dsa);
  41942. AssertNotNull(dsa = DSA_new());
  41943. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41944. NULL, 0, NULL, NULL, NULL), 1);
  41945. AssertIntEQ(DSA_generate_key(dsa), 1);
  41946. DSA_get0_pqg(dsa, &p, &q, &g);
  41947. DSA_get0_key(dsa, &pub, &priv);
  41948. AssertNotNull(p = BN_dup(p));
  41949. AssertNotNull(q = BN_dup(q));
  41950. AssertNotNull(g = BN_dup(g));
  41951. AssertNotNull(pub = BN_dup(pub));
  41952. AssertNotNull(priv = BN_dup(priv));
  41953. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  41954. AssertNotNull(dsa2 = DSA_new());
  41955. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  41956. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  41957. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  41958. DSA_free(dsa);
  41959. DSA_free(dsa2);
  41960. DSA_SIG_free(sig);
  41961. res = TEST_RES_CHECK(1);
  41962. #endif
  41963. return res;
  41964. }
  41965. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  41966. {
  41967. int res = TEST_SKIPPED;
  41968. #ifdef HAVE_ECC
  41969. WOLFSSL_EC_KEY *ecKey = NULL;
  41970. WOLFSSL_EC_KEY *ecGet1 = NULL;
  41971. EVP_PKEY *pkey = NULL;
  41972. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  41973. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41974. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  41975. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41976. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41977. /* Should fail since ecKey is empty */
  41978. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41979. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41980. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41981. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  41982. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  41983. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  41984. wolfSSL_EC_KEY_free(ecKey);
  41985. wolfSSL_EC_KEY_free(ecGet1);
  41986. EVP_PKEY_free(pkey);
  41987. res = TEST_RES_CHECK(1);
  41988. #endif /* HAVE_ECC */
  41989. return res;
  41990. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  41991. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  41992. {
  41993. int res = TEST_SKIPPED;
  41994. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41995. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41996. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41997. DH *dh = NULL;
  41998. DH *setDh = NULL;
  41999. EVP_PKEY *pkey = NULL;
  42000. FILE* f = NULL;
  42001. unsigned char buf[4096];
  42002. const unsigned char* pt = buf;
  42003. const char* dh2048 = "./certs/dh2048.der";
  42004. long len = 0;
  42005. int code = -1;
  42006. XMEMSET(buf, 0, sizeof(buf));
  42007. f = XFOPEN(dh2048, "rb");
  42008. AssertTrue(f != XBADFILE);
  42009. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  42010. XFCLOSE(f);
  42011. /* Load dh2048.der into DH with internal format */
  42012. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42013. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42014. AssertIntEQ(code, 0);
  42015. code = -1;
  42016. pkey = wolfSSL_EVP_PKEY_new();
  42017. /* Set DH into PKEY */
  42018. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42019. /* Get DH from PKEY */
  42020. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  42021. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  42022. AssertIntEQ(code, 0);
  42023. EVP_PKEY_free(pkey);
  42024. DH_free(setDh);
  42025. DH_free(dh);
  42026. res = TEST_RES_CHECK(1);
  42027. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  42028. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42029. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  42030. return res;
  42031. } /* END test_EVP_PKEY_set1_get1_DH */
  42032. static int test_wolfSSL_CTX_ctrl(void)
  42033. {
  42034. int res = TEST_SKIPPED;
  42035. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42036. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42037. char caFile[] = "./certs/client-ca.pem";
  42038. char clientFile[] = "./certs/client-cert.pem";
  42039. SSL_CTX* ctx;
  42040. X509* x509 = NULL;
  42041. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42042. byte buf[6000];
  42043. char file[] = "./certs/dsaparams.pem";
  42044. XFILE f;
  42045. int bytes;
  42046. BIO* bio;
  42047. DSA* dsa;
  42048. DH* dh;
  42049. #endif
  42050. #ifdef HAVE_ECC
  42051. WOLFSSL_EC_KEY* ecKey;
  42052. #endif
  42053. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42054. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  42055. AssertNotNull(x509);
  42056. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  42057. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  42058. AssertNotNull(x509);
  42059. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42060. /* Initialize DH */
  42061. f = XFOPEN(file, "rb");
  42062. AssertTrue((f != XBADFILE));
  42063. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  42064. XFCLOSE(f);
  42065. bio = BIO_new_mem_buf((void*)buf, bytes);
  42066. AssertNotNull(bio);
  42067. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  42068. AssertNotNull(dsa);
  42069. dh = wolfSSL_DSA_dup_DH(dsa);
  42070. AssertNotNull(dh);
  42071. #endif
  42072. #ifdef HAVE_ECC
  42073. /* Initialize WOLFSSL_EC_KEY */
  42074. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42075. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  42076. #endif
  42077. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  42078. /* additional test of getting EVP_PKEY key size from X509
  42079. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  42080. * allowed with user RSA */
  42081. {
  42082. EVP_PKEY* pkey;
  42083. #if defined(HAVE_ECC)
  42084. X509* ecX509;
  42085. #endif /* HAVE_ECC */
  42086. AssertNotNull(pkey = X509_get_pubkey(x509));
  42087. /* current RSA key is 2048 bit (256 bytes) */
  42088. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  42089. EVP_PKEY_free(pkey);
  42090. #if defined(HAVE_ECC)
  42091. #if defined(USE_CERT_BUFFERS_256)
  42092. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  42093. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  42094. SSL_FILETYPE_ASN1));
  42095. #else
  42096. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  42097. cliEccCertFile, SSL_FILETYPE_PEM));
  42098. #endif
  42099. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  42100. /* current ECC key is 256 bit (32 bytes) */
  42101. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  42102. X509_free(ecX509);
  42103. EVP_PKEY_free(pkey);
  42104. #endif /* HAVE_ECC */
  42105. }
  42106. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  42107. /* Tests should fail with passed in NULL pointer */
  42108. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  42109. SSL_FAILURE);
  42110. #if !defined(NO_DH) && !defined(NO_DSA)
  42111. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  42112. SSL_FAILURE);
  42113. #endif
  42114. #ifdef HAVE_ECC
  42115. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  42116. SSL_FAILURE);
  42117. #endif
  42118. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  42119. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  42120. */
  42121. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  42122. SSL_SUCCESS);
  42123. /* Test with SSL_CTRL_OPTIONS
  42124. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  42125. */
  42126. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  42127. == SSL_OP_NO_TLSv1);
  42128. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  42129. /* Test with SSL_CTRL_SET_TMP_DH
  42130. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  42131. */
  42132. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42133. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  42134. SSL_SUCCESS);
  42135. #endif
  42136. /* Test with SSL_CTRL_SET_TMP_ECDH
  42137. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  42138. */
  42139. #ifdef HAVE_ECC
  42140. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  42141. SSL_SUCCESS);
  42142. #endif
  42143. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42144. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  42145. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42146. #endif
  42147. /* Test for min/max proto */
  42148. #ifndef WOLFSSL_NO_TLS12
  42149. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42150. 0, NULL), SSL_SUCCESS);
  42151. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42152. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42153. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42154. #endif
  42155. #ifdef WOLFSSL_TLS13
  42156. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42157. 0, NULL), SSL_SUCCESS);
  42158. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42159. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  42160. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  42161. #ifndef WOLFSSL_NO_TLS12
  42162. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42163. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42164. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  42165. #endif
  42166. #endif
  42167. /* Cleanup and Pass */
  42168. #if !defined(NO_DH) && !defined(NO_DSA)
  42169. #ifndef NO_BIO
  42170. BIO_free(bio);
  42171. DSA_free(dsa);
  42172. DH_free(dh);
  42173. #endif
  42174. #endif
  42175. #ifdef HAVE_ECC
  42176. wolfSSL_EC_KEY_free(ecKey);
  42177. #endif
  42178. SSL_CTX_free(ctx);
  42179. res = TEST_RES_CHECK(1);
  42180. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42181. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  42182. return res;
  42183. }
  42184. static int test_wolfSSL_EVP_PKEY_assign(void)
  42185. {
  42186. int res = TEST_SKIPPED;
  42187. int type;
  42188. WOLFSSL_EVP_PKEY* pkey;
  42189. #ifndef NO_RSA
  42190. WOLFSSL_RSA* rsa;
  42191. #endif
  42192. #ifndef NO_DSA
  42193. WOLFSSL_DSA* dsa;
  42194. #endif
  42195. #ifdef HAVE_ECC
  42196. WOLFSSL_EC_KEY* ecKey;
  42197. #endif
  42198. (void)pkey;
  42199. #ifndef NO_RSA
  42200. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42201. type = EVP_PKEY_RSA;
  42202. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42203. AssertNotNull(rsa = wolfSSL_RSA_new());
  42204. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  42205. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42206. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  42207. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  42208. wolfSSL_EVP_PKEY_free(pkey);
  42209. res = TEST_RES_CHECK(1);
  42210. }
  42211. #endif /* NO_RSA */
  42212. #ifndef NO_DSA
  42213. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42214. type = EVP_PKEY_DSA;
  42215. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42216. AssertNotNull(dsa = wolfSSL_DSA_new());
  42217. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  42218. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42219. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  42220. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  42221. wolfSSL_EVP_PKEY_free(pkey);
  42222. res = TEST_RES_CHECK(1);
  42223. }
  42224. #endif /* NO_DSA */
  42225. #ifdef HAVE_ECC
  42226. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42227. type = EVP_PKEY_EC;
  42228. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42229. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  42230. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  42231. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  42232. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  42233. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  42234. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42235. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  42236. wolfSSL_EVP_PKEY_free(pkey);
  42237. res = TEST_RES_CHECK(1);
  42238. }
  42239. #endif /* HAVE_ECC */
  42240. (void)type;
  42241. return res;
  42242. }
  42243. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42244. {
  42245. WOLFSSL_EVP_PKEY* pkey;
  42246. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42247. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42248. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42249. EVP_PKEY_free(pkey);
  42250. return TEST_RES_CHECK(1);
  42251. }
  42252. static int test_wolfSSL_EVP_PKEY_id(void)
  42253. {
  42254. WOLFSSL_EVP_PKEY* pkey;
  42255. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42256. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42257. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42258. EVP_PKEY_free(pkey);
  42259. return TEST_RES_CHECK(1);
  42260. }
  42261. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42262. {
  42263. int res = TEST_SKIPPED;
  42264. #if defined(OPENSSL_ALL) && \
  42265. !defined(NO_ECC_SECP) && \
  42266. /* This last bit is taken from ecc.c. It is the condition that
  42267. * defines ECC256 */ \
  42268. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42269. ECC_MIN_KEY_SZ <= 256)
  42270. EVP_PKEY_CTX* ctx;
  42271. EVP_PKEY* pkey = NULL;
  42272. /* Test error conditions. */
  42273. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42274. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42275. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42276. #ifndef NO_RSA
  42277. EVP_PKEY_CTX_free(ctx);
  42278. /* Parameter generation for RSA not supported yet. */
  42279. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42280. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42281. #endif
  42282. #ifdef HAVE_ECC
  42283. EVP_PKEY_CTX_free(ctx);
  42284. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42285. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42286. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42287. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42288. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42289. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42290. WOLFSSL_SUCCESS);
  42291. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42292. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42293. #endif
  42294. EVP_PKEY_CTX_free(ctx);
  42295. EVP_PKEY_free(pkey);
  42296. res = TEST_RES_CHECK(1);
  42297. #endif
  42298. return res;
  42299. }
  42300. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42301. {
  42302. WOLFSSL_EVP_PKEY* pkey = NULL;
  42303. EVP_PKEY_CTX* ctx = NULL;
  42304. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42305. WOLFSSL_EVP_PKEY* params = NULL;
  42306. DH* dh = NULL;
  42307. const BIGNUM* pubkey = NULL;
  42308. const BIGNUM* privkey = NULL;
  42309. ASN1_INTEGER* asn1int = NULL;
  42310. unsigned int length = 0;
  42311. byte* derBuffer = NULL;
  42312. #endif
  42313. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42314. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42315. /* Bad cases */
  42316. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42317. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42318. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42319. /* Good case */
  42320. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42321. EVP_PKEY_CTX_free(ctx);
  42322. EVP_PKEY_free(pkey);
  42323. pkey = NULL;
  42324. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42325. /* Test DH keygen */
  42326. {
  42327. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  42328. AssertNotNull(dh = DH_get_2048_256());
  42329. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42330. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42331. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42332. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42333. DH_free(dh);
  42334. EVP_PKEY_CTX_free(ctx);
  42335. EVP_PKEY_free(params);
  42336. /* try exporting generated key to DER, to verify */
  42337. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  42338. DH_get0_key(dh, &pubkey, &privkey);
  42339. AssertNotNull(pubkey);
  42340. AssertNotNull(privkey);
  42341. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  42342. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  42343. ASN1_INTEGER_free(asn1int);
  42344. DH_free(dh);
  42345. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42346. EVP_PKEY_free(pkey);
  42347. }
  42348. #endif
  42349. return TEST_RES_CHECK(1);
  42350. }
  42351. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  42352. {
  42353. WOLFSSL_EVP_PKEY* pkey;
  42354. EVP_PKEY_CTX *ctx;
  42355. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42356. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42357. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42358. EVP_PKEY_CTX_free(ctx);
  42359. EVP_PKEY_free(pkey);
  42360. return TEST_RES_CHECK(1);
  42361. }
  42362. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  42363. {
  42364. int res = TEST_SKIPPED;
  42365. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  42366. WOLFSSL_EVP_PKEY* pkey;
  42367. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42368. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  42369. EVP_PKEY_free(pkey);
  42370. res = TEST_RES_CHECK(1);
  42371. #endif
  42372. return res;
  42373. }
  42374. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  42375. {
  42376. int res = TEST_SKIPPED;
  42377. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  42378. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  42379. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  42380. !defined(NO_FILESYSTEM)
  42381. WOLFSSL_EVP_PKEY* params = NULL;
  42382. WOLFSSL_EVP_PKEY* copy = NULL;
  42383. DH* dh = NULL;
  42384. BIGNUM* p1;
  42385. BIGNUM* g1;
  42386. BIGNUM* q1;
  42387. BIGNUM* p2;
  42388. BIGNUM* g2;
  42389. BIGNUM* q2;
  42390. /* create DH with DH_get_2048_256 params */
  42391. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  42392. AssertNotNull(dh = DH_get_2048_256());
  42393. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42394. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  42395. (const BIGNUM**)&q1,
  42396. (const BIGNUM**)&g1);
  42397. DH_free(dh);
  42398. /* create DH with random generated DH params */
  42399. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  42400. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  42401. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  42402. DH_free(dh);
  42403. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  42404. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  42405. AssertNotNull(dh->p);
  42406. AssertNotNull(dh->g);
  42407. AssertNotNull(dh->q);
  42408. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  42409. (const BIGNUM**)&q2,
  42410. (const BIGNUM**)&g2);
  42411. AssertIntEQ(BN_cmp(p1, p2), 0);
  42412. AssertIntEQ(BN_cmp(q1, q2), 0);
  42413. AssertIntEQ(BN_cmp(g1, g2), 0);
  42414. DH_free(dh);
  42415. EVP_PKEY_free(copy);
  42416. EVP_PKEY_free(params);
  42417. res = TEST_RES_CHECK(1);
  42418. #endif
  42419. return res;
  42420. }
  42421. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  42422. {
  42423. WOLFSSL_EVP_PKEY* pkey;
  42424. EVP_PKEY_CTX *ctx;
  42425. int bits = 2048;
  42426. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42427. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42428. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  42429. WOLFSSL_SUCCESS);
  42430. EVP_PKEY_CTX_free(ctx);
  42431. EVP_PKEY_free(pkey);
  42432. return TEST_RES_CHECK(1);
  42433. }
  42434. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  42435. {
  42436. /* This is large enough to be used for all key sizes */
  42437. byte key[AES_256_KEY_SIZE] = {0};
  42438. byte iv[AES_BLOCK_SIZE] = {0};
  42439. int i, enumlen;
  42440. EVP_CIPHER_CTX *ctx;
  42441. const EVP_CIPHER *init;
  42442. int enumArray[] = {
  42443. #ifdef HAVE_AES_CBC
  42444. NID_aes_128_cbc,
  42445. #endif
  42446. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42447. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42448. #ifdef HAVE_AESGCM
  42449. NID_aes_128_gcm,
  42450. #endif
  42451. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42452. #ifdef WOLFSSL_AES_COUNTER
  42453. NID_aes_128_ctr,
  42454. #endif
  42455. #ifndef NO_DES3
  42456. NID_des_cbc,
  42457. NID_des_ede3_cbc,
  42458. #endif
  42459. };
  42460. int iv_lengths[] = {
  42461. #ifdef HAVE_AES_CBC
  42462. AES_BLOCK_SIZE,
  42463. #endif
  42464. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42465. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42466. #ifdef HAVE_AESGCM
  42467. GCM_NONCE_MID_SZ,
  42468. #endif
  42469. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42470. #ifdef WOLFSSL_AES_COUNTER
  42471. AES_BLOCK_SIZE,
  42472. #endif
  42473. #ifndef NO_DES3
  42474. DES_BLOCK_SIZE,
  42475. DES_BLOCK_SIZE,
  42476. #endif
  42477. };
  42478. enumlen = (sizeof(enumArray)/sizeof(int));
  42479. for (i = 0; i < enumlen; i++) {
  42480. ctx = EVP_CIPHER_CTX_new();
  42481. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  42482. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42483. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42484. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  42485. EVP_CIPHER_CTX_free(ctx);
  42486. }
  42487. return TEST_RES_CHECK(1);
  42488. }
  42489. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  42490. {
  42491. int res = TEST_SKIPPED;
  42492. #if !defined(NO_DES3)
  42493. byte key[AES_256_KEY_SIZE] = {0};
  42494. byte iv[AES_BLOCK_SIZE] = {0};
  42495. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42496. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42497. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42498. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42499. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  42500. EVP_CIPHER_CTX_free(ctx);
  42501. res = TEST_RES_CHECK(1);
  42502. #endif
  42503. return res;
  42504. }
  42505. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  42506. {
  42507. int res = TEST_SKIPPED;
  42508. #if !defined(NO_DES3)
  42509. byte key[AES_256_KEY_SIZE] = {0};
  42510. byte iv[AES_BLOCK_SIZE] = {0};
  42511. int keylen;
  42512. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42513. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42514. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42515. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42516. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42517. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  42518. WOLFSSL_SUCCESS);
  42519. EVP_CIPHER_CTX_free(ctx);
  42520. res = TEST_RES_CHECK(1);
  42521. #endif
  42522. return res;
  42523. }
  42524. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  42525. {
  42526. int res = TEST_SKIPPED;
  42527. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  42528. byte key[DES3_KEY_SIZE] = {0};
  42529. byte iv[DES_BLOCK_SIZE] = {0};
  42530. int ivLen, keyLen;
  42531. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42532. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42533. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42534. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42535. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  42536. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42537. /* Bad cases */
  42538. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  42539. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  42540. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  42541. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  42542. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  42543. /* Good case */
  42544. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  42545. EVP_CIPHER_CTX_free(ctx);
  42546. res = TEST_RES_CHECK(1);
  42547. #endif
  42548. return res;
  42549. }
  42550. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  42551. {
  42552. WOLFSSL_ENGINE* e = NULL;
  42553. int id = 0;
  42554. EVP_PKEY_CTX *ctx;
  42555. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  42556. EVP_PKEY_CTX_free(ctx);
  42557. return TEST_RES_CHECK(1);
  42558. }
  42559. static int test_wolfSSL_EVP_rc4(void)
  42560. {
  42561. int res = TEST_SKIPPED;
  42562. #if !defined(NO_RC4)
  42563. res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
  42564. #endif
  42565. return res;
  42566. }
  42567. static int test_wolfSSL_EVP_enc_null(void)
  42568. {
  42569. return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
  42570. }
  42571. static int test_wolfSSL_EVP_rc2_cbc(void)
  42572. {
  42573. int res = TEST_SKIPPED;
  42574. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  42575. res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
  42576. #endif
  42577. return res;
  42578. }
  42579. static int test_wolfSSL_EVP_mdc2(void)
  42580. {
  42581. int res = TEST_SKIPPED;
  42582. #if !defined(NO_WOLFSSL_STUB)
  42583. res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
  42584. #endif
  42585. return res;
  42586. }
  42587. static int test_wolfSSL_EVP_md4(void)
  42588. {
  42589. int res = TEST_SKIPPED;
  42590. #if !defined(NO_MD4)
  42591. res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
  42592. #endif
  42593. return res;
  42594. }
  42595. static int test_wolfSSL_EVP_aes_256_gcm(void)
  42596. {
  42597. int res = TEST_SKIPPED;
  42598. #ifdef HAVE_AESGCM
  42599. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
  42600. #endif
  42601. return res;
  42602. }
  42603. static int test_wolfSSL_EVP_aes_192_gcm(void)
  42604. {
  42605. int res = TEST_SKIPPED;
  42606. #ifdef HAVE_AESGCM
  42607. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
  42608. #endif
  42609. return res;
  42610. }
  42611. static int test_wolfSSL_EVP_aes_256_ccm(void)
  42612. {
  42613. int res = TEST_SKIPPED;
  42614. #ifdef HAVE_AESCCM
  42615. res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_ccm() != NULL);
  42616. #endif
  42617. return res;
  42618. }
  42619. static int test_wolfSSL_EVP_aes_192_ccm(void)
  42620. {
  42621. int res = TEST_SKIPPED;
  42622. #ifdef HAVE_AESCCM
  42623. res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_ccm() != NULL);
  42624. #endif
  42625. return res;
  42626. }
  42627. static int test_wolfSSL_EVP_aes_128_ccm(void)
  42628. {
  42629. int res = TEST_SKIPPED;
  42630. #ifdef HAVE_AESCCM
  42631. res = TEST_RES_CHECK(wolfSSL_EVP_aes_128_ccm() != NULL);
  42632. #endif
  42633. return res;
  42634. }
  42635. static int test_wolfSSL_EVP_ripemd160(void)
  42636. {
  42637. int res = TEST_SKIPPED;
  42638. #if !defined(NO_WOLFSSL_STUB)
  42639. res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
  42640. #endif
  42641. return res;
  42642. }
  42643. static int test_wolfSSL_EVP_get_digestbynid(void)
  42644. {
  42645. #ifndef NO_MD5
  42646. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42647. #endif
  42648. #ifndef NO_SHA
  42649. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42650. #endif
  42651. #ifndef NO_SHA256
  42652. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42653. #endif
  42654. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  42655. return TEST_RES_CHECK(1);
  42656. }
  42657. static int test_wolfSSL_EVP_MD_nid(void)
  42658. {
  42659. #ifndef NO_MD5
  42660. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42661. #endif
  42662. #ifndef NO_SHA
  42663. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42664. #endif
  42665. #ifndef NO_SHA256
  42666. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42667. #endif
  42668. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  42669. return TEST_RES_CHECK(1);
  42670. }
  42671. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42672. {
  42673. int res = TEST_SKIPPED;
  42674. #if defined(HAVE_ECC)
  42675. WOLFSSL_EVP_PKEY* pkey;
  42676. AssertNotNull(pkey = EVP_PKEY_new());
  42677. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  42678. EVP_PKEY_free(pkey);
  42679. res = TEST_RES_CHECK(1);
  42680. #endif
  42681. return res;
  42682. }
  42683. static int test_wolfSSL_EVP_X_STATE(void)
  42684. {
  42685. int res = TEST_SKIPPED;
  42686. #if !defined(NO_DES3) && !defined(NO_RC4)
  42687. byte key[DES3_KEY_SIZE] = {0};
  42688. byte iv[DES_IV_SIZE] = {0};
  42689. EVP_CIPHER_CTX *ctx;
  42690. const EVP_CIPHER *init;
  42691. /* Bad test cases */
  42692. ctx = EVP_CIPHER_CTX_new();
  42693. init = EVP_des_ede3_cbc();
  42694. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42695. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42696. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  42697. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  42698. EVP_CIPHER_CTX_free(ctx);
  42699. /* Good test case */
  42700. ctx = EVP_CIPHER_CTX_new();
  42701. init = wolfSSL_EVP_rc4();
  42702. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42703. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42704. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  42705. EVP_CIPHER_CTX_free(ctx);
  42706. res = TEST_RES_CHECK(1);
  42707. #endif
  42708. return res;
  42709. }
  42710. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42711. {
  42712. int res = TEST_SKIPPED;
  42713. #if !defined(NO_DES3) && !defined(NO_RC4)
  42714. byte key[DES3_KEY_SIZE] = {0};
  42715. byte iv[DES_IV_SIZE] = {0};
  42716. EVP_CIPHER_CTX *ctx;
  42717. const EVP_CIPHER *init;
  42718. /* Bad test cases */
  42719. ctx = EVP_CIPHER_CTX_new();
  42720. init = EVP_des_ede3_cbc();
  42721. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42722. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42723. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42724. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42725. EVP_CIPHER_CTX_free(ctx);
  42726. /* Good test case */
  42727. ctx = EVP_CIPHER_CTX_new();
  42728. init = wolfSSL_EVP_rc4();
  42729. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42730. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42731. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42732. EVP_CIPHER_CTX_free(ctx);
  42733. res = TEST_RES_CHECK(1);
  42734. #endif
  42735. return res;
  42736. }
  42737. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42738. {
  42739. int res = TEST_SKIPPED;
  42740. #ifdef HAVE_AES_CBC
  42741. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42742. #ifdef WOLFSSL_AES_128
  42743. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42744. #endif
  42745. #ifdef WOLFSSL_AES_192
  42746. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42747. #endif
  42748. #ifdef WOLFSSL_AES_256
  42749. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42750. #endif
  42751. res = TEST_RES_CHECK(1);
  42752. }
  42753. #endif
  42754. #ifdef HAVE_AESGCM
  42755. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42756. #ifdef WOLFSSL_AES_128
  42757. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42758. #endif
  42759. #ifdef WOLFSSL_AES_192
  42760. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42761. #endif
  42762. #ifdef WOLFSSL_AES_256
  42763. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42764. #endif
  42765. res = TEST_RES_CHECK(1);
  42766. }
  42767. #endif
  42768. #ifdef HAVE_AESCCM
  42769. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42770. #ifdef WOLFSSL_AES_128
  42771. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42772. #endif
  42773. #ifdef WOLFSSL_AES_192
  42774. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42775. #endif
  42776. #ifdef WOLFSSL_AES_256
  42777. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42778. #endif
  42779. res = TEST_RES_CHECK(1);
  42780. }
  42781. #endif
  42782. #ifdef WOLFSSL_AES_COUNTER
  42783. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42784. #ifdef WOLFSSL_AES_128
  42785. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42786. #endif
  42787. #ifdef WOLFSSL_AES_192
  42788. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42789. #endif
  42790. #ifdef WOLFSSL_AES_256
  42791. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42792. #endif
  42793. res = TEST_RES_CHECK(1);
  42794. }
  42795. #endif
  42796. #ifdef HAVE_AES_ECB
  42797. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42798. #ifdef WOLFSSL_AES_128
  42799. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42800. #endif
  42801. #ifdef WOLFSSL_AES_192
  42802. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42803. #endif
  42804. #ifdef WOLFSSL_AES_256
  42805. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42806. #endif
  42807. res = TEST_RES_CHECK(1);
  42808. }
  42809. #endif
  42810. #ifdef WOLFSSL_AES_OFB
  42811. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42812. #ifdef WOLFSSL_AES_128
  42813. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42814. #endif
  42815. #ifdef WOLFSSL_AES_192
  42816. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42817. #endif
  42818. #ifdef WOLFSSL_AES_256
  42819. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42820. #endif
  42821. res = TEST_RES_CHECK(1);
  42822. }
  42823. #endif
  42824. #ifndef NO_RC4
  42825. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42826. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42827. res = TEST_RES_CHECK(1);
  42828. }
  42829. #endif
  42830. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42831. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  42832. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42833. res = TEST_RES_CHECK(1);
  42834. }
  42835. #endif
  42836. return res;
  42837. }
  42838. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42839. {
  42840. int i, enumlen;
  42841. int enumArray[] = {
  42842. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42843. #ifdef WOLFSSL_AES_128
  42844. NID_aes_128_cbc,
  42845. #endif
  42846. #ifdef WOLFSSL_AES_192
  42847. NID_aes_192_cbc,
  42848. #endif
  42849. #ifdef WOLFSSL_AES_256
  42850. NID_aes_256_cbc,
  42851. #endif
  42852. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42853. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42854. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42855. #ifdef HAVE_AESGCM
  42856. #ifdef WOLFSSL_AES_128
  42857. NID_aes_128_gcm,
  42858. #endif
  42859. #ifdef WOLFSSL_AES_192
  42860. NID_aes_192_gcm,
  42861. #endif
  42862. #ifdef WOLFSSL_AES_256
  42863. NID_aes_256_gcm,
  42864. #endif
  42865. #endif /* HAVE_AESGCM */
  42866. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42867. #ifdef WOLFSSL_AES_COUNTER
  42868. #ifdef WOLFSSL_AES_128
  42869. NID_aes_128_ctr,
  42870. #endif
  42871. #ifdef WOLFSSL_AES_192
  42872. NID_aes_192_ctr,
  42873. #endif
  42874. #ifdef WOLFSSL_AES_256
  42875. NID_aes_256_ctr,
  42876. #endif
  42877. #endif
  42878. #ifndef NO_DES3
  42879. NID_des_cbc,
  42880. NID_des_ede3_cbc,
  42881. #endif
  42882. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42883. NID_chacha20_poly1305,
  42884. #endif
  42885. };
  42886. int iv_lengths[] = {
  42887. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42888. #ifdef WOLFSSL_AES_128
  42889. AES_BLOCK_SIZE,
  42890. #endif
  42891. #ifdef WOLFSSL_AES_192
  42892. AES_BLOCK_SIZE,
  42893. #endif
  42894. #ifdef WOLFSSL_AES_256
  42895. AES_BLOCK_SIZE,
  42896. #endif
  42897. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42898. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42899. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42900. #ifdef HAVE_AESGCM
  42901. #ifdef WOLFSSL_AES_128
  42902. GCM_NONCE_MID_SZ,
  42903. #endif
  42904. #ifdef WOLFSSL_AES_192
  42905. GCM_NONCE_MID_SZ,
  42906. #endif
  42907. #ifdef WOLFSSL_AES_256
  42908. GCM_NONCE_MID_SZ,
  42909. #endif
  42910. #endif /* HAVE_AESGCM */
  42911. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42912. #ifdef WOLFSSL_AES_COUNTER
  42913. #ifdef WOLFSSL_AES_128
  42914. AES_BLOCK_SIZE,
  42915. #endif
  42916. #ifdef WOLFSSL_AES_192
  42917. AES_BLOCK_SIZE,
  42918. #endif
  42919. #ifdef WOLFSSL_AES_256
  42920. AES_BLOCK_SIZE,
  42921. #endif
  42922. #endif
  42923. #ifndef NO_DES3
  42924. DES_BLOCK_SIZE,
  42925. DES_BLOCK_SIZE,
  42926. #endif
  42927. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42928. CHACHA20_POLY1305_AEAD_IV_SIZE,
  42929. #endif
  42930. };
  42931. enumlen = (sizeof(enumArray)/sizeof(int));
  42932. for (i = 0; i < enumlen; i++) {
  42933. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  42934. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  42935. }
  42936. return TEST_RES_CHECK(1);
  42937. }
  42938. static int test_wolfSSL_EVP_SignInit_ex(void)
  42939. {
  42940. WOLFSSL_EVP_MD_CTX mdCtx;
  42941. WOLFSSL_ENGINE* e = 0;
  42942. const EVP_MD* md;
  42943. md = "SHA256";
  42944. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42945. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  42946. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42947. return TEST_RES_CHECK(1);
  42948. }
  42949. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  42950. {
  42951. int res = TEST_SKIPPED;
  42952. #if !defined(NO_SHA256)
  42953. WOLFSSL_EVP_MD_CTX mdCtx;
  42954. unsigned int s = 0;
  42955. unsigned char md[WC_SHA256_DIGEST_SIZE];
  42956. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  42957. /* Bad Case */
  42958. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42959. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42960. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  42961. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42962. #else
  42963. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42964. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  42965. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42966. #endif
  42967. /* Good Case */
  42968. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42969. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  42970. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  42971. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42972. res = TEST_RES_CHECK(1);
  42973. #endif
  42974. return res;
  42975. }
  42976. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  42977. {
  42978. int res = TEST_SKIPPED;
  42979. #if !defined(NO_DH) && \
  42980. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42981. FILE* f = NULL;
  42982. unsigned char buf[4096];
  42983. const unsigned char* pt = buf;
  42984. const char* params1 = "./certs/dh2048.der";
  42985. long len = 0;
  42986. WOLFSSL_DH* dh = NULL;
  42987. WOLFSSL_EVP_PKEY* pkey;
  42988. XMEMSET(buf, 0, sizeof(buf));
  42989. f = XFOPEN(params1, "rb");
  42990. AssertTrue(f != XBADFILE);
  42991. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  42992. XFCLOSE(f);
  42993. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42994. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  42995. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42996. /* Bad cases */
  42997. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  42998. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  42999. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43000. /* Good case */
  43001. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43002. EVP_PKEY_free(pkey);
  43003. res = TEST_RES_CHECK(1);
  43004. #endif
  43005. return res;
  43006. }
  43007. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  43008. {
  43009. int res = TEST_SKIPPED;
  43010. #if defined(OPENSSL_EXTRA)
  43011. EVP_PKEY* pkey;
  43012. EVP_PKEY_CTX* ctx;
  43013. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43014. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43015. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  43016. /* void */
  43017. EVP_PKEY_CTX_free(ctx);
  43018. AssertTrue(1);
  43019. #else
  43020. /* int */
  43021. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  43022. #endif
  43023. EVP_PKEY_free(pkey);
  43024. res = TEST_RES_CHECK(1);
  43025. #endif
  43026. return res;
  43027. }
  43028. static int test_wolfSSL_EVP_PKEY_param_check(void)
  43029. {
  43030. int res = TEST_SKIPPED;
  43031. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  43032. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43033. DH *dh = NULL;
  43034. DH *setDh = NULL;
  43035. EVP_PKEY *pkey = NULL;
  43036. EVP_PKEY_CTX* ctx = NULL;
  43037. FILE* f = NULL;
  43038. unsigned char buf[512];
  43039. const unsigned char* pt = buf;
  43040. const char* dh2048 = "./certs/dh2048.der";
  43041. long len = 0;
  43042. int code = -1;
  43043. XMEMSET(buf, 0, sizeof(buf));
  43044. f = XFOPEN(dh2048, "rb");
  43045. AssertTrue(f != XBADFILE);
  43046. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  43047. XFCLOSE(f);
  43048. /* Load dh2048.der into DH with internal format */
  43049. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  43050. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43051. AssertIntEQ(code, 0);
  43052. code = -1;
  43053. pkey = wolfSSL_EVP_PKEY_new();
  43054. /* Set DH into PKEY */
  43055. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43056. /* create ctx from pkey */
  43057. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43058. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  43059. /* */
  43060. /* TO DO invlaid case */
  43061. /* */
  43062. EVP_PKEY_CTX_free(ctx);
  43063. EVP_PKEY_free(pkey);
  43064. DH_free(setDh);
  43065. DH_free(dh);
  43066. res = TEST_RES_CHECK(1);
  43067. #endif
  43068. #endif
  43069. return res;
  43070. }
  43071. static int test_wolfSSL_EVP_BytesToKey(void)
  43072. {
  43073. int res = TEST_SKIPPED;
  43074. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43075. byte key[AES_BLOCK_SIZE] = {0};
  43076. byte iv[AES_BLOCK_SIZE] = {0};
  43077. int sz = 5;
  43078. int count = 0;
  43079. const EVP_MD* md = "SHA256";
  43080. const EVP_CIPHER *type;
  43081. const unsigned char *salt = (unsigned char *)"salt1234";
  43082. const byte data[] = {
  43083. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43084. 0x72,0x6c,0x64
  43085. };
  43086. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  43087. /* Bad cases */
  43088. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  43089. 0);
  43090. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  43091. 16);
  43092. md = "2";
  43093. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43094. WOLFSSL_FAILURE);
  43095. /* Good case */
  43096. md = "SHA256";
  43097. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43098. 16);
  43099. res = TEST_RES_CHECK(1);
  43100. #endif
  43101. return res;
  43102. }
  43103. static int test_evp_cipher_aes_gcm(void)
  43104. {
  43105. int res = TEST_SKIPPED;
  43106. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  43107. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  43108. (HAVE_FIPS_VERSION >= 2)))
  43109. /*
  43110. * This test checks data at various points in the encrypt/decrypt process
  43111. * against known values produced using the same test with OpenSSL. This
  43112. * interop testing is critical for verifying the correctness of our
  43113. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  43114. * a flow supported by OpenSSL that uses the control command
  43115. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  43116. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  43117. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  43118. * wolfSSL OpenSSH clients because there was a bug in this flow that
  43119. * happened to "cancel out" if both sides of the connection had the bug.
  43120. */
  43121. enum {
  43122. NUM_ENCRYPTIONS = 3,
  43123. AAD_SIZE = 4
  43124. };
  43125. byte plainText1[] = {
  43126. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  43127. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43128. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  43129. };
  43130. byte plainText2[] = {
  43131. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  43132. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  43133. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  43134. };
  43135. byte plainText3[] = {
  43136. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  43137. 0x5b, 0x57, 0x10
  43138. };
  43139. byte* plainTexts[NUM_ENCRYPTIONS] = {
  43140. plainText1,
  43141. plainText2,
  43142. plainText3
  43143. };
  43144. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  43145. sizeof(plainText1),
  43146. sizeof(plainText2),
  43147. sizeof(plainText3)
  43148. };
  43149. byte aad1[AAD_SIZE] = {
  43150. 0x00, 0x00, 0x00, 0x01
  43151. };
  43152. byte aad2[AAD_SIZE] = {
  43153. 0x00, 0x00, 0x00, 0x10
  43154. };
  43155. byte aad3[AAD_SIZE] = {
  43156. 0x00, 0x00, 0x01, 0x00
  43157. };
  43158. byte* aads[NUM_ENCRYPTIONS] = {
  43159. aad1,
  43160. aad2,
  43161. aad3
  43162. };
  43163. const byte iv[GCM_NONCE_MID_SZ] = {
  43164. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  43165. };
  43166. byte currentIv[GCM_NONCE_MID_SZ];
  43167. const byte key[] = {
  43168. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  43169. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  43170. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  43171. };
  43172. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  43173. {
  43174. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43175. 0xEF
  43176. },
  43177. {
  43178. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43179. 0xF0
  43180. },
  43181. {
  43182. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43183. 0xF1
  43184. }
  43185. };
  43186. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  43187. {
  43188. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  43189. 0x3D, 0x32, 0x18, 0x34, 0xA9
  43190. },
  43191. {
  43192. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  43193. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  43194. },
  43195. {
  43196. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  43197. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  43198. }
  43199. };
  43200. const byte expCipherText1[] = {
  43201. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  43202. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  43203. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  43204. };
  43205. const byte expCipherText2[] = {
  43206. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  43207. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  43208. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  43209. };
  43210. const byte expCipherText3[] = {
  43211. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  43212. 0x80, 0x5E, 0x6B,
  43213. };
  43214. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  43215. expCipherText1,
  43216. expCipherText2,
  43217. expCipherText3
  43218. };
  43219. byte* cipherText;
  43220. byte* calcPlainText;
  43221. byte tag[AES_BLOCK_SIZE];
  43222. EVP_CIPHER_CTX* encCtx = NULL;
  43223. EVP_CIPHER_CTX* decCtx = NULL;
  43224. int i, j, outl;
  43225. /****************************************************/
  43226. for (i = 0; i < 3; ++i) {
  43227. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  43228. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  43229. /* First iteration, set key before IV. */
  43230. if (i == 0) {
  43231. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  43232. SSL_SUCCESS);
  43233. /*
  43234. * The call to EVP_CipherInit below (with NULL key) should clear the
  43235. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43236. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43237. * behavior.
  43238. */
  43239. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43240. (void*)iv), SSL_SUCCESS);
  43241. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43242. SSL_SUCCESS);
  43243. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43244. currentIv), SSL_FAILURE);
  43245. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43246. SSL_SUCCESS);
  43247. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43248. SSL_SUCCESS);
  43249. }
  43250. /* Second iteration, IV before key. */
  43251. else {
  43252. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43253. SSL_SUCCESS);
  43254. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43255. SSL_SUCCESS);
  43256. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43257. SSL_SUCCESS);
  43258. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43259. SSL_SUCCESS);
  43260. }
  43261. /*
  43262. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43263. * been issued first.
  43264. */
  43265. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43266. currentIv), SSL_FAILURE);
  43267. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43268. (void*)iv), SSL_SUCCESS);
  43269. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43270. (void*)iv), SSL_SUCCESS);
  43271. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43272. /*************** Encrypt ***************/
  43273. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43274. currentIv), SSL_SUCCESS);
  43275. /* Check current IV against expected. */
  43276. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43277. /* Add AAD. */
  43278. if (i == 2) {
  43279. /* Test streaming API. */
  43280. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43281. AAD_SIZE), SSL_SUCCESS);
  43282. }
  43283. else {
  43284. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43285. AAD_SIZE);
  43286. }
  43287. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43288. DYNAMIC_TYPE_TMP_BUFFER));
  43289. /* Encrypt plaintext. */
  43290. if (i == 2) {
  43291. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43292. plainTexts[j], plainTextSzs[j]),
  43293. SSL_SUCCESS);
  43294. }
  43295. else {
  43296. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43297. plainTextSzs[j]), plainTextSzs[j]);
  43298. }
  43299. if (i == 2) {
  43300. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43301. SSL_SUCCESS);
  43302. }
  43303. else {
  43304. /*
  43305. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43306. * akin to calling EVP_CipherFinal.
  43307. */
  43308. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43309. }
  43310. /* Check ciphertext against expected. */
  43311. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43312. 0);
  43313. /* Get and check tag against expected. */
  43314. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43315. sizeof(tag), tag), SSL_SUCCESS);
  43316. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43317. /*************** Decrypt ***************/
  43318. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43319. currentIv), SSL_SUCCESS);
  43320. /* Check current IV against expected. */
  43321. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43322. /* Add AAD. */
  43323. if (i == 2) {
  43324. /* Test streaming API. */
  43325. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  43326. AAD_SIZE), SSL_SUCCESS);
  43327. }
  43328. else {
  43329. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  43330. AAD_SIZE);
  43331. }
  43332. /* Set expected tag. */
  43333. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  43334. sizeof(tag), tag), SSL_SUCCESS);
  43335. /* Decrypt ciphertext. */
  43336. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43337. DYNAMIC_TYPE_TMP_BUFFER));
  43338. if (i == 2) {
  43339. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  43340. cipherText, plainTextSzs[j]),
  43341. SSL_SUCCESS);
  43342. }
  43343. else {
  43344. /* This first EVP_Cipher call will check the tag, too. */
  43345. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  43346. plainTextSzs[j]), plainTextSzs[j]);
  43347. }
  43348. if (i == 2) {
  43349. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  43350. SSL_SUCCESS);
  43351. }
  43352. else {
  43353. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  43354. }
  43355. /* Check plaintext against expected. */
  43356. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  43357. 0);
  43358. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43359. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43360. }
  43361. EVP_CIPHER_CTX_free(encCtx);
  43362. EVP_CIPHER_CTX_free(decCtx);
  43363. }
  43364. res = TEST_RES_CHECK(1);
  43365. #endif
  43366. return res;
  43367. }
  43368. static int test_wolfSSL_OBJ_ln(void)
  43369. {
  43370. const int nid_set[] = {
  43371. NID_commonName,
  43372. NID_serialNumber,
  43373. NID_countryName,
  43374. NID_localityName,
  43375. NID_stateOrProvinceName,
  43376. NID_organizationName,
  43377. NID_organizationalUnitName,
  43378. NID_domainComponent,
  43379. NID_businessCategory,
  43380. NID_jurisdictionCountryName,
  43381. NID_jurisdictionStateOrProvinceName,
  43382. NID_emailAddress
  43383. };
  43384. const char* ln_set[] = {
  43385. "commonName",
  43386. "serialNumber",
  43387. "countryName",
  43388. "localityName",
  43389. "stateOrProvinceName",
  43390. "organizationName",
  43391. "organizationalUnitName",
  43392. "domainComponent",
  43393. "businessCategory",
  43394. "jurisdictionCountryName",
  43395. "jurisdictionStateOrProvinceName",
  43396. "emailAddress",
  43397. };
  43398. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  43399. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  43400. #ifdef HAVE_ECC
  43401. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43402. {
  43403. EC_builtin_curve r[27];
  43404. size_t nCurves = sizeof(r) / sizeof(r[0]);
  43405. nCurves = EC_get_builtin_curves(r,nCurves);
  43406. for (i = 0; i < nCurves; i++) {
  43407. /* skip ECC_CURVE_INVALID */
  43408. if (r[i].nid != ECC_CURVE_INVALID) {
  43409. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  43410. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  43411. }
  43412. }
  43413. }
  43414. #endif
  43415. #endif
  43416. for (i = 0; i < maxIdx; i++) {
  43417. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  43418. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  43419. }
  43420. return TEST_RES_CHECK(1);
  43421. }
  43422. static int test_wolfSSL_OBJ_sn(void)
  43423. {
  43424. int i = 0, maxIdx = 7;
  43425. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  43426. NID_stateOrProvinceName,NID_organizationName,
  43427. NID_organizationalUnitName,NID_emailAddress};
  43428. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  43429. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  43430. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  43431. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  43432. WOLFSSL_EMAIL_ADDR};
  43433. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  43434. for (i = 0; i < maxIdx; i++) {
  43435. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  43436. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  43437. }
  43438. return TEST_RES_CHECK(1);
  43439. }
  43440. #if !defined(NO_BIO)
  43441. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  43442. {
  43443. return lh_strhash(s[3]);
  43444. }
  43445. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  43446. {
  43447. return XSTRCMP(a[3], b[3]);
  43448. }
  43449. #endif
  43450. static int test_wolfSSL_TXT_DB(void)
  43451. {
  43452. int res = TEST_SKIPPED;
  43453. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43454. BIO *bio;
  43455. TXT_DB *db = NULL;
  43456. const int columns = 6;
  43457. const char *fields[6] = {
  43458. "V",
  43459. "320926161116Z",
  43460. "",
  43461. "12BD",
  43462. "unknown",
  43463. "/CN=rsa doe",
  43464. };
  43465. char** fields_copy;
  43466. /* Test read */
  43467. AssertNotNull(bio = BIO_new(BIO_s_file()));
  43468. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  43469. AssertNotNull(db = TXT_DB_read(bio, columns));
  43470. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  43471. DYNAMIC_TYPE_OPENSSL));
  43472. XMEMCPY(fields_copy, fields, sizeof(fields));
  43473. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  43474. BIO_free(bio);
  43475. /* Test write */
  43476. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  43477. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  43478. BIO_free(bio);
  43479. /* Test index */
  43480. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  43481. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  43482. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43483. fields[3] = "12DA";
  43484. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43485. fields[3] = "FFFF";
  43486. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43487. fields[3] = "";
  43488. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43489. TXT_DB_free(db);
  43490. res = TEST_RES_CHECK(1);
  43491. #endif
  43492. return res;
  43493. }
  43494. static int test_wolfSSL_NCONF(void)
  43495. {
  43496. int res = TEST_SKIPPED;
  43497. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43498. const char* confFile = "./tests/NCONF_test.cnf";
  43499. CONF* conf = NULL;
  43500. long eline = 0;
  43501. long num = 0;
  43502. AssertNotNull(conf = NCONF_new(NULL));
  43503. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  43504. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  43505. AssertIntEQ(num, 1234);
  43506. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  43507. AssertIntEQ(num, 4321);
  43508. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  43509. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  43510. "./test-dir/file1");
  43511. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  43512. "./test-dir/file1:./test-dir/file2:./section1:file2");
  43513. NCONF_free(conf);
  43514. res = TEST_RES_CHECK(1);
  43515. #endif
  43516. return res;
  43517. }
  43518. #endif /* OPENSSL_ALL */
  43519. static int test_wolfSSL_X509V3_EXT_get(void) {
  43520. int res = TEST_SKIPPED;
  43521. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43522. FILE* f;
  43523. int numOfExt =0;
  43524. int extNid = 0;
  43525. int i = 0;
  43526. WOLFSSL_X509* x509;
  43527. WOLFSSL_X509_EXTENSION* ext;
  43528. const WOLFSSL_v3_ext_method* method;
  43529. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43530. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43531. fclose(f);
  43532. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  43533. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43534. for (i = 0; i < numOfExt; i++) {
  43535. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43536. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  43537. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43538. AssertIntEQ(method->ext_nid, extNid);
  43539. }
  43540. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  43541. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  43542. wolfSSL_X509_free(x509);
  43543. res = TEST_RES_CHECK(1);
  43544. #endif
  43545. return res;
  43546. }
  43547. static int test_wolfSSL_X509V3_EXT_nconf(void)
  43548. {
  43549. int res = TEST_SKIPPED;
  43550. #ifdef OPENSSL_ALL
  43551. const char *ext_names[] = {
  43552. "subjectKeyIdentifier",
  43553. "authorityKeyIdentifier",
  43554. "subjectAltName",
  43555. "keyUsage",
  43556. };
  43557. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  43558. int ext_nids[] = {
  43559. NID_subject_key_identifier,
  43560. NID_authority_key_identifier,
  43561. NID_subject_alt_name,
  43562. NID_key_usage,
  43563. };
  43564. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  43565. const char *ext_values[] = {
  43566. "hash",
  43567. "hash",
  43568. "DNS:example.com, IP:127.0.0.1",
  43569. "digitalSignature,keyEncipherment,dataEncipherment",
  43570. };
  43571. size_t i;
  43572. X509_EXTENSION* ext;
  43573. X509* x509 = X509_new();
  43574. for (i = 0; i < ext_names_count; i++) {
  43575. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  43576. AssertNotNull(ext);
  43577. X509_EXTENSION_free(ext);
  43578. }
  43579. for (i = 0; i < ext_nids_count; i++) {
  43580. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  43581. AssertNotNull(ext);
  43582. X509_EXTENSION_free(ext);
  43583. }
  43584. /* Test adding extension to X509 */
  43585. for (i = 0; i < ext_nids_count; i++) {
  43586. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  43587. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  43588. X509_EXTENSION_free(ext);
  43589. }
  43590. X509_free(x509);
  43591. res = TEST_RES_CHECK(1);
  43592. #endif
  43593. return res;
  43594. }
  43595. static int test_wolfSSL_X509V3_EXT(void) {
  43596. int res = TEST_SKIPPED;
  43597. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43598. FILE* f;
  43599. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  43600. char* str;
  43601. unsigned char* data;
  43602. const WOLFSSL_v3_ext_method* method;
  43603. WOLFSSL_X509* x509;
  43604. WOLFSSL_X509_EXTENSION* ext;
  43605. WOLFSSL_X509_EXTENSION* ext2;
  43606. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  43607. WOLFSSL_ASN1_STRING* asn1str;
  43608. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  43609. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  43610. WOLFSSL_BASIC_CONSTRAINTS* bc;
  43611. WOLFSSL_ACCESS_DESCRIPTION* ad;
  43612. WOLFSSL_GENERAL_NAME* gn;
  43613. /* Check NULL argument */
  43614. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  43615. /* Using OCSP cert with X509V3 extensions */
  43616. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  43617. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43618. fclose(f);
  43619. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43620. /* Basic Constraints */
  43621. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43622. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43623. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  43624. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  43625. AssertIntEQ(bc->ca, 1);
  43626. AssertNull(bc->pathlen);
  43627. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  43628. i++;
  43629. /* Subject Key Identifier */
  43630. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43631. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43632. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  43633. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43634. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  43635. asn1str));
  43636. X509_EXTENSION_free(ext2);
  43637. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43638. AssertNotNull(method->i2s);
  43639. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43640. wolfSSL_ASN1_STRING_free(asn1str);
  43641. actual = strcmp(str,
  43642. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43643. AssertIntEQ(actual, 0);
  43644. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43645. i++;
  43646. /* Authority Key Identifier */
  43647. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43648. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43649. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43650. AssertNotNull(aKeyId =
  43651. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  43652. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43653. AssertNotNull(asn1str = aKeyId->keyid);
  43654. AssertNotNull(str =
  43655. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  43656. actual = strcmp(str,
  43657. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43658. AssertIntEQ(actual, 0);
  43659. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43660. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43661. i++;
  43662. /* Key Usage */
  43663. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43664. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43665. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43666. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43667. #if defined(WOLFSSL_QT)
  43668. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43669. #else
  43670. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43671. #endif
  43672. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43673. #ifdef BIG_ENDIAN_ORDER
  43674. actual = data[1];
  43675. #else
  43676. actual = data[0];
  43677. #endif
  43678. AssertIntEQ(actual, expected);
  43679. wolfSSL_ASN1_STRING_free(asn1str);
  43680. #if 1
  43681. i++;
  43682. /* Authority Info Access */
  43683. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43684. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43685. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43686. AssertNotNull(aia =
  43687. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  43688. #if defined(WOLFSSL_QT)
  43689. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43690. #else
  43691. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43692. #endif
  43693. /* URI entry is an ACCESS_DESCRIPTION type */
  43694. #if defined(WOLFSSL_QT)
  43695. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43696. #else
  43697. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43698. #endif
  43699. AssertNotNull(adObj = ad->method);
  43700. /* Make sure nid is OCSP */
  43701. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43702. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43703. AssertNotNull(gn = ad->location);
  43704. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43705. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43706. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43707. #if defined(WOLFSSL_QT)
  43708. str = (char*)ASN1_STRING_get0_data(asn1str);
  43709. #else
  43710. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  43711. #endif
  43712. actual = strcmp(str, "http://127.0.0.1:22220");
  43713. AssertIntEQ(actual, 0);
  43714. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43715. #else
  43716. (void) aia; (void) ad; (void) adObj; (void) gn;
  43717. #endif
  43718. wolfSSL_X509_free(x509);
  43719. res = TEST_RES_CHECK(1);
  43720. #endif
  43721. return res;
  43722. }
  43723. static int test_wolfSSL_X509_get_extension_flags(void)
  43724. {
  43725. int res = TEST_SKIPPED;
  43726. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43727. XFILE f;
  43728. X509* x509;
  43729. unsigned int extFlags;
  43730. unsigned int keyUsageFlags;
  43731. unsigned int extKeyUsageFlags;
  43732. /* client-int-cert.pem has the following extension flags. */
  43733. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43734. /* and the following key usage flags. */
  43735. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43736. | KU_NON_REPUDIATION
  43737. | KU_KEY_ENCIPHERMENT;
  43738. /* and the following extended key usage flags. */
  43739. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43740. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  43741. AssertTrue(f != XBADFILE);
  43742. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43743. XFCLOSE(f);
  43744. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  43745. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43746. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43747. X509_free(x509);
  43748. /* client-cert-ext.pem has the following extension flags. */
  43749. extFlags = EXFLAG_KUSAGE;
  43750. /* and the following key usage flags. */
  43751. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43752. | KU_KEY_CERT_SIGN
  43753. | KU_CRL_SIGN;
  43754. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  43755. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43756. XFCLOSE(f);
  43757. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  43758. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43759. X509_free(x509);
  43760. res = TEST_RES_CHECK(1);
  43761. #endif /* OPENSSL_ALL */
  43762. return res;
  43763. }
  43764. static int test_wolfSSL_X509_get_ext(void)
  43765. {
  43766. int res = TEST_SKIPPED;
  43767. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43768. int ret = 0;
  43769. FILE* f;
  43770. WOLFSSL_X509* x509;
  43771. WOLFSSL_X509_EXTENSION* foundExtension;
  43772. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43773. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43774. fclose(f);
  43775. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43776. /* wolfSSL_X509_get_ext() valid input */
  43777. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43778. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43779. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43780. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43781. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43782. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43783. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43784. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43785. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43786. wolfSSL_X509_free(x509);
  43787. res = TEST_RES_CHECK(1);
  43788. #endif
  43789. return res;
  43790. }
  43791. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43792. {
  43793. int res = TEST_SKIPPED;
  43794. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43795. int rc;
  43796. FILE* f;
  43797. WOLFSSL_X509* x509;
  43798. ASN1_OBJECT* obj = NULL;
  43799. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  43800. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43801. fclose(f);
  43802. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  43803. AssertIntGE(rc, 0);
  43804. /* Start search from last location (should fail) */
  43805. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  43806. AssertIntGE(rc, -1);
  43807. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  43808. AssertIntGE(rc, -1);
  43809. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  43810. AssertIntEQ(rc, -1);
  43811. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  43812. AssertIntEQ(rc, -1);
  43813. /* NID_ext_key_usage, check also its nid and oid */
  43814. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  43815. AssertIntGT(rc, -1);
  43816. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  43817. AssertIntEQ(obj->nid, NID_ext_key_usage);
  43818. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  43819. wolfSSL_X509_free(x509);
  43820. res = TEST_RES_CHECK(1);
  43821. #endif
  43822. return res;
  43823. }
  43824. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  43825. {
  43826. int res = TEST_SKIPPED;
  43827. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43828. int rc;
  43829. XFILE f;
  43830. WOLFSSL_X509* x509;
  43831. WOLFSSL_X509_EXTENSION* ext;
  43832. WOLFSSL_ASN1_STRING* sanString;
  43833. byte* sanDer;
  43834. const byte expectedDer[] = {
  43835. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  43836. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  43837. f = XFOPEN("./certs/server-cert.pem", "rb");
  43838. AssertTrue(f != XBADFILE);
  43839. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43840. fclose(f);
  43841. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  43842. AssertIntNE(rc, -1);
  43843. AssertNotNull(ext = X509_get_ext(x509, rc));
  43844. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  43845. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  43846. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  43847. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  43848. X509_free(x509);
  43849. res = TEST_RES_CHECK(1);
  43850. #endif
  43851. return res;
  43852. }
  43853. static int test_wolfSSL_X509_EXTENSION_new(void)
  43854. {
  43855. int res = TEST_SKIPPED;
  43856. #if defined (OPENSSL_ALL)
  43857. WOLFSSL_X509_EXTENSION* ext;
  43858. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  43859. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  43860. ext->obj->nid = WOLFSSL_SUCCESS;
  43861. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  43862. wolfSSL_X509_EXTENSION_free(ext);
  43863. res = TEST_RES_CHECK(1);
  43864. #endif
  43865. return res;
  43866. }
  43867. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  43868. {
  43869. int res = TEST_SKIPPED;
  43870. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43871. WOLFSSL_X509* x509;
  43872. WOLFSSL_X509_EXTENSION* ext;
  43873. WOLFSSL_ASN1_OBJECT* o;
  43874. FILE* file;
  43875. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43876. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43877. fclose(file);
  43878. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  43879. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43880. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  43881. AssertIntEQ(o->nid, 128);
  43882. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  43883. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  43884. wolfSSL_X509_free(x509);
  43885. res = TEST_RES_CHECK(1);
  43886. #endif
  43887. return res;
  43888. }
  43889. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  43890. {
  43891. int res = TEST_SKIPPED;
  43892. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43893. WOLFSSL_X509* x509;
  43894. WOLFSSL_X509_EXTENSION* ext;
  43895. WOLFSSL_ASN1_STRING* str;
  43896. FILE* file;
  43897. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43898. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43899. fclose(file);
  43900. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43901. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  43902. wolfSSL_X509_free(x509);
  43903. res = TEST_RES_CHECK(1);
  43904. #endif
  43905. return res;
  43906. }
  43907. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  43908. {
  43909. int res = TEST_SKIPPED;
  43910. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43911. WOLFSSL_X509* x509;
  43912. WOLFSSL_X509_EXTENSION* ext;
  43913. FILE* file;
  43914. int crit;
  43915. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  43916. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43917. fclose(file);
  43918. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43919. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  43920. AssertIntEQ(crit, 0);
  43921. wolfSSL_X509_free(x509);
  43922. res = TEST_RES_CHECK(1);
  43923. #endif
  43924. return res;
  43925. }
  43926. static int test_wolfSSL_X509V3_EXT_print(void)
  43927. {
  43928. int res = TEST_SKIPPED;
  43929. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  43930. !defined(NO_RSA)
  43931. {
  43932. FILE* f;
  43933. WOLFSSL_X509* x509;
  43934. X509_EXTENSION * ext = NULL;
  43935. int loc;
  43936. BIO *bio = NULL;
  43937. AssertNotNull(f = fopen(svrCertFile, "rb"));
  43938. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43939. fclose(f);
  43940. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  43941. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  43942. AssertIntGT(loc, -1);
  43943. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43944. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43945. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  43946. AssertIntGT(loc, -1);
  43947. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43948. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43949. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  43950. AssertIntGT(loc, -1);
  43951. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43952. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43953. wolfSSL_BIO_free(bio);
  43954. wolfSSL_X509_free(x509);
  43955. }
  43956. {
  43957. X509 *x509;
  43958. BIO *bio;
  43959. X509_EXTENSION *ext;
  43960. unsigned int i;
  43961. unsigned int idx;
  43962. /* Some NIDs to test with */
  43963. int nids[] = {
  43964. /* NID_key_usage, currently X509_get_ext returns this as a bit
  43965. * string, which messes up X509V3_EXT_print */
  43966. /* NID_ext_key_usage, */
  43967. NID_subject_alt_name,
  43968. };
  43969. int* n;
  43970. AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  43971. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  43972. WOLFSSL_FILETYPE_PEM));
  43973. fprintf(stderr, "\nPrinting extension values:\n");
  43974. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  43975. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  43976. * update the index */
  43977. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  43978. AssertNotNull(ext = X509_get_ext(x509, idx));
  43979. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  43980. fprintf(stderr, "\n");
  43981. }
  43982. BIO_free(bio);
  43983. X509_free(x509);
  43984. }
  43985. res = TEST_RES_CHECK(1);
  43986. #endif
  43987. return res;
  43988. }
  43989. static int test_wolfSSL_X509_cmp(void)
  43990. {
  43991. int res = TEST_SKIPPED;
  43992. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43993. FILE* file1;
  43994. FILE* file2;
  43995. WOLFSSL_X509* cert1;
  43996. WOLFSSL_X509* cert2;
  43997. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  43998. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  43999. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  44000. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  44001. fclose(file1);
  44002. fclose(file2);
  44003. /* wolfSSL_X509_cmp() testing matching certs */
  44004. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  44005. /* wolfSSL_X509_cmp() testing mismatched certs */
  44006. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  44007. /* wolfSSL_X509_cmp() testing NULL, valid args */
  44008. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  44009. /* wolfSSL_X509_cmp() testing valid, NULL args */
  44010. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  44011. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  44012. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  44013. wolfSSL_X509_free(cert1);
  44014. wolfSSL_X509_free(cert2);
  44015. res = TEST_RES_CHECK(1);
  44016. #endif
  44017. return res;
  44018. }
  44019. static int test_wolfSSL_PKEY_up_ref(void)
  44020. {
  44021. int res = TEST_SKIPPED;
  44022. #if defined(OPENSSL_ALL)
  44023. EVP_PKEY* pkey;
  44024. pkey = EVP_PKEY_new();
  44025. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  44026. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44027. EVP_PKEY_free(pkey);
  44028. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44029. EVP_PKEY_free(pkey);
  44030. EVP_PKEY_free(pkey);
  44031. res = TEST_RES_CHECK(1);
  44032. #endif
  44033. return res;
  44034. }
  44035. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  44036. {
  44037. int res = TEST_SKIPPED;
  44038. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44039. EVP_PKEY* pkey;
  44040. const unsigned char* p;
  44041. unsigned char *der = NULL, *tmp = NULL;
  44042. int derLen;
  44043. p = client_keypub_der_2048;
  44044. /* Check that key can be successfully decoded. */
  44045. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  44046. sizeof_client_keypub_der_2048));
  44047. /* Check that key can be successfully encoded. */
  44048. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44049. /* Ensure that the encoded version matches the original. */
  44050. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  44051. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44052. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44053. * is advanced. */
  44054. tmp = der;
  44055. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44056. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  44057. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44058. AssertTrue(der + derLen == tmp);
  44059. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44060. EVP_PKEY_free(pkey);
  44061. res = TEST_RES_CHECK(1);
  44062. #endif
  44063. return res;
  44064. }
  44065. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  44066. {
  44067. int res = TEST_SKIPPED;
  44068. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  44069. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44070. EVP_PKEY* pkey;
  44071. const unsigned char* p;
  44072. unsigned char *der = NULL, *tmp = NULL;
  44073. int derLen;
  44074. unsigned char pub_buf[65];
  44075. const int pub_len = 65;
  44076. BN_CTX * ctx;
  44077. EC_GROUP * curve;
  44078. EC_KEY * ephemeral_key;
  44079. const EC_POINT * h;
  44080. /* Generate an x963 key pair and get public part into pub_buf */
  44081. AssertNotNull(ctx = BN_CTX_new());
  44082. AssertNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  44083. AssertNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  44084. NID_X9_62_prime256v1));
  44085. AssertIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  44086. AssertNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  44087. AssertIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  44088. POINT_CONVERSION_UNCOMPRESSED,
  44089. pub_buf, pub_len, ctx));
  44090. /* Prepare the EVP_PKEY */
  44091. AssertNotNull(pkey = EVP_PKEY_new());
  44092. p = pub_buf;
  44093. /* Check that key can be successfully decoded. */
  44094. AssertNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  44095. pub_len));
  44096. /* Check that key can be successfully encoded. */
  44097. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44098. /* Ensure that the encoded version matches the original. */
  44099. AssertIntEQ(derLen, pub_len);
  44100. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44101. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44102. * is advanced. */
  44103. tmp = der;
  44104. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44105. AssertIntEQ(derLen, pub_len);
  44106. AssertIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44107. AssertTrue(der + derLen == tmp);
  44108. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44109. EVP_PKEY_free(pkey);
  44110. EC_KEY_free(ephemeral_key);
  44111. EC_GROUP_free(curve);
  44112. res = TEST_RES_CHECK(1);
  44113. #endif
  44114. return res;
  44115. }
  44116. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  44117. {
  44118. int res = TEST_SKIPPED;
  44119. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  44120. DSA* dsa;
  44121. char file[] = "./certs/dsaparams.der";
  44122. XFILE f;
  44123. int derInLen;
  44124. byte* derIn;
  44125. int derOutLen;
  44126. byte* derOut = NULL;
  44127. f = XFOPEN(file, "rb");
  44128. AssertTrue(f != XBADFILE);
  44129. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  44130. derInLen = (int)XFTELL(f);
  44131. AssertTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  44132. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  44133. DYNAMIC_TYPE_TMP_BUFFER));
  44134. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  44135. XFCLOSE(f);
  44136. /* Check that params can be successfully decoded. */
  44137. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  44138. /* Check that params can be successfully encoded. */
  44139. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  44140. /* Ensure that the encoded version matches the original. */
  44141. AssertIntEQ(derInLen, derOutLen);
  44142. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  44143. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44144. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44145. DSA_free(dsa);
  44146. res = TEST_RES_CHECK(1);
  44147. #endif
  44148. return res;
  44149. }
  44150. static int test_wolfSSL_i2d_PrivateKey(void)
  44151. {
  44152. int res = TEST_SKIPPED;
  44153. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  44154. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44155. {
  44156. EVP_PKEY* pkey;
  44157. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  44158. unsigned char buf[FOURK_BUF];
  44159. unsigned char* pt = NULL;
  44160. int bufSz;
  44161. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44162. (long)sizeof_server_key_der_2048));
  44163. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44164. pt = buf;
  44165. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44166. AssertIntNE((pt - buf), 0);
  44167. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44168. EVP_PKEY_free(pkey);
  44169. }
  44170. #endif
  44171. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44172. {
  44173. EVP_PKEY* pkey;
  44174. const unsigned char* client_key =
  44175. (const unsigned char*)ecc_clikey_der_256;
  44176. unsigned char buf[FOURK_BUF];
  44177. unsigned char* pt = NULL;
  44178. int bufSz;
  44179. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44180. sizeof_ecc_clikey_der_256)));
  44181. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44182. pt = buf;
  44183. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44184. AssertIntNE((pt - buf), 0);
  44185. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44186. EVP_PKEY_free(pkey);
  44187. }
  44188. #endif
  44189. res = TEST_RES_CHECK(1);
  44190. #endif
  44191. return res;
  44192. }
  44193. static int test_wolfSSL_OCSP_id_get0_info(void)
  44194. {
  44195. int res = TEST_SKIPPED;
  44196. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  44197. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44198. X509* cert;
  44199. X509* issuer;
  44200. OCSP_CERTID* id;
  44201. OCSP_CERTID* id2;
  44202. ASN1_STRING* name = NULL;
  44203. ASN1_OBJECT* pmd = NULL;
  44204. ASN1_STRING* keyHash = NULL;
  44205. ASN1_INTEGER* serial = NULL;
  44206. ASN1_INTEGER* x509Int;
  44207. AssertNotNull(cert =
  44208. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  44209. AssertNotNull(issuer =
  44210. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  44211. id = OCSP_cert_to_id(NULL, cert, issuer);
  44212. AssertNotNull(id);
  44213. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  44214. AssertNotNull(id2);
  44215. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44216. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44217. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44218. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44219. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44220. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44221. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44222. AssertNotNull(serial);
  44223. /* compare serial number to one in cert, should be equal */
  44224. x509Int = X509_get_serialNumber(cert);
  44225. AssertNotNull(x509Int);
  44226. AssertIntEQ(x509Int->length, serial->length);
  44227. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44228. /* test OCSP_id_cmp */
  44229. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44230. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  44231. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  44232. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  44233. id->issuerHash[0] = ~id->issuerHash[0];
  44234. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  44235. OCSP_CERTID_free(id);
  44236. OCSP_CERTID_free(id2);
  44237. X509_free(cert); /* free's x509Int */
  44238. X509_free(issuer);
  44239. res = TEST_RES_CHECK(1);
  44240. #endif
  44241. return res;
  44242. }
  44243. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  44244. {
  44245. int res = TEST_SKIPPED;
  44246. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44247. WOLFSSL_OCSP_CERTID certId;
  44248. byte* targetBuffer;
  44249. byte* beginTargetBuffer;
  44250. /* OCSP CertID bytes taken from PCAP */
  44251. byte rawCertId[] = {
  44252. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44253. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44254. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44255. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44256. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44257. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44258. 0xcf, 0xbc, 0x91
  44259. };
  44260. int ret, i;
  44261. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  44262. certId.rawCertId = rawCertId;
  44263. certId.rawCertIdSize = sizeof(rawCertId);
  44264. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44265. beginTargetBuffer = targetBuffer;
  44266. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  44267. /* If target buffer is not null, function increments targetBuffer to point
  44268. just past the end of the encoded data. */
  44269. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  44270. /* Function returns the size of the encoded data. */
  44271. AssertIntEQ(ret, sizeof(rawCertId));
  44272. for (i = 0; i < ret; ++i)
  44273. {
  44274. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  44275. }
  44276. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44277. targetBuffer = NULL;
  44278. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  44279. /* If target buffer is null, function allocates memory for a buffer and
  44280. copies the encoded data into it. targetBuffer then points to the start of
  44281. this newly allocate buffer. */
  44282. AssertIntEQ(ret, sizeof(rawCertId));
  44283. for (i = 0; i < ret; ++i)
  44284. {
  44285. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  44286. }
  44287. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  44288. res = TEST_RES_CHECK(1);
  44289. #endif
  44290. return res;
  44291. }
  44292. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  44293. {
  44294. int res = TEST_SKIPPED;
  44295. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44296. WOLFSSL_OCSP_CERTID* certId;
  44297. WOLFSSL_OCSP_CERTID* certIdBad;
  44298. const unsigned char* rawCertIdPtr;
  44299. const unsigned char rawCertId[] = {
  44300. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44301. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44302. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44303. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44304. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44305. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44306. 0xcf, 0xbc, 0x91
  44307. };
  44308. rawCertIdPtr = &rawCertId[0];
  44309. /* If the cert ID is NULL the function should allocate it and copy the
  44310. * data to it. */
  44311. certId = NULL;
  44312. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  44313. AssertNotNull(certId);
  44314. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44315. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44316. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  44317. /* If the cert ID is not NULL the function will just copy the data to it. */
  44318. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  44319. DYNAMIC_TYPE_TMP_BUFFER);
  44320. AssertNotNull(certId);
  44321. XMEMSET(certId, 0, sizeof(*certId));
  44322. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  44323. rawCertIdPtr = &rawCertId[0];
  44324. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  44325. AssertNotNull(certId);
  44326. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44327. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44328. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44329. /* The below tests should fail when passed bad parameters. NULL should
  44330. * always be returned. */
  44331. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  44332. AssertNull(certIdBad);
  44333. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  44334. AssertNull(certIdBad);
  44335. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  44336. AssertNull(certIdBad);
  44337. res = TEST_RES_CHECK(1);
  44338. #endif
  44339. return res;
  44340. }
  44341. static int test_wolfSSL_OCSP_id_cmp(void)
  44342. {
  44343. int res = TEST_SKIPPED;
  44344. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44345. OCSP_CERTID id1;
  44346. OCSP_CERTID id2;
  44347. XMEMSET(&id1, 0, sizeof(id1));
  44348. XMEMSET(&id2, 0, sizeof(id2));
  44349. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  44350. res = TEST_RES_CHECK(1);
  44351. #endif
  44352. return res;
  44353. }
  44354. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  44355. {
  44356. int res = TEST_SKIPPED;
  44357. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44358. WOLFSSL_OCSP_SINGLERESP single;
  44359. const WOLFSSL_OCSP_CERTID* certId;
  44360. XMEMSET(&single, 0, sizeof(single));
  44361. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  44362. AssertPtrEq(&single, certId);
  44363. res = TEST_RES_CHECK(1);
  44364. #endif
  44365. return res;
  44366. }
  44367. static int test_wolfSSL_OCSP_single_get0_status(void)
  44368. {
  44369. int res = TEST_SKIPPED;
  44370. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44371. WOLFSSL_OCSP_SINGLERESP single;
  44372. CertStatus certStatus;
  44373. WOLFSSL_ASN1_TIME* thisDate;
  44374. WOLFSSL_ASN1_TIME* nextDate;
  44375. int ret, i;
  44376. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44377. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  44378. /* Fill the date fields with some dummy data. */
  44379. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  44380. certStatus.thisDateParsed.data[i] = i;
  44381. certStatus.nextDateParsed.data[i] = i;
  44382. }
  44383. certStatus.status = CERT_GOOD;
  44384. single.status = &certStatus;
  44385. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  44386. &nextDate);
  44387. AssertIntEQ(ret, CERT_GOOD);
  44388. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  44389. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  44390. res = TEST_RES_CHECK(1);
  44391. #endif
  44392. return res;
  44393. }
  44394. static int test_wolfSSL_OCSP_resp_count(void)
  44395. {
  44396. int res = TEST_SKIPPED;
  44397. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44398. WOLFSSL_OCSP_BASICRESP basicResp;
  44399. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44400. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44401. int count;
  44402. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44403. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44404. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44405. count = wolfSSL_OCSP_resp_count(&basicResp);
  44406. AssertIntEQ(count, 0);
  44407. basicResp.single = &singleRespOne;
  44408. count = wolfSSL_OCSP_resp_count(&basicResp);
  44409. AssertIntEQ(count, 1);
  44410. singleRespOne.next = &singleRespTwo;
  44411. count = wolfSSL_OCSP_resp_count(&basicResp);
  44412. AssertIntEQ(count, 2);
  44413. res = TEST_RES_CHECK(1);
  44414. #endif
  44415. return res;
  44416. }
  44417. static int test_wolfSSL_OCSP_resp_get0(void)
  44418. {
  44419. int res = TEST_SKIPPED;
  44420. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44421. WOLFSSL_OCSP_BASICRESP basicResp;
  44422. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44423. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44424. WOLFSSL_OCSP_SINGLERESP* ret;
  44425. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44426. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44427. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44428. basicResp.single = &singleRespOne;
  44429. singleRespOne.next = &singleRespTwo;
  44430. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  44431. AssertPtrEq(ret, &singleRespOne);
  44432. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  44433. AssertPtrEq(ret, &singleRespTwo);
  44434. res = TEST_RES_CHECK(1);
  44435. #endif
  44436. return res;
  44437. }
  44438. static int test_wolfSSL_EVP_PKEY_derive(void)
  44439. {
  44440. int res = TEST_SKIPPED;
  44441. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44442. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  44443. EVP_PKEY_CTX *ctx;
  44444. unsigned char *skey;
  44445. size_t skeylen;
  44446. EVP_PKEY *pkey, *peerkey;
  44447. const unsigned char* key;
  44448. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  44449. /* DH */
  44450. key = dh_key_der_2048;
  44451. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44452. sizeof_dh_key_der_2048)));
  44453. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  44454. key = dh_key_der_2048;
  44455. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44456. sizeof_dh_key_der_2048)));
  44457. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  44458. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44459. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44460. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44461. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44462. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  44463. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44464. EVP_PKEY_CTX_free(ctx);
  44465. EVP_PKEY_free(peerkey);
  44466. EVP_PKEY_free(pkey);
  44467. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44468. #endif
  44469. #ifdef HAVE_ECC
  44470. /* ECDH */
  44471. key = ecc_clikey_der_256;
  44472. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  44473. sizeof_ecc_clikey_der_256)));
  44474. key = ecc_clikeypub_der_256;
  44475. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  44476. sizeof_ecc_clikeypub_der_256)));
  44477. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44478. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44479. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44480. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44481. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  44482. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44483. EVP_PKEY_CTX_free(ctx);
  44484. EVP_PKEY_free(peerkey);
  44485. EVP_PKEY_free(pkey);
  44486. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44487. #endif /* HAVE_ECC */
  44488. res = TEST_RES_CHECK(1);
  44489. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  44490. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44491. return res;
  44492. }
  44493. static int test_wolfSSL_EVP_PBE_scrypt(void)
  44494. {
  44495. int res = TEST_SKIPPED;
  44496. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  44497. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  44498. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  44499. int ret;
  44500. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  44501. int pwdlen = sizeof(pwd);
  44502. const byte salt[] = {'N','a','C','l'};
  44503. int saltlen = sizeof(salt);
  44504. byte key[80];
  44505. word64 numOvr32 = (word64)INT32_MAX + 1;
  44506. /* expected derived key for N:16, r:1, p:1 */
  44507. const byte expectedKey[] = {
  44508. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  44509. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  44510. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  44511. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  44512. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  44513. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  44514. 0xE7, 0xE9, 0xC0, 0x9A};
  44515. /* N r p mx key keylen */
  44516. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  44517. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  44518. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  44519. AssertIntEQ(ret, 0); /* N must be power of 2 */
  44520. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  44521. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  44522. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  44523. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  44524. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  44525. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  44526. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  44527. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  44528. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  44529. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  44530. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  44531. AssertIntEQ(ret, 1); /* should succeed */
  44532. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  44533. key, 64);
  44534. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  44535. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  44536. key, 64);
  44537. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  44538. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  44539. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  44540. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  44541. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  44542. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  44543. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  44544. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  44545. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  44546. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  44547. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  44548. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  44549. AssertIntEQ(ret, 1);
  44550. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  44551. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  44552. res = TEST_RES_CHECK(1);
  44553. #endif /* !NO_PWDBASED && !NO_SHA256 */
  44554. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  44555. return res;
  44556. }
  44557. static int test_no_op_functions(void)
  44558. {
  44559. int res = TEST_SKIPPED;
  44560. #if defined(OPENSSL_EXTRA)
  44561. /* this makes sure wolfSSL can compile and run these no-op functions */
  44562. SSL_load_error_strings();
  44563. ENGINE_load_builtin_engines();
  44564. OpenSSL_add_all_ciphers();
  44565. AssertIntEQ(CRYPTO_malloc_init(), 0);
  44566. res = TEST_RES_CHECK(1);
  44567. #endif
  44568. return res;
  44569. }
  44570. static int test_wolfSSL_CRYPTO_memcmp(void)
  44571. {
  44572. int res = TEST_SKIPPED;
  44573. #ifdef OPENSSL_EXTRA
  44574. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44575. "implementation of TLS/SSL for embedded devices to the cloud.";
  44576. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44577. "implementation of TLS/SSL for embedded devices to the cloud.";
  44578. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44579. "implementation of TLS/SSL for embedded devices to the cloud!";
  44580. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  44581. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  44582. res = TEST_RES_CHECK(1);
  44583. #endif
  44584. return res;
  44585. }
  44586. /*----------------------------------------------------------------------------*
  44587. | wolfCrypt ASN
  44588. *----------------------------------------------------------------------------*/
  44589. static int test_wc_CreateEncryptedPKCS8Key(void)
  44590. {
  44591. int res = TEST_SKIPPED;
  44592. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44593. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44594. WC_RNG rng;
  44595. byte* encKey = NULL;
  44596. word32 encKeySz = 0;
  44597. word32 decKeySz = 0;
  44598. const char password[] = "Lorem ipsum dolor sit amet";
  44599. word32 passwordSz = (word32)XSTRLEN(password);
  44600. word32 tradIdx = 0;
  44601. AssertIntEQ(wc_InitRng(&rng), 0);
  44602. /* Call with NULL for out buffer to get necessary length. */
  44603. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44604. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44605. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44606. LENGTH_ONLY_E);
  44607. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44608. DYNAMIC_TYPE_TMP_BUFFER));
  44609. /* Call with the allocated out buffer. */
  44610. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44611. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44612. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44613. 0);
  44614. /* Decrypt the encrypted PKCS8 key we just made. */
  44615. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44616. passwordSz)), 0);
  44617. /* encKey now holds the decrypted key (decrypted in place). */
  44618. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44619. /* Check that the decrypted key matches the key prior to encryption. */
  44620. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44621. sizeof_server_key_der_2048), 0);
  44622. if (encKey != NULL)
  44623. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44624. wc_FreeRng(&rng);
  44625. res = TEST_RES_CHECK(1);
  44626. #endif
  44627. return res;
  44628. }
  44629. static int test_wc_GetPkcs8TraditionalOffset(void)
  44630. {
  44631. int res = TEST_SKIPPED;
  44632. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44633. int length, derSz;
  44634. word32 inOutIdx;
  44635. const char* path = "./certs/server-keyPkcs8.der";
  44636. XFILE file;
  44637. byte der[2048];
  44638. file = XFOPEN(path, "rb");
  44639. AssertTrue(file != XBADFILE);
  44640. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  44641. XFCLOSE(file);
  44642. /* valid case */
  44643. inOutIdx = 0;
  44644. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44645. AssertIntGT(length, 0);
  44646. /* inOutIdx > sz */
  44647. inOutIdx = 4000;
  44648. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44649. AssertIntEQ(length, BAD_FUNC_ARG);
  44650. /* null input */
  44651. inOutIdx = 0;
  44652. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  44653. AssertIntEQ(length, BAD_FUNC_ARG);
  44654. /* invalid input, fill buffer with 1's */
  44655. XMEMSET(der, 1, sizeof(der));
  44656. inOutIdx = 0;
  44657. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  44658. AssertIntEQ(length, ASN_PARSE_E);
  44659. res = TEST_RES_CHECK(1);
  44660. #endif /* NO_ASN */
  44661. return res;
  44662. }
  44663. static int test_wc_SetSubjectRaw(void)
  44664. {
  44665. int res = TEST_SKIPPED;
  44666. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44667. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44668. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44669. WOLFSSL_X509* x509;
  44670. int peerCertSz;
  44671. const byte* peerCertBuf;
  44672. Cert forgedCert;
  44673. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44674. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44675. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44676. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44677. wolfSSL_FreeX509(x509);
  44678. res = TEST_RES_CHECK(1);
  44679. #endif
  44680. return res;
  44681. }
  44682. static int test_wc_GetSubjectRaw(void)
  44683. {
  44684. int res = TEST_SKIPPED;
  44685. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44686. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44687. Cert cert;
  44688. byte *subjectRaw;
  44689. AssertIntEQ(0, wc_InitCert(&cert));
  44690. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44691. res = TEST_RES_CHECK(1);
  44692. #endif
  44693. return res;
  44694. }
  44695. static int test_wc_SetIssuerRaw(void)
  44696. {
  44697. int res = TEST_SKIPPED;
  44698. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44699. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44700. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44701. WOLFSSL_X509* x509;
  44702. int peerCertSz;
  44703. const byte* peerCertBuf;
  44704. Cert forgedCert;
  44705. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44706. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44707. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44708. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44709. wolfSSL_FreeX509(x509);
  44710. res = TEST_RES_CHECK(1);
  44711. #endif
  44712. return res;
  44713. }
  44714. static int test_wc_SetIssueBuffer(void)
  44715. {
  44716. int res = TEST_SKIPPED;
  44717. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44718. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44719. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44720. WOLFSSL_X509* x509;
  44721. int peerCertSz;
  44722. const byte* peerCertBuf;
  44723. Cert forgedCert;
  44724. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  44725. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44726. AssertIntEQ(0, wc_InitCert(&forgedCert));
  44727. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44728. wolfSSL_FreeX509(x509);
  44729. res = TEST_RES_CHECK(1);
  44730. #endif
  44731. return res;
  44732. }
  44733. /*
  44734. * Testing wc_SetSubjectKeyId
  44735. */
  44736. static int test_wc_SetSubjectKeyId(void)
  44737. {
  44738. int res = TEST_SKIPPED;
  44739. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44740. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44741. Cert cert;
  44742. const char* file = "certs/ecc-client-keyPub.pem";
  44743. AssertIntEQ(0, wc_InitCert(&cert));
  44744. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44745. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44746. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44747. res = TEST_RES_CHECK(1);
  44748. #endif
  44749. return res;
  44750. } /* END test_wc_SetSubjectKeyId */
  44751. /*
  44752. * Testing wc_SetSubject
  44753. */
  44754. static int test_wc_SetSubject(void)
  44755. {
  44756. int res = TEST_SKIPPED;
  44757. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44758. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44759. Cert cert;
  44760. const char* file = "./certs/ca-ecc-cert.pem";
  44761. AssertIntEQ(0, wc_InitCert(&cert));
  44762. AssertIntEQ(0, wc_SetSubject(&cert, file));
  44763. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44764. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44765. res = TEST_RES_CHECK(1);
  44766. #endif
  44767. return res;
  44768. } /* END test_wc_SetSubject */
  44769. static int test_CheckCertSignature(void)
  44770. {
  44771. int res = TEST_SKIPPED;
  44772. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44773. WOLFSSL_CERT_MANAGER* cm = NULL;
  44774. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44775. FILE* fp;
  44776. byte cert[4096];
  44777. int certSz;
  44778. #endif
  44779. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44780. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44781. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44782. #ifndef NO_RSA
  44783. #ifdef USE_CERT_BUFFERS_1024
  44784. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44785. sizeof_server_cert_der_1024, NULL, cm));
  44786. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44787. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44788. WOLFSSL_FILETYPE_ASN1));
  44789. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44790. sizeof_server_cert_der_1024, NULL, cm));
  44791. #elif defined(USE_CERT_BUFFERS_2048)
  44792. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  44793. sizeof_server_cert_der_2048, NULL, cm));
  44794. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44795. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  44796. WOLFSSL_FILETYPE_ASN1));
  44797. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  44798. sizeof_server_cert_der_2048, NULL, cm));
  44799. #endif
  44800. #endif
  44801. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44802. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  44803. sizeof_serv_ecc_der_256, NULL, cm));
  44804. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44805. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  44806. WOLFSSL_FILETYPE_ASN1));
  44807. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  44808. NULL, cm));
  44809. #endif
  44810. #if !defined(NO_FILESYSTEM)
  44811. wolfSSL_CertManagerFree(cm);
  44812. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44813. #ifndef NO_RSA
  44814. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  44815. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44816. XFCLOSE(fp);
  44817. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44818. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44819. "./certs/ca-cert.pem", NULL));
  44820. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44821. #endif
  44822. #ifdef HAVE_ECC
  44823. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  44824. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44825. XFCLOSE(fp);
  44826. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44827. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44828. "./certs/ca-ecc-cert.pem", NULL));
  44829. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44830. #endif
  44831. #endif
  44832. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44833. (void)fp;
  44834. (void)cert;
  44835. (void)certSz;
  44836. #endif
  44837. wolfSSL_CertManagerFree(cm);
  44838. res = TEST_RES_CHECK(1);
  44839. #endif
  44840. return res;
  44841. }
  44842. static int test_wc_ParseCert(void)
  44843. {
  44844. int res = TEST_SKIPPED;
  44845. #if !defined(NO_CERTS) && !defined(NO_RSA)
  44846. DecodedCert decodedCert;
  44847. const byte* rawCert = client_cert_der_2048;
  44848. const int rawCertSize = sizeof_client_cert_der_2048;
  44849. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  44850. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44851. #ifndef IGNORE_NAME_CONSTRAINTS
  44852. /* check that the subjects emailAddress was not put in the alt name list */
  44853. AssertNotNull(decodedCert.subjectEmail);
  44854. AssertNull(decodedCert.altEmailNames);
  44855. #endif
  44856. wc_FreeDecodedCert(&decodedCert);
  44857. res = TEST_RES_CHECK(1);
  44858. #endif
  44859. return res;
  44860. }
  44861. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  44862. * the API safely errors out on badly-formed ASN input.
  44863. * NOTE: Test not compatible with released FIPS implementations!
  44864. */
  44865. static int test_wc_ParseCert_Error(void)
  44866. {
  44867. int res = TEST_SKIPPED;
  44868. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  44869. (!defined(HAVE_FIPS) || \
  44870. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  44871. DecodedCert decodedCert;
  44872. int i;
  44873. /* Certificate data */
  44874. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  44875. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44876. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44877. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  44878. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  44879. /* Test data */
  44880. const struct testStruct {
  44881. const byte* c;
  44882. const int cSz;
  44883. const int expRet;
  44884. } t[] = {
  44885. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  44886. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  44887. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  44888. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  44889. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  44890. };
  44891. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  44892. for (i = 0; i < tSz; i++) {
  44893. WOLFSSL_MSG_EX("i == %d", i);
  44894. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  44895. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  44896. wc_FreeDecodedCert(&decodedCert);
  44897. }
  44898. res = TEST_RES_CHECK(1);
  44899. #endif
  44900. return res;
  44901. }
  44902. static int test_MakeCertWithPathLen(void)
  44903. {
  44904. int res = TEST_SKIPPED;
  44905. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  44906. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44907. const byte expectedPathLen = 7;
  44908. Cert cert;
  44909. DecodedCert decodedCert;
  44910. byte der[FOURK_BUF];
  44911. int derSize = 0;
  44912. WC_RNG rng;
  44913. ecc_key key;
  44914. AssertIntEQ(wc_InitRng(&rng), 0);
  44915. AssertIntEQ(wc_ecc_init(&key), 0);
  44916. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44917. AssertIntEQ(wc_InitCert(&cert), 0);
  44918. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44919. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44920. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44921. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44922. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44923. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  44924. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  44925. cert.selfSigned = 1;
  44926. cert.isCA = 1;
  44927. cert.pathLen = expectedPathLen;
  44928. cert.pathLenSet = 1;
  44929. cert.sigType = CTC_SHA256wECDSA;
  44930. #ifdef WOLFSSL_CERT_EXT
  44931. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  44932. #endif
  44933. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44934. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  44935. &key, &rng);
  44936. AssertIntGE(derSize, 0);
  44937. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44938. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44939. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  44940. wc_FreeDecodedCert(&decodedCert);
  44941. AssertIntEQ(wc_ecc_free(&key), 0);
  44942. AssertIntEQ(wc_FreeRng(&rng), 0);
  44943. res = TEST_RES_CHECK(1);
  44944. #endif
  44945. return res;
  44946. }
  44947. /*----------------------------------------------------------------------------*
  44948. | wolfCrypt ECC
  44949. *----------------------------------------------------------------------------*/
  44950. static int test_wc_ecc_get_curve_size_from_name(void)
  44951. {
  44952. int res = TEST_SKIPPED;
  44953. #ifdef HAVE_ECC
  44954. int ret;
  44955. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44956. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  44957. AssertIntEQ(ret, 32);
  44958. #endif
  44959. /* invalid case */
  44960. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  44961. AssertIntEQ(ret, -1);
  44962. /* NULL input */
  44963. ret = wc_ecc_get_curve_size_from_name(NULL);
  44964. AssertIntEQ(ret, BAD_FUNC_ARG);
  44965. res = TEST_RES_CHECK(1);
  44966. #endif /* HAVE_ECC */
  44967. return res;
  44968. }
  44969. static int test_wc_ecc_get_curve_id_from_name(void)
  44970. {
  44971. int res = TEST_SKIPPED;
  44972. #ifdef HAVE_ECC
  44973. int id;
  44974. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44975. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  44976. AssertIntEQ(id, ECC_SECP256R1);
  44977. #endif
  44978. /* invalid case */
  44979. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  44980. AssertIntEQ(id, -1);
  44981. /* NULL input */
  44982. id = wc_ecc_get_curve_id_from_name(NULL);
  44983. AssertIntEQ(id, BAD_FUNC_ARG);
  44984. res = TEST_RES_CHECK(1);
  44985. #endif /* HAVE_ECC */
  44986. return res;
  44987. }
  44988. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44989. !defined(HAVE_SELFTEST) && \
  44990. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44991. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  44992. {
  44993. int id;
  44994. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44995. int curve_id;
  44996. ecc_key* key;
  44997. const ecc_set_type* params;
  44998. int ret;
  44999. #endif
  45000. WOLFSSL_EC_KEY *ecKey = NULL;
  45001. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45002. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  45003. AssertIntEQ(id, ECC_SECP256R1);
  45004. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  45005. AssertNotNull(ecKey);
  45006. ret = EC_KEY_generate_key(ecKey);
  45007. if (ret == 0) {
  45008. /* normal test */
  45009. key = (ecc_key*)ecKey->internal;
  45010. params = key->dp;
  45011. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  45012. AssertIntEQ(curve_id, id);
  45013. }
  45014. #endif
  45015. /* invalid case, NULL input*/
  45016. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  45017. AssertIntEQ(id, BAD_FUNC_ARG);
  45018. wolfSSL_EC_KEY_free(ecKey);
  45019. return TEST_RES_CHECK(1);
  45020. }
  45021. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45022. static int test_wc_ecc_get_curve_id_from_params(void)
  45023. {
  45024. int res = TEST_SKIPPED;
  45025. #ifdef HAVE_ECC
  45026. int id;
  45027. const byte prime[] =
  45028. {
  45029. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45030. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45031. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45032. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  45033. };
  45034. const byte primeInvalid[] =
  45035. {
  45036. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45038. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45039. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  45040. };
  45041. const byte Af[] =
  45042. {
  45043. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45045. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45046. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  45047. };
  45048. const byte Bf[] =
  45049. {
  45050. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  45051. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  45052. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  45053. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  45054. };
  45055. const byte order[] =
  45056. {
  45057. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  45058. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45059. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  45060. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  45061. };
  45062. const byte Gx[] =
  45063. {
  45064. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  45065. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  45066. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  45067. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  45068. };
  45069. const byte Gy[] =
  45070. {
  45071. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  45072. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  45073. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  45074. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  45075. };
  45076. int cofactor = 1;
  45077. int fieldSize = 256;
  45078. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45079. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  45080. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45081. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45082. AssertIntEQ(id, ECC_SECP256R1);
  45083. #endif
  45084. /* invalid case, fieldSize = 0 */
  45085. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  45086. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45087. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45088. AssertIntEQ(id, ECC_CURVE_INVALID);
  45089. /* invalid case, NULL prime */
  45090. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  45091. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45092. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45093. AssertIntEQ(id, BAD_FUNC_ARG);
  45094. /* invalid case, invalid prime */
  45095. id = wc_ecc_get_curve_id_from_params(fieldSize,
  45096. primeInvalid, sizeof(primeInvalid),
  45097. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45098. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  45099. AssertIntEQ(id, ECC_CURVE_INVALID);
  45100. res = TEST_RES_CHECK(1);
  45101. #endif
  45102. return res;
  45103. }
  45104. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45105. {
  45106. int res = TEST_SKIPPED;
  45107. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45108. !defined(HAVE_FAST_RSA)
  45109. WOLFSSL_RSA* rsa = NULL;
  45110. WOLFSSL_EVP_PKEY* pkey = NULL;
  45111. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45112. const char* in = "What is easy to do is easy not to do.";
  45113. size_t inlen = XSTRLEN(in);
  45114. size_t outEncLen = 0;
  45115. byte* outEnc = NULL;
  45116. byte* outDec = NULL;
  45117. size_t outDecLen = 0;
  45118. size_t rsaKeySz = 2048/8; /* Bytes */
  45119. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45120. byte* inTmp = NULL;
  45121. byte* outEncTmp = NULL;
  45122. byte* outDecTmp = NULL;
  45123. #endif
  45124. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45125. XMEMSET(outEnc, 0, rsaKeySz);
  45126. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45127. XMEMSET(outDec, 0, rsaKeySz);
  45128. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45129. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45130. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45131. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45132. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45133. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45134. WOLFSSL_SUCCESS);
  45135. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45136. since ctx uses it.*/
  45137. AssertIntEQ(pkey->ref.count, 2);
  45138. EVP_PKEY_free(pkey);
  45139. AssertIntEQ(pkey->ref.count, 1);
  45140. /* Encrypt data */
  45141. /* Check that we can get the required output buffer length by passing in a
  45142. * NULL output buffer. */
  45143. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45144. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45145. AssertIntEQ(rsaKeySz, outEncLen);
  45146. /* Now do the actual encryption. */
  45147. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45148. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45149. /* Decrypt data */
  45150. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45151. /* Check that we can get the required output buffer length by passing in a
  45152. * NULL output buffer. */
  45153. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45154. WOLFSSL_SUCCESS);
  45155. AssertIntEQ(rsaKeySz, outDecLen);
  45156. /* Now do the actual decryption. */
  45157. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45158. WOLFSSL_SUCCESS);
  45159. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45160. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45161. /* The input length must be the same size as the RSA key.*/
  45162. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45163. XMEMSET(inTmp, 9, rsaKeySz);
  45164. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45165. XMEMSET(outEncTmp, 0, rsaKeySz);
  45166. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45167. XMEMSET(outDecTmp, 0, rsaKeySz);
  45168. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45169. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45170. WOLFSSL_SUCCESS);
  45171. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45172. WOLFSSL_SUCCESS);
  45173. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45174. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  45175. WOLFSSL_SUCCESS);
  45176. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45177. #endif
  45178. EVP_PKEY_CTX_free(ctx);
  45179. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45180. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45181. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45182. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45183. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45184. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45185. #endif
  45186. res = TEST_RES_CHECK(1);
  45187. #endif
  45188. return res;
  45189. }
  45190. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  45191. {
  45192. int res = TEST_SKIPPED;
  45193. #if defined(OPENSSL_EXTRA)
  45194. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45195. WOLFSSL_DSA* dsa = NULL;
  45196. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45197. WOLFSSL_EVP_PKEY* pkey = NULL;
  45198. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45199. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  45200. const char* in = "What is easy to do is easy not to do.";
  45201. size_t inlen = XSTRLEN(in);
  45202. byte hash[SHA256_DIGEST_LENGTH] = {0};
  45203. byte zero[SHA256_DIGEST_LENGTH] = {0};
  45204. SHA256_CTX c;
  45205. byte* sig = NULL;
  45206. byte* sigVerify = NULL;
  45207. size_t siglen;
  45208. size_t siglenOnlyLen;
  45209. size_t keySz = 2048/8; /* Bytes */
  45210. int i;
  45211. int encs[3] = {0};
  45212. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45213. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45214. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45215. encs[0] = EVP_PKEY_RSA;
  45216. #endif
  45217. #endif
  45218. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45219. encs[1] = EVP_PKEY_DSA;
  45220. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45221. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45222. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45223. encs[2] = EVP_PKEY_EC;
  45224. #endif
  45225. #endif
  45226. AssertNotNull(sig =
  45227. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45228. AssertNotNull(sigVerify =
  45229. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45230. for (i = 0; i < 3; i++) {
  45231. if (encs[i] == 0)
  45232. continue;
  45233. siglen = keySz;
  45234. XMEMSET(sig, 0, keySz);
  45235. XMEMSET(sigVerify, 0, keySz);
  45236. /* Generate hash */
  45237. SHA256_Init(&c);
  45238. SHA256_Update(&c, in, inlen);
  45239. SHA256_Final(hash, &c);
  45240. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45241. /* workaround for small stack cache case */
  45242. wc_Sha256Free((wc_Sha256*)&c);
  45243. #endif
  45244. /* Generate key */
  45245. AssertNotNull(pkey = EVP_PKEY_new());
  45246. switch (encs[i]) {
  45247. case EVP_PKEY_RSA:
  45248. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45249. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45250. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45251. {
  45252. WOLFSSL_RSA* rsa = NULL;
  45253. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45254. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45255. }
  45256. #endif
  45257. #endif
  45258. break;
  45259. case EVP_PKEY_DSA:
  45260. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45261. AssertNotNull(dsa = DSA_new());
  45262. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  45263. NULL, 0, NULL, NULL, NULL), 1);
  45264. AssertIntEQ(DSA_generate_key(dsa), 1);
  45265. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  45266. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45267. break;
  45268. case EVP_PKEY_EC:
  45269. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45270. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45271. {
  45272. WOLFSSL_EC_KEY* ecKey = NULL;
  45273. AssertNotNull(ecKey = EC_KEY_new());
  45274. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  45275. AssertIntEQ(
  45276. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45277. }
  45278. #endif
  45279. #endif
  45280. break;
  45281. }
  45282. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45283. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45284. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45285. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45286. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45287. if (encs[i] == EVP_PKEY_RSA)
  45288. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45289. WOLFSSL_SUCCESS);
  45290. #endif
  45291. #endif
  45292. /* Check returning only length */
  45293. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  45294. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45295. AssertIntGT(siglenOnlyLen, 0);
  45296. /* Sign data */
  45297. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  45298. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45299. AssertIntGE(siglenOnlyLen, siglen);
  45300. /* Verify signature */
  45301. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  45302. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45303. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45304. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45305. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45306. if (encs[i] == EVP_PKEY_RSA)
  45307. AssertIntEQ(
  45308. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  45309. WOLFSSL_SUCCESS);
  45310. #endif
  45311. #endif
  45312. AssertIntEQ(EVP_PKEY_verify(
  45313. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  45314. WOLFSSL_SUCCESS);
  45315. AssertIntEQ(EVP_PKEY_verify(
  45316. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  45317. WOLFSSL_FAILURE);
  45318. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45319. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45320. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45321. if (encs[i] == EVP_PKEY_RSA) {
  45322. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  45323. /* Try RSA sign/verify with no padding. */
  45324. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45325. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45326. WOLFSSL_SUCCESS);
  45327. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45328. siglen), WOLFSSL_SUCCESS);
  45329. AssertIntGE(siglenOnlyLen, siglen);
  45330. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45331. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45332. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  45333. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45334. siglen), WOLFSSL_SUCCESS);
  45335. #endif
  45336. /* Wrong padding schemes. */
  45337. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45338. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  45339. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45340. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45341. siglen), WOLFSSL_SUCCESS);
  45342. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45343. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45344. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45345. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45346. siglen), WOLFSSL_SUCCESS);
  45347. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45348. WOLFSSL_SUCCESS);
  45349. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45350. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  45351. }
  45352. #endif
  45353. #endif
  45354. /* error cases */
  45355. siglen = keySz; /* Reset because sig size may vary slightly */
  45356. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  45357. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45358. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  45359. WOLFSSL_SUCCESS);
  45360. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  45361. WOLFSSL_SUCCESS);
  45362. EVP_PKEY_free(pkey);
  45363. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45364. DSA_free(dsa);
  45365. dsa = NULL;
  45366. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45367. EVP_PKEY_CTX_free(ctx_verify);
  45368. EVP_PKEY_CTX_free(ctx);
  45369. }
  45370. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45371. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45372. res = TEST_RES_CHECK(1);
  45373. #endif /* OPENSSL_EXTRA */
  45374. return res;
  45375. }
  45376. static int test_EVP_PKEY_rsa(void)
  45377. {
  45378. int res = TEST_SKIPPED;
  45379. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45380. WOLFSSL_RSA* rsa;
  45381. WOLFSSL_EVP_PKEY* pkey;
  45382. AssertNotNull(rsa = wolfSSL_RSA_new());
  45383. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45384. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  45385. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  45386. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45387. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  45388. wolfSSL_EVP_PKEY_free(pkey);
  45389. res = TEST_RES_CHECK(1);
  45390. #endif
  45391. return res;
  45392. }
  45393. static int test_EVP_PKEY_ec(void)
  45394. {
  45395. int res = TEST_SKIPPED;
  45396. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45397. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45398. WOLFSSL_EC_KEY* ecKey;
  45399. WOLFSSL_EVP_PKEY* pkey;
  45400. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  45401. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45402. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45403. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45404. /* Should fail since ecKey is empty */
  45405. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45406. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45407. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45408. wolfSSL_EVP_PKEY_free(pkey);
  45409. res = TEST_RES_CHECK(1);
  45410. #endif
  45411. #endif
  45412. return res;
  45413. }
  45414. static int test_EVP_PKEY_cmp(void)
  45415. {
  45416. int res = TEST_SKIPPED;
  45417. #if defined(OPENSSL_EXTRA)
  45418. EVP_PKEY *a, *b;
  45419. const unsigned char *in;
  45420. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45421. in = client_key_der_2048;
  45422. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45423. &in, (long)sizeof_client_key_der_2048));
  45424. in = client_key_der_2048;
  45425. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45426. &in, (long)sizeof_client_key_der_2048));
  45427. /* Test success case RSA */
  45428. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45429. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  45430. #else
  45431. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45432. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45433. EVP_PKEY_free(b);
  45434. EVP_PKEY_free(a);
  45435. #endif
  45436. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45437. in = ecc_clikey_der_256;
  45438. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45439. &in, (long)sizeof_ecc_clikey_der_256));
  45440. in = ecc_clikey_der_256;
  45441. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45442. &in, (long)sizeof_ecc_clikey_der_256));
  45443. /* Test success case ECC */
  45444. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45445. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  45446. #else
  45447. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45448. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45449. EVP_PKEY_free(b);
  45450. EVP_PKEY_free(a);
  45451. #endif
  45452. /* Test failure cases */
  45453. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  45454. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45455. in = client_key_der_2048;
  45456. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45457. &in, (long)sizeof_client_key_der_2048));
  45458. in = ecc_clikey_der_256;
  45459. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45460. &in, (long)sizeof_ecc_clikey_der_256));
  45461. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45462. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  45463. #else
  45464. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  45465. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45466. EVP_PKEY_free(b);
  45467. EVP_PKEY_free(a);
  45468. #endif
  45469. /* invalid or empty failure cases */
  45470. a = EVP_PKEY_new();
  45471. b = EVP_PKEY_new();
  45472. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45473. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45474. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45475. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45476. #ifdef NO_RSA
  45477. /* Type check will fail since RSA is the default EVP key type */
  45478. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  45479. #else
  45480. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  45481. #endif
  45482. #else
  45483. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45484. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45485. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45486. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  45487. #endif
  45488. EVP_PKEY_free(b);
  45489. EVP_PKEY_free(a);
  45490. (void)in;
  45491. res = TEST_RES_CHECK(1);
  45492. #endif
  45493. return res;
  45494. }
  45495. static int test_ERR_load_crypto_strings(void)
  45496. {
  45497. int res = TEST_SKIPPED;
  45498. #if defined(OPENSSL_ALL)
  45499. ERR_load_crypto_strings();
  45500. res = TEST_RES_CHECK(1);
  45501. #endif
  45502. return res;
  45503. }
  45504. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45505. static void free_x509(X509* x)
  45506. {
  45507. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45508. }
  45509. #endif
  45510. static int test_sk_X509(void)
  45511. {
  45512. int res = TEST_SKIPPED;
  45513. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45514. {
  45515. STACK_OF(X509)* s;
  45516. AssertNotNull(s = sk_X509_new_null());
  45517. AssertIntEQ(sk_X509_num(s), 0);
  45518. sk_X509_pop_free(s, NULL);
  45519. AssertNotNull(s = sk_X509_new_null());
  45520. AssertIntEQ(sk_X509_num(s), 0);
  45521. sk_X509_pop_free(s, NULL);
  45522. AssertNotNull(s = sk_X509_new_null());
  45523. sk_X509_push(s, (X509*)1);
  45524. AssertIntEQ(sk_X509_num(s), 1);
  45525. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45526. sk_X509_push(s, (X509*)2);
  45527. AssertIntEQ(sk_X509_num(s), 2);
  45528. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45529. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45530. sk_X509_push(s, (X509*)2);
  45531. sk_X509_pop_free(s, free_x509);
  45532. }
  45533. {
  45534. /* Push a list of 10 X509s onto stack, then verify that
  45535. * value(), push(), shift(), and pop() behave as expected. */
  45536. STACK_OF(X509)* s;
  45537. X509* xList[10];
  45538. int i = 0;
  45539. const int len = (sizeof(xList) / sizeof(xList[0]));
  45540. for (i = 0; i < len; ++i)
  45541. AssertNotNull(xList[i] = X509_new());
  45542. /* test push, pop, and free */
  45543. AssertNotNull(s = sk_X509_new_null());
  45544. for (i = 0; i < len; ++i) {
  45545. sk_X509_push(s, xList[i]);
  45546. AssertIntEQ(sk_X509_num(s), i + 1);
  45547. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45548. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45549. }
  45550. /* pop returns and removes last pushed on stack, which is index 0
  45551. * in sk_x509_value */
  45552. for (i = 0; i < len; ++i) {
  45553. X509 * x = sk_X509_value(s, 0);
  45554. X509 * y = sk_X509_pop(s);
  45555. X509 * z = xList[len - 1 - i];
  45556. AssertIntEQ((x == y), 1);
  45557. AssertIntEQ((x == z), 1);
  45558. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  45559. }
  45560. sk_free(s);
  45561. /* test push, shift, and free */
  45562. AssertNotNull(s = sk_X509_new_null());
  45563. for (i = 0; i < len; ++i) {
  45564. sk_X509_push(s, xList[i]);
  45565. AssertIntEQ(sk_X509_num(s), i + 1);
  45566. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45567. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45568. }
  45569. /* shift returns and removes first pushed on stack, which is index i
  45570. * in sk_x509_value() */
  45571. for (i = 0; i < len; ++i) {
  45572. X509 * x = sk_X509_value(s, len - 1 - i);
  45573. X509 * y = sk_X509_shift(s);
  45574. X509 * z = xList[i];
  45575. AssertIntEQ((x == y), 1);
  45576. AssertIntEQ((x == z), 1);
  45577. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  45578. }
  45579. sk_free(s);
  45580. for (i = 0; i < len; ++i)
  45581. X509_free(xList[i]);
  45582. }
  45583. res = TEST_RES_CHECK(1);
  45584. #endif
  45585. return res;
  45586. }
  45587. static int test_sk_X509_CRL(void)
  45588. {
  45589. int res = TEST_SKIPPED;
  45590. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45591. X509_CRL* crl;
  45592. XFILE fp;
  45593. STACK_OF(X509_CRL)* s;
  45594. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  45595. AssertTrue((fp != XBADFILE));
  45596. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  45597. XFCLOSE(fp);
  45598. AssertNotNull(s = sk_X509_CRL_new());
  45599. AssertIntEQ(sk_X509_CRL_num(s), 0);
  45600. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  45601. AssertIntEQ(sk_X509_CRL_num(s), 1);
  45602. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  45603. sk_X509_CRL_free(s);
  45604. res = TEST_RES_CHECK(1);
  45605. #endif
  45606. return res;
  45607. }
  45608. static int test_X509_get_signature_nid(void)
  45609. {
  45610. int res = TEST_SKIPPED;
  45611. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45612. X509* x509;
  45613. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  45614. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45615. SSL_FILETYPE_PEM));
  45616. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45617. X509_free(x509);
  45618. res = TEST_RES_CHECK(1);
  45619. #endif
  45620. return res;
  45621. }
  45622. static int test_X509_REQ(void)
  45623. {
  45624. int res = TEST_SKIPPED;
  45625. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45626. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45627. X509_NAME* name;
  45628. #ifndef NO_RSA
  45629. X509_NAME* subject;
  45630. #endif
  45631. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45632. X509_REQ* req;
  45633. EVP_PKEY* priv;
  45634. EVP_PKEY* pub;
  45635. unsigned char* der = NULL;
  45636. int len;
  45637. #endif
  45638. #ifndef NO_RSA
  45639. EVP_MD_CTX *mctx = NULL;
  45640. EVP_PKEY_CTX *pkctx = NULL;
  45641. #ifdef USE_CERT_BUFFERS_1024
  45642. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45643. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45644. #elif defined(USE_CERT_BUFFERS_2048)
  45645. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45646. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45647. #endif
  45648. #endif
  45649. #ifdef HAVE_ECC
  45650. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45651. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45652. #endif
  45653. AssertNotNull(name = X509_NAME_new());
  45654. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45655. (byte*)"wolfssl.com", 11, 0, 1),
  45656. WOLFSSL_SUCCESS);
  45657. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45658. (byte*)"support@wolfssl.com", 19, -1,
  45659. 1), WOLFSSL_SUCCESS);
  45660. #ifndef NO_RSA
  45661. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45662. (long)sizeof_client_key_der_2048));
  45663. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45664. (long)sizeof_client_keypub_der_2048));
  45665. AssertNotNull(req = X509_REQ_new());
  45666. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45667. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  45668. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45669. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  45670. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  45671. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45672. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  45673. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45674. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  45675. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45676. len = i2d_X509_REQ(req, &der);
  45677. DEBUG_WRITE_DER(der, len, "req.der");
  45678. #ifdef USE_CERT_BUFFERS_1024
  45679. AssertIntEQ(len, 381);
  45680. #else
  45681. AssertIntEQ(len, 643);
  45682. #endif
  45683. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45684. der = NULL;
  45685. mctx = EVP_MD_CTX_new();
  45686. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  45687. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  45688. EVP_MD_CTX_free(mctx);
  45689. X509_REQ_free(NULL);
  45690. X509_REQ_free(req);
  45691. /* Test getting the subject from a newly created X509_REQ */
  45692. AssertNotNull(req = X509_REQ_new());
  45693. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  45694. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  45695. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  45696. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  45697. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  45698. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  45699. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  45700. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  45701. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  45702. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  45703. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  45704. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  45705. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  45706. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45707. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45708. len = i2d_X509_REQ(req, &der);
  45709. DEBUG_WRITE_DER(der, len, "req2.der");
  45710. #ifdef USE_CERT_BUFFERS_1024
  45711. AssertIntEQ(len, 435);
  45712. #else
  45713. AssertIntEQ(len, 696);
  45714. #endif
  45715. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45716. der = NULL;
  45717. EVP_PKEY_free(pub);
  45718. EVP_PKEY_free(priv);
  45719. X509_REQ_free(req);
  45720. #endif
  45721. #ifdef HAVE_ECC
  45722. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  45723. sizeof_ecc_clikey_der_256));
  45724. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  45725. sizeof_ecc_clikeypub_der_256));
  45726. AssertNotNull(req = X509_REQ_new());
  45727. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45728. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45729. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45730. /* Signature is random and may be shorter or longer. */
  45731. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  45732. AssertIntLE(len, 253);
  45733. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45734. X509_REQ_free(req);
  45735. EVP_PKEY_free(pub);
  45736. EVP_PKEY_free(priv);
  45737. #ifdef FP_ECC
  45738. wc_ecc_fp_free();
  45739. #endif
  45740. #endif /* HAVE_ECC */
  45741. X509_NAME_free(name);
  45742. res = TEST_RES_CHECK(1);
  45743. #endif
  45744. return res;
  45745. }
  45746. static int test_wolfssl_PKCS7(void)
  45747. {
  45748. int res = TEST_SKIPPED;
  45749. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45750. !defined(NO_RSA)
  45751. PKCS7* pkcs7;
  45752. byte data[FOURK_BUF];
  45753. word32 len = sizeof(data);
  45754. const byte* p = data;
  45755. byte content[] = "Test data to encode.";
  45756. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45757. BIO* bio;
  45758. byte key[sizeof(client_key_der_2048)];
  45759. word32 keySz = (word32)sizeof(key);
  45760. byte* out = NULL;
  45761. #endif
  45762. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  45763. (word32)sizeof(content),
  45764. 0, 0, 0, RSA_TYPE)), 0);
  45765. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  45766. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  45767. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45768. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  45769. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  45770. PKCS7_free(pkcs7);
  45771. /* fail case, without PKCS7_NOVERIFY */
  45772. p = data;
  45773. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45774. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45775. 0), WOLFSSL_FAILURE);
  45776. PKCS7_free(pkcs7);
  45777. /* success case, with PKCS7_NOVERIFY */
  45778. p = data;
  45779. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45780. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45781. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  45782. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45783. /* test i2d */
  45784. XMEMCPY(key, client_key_der_2048, keySz);
  45785. pkcs7->privateKey = key;
  45786. pkcs7->privateKeySz = (word32)sizeof(key);
  45787. pkcs7->encryptOID = RSAk;
  45788. #ifdef NO_SHA
  45789. pkcs7->hashOID = SHA256h;
  45790. #else
  45791. pkcs7->hashOID = SHAh;
  45792. #endif
  45793. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45794. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  45795. #ifndef NO_ASN_TIME
  45796. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  45797. #else
  45798. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  45799. #endif
  45800. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45801. BIO_free(bio);
  45802. #endif
  45803. PKCS7_free(NULL);
  45804. PKCS7_free(pkcs7);
  45805. res = TEST_RES_CHECK(1);
  45806. #endif
  45807. return res;
  45808. }
  45809. static int test_wolfSSL_PKCS7_sign(void)
  45810. {
  45811. int res = TEST_SKIPPED;
  45812. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45813. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45814. PKCS7* p7 = NULL;
  45815. PKCS7* p7Ver = NULL;
  45816. byte* out = NULL;
  45817. byte* tmpPtr = NULL;
  45818. int outLen = 0;
  45819. int flags = 0;
  45820. byte data[] = "Test data to encode.";
  45821. const char* cert = "./certs/server-cert.pem";
  45822. const char* key = "./certs/server-key.pem";
  45823. const char* ca = "./certs/ca-cert.pem";
  45824. WOLFSSL_BIO* certBio = NULL;
  45825. WOLFSSL_BIO* keyBio = NULL;
  45826. WOLFSSL_BIO* caBio = NULL;
  45827. WOLFSSL_BIO* inBio = NULL;
  45828. X509* signCert = NULL;
  45829. EVP_PKEY* signKey = NULL;
  45830. X509* caCert = NULL;
  45831. X509_STORE* store = NULL;
  45832. /* read signer cert/key into BIO */
  45833. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  45834. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  45835. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45836. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45837. /* read CA cert into store (for verify) */
  45838. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  45839. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45840. AssertNotNull(store = X509_STORE_new());
  45841. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45842. /* data to be signed into BIO */
  45843. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45844. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45845. /* PKCS7_sign, bad args: signer NULL */
  45846. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  45847. /* PKCS7_sign, bad args: signer key NULL */
  45848. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  45849. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  45850. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  45851. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  45852. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  45853. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  45854. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  45855. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  45856. {
  45857. flags = PKCS7_BINARY;
  45858. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45859. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45860. /* verify with d2i_PKCS7 */
  45861. tmpPtr = out;
  45862. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45863. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45864. PKCS7_free(p7Ver);
  45865. /* verify with wc_PKCS7_VerifySignedData */
  45866. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45867. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  45868. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45869. /* compare the signer found to expected signer */
  45870. AssertIntNE(p7Ver->verifyCertSz, 0);
  45871. tmpPtr = NULL;
  45872. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  45873. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  45874. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  45875. tmpPtr = NULL;
  45876. wc_PKCS7_Free(p7Ver);
  45877. AssertNotNull(out);
  45878. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45879. out = NULL;
  45880. PKCS7_free(p7);
  45881. }
  45882. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  45883. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  45884. {
  45885. /* re-populate input BIO, may have been consumed */
  45886. BIO_free(inBio);
  45887. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45888. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45889. flags = PKCS7_BINARY | PKCS7_STREAM;
  45890. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45891. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45892. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45893. /* PKCS7_final, bad args: PKCS7 null */
  45894. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  45895. /* PKCS7_final, bad args: PKCS7 null */
  45896. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  45897. tmpPtr = out;
  45898. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45899. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45900. PKCS7_free(p7Ver);
  45901. AssertNotNull(out);
  45902. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45903. out = NULL;
  45904. PKCS7_free(p7);
  45905. }
  45906. /* TEST SUCCESS: Detached, not streaming, not MIME */
  45907. {
  45908. /* re-populate input BIO, may have been consumed */
  45909. BIO_free(inBio);
  45910. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45911. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45912. flags = PKCS7_BINARY | PKCS7_DETACHED;
  45913. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45914. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45915. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45916. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45917. p7Ver->content = data;
  45918. p7Ver->contentSz = sizeof(data);
  45919. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45920. wc_PKCS7_Free(p7Ver);
  45921. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  45922. * yet support detached content */
  45923. tmpPtr = out;
  45924. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45925. PKCS7_free(p7Ver);
  45926. AssertNotNull(out);
  45927. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45928. out = NULL;
  45929. PKCS7_free(p7);
  45930. }
  45931. /* TEST SUCCESS: Detached, streaming, not MIME */
  45932. {
  45933. /* re-populate input BIO, may have been consumed */
  45934. BIO_free(inBio);
  45935. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  45936. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45937. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  45938. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45939. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45940. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45941. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45942. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45943. p7Ver->content = data;
  45944. p7Ver->contentSz = sizeof(data);
  45945. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45946. wc_PKCS7_Free(p7Ver);
  45947. AssertNotNull(out);
  45948. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45949. PKCS7_free(p7);
  45950. }
  45951. X509_STORE_free(store);
  45952. X509_free(caCert);
  45953. X509_free(signCert);
  45954. EVP_PKEY_free(signKey);
  45955. BIO_free(inBio);
  45956. BIO_free(keyBio);
  45957. BIO_free(certBio);
  45958. BIO_free(caBio);
  45959. res = TEST_RES_CHECK(1);
  45960. #endif
  45961. return res;
  45962. }
  45963. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  45964. {
  45965. int res = TEST_SKIPPED;
  45966. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  45967. PKCS7_SIGNED* pkcs7;
  45968. pkcs7 = PKCS7_SIGNED_new();
  45969. AssertNotNull(pkcs7);
  45970. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  45971. PKCS7_SIGNED_free(pkcs7);
  45972. res = TEST_RES_CHECK(1);
  45973. #endif
  45974. return res;
  45975. }
  45976. #ifndef NO_BIO
  45977. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  45978. {
  45979. int res = TEST_SKIPPED;
  45980. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  45981. PKCS7* pkcs7 = NULL;
  45982. BIO* bio = NULL;
  45983. const byte* cert_buf = NULL;
  45984. int ret = 0;
  45985. WC_RNG rng;
  45986. const byte data[] = { /* Hello World */
  45987. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45988. 0x72,0x6c,0x64
  45989. };
  45990. #ifndef NO_RSA
  45991. #if defined(USE_CERT_BUFFERS_2048)
  45992. byte key[sizeof(client_key_der_2048)];
  45993. byte cert[sizeof(client_cert_der_2048)];
  45994. word32 keySz = (word32)sizeof(key);
  45995. word32 certSz = (word32)sizeof(cert);
  45996. XMEMSET(key, 0, keySz);
  45997. XMEMSET(cert, 0, certSz);
  45998. XMEMCPY(key, client_key_der_2048, keySz);
  45999. XMEMCPY(cert, client_cert_der_2048, certSz);
  46000. #elif defined(USE_CERT_BUFFERS_1024)
  46001. byte key[sizeof_client_key_der_1024];
  46002. byte cert[sizeof(sizeof_client_cert_der_1024)];
  46003. word32 keySz = (word32)sizeof(key);
  46004. word32 certSz = (word32)sizeof(cert);
  46005. XMEMSET(key, 0, keySz);
  46006. XMEMSET(cert, 0, certSz);
  46007. XMEMCPY(key, client_key_der_1024, keySz);
  46008. XMEMCPY(cert, client_cert_der_1024, certSz);
  46009. #else
  46010. unsigned char cert[ONEK_BUF];
  46011. unsigned char key[ONEK_BUF];
  46012. XFILE fp;
  46013. int certSz;
  46014. int keySz;
  46015. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  46016. AssertTrue((fp != XBADFILE));
  46017. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  46018. XFCLOSE(fp);
  46019. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  46020. AssertTrue(fp != XBADFILE);
  46021. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  46022. XFCLOSE(fp);
  46023. #endif
  46024. #elif defined(HAVE_ECC)
  46025. #if defined(USE_CERT_BUFFERS_256)
  46026. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46027. unsigned char key[sizeof(ecc_clikey_der_256)];
  46028. int certSz = (int)sizeof(cert);
  46029. int keySz = (int)sizeof(key);
  46030. XMEMSET(cert, 0, certSz);
  46031. XMEMSET(key, 0, keySz);
  46032. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  46033. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  46034. #else
  46035. unsigned char cert[ONEK_BUF];
  46036. unsigned char key[ONEK_BUF];
  46037. XFILE fp;
  46038. int certSz, keySz;
  46039. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  46040. AssertTrue(fp != XBADFILE);
  46041. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  46042. XFCLOSE(fp);
  46043. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  46044. AssertTrue(fp != XBADFILE);
  46045. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  46046. XFCLOSE(fp);
  46047. #endif
  46048. #else
  46049. #error PKCS7 requires ECC or RSA
  46050. #endif
  46051. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  46052. /* initialize with DER encoded cert */
  46053. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  46054. /* init rng */
  46055. AssertIntEQ(wc_InitRng(&rng), 0);
  46056. pkcs7->rng = &rng;
  46057. pkcs7->content = (byte*)data; /* not used for ex */
  46058. pkcs7->contentSz = (word32)sizeof(data);
  46059. pkcs7->contentOID = SIGNED_DATA;
  46060. pkcs7->privateKey = key;
  46061. pkcs7->privateKeySz = (word32)sizeof(key);
  46062. pkcs7->encryptOID = RSAk;
  46063. #ifdef NO_SHA
  46064. pkcs7->hashOID = SHA256h;
  46065. #else
  46066. pkcs7->hashOID = SHAh;
  46067. #endif
  46068. pkcs7->signedAttribs = NULL;
  46069. pkcs7->signedAttribsSz = 0;
  46070. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  46071. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  46072. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  46073. /* Read PKCS#7 PEM from BIO */
  46074. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  46075. AssertIntGE(ret, 0);
  46076. BIO_free(bio);
  46077. wc_PKCS7_Free(pkcs7);
  46078. wc_FreeRng(&rng);
  46079. res = TEST_RES_CHECK(1);
  46080. #endif
  46081. return res;
  46082. }
  46083. #ifdef HAVE_SMIME
  46084. static int test_wolfSSL_SMIME_read_PKCS7(void)
  46085. {
  46086. int res = TEST_SKIPPED;
  46087. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  46088. !defined(NO_RSA)
  46089. PKCS7* pkcs7 = NULL;
  46090. BIO* bio = NULL;
  46091. BIO* bcont = NULL;
  46092. BIO* out = NULL;
  46093. const byte* outBuf = NULL;
  46094. int outBufLen = 0;
  46095. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  46096. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  46097. /* smime-test.p7s */
  46098. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  46099. AssertNotNull(bio);
  46100. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46101. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46102. AssertNotNull(pkcs7);
  46103. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46104. PKCS7_NOVERIFY), SSL_SUCCESS);
  46105. XFCLOSE(smimeTestFile);
  46106. if (bcont) BIO_free(bcont);
  46107. wolfSSL_PKCS7_free(pkcs7);
  46108. /* smime-test-multipart.p7s */
  46109. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  46110. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46111. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46112. AssertNotNull(pkcs7);
  46113. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46114. PKCS7_NOVERIFY), SSL_SUCCESS);
  46115. XFCLOSE(smimeTestFile);
  46116. if (bcont) BIO_free(bcont);
  46117. wolfSSL_PKCS7_free(pkcs7);
  46118. /* smime-test-multipart-badsig.p7s */
  46119. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  46120. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46121. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46122. AssertNull(pkcs7);
  46123. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46124. PKCS7_NOVERIFY), SSL_FAILURE);
  46125. XFCLOSE(smimeTestFile);
  46126. if (bcont) BIO_free(bcont);
  46127. wolfSSL_PKCS7_free(pkcs7);
  46128. /* smime-test-canon.p7s */
  46129. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46130. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46131. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46132. AssertNotNull(pkcs7);
  46133. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46134. PKCS7_NOVERIFY), SSL_SUCCESS);
  46135. XFCLOSE(smimeTestFile);
  46136. if (bcont) BIO_free(bcont);
  46137. wolfSSL_PKCS7_free(pkcs7);
  46138. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  46139. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46140. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46141. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46142. AssertNotNull(pkcs7);
  46143. out = wolfSSL_BIO_new(BIO_s_mem());
  46144. AssertNotNull(out);
  46145. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  46146. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  46147. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  46148. /* Content-Type should not show up at beginning of output buffer */
  46149. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  46150. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  46151. BIO_free(out);
  46152. BIO_free(bio);
  46153. if (bcont) BIO_free(bcont);
  46154. wolfSSL_PKCS7_free(pkcs7);
  46155. res = TEST_RES_CHECK(1);
  46156. #endif
  46157. return res;
  46158. }
  46159. static int test_wolfSSL_SMIME_write_PKCS7(void)
  46160. {
  46161. int res = TEST_SKIPPED;
  46162. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  46163. PKCS7* p7 = NULL;
  46164. PKCS7* p7Ver = NULL;
  46165. int flags = 0;
  46166. byte data[] = "Test data to encode.";
  46167. const char* cert = "./certs/server-cert.pem";
  46168. const char* key = "./certs/server-key.pem";
  46169. const char* ca = "./certs/ca-cert.pem";
  46170. WOLFSSL_BIO* certBio = NULL;
  46171. WOLFSSL_BIO* keyBio = NULL;
  46172. WOLFSSL_BIO* caBio = NULL;
  46173. WOLFSSL_BIO* inBio = NULL;
  46174. WOLFSSL_BIO* outBio = NULL;
  46175. WOLFSSL_BIO* content = NULL;
  46176. X509* signCert = NULL;
  46177. EVP_PKEY* signKey = NULL;
  46178. X509* caCert = NULL;
  46179. X509_STORE* store = NULL;
  46180. /* read signer cert/key into BIO */
  46181. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  46182. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  46183. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46184. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46185. /* read CA cert into store (for verify) */
  46186. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  46187. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46188. AssertNotNull(store = X509_STORE_new());
  46189. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46190. /* generate and verify SMIME: not detached */
  46191. {
  46192. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46193. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46194. flags = PKCS7_STREAM;
  46195. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46196. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46197. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46198. /* bad arg: out NULL */
  46199. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  46200. /* bad arg: pkcs7 NULL */
  46201. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  46202. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46203. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46204. BIO_free(content);
  46205. BIO_free(inBio);
  46206. BIO_free(outBio);
  46207. PKCS7_free(p7Ver);
  46208. PKCS7_free(p7);
  46209. }
  46210. /* generate and verify SMIME: not detached, add Content-Type */
  46211. {
  46212. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46213. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46214. flags = PKCS7_STREAM | PKCS7_TEXT;
  46215. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46216. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46217. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46218. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46219. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46220. BIO_free(content);
  46221. BIO_free(inBio);
  46222. BIO_free(outBio);
  46223. PKCS7_free(p7Ver);
  46224. PKCS7_free(p7);
  46225. }
  46226. /* generate and verify SMIME: detached */
  46227. {
  46228. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46229. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46230. flags = PKCS7_DETACHED | PKCS7_STREAM;
  46231. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46232. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46233. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46234. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46235. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46236. BIO_free(content);
  46237. BIO_free(inBio);
  46238. BIO_free(outBio);
  46239. PKCS7_free(p7Ver);
  46240. PKCS7_free(p7);
  46241. }
  46242. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  46243. {
  46244. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  46245. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46246. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  46247. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46248. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  46249. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46250. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46251. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46252. BIO_free(content);
  46253. BIO_free(inBio);
  46254. BIO_free(outBio);
  46255. PKCS7_free(p7Ver);
  46256. PKCS7_free(p7);
  46257. }
  46258. X509_STORE_free(store);
  46259. X509_free(caCert);
  46260. X509_free(signCert);
  46261. EVP_PKEY_free(signKey);
  46262. BIO_free(keyBio);
  46263. BIO_free(certBio);
  46264. BIO_free(caBio);
  46265. res = TEST_RES_CHECK(1);
  46266. #endif
  46267. return res;
  46268. }
  46269. #endif /* HAVE_SMIME */
  46270. #endif /* !NO_BIO */
  46271. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  46272. * returns 0) */
  46273. static int test_X509_STORE_No_SSL_CTX(void)
  46274. {
  46275. int res = TEST_SKIPPED;
  46276. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  46277. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  46278. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46279. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46280. !defined(NO_RSA)
  46281. X509_STORE * store;
  46282. X509_STORE_CTX * storeCtx;
  46283. X509_CRL * crl;
  46284. X509 * ca;
  46285. X509 * cert;
  46286. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46287. const char srvCert[] = "./certs/server-cert.pem";
  46288. const char caCert[] = "./certs/ca-cert.pem";
  46289. const char caDir[] = "./certs/crl/hash_pem";
  46290. XFILE fp;
  46291. X509_LOOKUP * lookup;
  46292. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46293. /* Set up store with CA */
  46294. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46295. SSL_FILETYPE_PEM)));
  46296. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46297. /* Add CRL lookup directory to store
  46298. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  46299. * of crl.pem */
  46300. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46301. X509_LOOKUP_hash_dir())));
  46302. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  46303. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  46304. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46305. SSL_SUCCESS);
  46306. /* Add CRL to store NOT containing the verified certificate, which
  46307. * forces use of the CRL lookup directory */
  46308. fp = XFOPEN(cliCrlPem, "rb");
  46309. AssertTrue((fp != XBADFILE));
  46310. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46311. NULL, NULL));
  46312. XFCLOSE(fp);
  46313. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46314. /* Create verification context outside of an SSL session */
  46315. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46316. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46317. SSL_FILETYPE_PEM)));
  46318. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46319. /* Perform verification, which should NOT indicate CRL missing due to the
  46320. * store CM's X509 store pointer being NULL */
  46321. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46322. X509_CRL_free(crl);
  46323. X509_STORE_free(store);
  46324. X509_STORE_CTX_free(storeCtx);
  46325. X509_free(cert);
  46326. X509_free(ca);
  46327. res = TEST_RES_CHECK(1);
  46328. #endif
  46329. return res;
  46330. }
  46331. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  46332. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  46333. static int test_X509_LOOKUP_add_dir(void)
  46334. {
  46335. int res = TEST_SKIPPED;
  46336. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  46337. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  46338. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46339. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46340. !defined(NO_RSA)
  46341. X509_STORE * store;
  46342. X509_STORE_CTX * storeCtx;
  46343. X509_CRL * crl;
  46344. X509 * ca;
  46345. X509 * cert;
  46346. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46347. const char srvCert[] = "./certs/server-cert.pem";
  46348. const char caCert[] = "./certs/ca-cert.pem";
  46349. const char caDir[] = "./certs/crl/hash_der";
  46350. XFILE fp;
  46351. X509_LOOKUP * lookup;
  46352. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46353. /* Set up store with CA */
  46354. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46355. SSL_FILETYPE_PEM)));
  46356. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46357. /* Add CRL lookup directory to store.
  46358. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  46359. * of crl.der */
  46360. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46361. X509_LOOKUP_hash_dir())));
  46362. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  46363. SSL_SUCCESS);
  46364. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46365. SSL_SUCCESS);
  46366. /* Add CRL to store NOT containing the verified certificate, which
  46367. * forces use of the CRL lookup directory */
  46368. fp = XFOPEN(cliCrlPem, "rb");
  46369. AssertTrue((fp != XBADFILE));
  46370. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46371. NULL, NULL));
  46372. XFCLOSE(fp);
  46373. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46374. /* Create verification context outside of an SSL session */
  46375. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46376. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46377. SSL_FILETYPE_PEM)));
  46378. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46379. /* Perform verification, which should NOT return CRL missing */
  46380. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46381. X509_CRL_free(crl);
  46382. X509_STORE_free(store);
  46383. X509_STORE_CTX_free(storeCtx);
  46384. X509_free(cert);
  46385. X509_free(ca);
  46386. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  46387. * We should get CRL_MISSING at the end, because the lookup
  46388. * dir has only ASN1 CRLs. */
  46389. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  46390. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46391. SSL_FILETYPE_PEM)));
  46392. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46393. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  46394. X509_LOOKUP_hash_dir())));
  46395. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  46396. SSL_SUCCESS);
  46397. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46398. SSL_SUCCESS);
  46399. fp = XFOPEN(cliCrlPem, "rb");
  46400. AssertTrue((fp != XBADFILE));
  46401. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46402. NULL, NULL));
  46403. XFCLOSE(fp);
  46404. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46405. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  46406. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46407. SSL_FILETYPE_PEM)));
  46408. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46409. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46410. * in dir containing only ASN1/DER. */
  46411. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  46412. X509_CRL_free(crl);
  46413. X509_STORE_free(store);
  46414. X509_STORE_CTX_free(storeCtx);
  46415. X509_free(cert);
  46416. X509_free(ca);
  46417. res = TEST_RES_CHECK(1);
  46418. #endif
  46419. return res;
  46420. }
  46421. /*----------------------------------------------------------------------------*
  46422. | Certificate Failure Checks
  46423. *----------------------------------------------------------------------------*/
  46424. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46425. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46426. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46427. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46428. int type)
  46429. {
  46430. int ret;
  46431. WOLFSSL_CERT_MANAGER* cm = NULL;
  46432. switch (type) {
  46433. case TESTING_RSA:
  46434. #ifdef NO_RSA
  46435. fprintf(stderr, "RSA disabled, skipping test\n");
  46436. return ASN_SIG_CONFIRM_E;
  46437. #else
  46438. break;
  46439. #endif
  46440. case TESTING_ECC:
  46441. #ifndef HAVE_ECC
  46442. fprintf(stderr, "ECC disabled, skipping test\n");
  46443. return ASN_SIG_CONFIRM_E;
  46444. #else
  46445. break;
  46446. #endif
  46447. default:
  46448. fprintf(stderr, "Bad function argument\n");
  46449. return BAD_FUNC_ARG;
  46450. }
  46451. cm = wolfSSL_CertManagerNew();
  46452. if (cm == NULL) {
  46453. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46454. return -1;
  46455. }
  46456. #ifndef NO_FILESYSTEM
  46457. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46458. if (ret != WOLFSSL_SUCCESS) {
  46459. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46460. wolfSSL_CertManagerFree(cm);
  46461. return ret;
  46462. }
  46463. #else
  46464. (void)ca;
  46465. #endif
  46466. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  46467. /* Let AssertIntEQ handle return code */
  46468. wolfSSL_CertManagerFree(cm);
  46469. return ret;
  46470. }
  46471. #if !defined(NO_FILESYSTEM)
  46472. static int test_RsaSigFailure_cm(void)
  46473. {
  46474. int ret = 0;
  46475. const char* ca_cert = "./certs/ca-cert.pem";
  46476. const char* server_cert = "./certs/server-cert.der";
  46477. byte* cert_buf = NULL;
  46478. size_t cert_sz = 0;
  46479. ret = load_file(server_cert, &cert_buf, &cert_sz);
  46480. if (ret == 0) {
  46481. /* corrupt DER - invert last byte, which is signature */
  46482. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46483. /* test bad cert */
  46484. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  46485. }
  46486. if (cert_buf)
  46487. free(cert_buf);
  46488. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46489. if (ret == WOLFSSL_FATAL_ERROR) {
  46490. ret = 0;
  46491. }
  46492. #else
  46493. if (ret == ASN_SIG_CONFIRM_E) {
  46494. ret = 0;
  46495. }
  46496. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46497. return TEST_RES_CHECK(ret == 0);
  46498. }
  46499. static int test_EccSigFailure_cm(void)
  46500. {
  46501. int ret = 0;
  46502. /* self-signed ECC cert, so use server cert as CA */
  46503. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46504. const char* server_cert = "./certs/server-ecc.der";
  46505. byte* cert_buf = NULL;
  46506. size_t cert_sz = 0;
  46507. ret = load_file(server_cert, &cert_buf, &cert_sz);
  46508. if (ret == 0) {
  46509. /* corrupt DER - invert last byte, which is signature */
  46510. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46511. /* test bad cert */
  46512. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  46513. }
  46514. if (cert_buf)
  46515. free(cert_buf);
  46516. #ifdef FP_ECC
  46517. wc_ecc_fp_free();
  46518. #endif
  46519. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46520. if (ret == WOLFSSL_FATAL_ERROR) {
  46521. ret = 0;
  46522. }
  46523. #else
  46524. if (ret == ASN_SIG_CONFIRM_E) {
  46525. ret = 0;
  46526. }
  46527. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46528. return TEST_RES_CHECK(ret == 0);
  46529. }
  46530. #endif /* !NO_FILESYSTEM */
  46531. #endif /* NO_CERTS */
  46532. #ifdef WOLFSSL_TLS13
  46533. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46534. #ifdef WC_SHA384_DIGEST_SIZE
  46535. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46536. #else
  46537. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46538. #endif
  46539. #endif
  46540. #ifdef WOLFSSL_EARLY_DATA
  46541. static const char earlyData[] = "Early Data";
  46542. static char earlyDataBuffer[1];
  46543. #endif
  46544. static int test_tls13_apis(void)
  46545. {
  46546. int ret = 0;
  46547. #ifndef WOLFSSL_NO_TLS12
  46548. #ifndef NO_WOLFSSL_CLIENT
  46549. WOLFSSL_CTX* clientTls12Ctx;
  46550. WOLFSSL* clientTls12Ssl;
  46551. #endif
  46552. #ifndef NO_WOLFSSL_SERVER
  46553. WOLFSSL_CTX* serverTls12Ctx;
  46554. WOLFSSL* serverTls12Ssl;
  46555. #endif
  46556. #endif
  46557. #ifndef NO_WOLFSSL_CLIENT
  46558. WOLFSSL_CTX* clientCtx;
  46559. WOLFSSL* clientSsl;
  46560. #endif
  46561. #ifndef NO_WOLFSSL_SERVER
  46562. WOLFSSL_CTX* serverCtx;
  46563. WOLFSSL* serverSsl;
  46564. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46565. const char* ourCert = svrCertFile;
  46566. const char* ourKey = svrKeyFile;
  46567. #endif
  46568. #endif
  46569. int required;
  46570. #ifdef WOLFSSL_EARLY_DATA
  46571. int outSz;
  46572. #endif
  46573. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46574. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46575. #ifdef HAVE_PQC
  46576. WOLFSSL_KYBER_LEVEL1
  46577. #else
  46578. WOLFSSL_ECC_SECP256R1
  46579. #endif
  46580. };
  46581. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46582. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46583. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46584. int numGroups = 2;
  46585. #endif
  46586. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46587. char groupList[] =
  46588. #ifndef NO_ECC_SECP
  46589. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  46590. "P-521:"
  46591. #endif
  46592. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  46593. "P-384:"
  46594. #endif
  46595. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  46596. "P-256"
  46597. #ifdef HAVE_PQC
  46598. ":P256_KYBER_LEVEL1"
  46599. #endif
  46600. #endif
  46601. #ifdef HAVE_PQC
  46602. ":KYBER_LEVEL1"
  46603. #endif
  46604. "";
  46605. #endif /* !defined(NO_ECC_SECP) */
  46606. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46607. (void)ret;
  46608. #ifndef WOLFSSL_NO_TLS12
  46609. #ifndef NO_WOLFSSL_CLIENT
  46610. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  46611. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  46612. #endif
  46613. #ifndef NO_WOLFSSL_SERVER
  46614. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  46615. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46616. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  46617. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  46618. #endif
  46619. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  46620. #endif
  46621. #endif
  46622. #ifndef NO_WOLFSSL_CLIENT
  46623. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  46624. clientSsl = wolfSSL_new(clientCtx);
  46625. #endif
  46626. #ifndef NO_WOLFSSL_SERVER
  46627. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  46628. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46629. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  46630. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  46631. #endif
  46632. serverSsl = wolfSSL_new(serverCtx);
  46633. AssertNotNull(serverSsl);
  46634. #endif
  46635. #ifdef WOLFSSL_SEND_HRR_COOKIE
  46636. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  46637. #ifndef NO_WOLFSSL_CLIENT
  46638. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  46639. #endif
  46640. #ifndef NO_WOLFSSL_SERVER
  46641. #ifndef WOLFSSL_NO_TLS12
  46642. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  46643. #endif
  46644. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  46645. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  46646. WOLFSSL_SUCCESS);
  46647. #endif
  46648. #endif
  46649. #ifdef HAVE_SUPPORTED_CURVES
  46650. #ifdef HAVE_ECC
  46651. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  46652. #ifndef NO_WOLFSSL_SERVER
  46653. do {
  46654. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  46655. #ifdef WOLFSSL_ASYNC_CRYPT
  46656. if (ret == WC_PENDING_E)
  46657. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  46658. #endif
  46659. } while (ret == WC_PENDING_E);
  46660. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46661. #endif
  46662. #ifndef NO_WOLFSSL_CLIENT
  46663. #ifndef WOLFSSL_NO_TLS12
  46664. do {
  46665. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  46666. #ifdef WOLFSSL_ASYNC_CRYPT
  46667. if (ret == WC_PENDING_E)
  46668. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  46669. #endif
  46670. } while (ret == WC_PENDING_E);
  46671. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46672. #endif
  46673. do {
  46674. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  46675. #ifdef WOLFSSL_ASYNC_CRYPT
  46676. if (ret == WC_PENDING_E)
  46677. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  46678. #endif
  46679. } while (ret == WC_PENDING_E);
  46680. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46681. #endif
  46682. #elif defined(HAVE_CURVE25519)
  46683. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  46684. #ifndef NO_WOLFSSL_SERVER
  46685. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  46686. WOLFSSL_SUCCESS);
  46687. #endif
  46688. #ifndef NO_WOLFSSL_CLIENT
  46689. #ifndef WOLFSSL_NO_TLS12
  46690. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  46691. WOLFSSL_SUCCESS);
  46692. #endif
  46693. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  46694. WOLFSSL_SUCCESS);
  46695. #endif
  46696. #elif defined(HAVE_CURVE448)
  46697. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  46698. #ifndef NO_WOLFSSL_SERVER
  46699. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  46700. WOLFSSL_SUCCESS);
  46701. #endif
  46702. #ifndef NO_WOLFSSL_CLIENT
  46703. #ifndef WOLFSSL_NO_TLS12
  46704. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  46705. WOLFSSL_SUCCESS);
  46706. #endif
  46707. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  46708. WOLFSSL_SUCCESS);
  46709. #endif
  46710. #else
  46711. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  46712. #ifndef NO_WOLFSSL_CLIENT
  46713. #ifndef WOLFSSL_NO_TLS12
  46714. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  46715. NOT_COMPILED_IN);
  46716. #endif
  46717. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  46718. NOT_COMPILED_IN);
  46719. #endif
  46720. #endif
  46721. #if defined(HAVE_PQC)
  46722. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  46723. #ifndef NO_WOLFSSL_SERVER
  46724. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  46725. WOLFSSL_SUCCESS);
  46726. #endif
  46727. #ifndef NO_WOLFSSL_CLIENT
  46728. #ifndef WOLFSSL_NO_TLS12
  46729. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  46730. BAD_FUNC_ARG);
  46731. #endif
  46732. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  46733. WOLFSSL_SUCCESS);
  46734. #endif
  46735. #endif
  46736. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  46737. #ifndef NO_WOLFSSL_SERVER
  46738. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  46739. #endif
  46740. #ifndef NO_WOLFSSL_CLIENT
  46741. #ifndef WOLFSSL_NO_TLS12
  46742. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  46743. #endif
  46744. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  46745. #endif
  46746. #endif /* HAVE_SUPPORTED_CURVES */
  46747. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46748. #ifndef NO_WOLFSSL_CLIENT
  46749. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  46750. #endif
  46751. #ifndef NO_WOLFSSL_SERVER
  46752. #ifndef WOLFSSL_NO_TLS12
  46753. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  46754. #endif
  46755. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  46756. #endif
  46757. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46758. #ifndef NO_WOLFSSL_CLIENT
  46759. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  46760. #endif
  46761. #ifndef NO_WOLFSSL_SERVER
  46762. #ifndef WOLFSSL_NO_TLS12
  46763. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  46764. #endif
  46765. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  46766. #endif
  46767. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46768. #ifndef NO_WOLFSSL_CLIENT
  46769. #ifndef WOLFSSL_NO_TLS12
  46770. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  46771. #endif
  46772. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  46773. #endif
  46774. #ifndef NO_WOLFSSL_SERVER
  46775. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  46776. #endif
  46777. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46778. #ifndef NO_WOLFSSL_CLIENT
  46779. #ifndef WOLFSSL_NO_TLS12
  46780. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  46781. #endif
  46782. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  46783. #endif
  46784. #ifndef NO_WOLFSSL_SERVER
  46785. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  46786. #endif
  46787. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  46788. #ifndef NO_WOLFSSL_CLIENT
  46789. #ifndef WOLFSSL_NO_TLS12
  46790. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  46791. #endif
  46792. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  46793. #endif
  46794. #ifndef NO_WOLFSSL_SERVER
  46795. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  46796. #endif
  46797. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  46798. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  46799. #ifndef NO_WOLFSSL_CLIENT
  46800. #ifndef WOLFSSL_NO_TLS12
  46801. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  46802. BAD_FUNC_ARG);
  46803. #endif
  46804. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  46805. #endif
  46806. #ifndef NO_WOLFSSL_SERVER
  46807. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  46808. #endif
  46809. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  46810. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46811. #ifndef NO_WOLFSSL_SERVER
  46812. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  46813. #endif
  46814. #ifndef NO_WOLFSSL_CLIENT
  46815. #ifndef WOLFSSL_NO_TLS12
  46816. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  46817. BAD_FUNC_ARG);
  46818. #endif
  46819. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  46820. #endif
  46821. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46822. #ifndef NO_WOLFSSL_SERVER
  46823. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  46824. #endif
  46825. #ifndef NO_WOLFSSL_CLIENT
  46826. #ifndef WOLFSSL_NO_TLS12
  46827. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  46828. BAD_FUNC_ARG);
  46829. #endif
  46830. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  46831. #endif
  46832. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  46833. #ifndef NO_WOLFSSL_CLIENT
  46834. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  46835. #endif
  46836. #ifndef NO_WOLFSSL_SERVER
  46837. #ifndef WOLFSSL_NO_TLS12
  46838. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  46839. BAD_FUNC_ARG);
  46840. #endif
  46841. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  46842. #endif
  46843. #endif
  46844. #ifdef HAVE_ECC
  46845. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  46846. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  46847. #ifndef NO_WOLFSSL_SERVER
  46848. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  46849. #endif
  46850. #ifndef NO_WOLFSSL_CLIENT
  46851. #ifndef WOLFSSL_NO_TLS12
  46852. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  46853. #endif
  46854. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  46855. #endif
  46856. #endif
  46857. #ifdef HAVE_SUPPORTED_CURVES
  46858. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46859. #ifndef NO_WOLFSSL_CLIENT
  46860. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  46861. #endif
  46862. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46863. #ifndef NO_WOLFSSL_CLIENT
  46864. #ifndef WOLFSSL_NO_TLS12
  46865. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  46866. BAD_FUNC_ARG);
  46867. #endif
  46868. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  46869. WOLFSSL_MAX_GROUP_COUNT + 1),
  46870. BAD_FUNC_ARG);
  46871. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  46872. WOLFSSL_SUCCESS);
  46873. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  46874. BAD_FUNC_ARG);
  46875. #endif
  46876. #ifndef NO_WOLFSSL_SERVER
  46877. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  46878. WOLFSSL_SUCCESS);
  46879. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  46880. BAD_FUNC_ARG);
  46881. #endif
  46882. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46883. #ifndef NO_WOLFSSL_CLIENT
  46884. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  46885. #endif
  46886. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46887. #ifndef NO_WOLFSSL_CLIENT
  46888. #ifndef WOLFSSL_NO_TLS12
  46889. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  46890. BAD_FUNC_ARG);
  46891. #endif
  46892. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  46893. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46894. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  46895. WOLFSSL_SUCCESS);
  46896. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  46897. BAD_FUNC_ARG);
  46898. #endif
  46899. #ifndef NO_WOLFSSL_SERVER
  46900. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  46901. WOLFSSL_SUCCESS);
  46902. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  46903. BAD_FUNC_ARG);
  46904. #endif
  46905. #ifdef OPENSSL_EXTRA
  46906. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46907. #ifndef NO_WOLFSSL_CLIENT
  46908. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  46909. #endif
  46910. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46911. #ifndef NO_WOLFSSL_CLIENT
  46912. #ifndef WOLFSSL_NO_TLS12
  46913. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  46914. WOLFSSL_FAILURE);
  46915. #endif
  46916. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  46917. WOLFSSL_SUCCESS);
  46918. #endif
  46919. #ifndef NO_WOLFSSL_SERVER
  46920. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  46921. WOLFSSL_SUCCESS);
  46922. #endif
  46923. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46924. #ifndef NO_WOLFSSL_CLIENT
  46925. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  46926. #endif
  46927. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46928. #ifndef NO_WOLFSSL_CLIENT
  46929. #ifndef WOLFSSL_NO_TLS12
  46930. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  46931. WOLFSSL_FAILURE);
  46932. #endif
  46933. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  46934. WOLFSSL_SUCCESS);
  46935. #endif
  46936. #ifndef NO_WOLFSSL_SERVER
  46937. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  46938. WOLFSSL_SUCCESS);
  46939. #endif
  46940. #endif /* OPENSSL_EXTRA */
  46941. #endif /* HAVE_SUPPORTED_CURVES */
  46942. #endif /* HAVE_ECC */
  46943. #ifdef WOLFSSL_EARLY_DATA
  46944. #ifndef OPENSSL_EXTRA
  46945. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46946. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46947. #else
  46948. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46949. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  46950. #endif
  46951. #ifndef NO_WOLFSSL_CLIENT
  46952. #ifndef OPENSSL_EXTRA
  46953. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46954. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46955. #else
  46956. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  46957. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  46958. #endif
  46959. #endif
  46960. #ifndef NO_WOLFSSL_SERVER
  46961. #ifndef WOLFSSL_NO_TLS12
  46962. #ifndef OPENSSL_EXTRA
  46963. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46964. BAD_FUNC_ARG);
  46965. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46966. #else
  46967. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  46968. BAD_FUNC_ARG);
  46969. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  46970. #endif
  46971. #endif
  46972. #ifndef OPENSSL_EXTRA
  46973. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46974. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), WOLFSSL_SUCCESS);
  46975. #else
  46976. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  46977. #endif
  46978. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  46979. #else
  46980. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  46981. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  46982. #endif
  46983. #endif
  46984. #ifndef OPENSSL_EXTRA
  46985. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46986. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46987. #else
  46988. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  46989. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  46990. #endif
  46991. #ifndef NO_WOLFSSL_CLIENT
  46992. #ifndef OPENSSL_EXTRA
  46993. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  46994. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  46995. #else
  46996. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  46997. #endif
  46998. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  46999. #else
  47000. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47001. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  47002. #endif
  47003. #endif
  47004. #ifndef NO_WOLFSSL_SERVER
  47005. #ifndef WOLFSSL_NO_TLS12
  47006. #ifndef OPENSSL_EXTRA
  47007. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47008. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47009. #else
  47010. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47011. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47012. #endif
  47013. #endif
  47014. #ifndef OPENSSL_EXTRA
  47015. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47016. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  47017. #else
  47018. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  47019. #endif
  47020. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  47021. #else
  47022. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  47023. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  47024. #endif
  47025. #endif
  47026. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  47027. &outSz), BAD_FUNC_ARG);
  47028. #ifndef NO_WOLFSSL_CLIENT
  47029. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  47030. &outSz), BAD_FUNC_ARG);
  47031. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  47032. BAD_FUNC_ARG);
  47033. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47034. sizeof(earlyData), NULL),
  47035. BAD_FUNC_ARG);
  47036. #endif
  47037. #ifndef NO_WOLFSSL_SERVER
  47038. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  47039. sizeof(earlyData), &outSz),
  47040. SIDE_ERROR);
  47041. #endif
  47042. #ifndef NO_WOLFSSL_CLIENT
  47043. #ifndef WOLFSSL_NO_TLS12
  47044. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  47045. sizeof(earlyData), &outSz),
  47046. BAD_FUNC_ARG);
  47047. #endif
  47048. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47049. sizeof(earlyData), &outSz),
  47050. WOLFSSL_FATAL_ERROR);
  47051. #endif
  47052. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  47053. sizeof(earlyDataBuffer), &outSz),
  47054. BAD_FUNC_ARG);
  47055. #ifndef NO_WOLFSSL_SERVER
  47056. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  47057. sizeof(earlyDataBuffer), &outSz),
  47058. BAD_FUNC_ARG);
  47059. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  47060. BAD_FUNC_ARG);
  47061. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47062. sizeof(earlyDataBuffer), NULL),
  47063. BAD_FUNC_ARG);
  47064. #endif
  47065. #ifndef NO_WOLFSSL_CLIENT
  47066. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  47067. sizeof(earlyDataBuffer), &outSz),
  47068. SIDE_ERROR);
  47069. #endif
  47070. #ifndef NO_WOLFSSL_SERVER
  47071. #ifndef WOLFSSL_NO_TLS12
  47072. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  47073. sizeof(earlyDataBuffer), &outSz),
  47074. BAD_FUNC_ARG);
  47075. #endif
  47076. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47077. sizeof(earlyDataBuffer), &outSz),
  47078. WOLFSSL_FATAL_ERROR);
  47079. #endif
  47080. #endif
  47081. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  47082. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  47083. #endif
  47084. #ifndef NO_WOLFSSL_SERVER
  47085. wolfSSL_free(serverSsl);
  47086. wolfSSL_CTX_free(serverCtx);
  47087. #endif
  47088. #ifndef NO_WOLFSSL_CLIENT
  47089. wolfSSL_free(clientSsl);
  47090. wolfSSL_CTX_free(clientCtx);
  47091. #endif
  47092. #ifndef WOLFSSL_NO_TLS12
  47093. #ifndef NO_WOLFSSL_SERVER
  47094. wolfSSL_free(serverTls12Ssl);
  47095. wolfSSL_CTX_free(serverTls12Ctx);
  47096. #endif
  47097. #ifndef NO_WOLFSSL_CLIENT
  47098. wolfSSL_free(clientTls12Ssl);
  47099. wolfSSL_CTX_free(clientTls12Ctx);
  47100. #endif
  47101. #endif
  47102. return TEST_RES_CHECK(1);
  47103. }
  47104. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47105. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47106. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47107. /* Called when writing. */
  47108. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47109. {
  47110. (void)ssl;
  47111. (void)buf;
  47112. (void)sz;
  47113. (void)ctx;
  47114. /* Force error return from wolfSSL_accept_TLSv13(). */
  47115. return WANT_WRITE;
  47116. }
  47117. /* Called when reading. */
  47118. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47119. {
  47120. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  47121. int len = (int)msg->length;
  47122. (void)ssl;
  47123. (void)sz;
  47124. /* Pass back as much of message as will fit in buffer. */
  47125. if (len > sz)
  47126. len = sz;
  47127. XMEMCPY(buf, msg->buffer, len);
  47128. /* Move over returned data. */
  47129. msg->buffer += len;
  47130. msg->length -= len;
  47131. /* Amount actually copied. */
  47132. return len;
  47133. }
  47134. #endif
  47135. static int test_tls13_cipher_suites(void)
  47136. {
  47137. int res = TEST_SKIPPED;
  47138. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47139. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47140. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47141. WOLFSSL_CTX* ctx;
  47142. WOLFSSL *ssl;
  47143. int i;
  47144. byte clientHello[] = {
  47145. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  47146. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  47147. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  47148. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  47149. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  47150. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  47151. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  47152. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  47153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  47155. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  47156. 0x13, 0x01,
  47157. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  47158. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  47159. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  47160. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  47161. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  47162. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  47163. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  47164. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  47165. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  47166. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  47167. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  47168. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  47169. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  47170. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  47171. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  47172. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  47173. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  47174. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  47175. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  47176. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  47177. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  47178. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  47179. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  47180. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  47181. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  47182. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  47183. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  47184. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  47185. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  47186. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  47187. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  47188. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  47189. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  47190. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  47191. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  47192. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47194. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  47195. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  47196. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  47197. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  47198. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  47199. };
  47200. WOLFSSL_BUFFER_INFO msg;
  47201. /* Offset into ClientHello message data of first cipher suite. */
  47202. const int csOff = 78;
  47203. /* Server cipher list. */
  47204. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  47205. /* Suite list with duplicates. */
  47206. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  47207. "TLS13-AES128-GCM-SHA256:"
  47208. "TLS13-AES256-GCM-SHA384:"
  47209. "TLS13-AES256-GCM-SHA384:"
  47210. "TLS13-AES128-GCM-SHA256";
  47211. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47212. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47213. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47214. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47215. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47216. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  47217. #endif
  47218. /* Set up wolfSSL context. */
  47219. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47220. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  47221. WOLFSSL_FILETYPE_PEM));
  47222. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  47223. WOLFSSL_FILETYPE_PEM));
  47224. /* Read from 'msg'. */
  47225. wolfSSL_SetIORecv(ctx, CsRecv);
  47226. /* No where to send to - dummy sender. */
  47227. wolfSSL_SetIOSend(ctx, CsSend);
  47228. /* Test cipher suite list with many copies of a cipher suite. */
  47229. AssertNotNull(ssl = wolfSSL_new(ctx));
  47230. msg.buffer = clientHello;
  47231. msg.length = (unsigned int)sizeof(clientHello);
  47232. wolfSSL_SetIOReadCtx(ssl, &msg);
  47233. /* Force server to have as many occurrences of same cipher suite as
  47234. * possible. */
  47235. {
  47236. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  47237. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  47238. for (i = 0; i < suites->suiteSz; i += 2) {
  47239. suites->suites[i + 0] = TLS13_BYTE;
  47240. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  47241. }
  47242. }
  47243. /* Test multiple occurrences of same cipher suite. */
  47244. wolfSSL_accept_TLSv13(ssl);
  47245. wolfSSL_free(ssl);
  47246. /* Set client order opposite to server order:
  47247. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  47248. clientHello[csOff + 0] = TLS13_BYTE;
  47249. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  47250. clientHello[csOff + 2] = TLS13_BYTE;
  47251. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  47252. /* Test server order negotiation. */
  47253. AssertNotNull(ssl = wolfSSL_new(ctx));
  47254. msg.buffer = clientHello;
  47255. msg.length = (unsigned int)sizeof(clientHello);
  47256. wolfSSL_SetIOReadCtx(ssl, &msg);
  47257. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47258. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47259. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  47260. wolfSSL_accept_TLSv13(ssl);
  47261. /* Check refined order - server order. */
  47262. AssertIntEQ(ssl->suites->suiteSz, 4);
  47263. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47264. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47265. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47266. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47267. wolfSSL_free(ssl);
  47268. /* Test client order negotiation. */
  47269. AssertNotNull(ssl = wolfSSL_new(ctx));
  47270. msg.buffer = clientHello;
  47271. msg.length = (unsigned int)sizeof(clientHello);
  47272. wolfSSL_SetIOReadCtx(ssl, &msg);
  47273. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47274. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47275. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  47276. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  47277. wolfSSL_accept_TLSv13(ssl);
  47278. /* Check refined order - client order. */
  47279. AssertIntEQ(ssl->suites->suiteSz, 4);
  47280. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47281. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47282. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47283. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47284. wolfSSL_free(ssl);
  47285. /* Check duplicate detection is working. */
  47286. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  47287. AssertIntEQ(ctx->suites->suiteSz, 4);
  47288. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47289. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47290. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47291. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47292. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47293. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  47294. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  47295. AssertIntEQ(ctx->suites->suiteSz, 4);
  47296. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47297. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47298. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47299. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47300. #endif
  47301. wolfSSL_CTX_free(ctx);
  47302. res = TEST_RES_CHECK(1);
  47303. #endif
  47304. return res;
  47305. }
  47306. #endif
  47307. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  47308. !defined(NO_OLD_TLS))
  47309. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  47310. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  47311. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47312. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  47313. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  47314. const unsigned char* priv, unsigned int privSz,
  47315. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  47316. unsigned char* out, unsigned int* outlen,
  47317. void* ctx)
  47318. {
  47319. int result;
  47320. /* Test fail when context associated with WOLFSSL is NULL */
  47321. if (ctx == NULL) {
  47322. return -1;
  47323. }
  47324. (void)ssl;
  47325. /* return 0 on success */
  47326. PRIVATE_KEY_UNLOCK();
  47327. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  47328. PRIVATE_KEY_LOCK();
  47329. return result;
  47330. }
  47331. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  47332. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  47333. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  47334. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  47335. WOLFSSL_SUCCESS);
  47336. #endif
  47337. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  47338. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  47339. WOLFSSL_SUCCESS);
  47340. #endif
  47341. }
  47342. static void test_dh_ssl_setup(WOLFSSL* ssl)
  47343. {
  47344. static int dh_test_ctx = 1;
  47345. int ret;
  47346. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  47347. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  47348. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47349. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47350. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47351. }
  47352. }
  47353. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  47354. {
  47355. int ret;
  47356. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  47357. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  47358. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47359. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47360. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47361. }
  47362. }
  47363. #endif
  47364. static int test_DhCallbacks(void)
  47365. {
  47366. int res = TEST_SKIPPED;
  47367. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  47368. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  47369. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47370. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  47371. WOLFSSL_CTX *ctx;
  47372. WOLFSSL *ssl;
  47373. tcp_ready ready;
  47374. func_args server_args;
  47375. func_args client_args;
  47376. THREAD_TYPE serverThread;
  47377. callback_functions func_cb_client;
  47378. callback_functions func_cb_server;
  47379. int test;
  47380. #ifndef NO_WOLFSSL_CLIENT
  47381. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47382. #else
  47383. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47384. #endif
  47385. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  47386. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  47387. /* load client ca cert */
  47388. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  47389. WOLFSSL_SUCCESS);
  47390. /* test with NULL arguments */
  47391. wolfSSL_SetDhAgreeCtx(NULL, &test);
  47392. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  47393. /* test success case */
  47394. test = 1;
  47395. AssertNotNull(ssl = wolfSSL_new(ctx));
  47396. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47397. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47398. wolfSSL_free(ssl);
  47399. wolfSSL_CTX_free(ctx);
  47400. /* test a connection where callback is used */
  47401. #ifdef WOLFSSL_TIRTOS
  47402. fdOpenSession(Task_self());
  47403. #endif
  47404. XMEMSET(&server_args, 0, sizeof(func_args));
  47405. XMEMSET(&client_args, 0, sizeof(func_args));
  47406. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  47407. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  47408. StartTCP();
  47409. InitTcpReady(&ready);
  47410. #if defined(USE_WINDOWS_API)
  47411. /* use RNG to get random port if using windows */
  47412. ready.port = GetRandomPort();
  47413. #endif
  47414. server_args.signal = &ready;
  47415. client_args.signal = &ready;
  47416. server_args.return_code = TEST_FAIL;
  47417. client_args.return_code = TEST_FAIL;
  47418. /* set callbacks to use DH functions */
  47419. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47420. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47421. #ifndef WOLFSSL_NO_TLS12
  47422. func_cb_client.method = wolfTLSv1_2_client_method;
  47423. #else
  47424. func_cb_client.method = wolfTLSv1_3_client_method;
  47425. #endif
  47426. client_args.callbacks = &func_cb_client;
  47427. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47428. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47429. #ifndef WOLFSSL_NO_TLS12
  47430. func_cb_server.method = wolfTLSv1_2_server_method;
  47431. #else
  47432. func_cb_server.method = wolfTLSv1_3_server_method;
  47433. #endif
  47434. server_args.callbacks = &func_cb_server;
  47435. start_thread(test_server_nofail, &server_args, &serverThread);
  47436. wait_tcp_ready(&server_args);
  47437. test_client_nofail(&client_args, NULL);
  47438. join_thread(serverThread);
  47439. AssertTrue(client_args.return_code);
  47440. AssertTrue(server_args.return_code);
  47441. FreeTcpReady(&ready);
  47442. #ifdef WOLFSSL_TIRTOS
  47443. fdOpenSession(Task_self());
  47444. #endif
  47445. /* now set user ctx to not be 1 so that the callback returns fail case */
  47446. #ifdef WOLFSSL_TIRTOS
  47447. fdOpenSession(Task_self());
  47448. #endif
  47449. XMEMSET(&server_args, 0, sizeof(func_args));
  47450. XMEMSET(&client_args, 0, sizeof(func_args));
  47451. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  47452. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  47453. StartTCP();
  47454. InitTcpReady(&ready);
  47455. #if defined(USE_WINDOWS_API)
  47456. /* use RNG to get random port if using windows */
  47457. ready.port = GetRandomPort();
  47458. #endif
  47459. server_args.signal = &ready;
  47460. client_args.signal = &ready;
  47461. server_args.return_code = TEST_FAIL;
  47462. client_args.return_code = TEST_FAIL;
  47463. /* set callbacks to use DH functions */
  47464. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47465. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47466. #ifndef WOLFSSL_NO_TLS12
  47467. func_cb_client.method = wolfTLSv1_2_client_method;
  47468. #else
  47469. func_cb_client.method = wolfTLSv1_3_client_method;
  47470. #endif
  47471. client_args.callbacks = &func_cb_client;
  47472. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47473. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47474. #ifndef WOLFSSL_NO_TLS12
  47475. func_cb_server.method = wolfTLSv1_2_server_method;
  47476. #else
  47477. func_cb_server.method = wolfTLSv1_3_server_method;
  47478. #endif
  47479. server_args.callbacks = &func_cb_server;
  47480. start_thread(test_server_nofail, &server_args, &serverThread);
  47481. wait_tcp_ready(&server_args);
  47482. test_client_nofail(&client_args, NULL);
  47483. join_thread(serverThread);
  47484. AssertIntEQ(client_args.return_code, TEST_FAIL);
  47485. AssertIntEQ(server_args.return_code, TEST_FAIL);
  47486. FreeTcpReady(&ready);
  47487. #ifdef WOLFSSL_TIRTOS
  47488. fdOpenSession(Task_self());
  47489. #endif
  47490. res = TEST_RES_CHECK(1);
  47491. #endif
  47492. return res;
  47493. }
  47494. #endif /* HAVE_PK_CALLBACKS */
  47495. #ifdef HAVE_HASHDRBG
  47496. #ifdef TEST_RESEED_INTERVAL
  47497. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47498. {
  47499. int i, ret;
  47500. WC_RNG rng;
  47501. byte key[32];
  47502. ret = wc_InitRng(&rng);
  47503. if (ret == 0) {
  47504. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47505. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  47506. if (ret != 0) {
  47507. break;
  47508. }
  47509. }
  47510. }
  47511. wc_FreeRng(&rng);
  47512. return TEST_RES_CHECK(ret == 0);
  47513. }
  47514. #endif /* TEST_RESEED_INTERVAL */
  47515. static int test_wc_RNG_GenerateBlock(void)
  47516. {
  47517. int i, ret;
  47518. WC_RNG rng;
  47519. byte key[32];
  47520. ret = wc_InitRng(&rng);
  47521. if (ret == 0) {
  47522. for (i = 0; i < 10; i++) {
  47523. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  47524. if (ret != 0) {
  47525. break;
  47526. }
  47527. }
  47528. }
  47529. wc_FreeRng(&rng);
  47530. (void)rng; /* for WC_NO_RNG case */
  47531. (void)key;
  47532. return TEST_RES_CHECK(ret == 0);
  47533. }
  47534. #endif
  47535. /*
  47536. * Testing get_rand_digit
  47537. */
  47538. static int test_get_rand_digit(void)
  47539. {
  47540. int res = TEST_SKIPPED;
  47541. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47542. int ret = 0;
  47543. WC_RNG rng;
  47544. mp_digit d;
  47545. ret = wc_InitRng(&rng);
  47546. if (ret == 0) {
  47547. ret = get_rand_digit(&rng, &d);
  47548. }
  47549. if (ret == 0) {
  47550. ret = get_rand_digit(NULL, NULL);
  47551. if (ret == BAD_FUNC_ARG) {
  47552. ret = 0;
  47553. }
  47554. }
  47555. if (ret == 0) {
  47556. ret = get_rand_digit(NULL, &d);
  47557. if (ret == BAD_FUNC_ARG) {
  47558. ret = 0;
  47559. }
  47560. }
  47561. if (ret == 0) {
  47562. ret = get_rand_digit(&rng, NULL);
  47563. if (ret == BAD_FUNC_ARG) {
  47564. ret = 0;
  47565. }
  47566. }
  47567. if (ret == 0) {
  47568. ret = wc_FreeRng(&rng);
  47569. }
  47570. res = TEST_RES_CHECK(ret == 0);
  47571. #endif
  47572. return res;
  47573. }/* End test_get_rand_digit*/
  47574. /*
  47575. * Testing get_digit_count
  47576. */
  47577. static int test_get_digit_count(void)
  47578. {
  47579. int res = TEST_SKIPPED;
  47580. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47581. int ret = 0;
  47582. mp_int a;
  47583. if (mp_init(&a) != MP_OKAY) {
  47584. ret = -1;
  47585. }
  47586. if (ret == 0) {
  47587. ret = get_digit_count(NULL);
  47588. }
  47589. if (ret == 0) {
  47590. ret = get_digit_count(&a);
  47591. }
  47592. mp_clear(&a);
  47593. res = TEST_RES_CHECK(ret == 0);
  47594. #endif
  47595. return res;
  47596. }/* End test_get_digit_count*/
  47597. /*
  47598. * Testing mp_cond_copy
  47599. */
  47600. static int test_mp_cond_copy(void)
  47601. {
  47602. int res = TEST_SKIPPED;
  47603. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47604. defined(WOLFSSL_PUBLIC_MP)
  47605. int ret = 0;
  47606. mp_int a;
  47607. mp_int b;
  47608. int copy = 0;
  47609. if (mp_init(&a) != MP_OKAY) {
  47610. ret = -1;
  47611. }
  47612. if (ret == 0) {
  47613. if (mp_init(&b) != MP_OKAY) {
  47614. ret = -1;
  47615. }
  47616. }
  47617. if (ret == 0) {
  47618. ret = mp_cond_copy(NULL, copy, NULL);
  47619. if (ret == BAD_FUNC_ARG) {
  47620. ret = 0;
  47621. }
  47622. }
  47623. if (ret == 0) {
  47624. ret = mp_cond_copy(NULL, copy, &b);
  47625. if (ret == BAD_FUNC_ARG) {
  47626. ret = 0;
  47627. }
  47628. }
  47629. if (ret == 0) {
  47630. ret = mp_cond_copy(&a, copy, NULL);
  47631. if (ret == BAD_FUNC_ARG) {
  47632. ret = 0;
  47633. }
  47634. }
  47635. if (ret == 0) {
  47636. ret = mp_cond_copy(&a, copy, &b);
  47637. }
  47638. mp_clear(&a);
  47639. mp_clear(&b);
  47640. res = TEST_RES_CHECK(ret == 0);
  47641. #endif
  47642. return res;
  47643. }/* End test_mp_cond_copy*/
  47644. /*
  47645. * Testing mp_rand
  47646. */
  47647. static int test_mp_rand(void)
  47648. {
  47649. int res = TEST_SKIPPED;
  47650. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47651. int ret = 0;
  47652. mp_int a;
  47653. int digits = 1;
  47654. WC_RNG rng;
  47655. if (mp_init(&a) != MP_OKAY) {
  47656. ret = -1;
  47657. }
  47658. if (ret == 0) {
  47659. ret = wc_InitRng(&rng);
  47660. }
  47661. if (ret == 0) {
  47662. ret = mp_rand(&a, digits, NULL);
  47663. if (ret == MISSING_RNG_E) {
  47664. ret = 0;
  47665. }
  47666. }
  47667. if (ret == 0) {
  47668. ret = mp_rand(NULL, digits, &rng);
  47669. if (ret == BAD_FUNC_ARG) {
  47670. ret = 0;
  47671. }
  47672. }
  47673. if (ret == 0) {
  47674. ret = mp_rand(&a, 0, &rng);
  47675. if (ret == BAD_FUNC_ARG) {
  47676. ret = 0;
  47677. }
  47678. }
  47679. if (ret == 0) {
  47680. ret = mp_rand(&a, digits, &rng);
  47681. }
  47682. mp_clear(&a);
  47683. wc_FreeRng(&rng);
  47684. res = TEST_RES_CHECK(ret == 0);
  47685. #endif
  47686. return res;
  47687. }/* End test_mp_rand*/
  47688. /*
  47689. * Testing get_digit
  47690. */
  47691. static int test_get_digit(void)
  47692. {
  47693. int res = TEST_SKIPPED;
  47694. #if defined(WOLFSSL_PUBLIC_MP)
  47695. int ret = 0;
  47696. mp_int a;
  47697. int n = 0;
  47698. if (mp_init(&a) != MP_OKAY) {
  47699. ret = -1;
  47700. }
  47701. if (ret == 0) {
  47702. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  47703. ret = -1;
  47704. }
  47705. }
  47706. if (ret == 0) {
  47707. if (get_digit(NULL, n) == 0) { /* Should hit this */
  47708. ret = 0;
  47709. }
  47710. }
  47711. if (ret == 0) {
  47712. if (get_digit(&a, n) != 0) { /* Should not hit this */
  47713. ret = -1;
  47714. }
  47715. }
  47716. if (ret == 0) {
  47717. if (get_digit(&a, n) == 0) { /* Should hit this */
  47718. ret = 0;
  47719. }
  47720. }
  47721. mp_clear(&a);
  47722. res = TEST_RES_CHECK(ret == 0);
  47723. #endif
  47724. return res;
  47725. }/* End test_get_digit*/
  47726. /*
  47727. * Testing wc_export_int
  47728. */
  47729. static int test_wc_export_int(void)
  47730. {
  47731. int res = TEST_SKIPPED;
  47732. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47733. defined(WOLFSSL_PUBLIC_MP)
  47734. int ret = 0;
  47735. mp_int mp;
  47736. byte buf[32];
  47737. word32 keySz = (word32)sizeof(buf);
  47738. word32 len = (word32)sizeof(buf);
  47739. if (mp_init(&mp) != MP_OKAY) {
  47740. ret = -1;
  47741. }
  47742. if (ret == 0) {
  47743. ret = mp_set(&mp, 1234);
  47744. }
  47745. if (ret == 0) {
  47746. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47747. if (ret == BAD_FUNC_ARG) {
  47748. ret = 0;
  47749. }
  47750. }
  47751. if (ret == 0) {
  47752. len = sizeof(buf)-1;
  47753. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47754. if (ret == BUFFER_E) {
  47755. ret = 0;
  47756. }
  47757. }
  47758. if (ret == 0) {
  47759. len = sizeof(buf);
  47760. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  47761. }
  47762. if (ret == 0) {
  47763. len = 4; /* test input too small */
  47764. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  47765. if (ret == BUFFER_E) {
  47766. ret = 0;
  47767. }
  47768. }
  47769. if (ret == 0) {
  47770. len = sizeof(buf);
  47771. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  47772. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47773. if (ret == 0 && len != 5) {
  47774. ret = BAD_FUNC_ARG;
  47775. }
  47776. }
  47777. mp_clear(&mp);
  47778. res = TEST_RES_CHECK(ret == 0);
  47779. #endif
  47780. return res;
  47781. }/* End test_wc_export_int*/
  47782. static int test_wc_InitRngNonce(void)
  47783. {
  47784. int res = TEST_SKIPPED;
  47785. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47786. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  47787. int ret;
  47788. WC_RNG rng;
  47789. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47790. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47791. word32 nonceSz = sizeof(nonce);
  47792. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  47793. wc_FreeRng(&rng);
  47794. res = TEST_RES_CHECK(ret == 0);
  47795. #endif
  47796. return res;
  47797. }/* End test_wc_InitRngNonce*/
  47798. /*
  47799. * Testing wc_InitRngNonce_ex
  47800. */
  47801. static int test_wc_InitRngNonce_ex(void)
  47802. {
  47803. int res = TEST_SKIPPED;
  47804. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47805. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  47806. int ret;
  47807. WC_RNG rng;
  47808. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47809. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47810. word32 nonceSz = sizeof(nonce);
  47811. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  47812. wc_FreeRng(&rng);
  47813. res = TEST_RES_CHECK(ret == 0);
  47814. #endif
  47815. return res;
  47816. }/*End test_wc_InitRngNonce_ex*/
  47817. static int test_wolfSSL_X509_CRL(void)
  47818. {
  47819. int res = TEST_SKIPPED;
  47820. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  47821. X509_CRL *crl;
  47822. char pem[][100] = {
  47823. "./certs/crl/crl.pem",
  47824. "./certs/crl/crl2.pem",
  47825. "./certs/crl/caEccCrl.pem",
  47826. "./certs/crl/eccCliCRL.pem",
  47827. "./certs/crl/eccSrvCRL.pem",
  47828. ""
  47829. };
  47830. #ifndef NO_BIO
  47831. BIO *bio;
  47832. #endif
  47833. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47834. char der[][100] = {
  47835. "./certs/crl/crl.der",
  47836. "./certs/crl/crl2.der",
  47837. ""};
  47838. #endif
  47839. XFILE fp;
  47840. int i;
  47841. for (i = 0; pem[i][0] != '\0'; i++)
  47842. {
  47843. fp = XFOPEN(pem[i], "rb");
  47844. AssertTrue((fp != XBADFILE));
  47845. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  47846. AssertNotNull(crl);
  47847. X509_CRL_free(crl);
  47848. XFCLOSE(fp);
  47849. fp = XFOPEN(pem[i], "rb");
  47850. AssertTrue((fp != XBADFILE));
  47851. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  47852. AssertNotNull(crl);
  47853. X509_CRL_free(crl);
  47854. XFCLOSE(fp);
  47855. }
  47856. #ifndef NO_BIO
  47857. for (i = 0; pem[i][0] != '\0'; i++)
  47858. {
  47859. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  47860. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  47861. X509_CRL_free(crl);
  47862. BIO_free(bio);
  47863. }
  47864. #endif
  47865. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47866. for (i = 0; der[i][0] != '\0'; i++) {
  47867. fp = XFOPEN(der[i], "rb");
  47868. AssertTrue((fp != XBADFILE));
  47869. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  47870. AssertNotNull(crl);
  47871. X509_CRL_free(crl);
  47872. XFCLOSE(fp);
  47873. fp = XFOPEN(der[i], "rb");
  47874. AssertTrue((fp != XBADFILE));
  47875. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  47876. AssertNotNull(crl);
  47877. X509_CRL_free(crl);
  47878. XFCLOSE(fp);
  47879. }
  47880. #endif
  47881. res = TEST_RES_CHECK(1);
  47882. #endif
  47883. return res;
  47884. }
  47885. static int test_wolfSSL_X509_load_crl_file(void)
  47886. {
  47887. int res = TEST_SKIPPED;
  47888. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47889. !defined(NO_RSA) && !defined(NO_BIO)
  47890. int i;
  47891. char pem[][100] = {
  47892. "./certs/crl/crl.pem",
  47893. "./certs/crl/crl2.pem",
  47894. "./certs/crl/caEccCrl.pem",
  47895. "./certs/crl/eccCliCRL.pem",
  47896. "./certs/crl/eccSrvCRL.pem",
  47897. ""
  47898. };
  47899. char der[][100] = {
  47900. "./certs/crl/crl.der",
  47901. "./certs/crl/crl2.der",
  47902. ""
  47903. };
  47904. WOLFSSL_X509_STORE* store;
  47905. WOLFSSL_X509_LOOKUP* lookup;
  47906. AssertNotNull(store = wolfSSL_X509_STORE_new());
  47907. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47908. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47909. X509_FILETYPE_PEM), 1);
  47910. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47911. X509_FILETYPE_PEM), 1);
  47912. if (store) {
  47913. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47914. WOLFSSL_FILETYPE_PEM), 1);
  47915. /* since store hasn't yet known the revoked cert*/
  47916. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47917. WOLFSSL_FILETYPE_PEM), 1);
  47918. }
  47919. for (i = 0; pem[i][0] != '\0'; i++)
  47920. {
  47921. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  47922. }
  47923. if (store) {
  47924. /* since store knows crl list */
  47925. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47926. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  47927. }
  47928. /* once feeing store */
  47929. X509_STORE_free(store);
  47930. store = NULL;
  47931. AssertNotNull(store = wolfSSL_X509_STORE_new());
  47932. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47933. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47934. X509_FILETYPE_PEM), 1);
  47935. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47936. X509_FILETYPE_PEM), 1);
  47937. if (store) {
  47938. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47939. WOLFSSL_FILETYPE_PEM), 1);
  47940. /* since store hasn't yet known the revoked cert*/
  47941. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47942. WOLFSSL_FILETYPE_PEM), 1);
  47943. }
  47944. for (i = 0; der[i][0] != '\0'; i++)
  47945. {
  47946. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  47947. }
  47948. if (store) {
  47949. /* since store knows crl list */
  47950. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  47951. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  47952. }
  47953. /* test for incorrect parameter */
  47954. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  47955. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  47956. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  47957. X509_STORE_free(store);
  47958. store = NULL;
  47959. res = TEST_RES_CHECK(1);
  47960. #endif
  47961. return res;
  47962. }
  47963. static int test_wolfSSL_d2i_X509_REQ(void)
  47964. {
  47965. int res = TEST_SKIPPED;
  47966. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  47967. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  47968. !defined(WOLFSSL_SP_MATH)
  47969. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  47970. * generated by libest
  47971. * ./certs/csr.attr.der contains sample attributes
  47972. * ./certs/csr.ext.der contains sample extensions */
  47973. const char* csrFile = "./certs/csr.signed.der";
  47974. const char* csrPopFile = "./certs/csr.attr.der";
  47975. const char* csrExtFile = "./certs/csr.ext.der";
  47976. /* ./certs/csr.dsa.pem is generated using
  47977. * openssl req -newkey dsa:certs/dsaparams.pem \
  47978. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  47979. * -outform PEM
  47980. * with the passphrase "wolfSSL"
  47981. */
  47982. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47983. const char* csrDsaFile = "./certs/csr.dsa.pem";
  47984. XFILE f;
  47985. #endif
  47986. BIO* bio = NULL;
  47987. X509* req = NULL;
  47988. EVP_PKEY *pub_key = NULL;
  47989. {
  47990. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  47991. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  47992. /*
  47993. * Extract the public key from the CSR
  47994. */
  47995. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  47996. /*
  47997. * Verify the signature in the CSR
  47998. */
  47999. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48000. X509_free(req);
  48001. BIO_free(bio);
  48002. EVP_PKEY_free(pub_key);
  48003. }
  48004. {
  48005. #ifdef OPENSSL_ALL
  48006. X509_ATTRIBUTE* attr;
  48007. ASN1_TYPE *at;
  48008. #endif
  48009. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48010. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48011. /*
  48012. * Extract the public key from the CSR
  48013. */
  48014. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48015. /*
  48016. * Verify the signature in the CSR
  48017. */
  48018. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48019. #ifdef OPENSSL_ALL
  48020. /*
  48021. * Obtain the challenge password from the CSR
  48022. */
  48023. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  48024. 1);
  48025. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  48026. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48027. AssertNotNull(at->value.asn1_string);
  48028. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  48029. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48030. #endif
  48031. X509_free(req);
  48032. BIO_free(bio);
  48033. EVP_PKEY_free(pub_key);
  48034. }
  48035. {
  48036. #ifdef OPENSSL_ALL
  48037. X509_ATTRIBUTE* attr;
  48038. ASN1_TYPE *at;
  48039. STACK_OF(X509_EXTENSION) *exts = NULL;
  48040. #endif
  48041. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48042. /* This CSR contains an Extension Request attribute so
  48043. * we test extension parsing in a CSR attribute here. */
  48044. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  48045. /*
  48046. * Extract the public key from the CSR
  48047. */
  48048. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48049. /*
  48050. * Verify the signature in the CSR
  48051. */
  48052. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48053. #ifdef OPENSSL_ALL
  48054. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  48055. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48056. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48057. /*
  48058. * Obtain the challenge password from the CSR
  48059. */
  48060. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  48061. 0);
  48062. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  48063. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48064. AssertNotNull(at->value.asn1_string);
  48065. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  48066. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  48067. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  48068. #endif
  48069. X509_free(req);
  48070. BIO_free(bio);
  48071. EVP_PKEY_free(pub_key);
  48072. }
  48073. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48074. {
  48075. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  48076. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  48077. /*
  48078. * Extract the public key from the CSR
  48079. */
  48080. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  48081. /*
  48082. * Verify the signature in the CSR
  48083. */
  48084. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48085. X509_free(req);
  48086. BIO_free(bio);
  48087. /* Run the same test, but with a file pointer instead of a BIO.
  48088. * (PEM_read_X509_REQ)*/
  48089. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48090. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48091. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  48092. X509_free(req);
  48093. EVP_PKEY_free(pub_key);
  48094. }
  48095. res = TEST_RES_CHECK(1);
  48096. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48097. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48098. return res;
  48099. }
  48100. static int test_wolfSSL_PEM_read_X509(void)
  48101. {
  48102. int res = TEST_SKIPPED;
  48103. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48104. !defined(NO_RSA)
  48105. X509 *x509 = NULL;
  48106. XFILE fp;
  48107. fp = XFOPEN(svrCertFile, "rb");
  48108. AssertTrue((fp != XBADFILE));
  48109. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  48110. X509_free(x509);
  48111. XFCLOSE(fp);
  48112. res = TEST_RES_CHECK(1);
  48113. #endif
  48114. return res;
  48115. }
  48116. static int test_wolfSSL_PEM_read(void)
  48117. {
  48118. int res = TEST_SKIPPED;
  48119. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48120. const char* filename = "./certs/server-keyEnc.pem";
  48121. XFILE fp;
  48122. char* name = NULL;
  48123. char* header = NULL;
  48124. byte* data = NULL;
  48125. long len;
  48126. EVP_CIPHER_INFO cipher;
  48127. WOLFSSL_BIO* bio;
  48128. byte* fileData;
  48129. size_t fileDataSz;
  48130. byte* out;
  48131. fp = XFOPEN(filename, "rb");
  48132. AssertTrue((fp != XBADFILE));
  48133. /* Fail cases. */
  48134. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  48135. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  48136. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  48137. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  48138. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48139. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48140. AssertIntGT(XSTRLEN(header), 0);
  48141. AssertIntGT(len, 0);
  48142. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48143. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  48144. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48145. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48146. DYNAMIC_TYPE_TMP_BUFFER));
  48147. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48148. XFCLOSE(fp);
  48149. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48150. /* Fail cases. */
  48151. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  48152. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  48153. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  48154. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  48155. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48156. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48157. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48158. /* Fail cases. */
  48159. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  48160. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  48161. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  48162. #ifndef NO_DES3
  48163. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  48164. #endif
  48165. /* Fail cases. */
  48166. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  48167. (void*)"yassl123"), WOLFSSL_FAILURE);
  48168. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  48169. (void*)"yassl123"), WOLFSSL_FAILURE);
  48170. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  48171. (void*)"yassl123"), WOLFSSL_FAILURE);
  48172. #if !defined(NO_DES3) && !defined(NO_MD5)
  48173. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  48174. (void*)"yassl123"), WOLFSSL_SUCCESS);
  48175. #endif
  48176. BIO_free(bio);
  48177. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48178. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48179. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48180. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48181. name = NULL;
  48182. header = NULL;
  48183. data = NULL;
  48184. fp = XFOPEN(svrKeyFile, "rb");
  48185. AssertTrue((fp != XBADFILE));
  48186. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48187. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48188. AssertIntEQ(XSTRLEN(header), 0);
  48189. AssertIntGT(len, 0);
  48190. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48191. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  48192. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48193. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48194. DYNAMIC_TYPE_TMP_BUFFER));
  48195. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48196. XFCLOSE(fp);
  48197. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48198. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48199. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48200. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48201. BIO_free(bio);
  48202. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48203. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48204. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48205. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48206. res = TEST_RES_CHECK(1);
  48207. #endif
  48208. return res;
  48209. }
  48210. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  48211. {
  48212. int res = TEST_SKIPPED;
  48213. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48214. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48215. const byte iv[12] = { 0 };
  48216. const byte key[16] = { 0 };
  48217. const byte cleartext[16] = { 0 };
  48218. const byte aad[] = {
  48219. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  48220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  48221. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  48222. 0x4f
  48223. };
  48224. byte out1Part[16];
  48225. byte outTag1Part[16];
  48226. byte out2Part[16];
  48227. byte outTag2Part[16];
  48228. byte decryptBuf[16];
  48229. int len;
  48230. int tlen;
  48231. EVP_CIPHER_CTX* ctx = NULL;
  48232. /* ENCRYPT */
  48233. /* Send AAD and data in 1 part */
  48234. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48235. tlen = 0;
  48236. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48237. 1);
  48238. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48239. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48240. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  48241. sizeof(cleartext)), 1);
  48242. tlen += len;
  48243. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  48244. tlen += len;
  48245. AssertIntEQ(tlen, sizeof(cleartext));
  48246. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48247. outTag1Part), 1);
  48248. EVP_CIPHER_CTX_free(ctx);
  48249. /* DECRYPT */
  48250. /* Send AAD and data in 1 part */
  48251. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48252. tlen = 0;
  48253. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48254. 1);
  48255. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48256. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48257. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  48258. sizeof(cleartext)), 1);
  48259. tlen += len;
  48260. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48261. outTag1Part), 1);
  48262. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  48263. tlen += len;
  48264. AssertIntEQ(tlen, sizeof(cleartext));
  48265. EVP_CIPHER_CTX_free(ctx);
  48266. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48267. /* ENCRYPT */
  48268. /* Send AAD and data in 2 parts */
  48269. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48270. tlen = 0;
  48271. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48272. 1);
  48273. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48274. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48275. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48276. 1);
  48277. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  48278. tlen += len;
  48279. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  48280. sizeof(cleartext) - 1), 1);
  48281. tlen += len;
  48282. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  48283. tlen += len;
  48284. AssertIntEQ(tlen, sizeof(cleartext));
  48285. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48286. outTag2Part), 1);
  48287. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  48288. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  48289. EVP_CIPHER_CTX_free(ctx);
  48290. /* DECRYPT */
  48291. /* Send AAD and data in 2 parts */
  48292. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  48293. tlen = 0;
  48294. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48295. 1);
  48296. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48297. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48298. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48299. 1);
  48300. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  48301. tlen += len;
  48302. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  48303. sizeof(cleartext) - 1), 1);
  48304. tlen += len;
  48305. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48306. outTag1Part), 1);
  48307. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  48308. tlen += len;
  48309. AssertIntEQ(tlen, sizeof(cleartext));
  48310. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48311. /* Test AAD re-use */
  48312. EVP_CIPHER_CTX_free(ctx);
  48313. res = TEST_RES_CHECK(1);
  48314. #endif
  48315. return res;
  48316. }
  48317. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  48318. {
  48319. int res = TEST_SKIPPED;
  48320. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48321. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48322. /* Zero length plain text */
  48323. byte key[] = {
  48324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48327. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48328. }; /* align */
  48329. byte iv[] = {
  48330. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48331. }; /* align */
  48332. byte plaintxt[1];
  48333. int ivSz = 12;
  48334. int plaintxtSz = 0;
  48335. unsigned char tag[16];
  48336. unsigned char tag_kat[] =
  48337. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  48338. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  48339. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48340. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48341. int ciphertxtSz = 0;
  48342. int decryptedtxtSz = 0;
  48343. int len = 0;
  48344. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48345. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48346. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  48347. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48348. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48349. plaintxtSz));
  48350. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48351. ciphertxtSz += len;
  48352. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48353. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48354. AssertIntEQ(0, ciphertxtSz);
  48355. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48356. EVP_CIPHER_CTX_init(de);
  48357. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  48358. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48359. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48360. decryptedtxtSz = len;
  48361. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48362. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48363. decryptedtxtSz += len;
  48364. AssertIntEQ(0, decryptedtxtSz);
  48365. EVP_CIPHER_CTX_free(en);
  48366. EVP_CIPHER_CTX_free(de);
  48367. res = TEST_RES_CHECK(1);
  48368. #endif
  48369. return res;
  48370. }
  48371. static int test_wolfssl_EVP_aes_gcm(void)
  48372. {
  48373. int res = TEST_SKIPPED;
  48374. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48375. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48376. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48377. byte *key = (byte*)"01234567890123456789012345678901";
  48378. /* A 128 bit IV */
  48379. byte *iv = (byte*)"0123456789012345";
  48380. int ivSz = AES_BLOCK_SIZE;
  48381. /* Message to be encrypted */
  48382. byte *plaintxt = (byte*)"for things to change you have to change";
  48383. /* Additional non-confidential data */
  48384. byte *aad = (byte*)"Don't spend major time on minor things.";
  48385. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48386. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48387. int aadSz = (int)XSTRLEN((char*)aad);
  48388. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48389. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48390. int ciphertxtSz = 0;
  48391. int decryptedtxtSz = 0;
  48392. int len = 0;
  48393. int i = 0;
  48394. EVP_CIPHER_CTX en[2];
  48395. EVP_CIPHER_CTX de[2];
  48396. for (i = 0; i < 2; i++) {
  48397. EVP_CIPHER_CTX_init(&en[i]);
  48398. if (i == 0) {
  48399. /* Default uses 96-bits IV length */
  48400. #ifdef WOLFSSL_AES_128
  48401. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  48402. #elif defined(WOLFSSL_AES_192)
  48403. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  48404. #elif defined(WOLFSSL_AES_256)
  48405. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  48406. #endif
  48407. }
  48408. else {
  48409. #ifdef WOLFSSL_AES_128
  48410. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  48411. #elif defined(WOLFSSL_AES_192)
  48412. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  48413. #elif defined(WOLFSSL_AES_256)
  48414. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  48415. #endif
  48416. /* non-default must to set the IV length first */
  48417. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48418. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48419. }
  48420. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48421. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48422. ciphertxtSz = len;
  48423. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48424. ciphertxtSz += len;
  48425. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  48426. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48427. EVP_CIPHER_CTX_init(&de[i]);
  48428. if (i == 0) {
  48429. /* Default uses 96-bits IV length */
  48430. #ifdef WOLFSSL_AES_128
  48431. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  48432. #elif defined(WOLFSSL_AES_192)
  48433. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  48434. #elif defined(WOLFSSL_AES_256)
  48435. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  48436. #endif
  48437. }
  48438. else {
  48439. #ifdef WOLFSSL_AES_128
  48440. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  48441. #elif defined(WOLFSSL_AES_192)
  48442. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  48443. #elif defined(WOLFSSL_AES_256)
  48444. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  48445. #endif
  48446. /* non-default must to set the IV length first */
  48447. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48448. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48449. }
  48450. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48451. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48452. decryptedtxtSz = len;
  48453. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48454. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48455. decryptedtxtSz += len;
  48456. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  48457. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48458. /* modify tag*/
  48459. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48460. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48461. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48462. /* fail due to wrong tag */
  48463. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48464. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48465. AssertIntEQ(0, len);
  48466. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48467. }
  48468. res = TEST_RES_CHECK(1);
  48469. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48470. return res;
  48471. }
  48472. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48473. {
  48474. int res = TEST_SKIPPED;
  48475. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48476. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48477. /* Zero length plain text */
  48478. byte key[] = {
  48479. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48480. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48483. }; /* align */
  48484. byte iv[] = {
  48485. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48486. }; /* align */
  48487. byte plaintxt[1];
  48488. int ivSz = 12;
  48489. int plaintxtSz = 0;
  48490. unsigned char tag[16];
  48491. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48492. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48493. int ciphertxtSz = 0;
  48494. int decryptedtxtSz = 0;
  48495. int len = 0;
  48496. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48497. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48498. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48499. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48500. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48501. plaintxtSz));
  48502. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48503. ciphertxtSz += len;
  48504. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48505. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48506. AssertIntEQ(0, ciphertxtSz);
  48507. EVP_CIPHER_CTX_init(de);
  48508. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48509. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48510. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48511. decryptedtxtSz = len;
  48512. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48513. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48514. decryptedtxtSz += len;
  48515. AssertIntEQ(0, decryptedtxtSz);
  48516. EVP_CIPHER_CTX_free(en);
  48517. EVP_CIPHER_CTX_free(de);
  48518. res = TEST_RES_CHECK(1);
  48519. #endif
  48520. return res;
  48521. }
  48522. static int test_wolfssl_EVP_aes_ccm(void)
  48523. {
  48524. int res = TEST_SKIPPED;
  48525. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48526. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48527. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48528. byte *key = (byte*)"01234567890123456789012345678901";
  48529. /* A 128 bit IV */
  48530. byte *iv = (byte*)"0123456789012";
  48531. int ivSz = (int)XSTRLEN((char*)iv);
  48532. /* Message to be encrypted */
  48533. byte *plaintxt = (byte*)"for things to change you have to change";
  48534. /* Additional non-confidential data */
  48535. byte *aad = (byte*)"Don't spend major time on minor things.";
  48536. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48537. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48538. int aadSz = (int)XSTRLEN((char*)aad);
  48539. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48540. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48541. int ciphertxtSz = 0;
  48542. int decryptedtxtSz = 0;
  48543. int len = 0;
  48544. int i = 0;
  48545. EVP_CIPHER_CTX en[2];
  48546. EVP_CIPHER_CTX de[2];
  48547. for (i = 0; i < 2; i++) {
  48548. EVP_CIPHER_CTX_init(&en[i]);
  48549. if (i == 0) {
  48550. /* Default uses 96-bits IV length */
  48551. #ifdef WOLFSSL_AES_128
  48552. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48553. EVP_aes_128_ccm(), NULL, key, iv));
  48554. #elif defined(WOLFSSL_AES_192)
  48555. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48556. EVP_aes_192_ccm(), NULL, key, iv));
  48557. #elif defined(WOLFSSL_AES_256)
  48558. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48559. EVP_aes_256_ccm(), NULL, key, iv));
  48560. #endif
  48561. }
  48562. else {
  48563. #ifdef WOLFSSL_AES_128
  48564. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48565. EVP_aes_128_ccm(), NULL, NULL, NULL));
  48566. #elif defined(WOLFSSL_AES_192)
  48567. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48568. EVP_aes_192_ccm(), NULL, NULL, NULL));
  48569. #elif defined(WOLFSSL_AES_256)
  48570. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48571. EVP_aes_256_ccm(), NULL, NULL, NULL));
  48572. #endif
  48573. /* non-default must to set the IV length first */
  48574. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  48575. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48576. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
  48577. NULL, NULL, key, iv));
  48578. }
  48579. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48580. AssertIntEQ(1, EVP_EncryptUpdate(&en[i],
  48581. ciphertxt, &len, plaintxt, plaintxtSz));
  48582. ciphertxtSz = len;
  48583. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48584. ciphertxtSz += len;
  48585. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
  48586. EVP_CTRL_CCM_GET_TAG, AES_BLOCK_SIZE, tag));
  48587. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48588. EVP_CIPHER_CTX_init(&de[i]);
  48589. if (i == 0) {
  48590. /* Default uses 96-bits IV length */
  48591. #ifdef WOLFSSL_AES_128
  48592. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48593. EVP_aes_128_ccm(), NULL, key, iv));
  48594. #elif defined(WOLFSSL_AES_192)
  48595. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48596. EVP_aes_192_ccm(), NULL, key, iv));
  48597. #elif defined(WOLFSSL_AES_256)
  48598. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48599. EVP_aes_256_ccm(), NULL, key, iv));
  48600. #endif
  48601. }
  48602. else {
  48603. #ifdef WOLFSSL_AES_128
  48604. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48605. EVP_aes_128_ccm(), NULL, NULL, NULL));
  48606. #elif defined(WOLFSSL_AES_192)
  48607. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48608. EVP_aes_192_ccm(), NULL, NULL, NULL));
  48609. #elif defined(WOLFSSL_AES_256)
  48610. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
  48611. EVP_aes_256_ccm(), NULL, NULL, NULL));
  48612. #endif
  48613. /* non-default must to set the IV length first */
  48614. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48615. EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48616. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48617. }
  48618. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48619. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  48620. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48621. decryptedtxtSz = len;
  48622. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48623. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48624. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i],
  48625. decryptedtxt, &len));
  48626. decryptedtxtSz += len;
  48627. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  48628. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48629. /* modify tag*/
  48630. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48631. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48632. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
  48633. EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
  48634. /* fail due to wrong tag */
  48635. AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
  48636. decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48637. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48638. AssertIntEQ(0, len);
  48639. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48640. }
  48641. res = TEST_RES_CHECK(1);
  48642. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  48643. return res;
  48644. }
  48645. static int test_wolfssl_EVP_chacha20_poly1305(void)
  48646. {
  48647. int res = TEST_SKIPPED;
  48648. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  48649. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  48650. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  48651. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48652. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  48653. byte cipherText[sizeof(plainText)];
  48654. byte decryptedText[sizeof(plainText)];
  48655. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  48656. EVP_CIPHER_CTX* ctx;
  48657. int outSz;
  48658. /* Encrypt. */
  48659. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48660. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48661. NULL), WOLFSSL_SUCCESS);
  48662. /* Invalid IV length. */
  48663. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48664. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  48665. /* Valid IV length. */
  48666. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48667. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48668. /* Invalid tag length. */
  48669. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48670. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  48671. /* Valid tag length. */
  48672. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48673. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  48674. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48675. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48676. WOLFSSL_SUCCESS);
  48677. AssertIntEQ(outSz, sizeof(aad));
  48678. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48679. sizeof(plainText)), WOLFSSL_SUCCESS);
  48680. AssertIntEQ(outSz, sizeof(plainText));
  48681. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48682. AssertIntEQ(outSz, 0);
  48683. /* Invalid tag length. */
  48684. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48685. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  48686. /* Valid tag length. */
  48687. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48688. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48689. EVP_CIPHER_CTX_free(ctx);
  48690. /* Decrypt. */
  48691. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48692. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48693. NULL), WOLFSSL_SUCCESS);
  48694. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48695. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48696. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48697. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48698. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48699. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48700. WOLFSSL_SUCCESS);
  48701. AssertIntEQ(outSz, sizeof(aad));
  48702. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48703. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48704. AssertIntEQ(outSz, sizeof(cipherText));
  48705. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48706. WOLFSSL_SUCCESS);
  48707. AssertIntEQ(outSz, 0);
  48708. EVP_CIPHER_CTX_free(ctx);
  48709. /* Test partial Inits. CipherInit() allow setting of key and iv
  48710. * in separate calls. */
  48711. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48712. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  48713. key, NULL, 1), WOLFSSL_SUCCESS);
  48714. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48715. WOLFSSL_SUCCESS);
  48716. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  48717. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  48718. AssertIntEQ(outSz, sizeof(aad));
  48719. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48720. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48721. AssertIntEQ(outSz, sizeof(cipherText));
  48722. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48723. WOLFSSL_SUCCESS);
  48724. AssertIntEQ(outSz, 0);
  48725. EVP_CIPHER_CTX_free(ctx);
  48726. res = TEST_RES_CHECK(1);
  48727. #endif
  48728. return res;
  48729. }
  48730. static int test_wolfssl_EVP_chacha20(void)
  48731. {
  48732. int res = TEST_SKIPPED;
  48733. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  48734. byte key[CHACHA_MAX_KEY_SZ];
  48735. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  48736. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48737. byte cipherText[sizeof(plainText)];
  48738. byte decryptedText[sizeof(plainText)];
  48739. EVP_CIPHER_CTX* ctx;
  48740. int outSz;
  48741. /* Encrypt. */
  48742. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48743. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48744. NULL), WOLFSSL_SUCCESS);
  48745. /* Any tag length must fail - not an AEAD cipher. */
  48746. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48747. 16, NULL), WOLFSSL_FAILURE);
  48748. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48749. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48750. sizeof(plainText)), WOLFSSL_SUCCESS);
  48751. AssertIntEQ(outSz, sizeof(plainText));
  48752. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48753. AssertIntEQ(outSz, 0);
  48754. EVP_CIPHER_CTX_free(ctx);
  48755. /* Decrypt. */
  48756. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48757. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48758. NULL), WOLFSSL_SUCCESS);
  48759. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48760. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48761. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48762. AssertIntEQ(outSz, sizeof(cipherText));
  48763. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48764. WOLFSSL_SUCCESS);
  48765. AssertIntEQ(outSz, 0);
  48766. EVP_CIPHER_CTX_free(ctx);
  48767. /* Test partial Inits. CipherInit() allow setting of key and iv
  48768. * in separate calls. */
  48769. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  48770. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  48771. key, NULL, 1), WOLFSSL_SUCCESS);
  48772. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48773. WOLFSSL_SUCCESS);
  48774. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48775. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48776. AssertIntEQ(outSz, sizeof(cipherText));
  48777. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48778. WOLFSSL_SUCCESS);
  48779. AssertIntEQ(outSz, 0);
  48780. EVP_CIPHER_CTX_free(ctx);
  48781. res = TEST_RES_CHECK(1);
  48782. #endif
  48783. return res;
  48784. }
  48785. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  48786. {
  48787. int res = TEST_SKIPPED;
  48788. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  48789. EVP_PKEY_CTX* ctx;
  48790. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48791. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  48792. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  48793. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  48794. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  48795. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  48796. byte outKey[34];
  48797. size_t outKeySz = sizeof(outKey);
  48798. /* These expected outputs were gathered by running the same test below using
  48799. * OpenSSL. */
  48800. const byte extractAndExpand[] = {
  48801. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  48802. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  48803. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  48804. };
  48805. const byte extractOnly[] = {
  48806. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  48807. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  48808. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  48809. };
  48810. const byte expandOnly[] = {
  48811. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  48812. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  48813. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  48814. };
  48815. const byte extractAndExpandAddInfo[] = {
  48816. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  48817. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  48818. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  48819. };
  48820. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  48821. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  48822. /* NULL ctx. */
  48823. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48824. /* NULL md. */
  48825. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  48826. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  48827. /* NULL ctx. */
  48828. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  48829. WOLFSSL_FAILURE);
  48830. /* NULL salt is ok. */
  48831. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  48832. WOLFSSL_SUCCESS);
  48833. /* Salt length <= 0. */
  48834. /* Length 0 salt is ok. */
  48835. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  48836. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  48837. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  48838. WOLFSSL_SUCCESS);
  48839. /* NULL ctx. */
  48840. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  48841. WOLFSSL_FAILURE);
  48842. /* NULL key. */
  48843. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  48844. WOLFSSL_FAILURE);
  48845. /* Key length <= 0 */
  48846. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  48847. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  48848. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  48849. WOLFSSL_SUCCESS);
  48850. /* NULL ctx. */
  48851. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  48852. WOLFSSL_FAILURE);
  48853. /* NULL info is ok. */
  48854. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  48855. WOLFSSL_SUCCESS);
  48856. /* Info length <= 0 */
  48857. /* Length 0 info is ok. */
  48858. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  48859. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  48860. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  48861. WOLFSSL_SUCCESS);
  48862. /* NULL ctx. */
  48863. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48864. WOLFSSL_FAILURE);
  48865. /* Extract and expand (default). */
  48866. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48867. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  48868. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  48869. /* Extract only. */
  48870. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48871. WOLFSSL_SUCCESS);
  48872. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48873. AssertIntEQ(outKeySz, sizeof(extractOnly));
  48874. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  48875. outKeySz = sizeof(outKey);
  48876. /* Expand only. */
  48877. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  48878. WOLFSSL_SUCCESS);
  48879. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48880. AssertIntEQ(outKeySz, sizeof(expandOnly));
  48881. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  48882. outKeySz = sizeof(outKey);
  48883. /* Extract and expand with appended additional info. */
  48884. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  48885. WOLFSSL_SUCCESS);
  48886. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  48887. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  48888. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48889. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  48890. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  48891. EVP_PKEY_CTX_free(ctx);
  48892. res = TEST_RES_CHECK(1);
  48893. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  48894. return res;
  48895. }
  48896. #ifndef NO_BIO
  48897. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  48898. {
  48899. int res = TEST_SKIPPED;
  48900. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48901. BIO* bio;
  48902. X509_INFO* info;
  48903. STACK_OF(X509_INFO)* sk;
  48904. char* subject;
  48905. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48906. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48907. AssertNotNull(bio = BIO_new(BIO_s_file()));
  48908. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  48909. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  48910. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  48911. /* using dereference to maintain testing for Apache port*/
  48912. AssertNotNull(info = sk_X509_INFO_pop(sk));
  48913. AssertNotNull(subject =
  48914. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  48915. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  48916. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48917. X509_INFO_free(info);
  48918. AssertNotNull(info = sk_X509_INFO_pop(sk));
  48919. AssertNotNull(subject =
  48920. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  48921. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  48922. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48923. X509_INFO_free(info);
  48924. AssertNull(info = sk_X509_INFO_pop(sk));
  48925. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  48926. BIO_free(bio);
  48927. res = TEST_RES_CHECK(1);
  48928. #endif
  48929. return res;
  48930. }
  48931. #endif /* !NO_BIO */
  48932. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  48933. {
  48934. int res = TEST_SKIPPED;
  48935. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48936. X509 *x509;
  48937. X509_NAME* name;
  48938. int idx = 0;
  48939. X509_NAME_ENTRY *ne;
  48940. ASN1_OBJECT *object = NULL;
  48941. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  48942. AssertNotNull(x509);
  48943. name = X509_get_subject_name(x509);
  48944. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  48945. AssertIntGE(idx, 0);
  48946. ne = X509_NAME_get_entry(name, idx);
  48947. AssertNotNull(ne);
  48948. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  48949. X509_free(x509);
  48950. res = TEST_RES_CHECK(1);
  48951. #endif
  48952. return res;
  48953. }
  48954. static int test_wolfSSL_X509_STORE_get1_certs(void)
  48955. {
  48956. int res = TEST_SKIPPED;
  48957. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  48958. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48959. X509_STORE_CTX *storeCtx;
  48960. X509_STORE *store;
  48961. X509 *caX509;
  48962. X509 *svrX509;
  48963. X509_NAME *subject;
  48964. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  48965. AssertNotNull(caX509 =
  48966. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  48967. AssertNotNull((svrX509 =
  48968. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  48969. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  48970. AssertNotNull(store = X509_STORE_new());
  48971. AssertNotNull(subject = X509_get_subject_name(caX509));
  48972. /* Errors */
  48973. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  48974. AssertNull(X509_STORE_get1_certs(NULL, subject));
  48975. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  48976. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  48977. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  48978. /* Should find the cert */
  48979. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48980. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  48981. sk_X509_pop_free(certs, NULL);
  48982. /* Should not find the cert */
  48983. AssertNotNull(subject = X509_get_subject_name(svrX509));
  48984. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48985. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  48986. sk_X509_pop_free(certs, NULL);
  48987. X509_STORE_free(store);
  48988. X509_STORE_CTX_free(storeCtx);
  48989. X509_free(svrX509);
  48990. X509_free(caX509);
  48991. res = TEST_RES_CHECK(1);
  48992. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  48993. return res;
  48994. }
  48995. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  48996. * have default (hidden) visibility, and in the absence of visibility, it's
  48997. * benign to mask out the library implementation.
  48998. */
  48999. #define WOLFSSL_MISC_INCLUDED
  49000. #include <wolfcrypt/src/misc.c>
  49001. static int test_ForceZero(void)
  49002. {
  49003. unsigned char data[32];
  49004. unsigned int i, j, len;
  49005. /* Test case with 0 length */
  49006. ForceZero(data, 0);
  49007. /* Test ForceZero */
  49008. for (i = 0; i < sizeof(data); i++) {
  49009. for (len = 1; len < sizeof(data) - i; len++) {
  49010. for (j = 0; j < sizeof(data); j++)
  49011. data[j] = j + 1;
  49012. ForceZero(data + i, len);
  49013. for (j = 0; j < sizeof(data); j++) {
  49014. if (j < i || j >= i + len) {
  49015. if (data[j] == 0x00)
  49016. return -10200;
  49017. }
  49018. else if (data[j] != 0x00)
  49019. return -10201;
  49020. }
  49021. }
  49022. }
  49023. return TEST_RES_CHECK(1);
  49024. }
  49025. #ifndef NO_BIO
  49026. static int test_wolfSSL_X509_print(void)
  49027. {
  49028. int res = TEST_SKIPPED;
  49029. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49030. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49031. X509 *x509;
  49032. BIO *bio;
  49033. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49034. const X509_ALGOR *cert_sig_alg;
  49035. #endif
  49036. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  49037. AssertNotNull(x509);
  49038. /* print to memory */
  49039. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  49040. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49041. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49042. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49043. /* Will print IP address subject alt name. */
  49044. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49045. #elif defined(NO_ASN_TIME)
  49046. /* Will print IP address subject alt name but not Validity. */
  49047. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49048. #else
  49049. /* Will print IP address subject alt name. */
  49050. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49051. #endif
  49052. #elif defined(NO_ASN_TIME)
  49053. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49054. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49055. #else
  49056. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  49057. #endif
  49058. BIO_free(bio);
  49059. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49060. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49061. /* Print signature */
  49062. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  49063. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  49064. #endif
  49065. /* print to stderr */
  49066. #if !defined(NO_WOLFSSL_DIR)
  49067. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49068. #endif
  49069. /* print again */
  49070. AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  49071. X509_free(x509);
  49072. BIO_free(bio);
  49073. res = TEST_RES_CHECK(1);
  49074. #endif
  49075. return res;
  49076. }
  49077. static int test_wolfSSL_X509_CRL_print(void)
  49078. {
  49079. int res = TEST_SKIPPED;
  49080. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  49081. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  49082. X509_CRL* crl;
  49083. BIO *bio;
  49084. XFILE fp;
  49085. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  49086. AssertTrue((fp != XBADFILE));
  49087. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49088. NULL, NULL));
  49089. XFCLOSE(fp);
  49090. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  49091. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  49092. X509_CRL_free(crl);
  49093. BIO_free(bio);
  49094. res = TEST_RES_CHECK(1);
  49095. #endif
  49096. return res;
  49097. }
  49098. static int test_wolfSSL_BIO_get_len(void)
  49099. {
  49100. int res = TEST_SKIPPED;
  49101. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49102. BIO *bio = NULL;
  49103. const char txt[] = "Some example text to push to the BIO.";
  49104. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  49105. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49106. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  49107. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  49108. BIO_free(bio);
  49109. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49110. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  49111. BIO_free(bio);
  49112. res = TEST_RES_CHECK(1);
  49113. #endif
  49114. return res;
  49115. }
  49116. #endif /* !NO_BIO */
  49117. static int test_wolfSSL_RSA(void)
  49118. {
  49119. int res = TEST_SKIPPED;
  49120. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  49121. defined(WOLFSSL_KEY_GEN)
  49122. RSA* rsa;
  49123. const BIGNUM *n;
  49124. const BIGNUM *e;
  49125. const BIGNUM *d;
  49126. const BIGNUM *p;
  49127. const BIGNUM *q;
  49128. const BIGNUM *dmp1;
  49129. const BIGNUM *dmq1;
  49130. const BIGNUM *iqmp;
  49131. AssertNotNull(rsa = RSA_new());
  49132. AssertIntEQ(RSA_size(NULL), 0);
  49133. AssertIntEQ(RSA_size(rsa), 0);
  49134. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  49135. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  49136. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  49137. #ifdef WOLFSSL_RSA_KEY_CHECK
  49138. AssertIntEQ(RSA_check_key(rsa), 0);
  49139. #endif
  49140. RSA_free(rsa);
  49141. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49142. AssertIntEQ(RSA_size(rsa), 256);
  49143. #ifdef WOLFSSL_RSA_KEY_CHECK
  49144. AssertIntEQ(RSA_check_key(NULL), 0);
  49145. AssertIntEQ(RSA_check_key(rsa), 1);
  49146. #endif
  49147. /* sanity check */
  49148. AssertIntEQ(RSA_bits(NULL), 0);
  49149. /* key */
  49150. AssertIntEQ(RSA_bits(rsa), 2048);
  49151. RSA_get0_key(rsa, &n, &e, &d);
  49152. AssertPtrEq(rsa->n, n);
  49153. AssertPtrEq(rsa->e, e);
  49154. AssertPtrEq(rsa->d, d);
  49155. AssertNotNull(n = BN_new());
  49156. AssertNotNull(e = BN_new());
  49157. AssertNotNull(d = BN_new());
  49158. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  49159. AssertPtrEq(rsa->n, n);
  49160. AssertPtrEq(rsa->e, e);
  49161. AssertPtrEq(rsa->d, d);
  49162. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  49163. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  49164. /* crt_params */
  49165. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  49166. AssertPtrEq(rsa->dmp1, dmp1);
  49167. AssertPtrEq(rsa->dmq1, dmq1);
  49168. AssertPtrEq(rsa->iqmp, iqmp);
  49169. AssertNotNull(dmp1 = BN_new());
  49170. AssertNotNull(dmq1 = BN_new());
  49171. AssertNotNull(iqmp = BN_new());
  49172. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49173. (BIGNUM*)iqmp), 1);
  49174. AssertPtrEq(rsa->dmp1, dmp1);
  49175. AssertPtrEq(rsa->dmq1, dmq1);
  49176. AssertPtrEq(rsa->iqmp, iqmp);
  49177. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  49178. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49179. (BIGNUM*)iqmp), 0);
  49180. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  49181. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  49182. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  49183. AssertNull(dmp1);
  49184. AssertNull(dmq1);
  49185. AssertNull(iqmp);
  49186. /* factors */
  49187. RSA_get0_factors(rsa, NULL, NULL);
  49188. RSA_get0_factors(rsa, &p, &q);
  49189. AssertPtrEq(rsa->p, p);
  49190. AssertPtrEq(rsa->q, q);
  49191. AssertNotNull(p = BN_new());
  49192. AssertNotNull(q = BN_new());
  49193. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  49194. AssertPtrEq(rsa->p, p);
  49195. AssertPtrEq(rsa->q, q);
  49196. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  49197. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  49198. RSA_get0_factors(NULL, NULL, NULL);
  49199. RSA_get0_factors(NULL, &p, &q);
  49200. AssertNull(p);
  49201. AssertNull(q);
  49202. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  49203. AssertIntEQ(RSA_bits(rsa), 21);
  49204. RSA_free(rsa);
  49205. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  49206. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  49207. AssertIntEQ(RSA_size(rsa), 384);
  49208. AssertIntEQ(RSA_bits(rsa), 3072);
  49209. RSA_free(rsa);
  49210. #endif
  49211. /* remove for now with odd key size until adjusting rsa key size check with
  49212. wc_MakeRsaKey()
  49213. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  49214. RSA_free(rsa);
  49215. */
  49216. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  49217. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  49218. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  49219. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  49220. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  49221. {
  49222. byte buff[FOURK_BUF];
  49223. byte der[FOURK_BUF];
  49224. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  49225. XFILE f;
  49226. int bytes;
  49227. /* test loading encrypted RSA private pem w/o password */
  49228. f = XFOPEN(PrivKeyPemFile, "rb");
  49229. AssertTrue((f != XBADFILE));
  49230. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  49231. XFCLOSE(f);
  49232. XMEMSET(der, 0, sizeof(der));
  49233. /* test that error value is returned with no password */
  49234. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  49235. }
  49236. #endif
  49237. res = TEST_RES_CHECK(1);
  49238. #endif
  49239. return res;
  49240. }
  49241. static int test_wolfSSL_RSA_DER(void)
  49242. {
  49243. int res = TEST_SKIPPED;
  49244. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49245. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  49246. RSA *rsa;
  49247. int i;
  49248. const unsigned char *buff = NULL;
  49249. unsigned char *newBuff = NULL;
  49250. struct tbl_s
  49251. {
  49252. const unsigned char *der;
  49253. int sz;
  49254. } tbl[] = {
  49255. #ifdef USE_CERT_BUFFERS_1024
  49256. {client_key_der_1024, sizeof_client_key_der_1024},
  49257. {server_key_der_1024, sizeof_server_key_der_1024},
  49258. #endif
  49259. #ifdef USE_CERT_BUFFERS_2048
  49260. {client_key_der_2048, sizeof_client_key_der_2048},
  49261. {server_key_der_2048, sizeof_server_key_der_2048},
  49262. #endif
  49263. {NULL, 0}
  49264. };
  49265. /* Public Key DER */
  49266. struct tbl_s pub[] = {
  49267. #ifdef USE_CERT_BUFFERS_1024
  49268. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  49269. #endif
  49270. #ifdef USE_CERT_BUFFERS_2048
  49271. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  49272. #endif
  49273. {NULL, 0}
  49274. };
  49275. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  49276. buff = pub[0].der;
  49277. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  49278. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  49279. buff = tbl[0].der;
  49280. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  49281. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  49282. rsa = RSA_new();
  49283. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  49284. RSA_free(rsa);
  49285. for (i = 0; tbl[i].der != NULL; i++)
  49286. {
  49287. /* Passing in pointer results in pointer moving. */
  49288. buff = tbl[i].der;
  49289. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  49290. AssertNotNull(rsa);
  49291. RSA_free(rsa);
  49292. }
  49293. for (i = 0; tbl[i].der != NULL; i++)
  49294. {
  49295. /* Passing in pointer results in pointer moving. */
  49296. buff = tbl[i].der;
  49297. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  49298. AssertNotNull(rsa);
  49299. RSA_free(rsa);
  49300. }
  49301. for (i = 0; pub[i].der != NULL; i++)
  49302. {
  49303. buff = pub[i].der;
  49304. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  49305. AssertNotNull(rsa);
  49306. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  49307. newBuff = NULL;
  49308. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  49309. AssertNotNull(newBuff);
  49310. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  49311. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49312. RSA_free(rsa);
  49313. }
  49314. res = TEST_RES_CHECK(1);
  49315. #endif
  49316. return res;
  49317. }
  49318. static int test_wolfSSL_RSA_print(void)
  49319. {
  49320. int res = TEST_SKIPPED;
  49321. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49322. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49323. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  49324. BIO *bio;
  49325. WOLFSSL_RSA* rsa = NULL;
  49326. AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49327. AssertNotNull(rsa = RSA_new());
  49328. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  49329. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  49330. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  49331. AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  49332. /* Some very large number of indent spaces. */
  49333. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  49334. /* RSA is empty. */
  49335. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  49336. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  49337. RSA_free(rsa);
  49338. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49339. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  49340. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  49341. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  49342. AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  49343. AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  49344. AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  49345. BIO_free(bio);
  49346. RSA_free(rsa);
  49347. res = TEST_RES_CHECK(1);
  49348. #endif
  49349. return res;
  49350. }
  49351. #ifndef NO_RSA
  49352. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  49353. {
  49354. int res = TEST_SKIPPED;
  49355. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49356. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49357. RSA *rsa;
  49358. const unsigned char *derBuf = client_key_der_2048;
  49359. unsigned char em[256] = {0}; /* len = 2048/8 */
  49360. /* Random data simulating a hash */
  49361. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  49362. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49363. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49364. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49365. };
  49366. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49367. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  49368. RSA_PSS_SALTLEN_DIGEST), 0);
  49369. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  49370. RSA_PSS_SALTLEN_DIGEST), 0);
  49371. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  49372. RSA_PSS_SALTLEN_DIGEST), 0);
  49373. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  49374. RSA_PSS_SALTLEN_DIGEST), 0);
  49375. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  49376. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  49377. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49378. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  49379. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49380. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  49381. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49382. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  49383. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49384. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49385. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49386. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  49387. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49388. RSA_PSS_SALTLEN_DIGEST), 1);
  49389. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49390. RSA_PSS_SALTLEN_DIGEST), 1);
  49391. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49392. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49393. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49394. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49395. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49396. RSA_PSS_SALTLEN_MAX), 1);
  49397. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49398. RSA_PSS_SALTLEN_MAX), 1);
  49399. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  49400. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  49401. RSA_free(rsa);
  49402. res = TEST_RES_CHECK(1);
  49403. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49404. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49405. return res;
  49406. }
  49407. #endif
  49408. static int test_wolfSSL_RSA_sign_sha3(void)
  49409. {
  49410. int res = TEST_SKIPPED;
  49411. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  49412. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49413. RSA *rsa;
  49414. const unsigned char *derBuf = client_key_der_2048;
  49415. unsigned char sigRet[256] = {0};
  49416. unsigned int sigLen = sizeof(sigRet);
  49417. /* Random data simulating a hash */
  49418. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  49419. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49420. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49421. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49422. };
  49423. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49424. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  49425. &sigLen, rsa), 1);
  49426. RSA_free(rsa);
  49427. res = TEST_RES_CHECK(1);
  49428. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  49429. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  49430. return res;
  49431. }
  49432. static int test_wolfSSL_RSA_get0_key(void)
  49433. {
  49434. int res = TEST_SKIPPED;
  49435. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  49436. RSA *rsa = NULL;
  49437. const BIGNUM* n = NULL;
  49438. const BIGNUM* e = NULL;
  49439. const BIGNUM* d = NULL;
  49440. const unsigned char* der;
  49441. int derSz;
  49442. #ifdef USE_CERT_BUFFERS_1024
  49443. der = client_key_der_1024;
  49444. derSz = sizeof_client_key_der_1024;
  49445. #elif defined(USE_CERT_BUFFERS_2048)
  49446. der = client_key_der_2048;
  49447. derSz = sizeof_client_key_der_2048;
  49448. #else
  49449. der = NULL;
  49450. derSz = 0;
  49451. #endif
  49452. if (der != NULL) {
  49453. RSA_get0_key(NULL, NULL, NULL, NULL);
  49454. RSA_get0_key(rsa, NULL, NULL, NULL);
  49455. RSA_get0_key(NULL, &n, &e, &d);
  49456. AssertNull(n);
  49457. AssertNull(e);
  49458. AssertNull(d);
  49459. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  49460. AssertNotNull(rsa);
  49461. RSA_get0_key(rsa, NULL, NULL, NULL);
  49462. RSA_get0_key(rsa, &n, NULL, NULL);
  49463. AssertNotNull(n);
  49464. RSA_get0_key(rsa, NULL, &e, NULL);
  49465. AssertNotNull(e);
  49466. RSA_get0_key(rsa, NULL, NULL, &d);
  49467. AssertNotNull(d);
  49468. RSA_get0_key(rsa, &n, &e, &d);
  49469. AssertNotNull(n);
  49470. AssertNotNull(e);
  49471. AssertNotNull(d);
  49472. RSA_free(rsa);
  49473. }
  49474. res = TEST_RES_CHECK(1);
  49475. #endif
  49476. return res;
  49477. }
  49478. static int test_wolfSSL_RSA_meth(void)
  49479. {
  49480. int res = TEST_SKIPPED;
  49481. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49482. RSA *rsa;
  49483. RSA_METHOD *rsa_meth;
  49484. #ifdef WOLFSSL_KEY_GEN
  49485. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49486. RSA_free(rsa);
  49487. #else
  49488. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49489. #endif
  49490. AssertNotNull(RSA_get_default_method());
  49491. wolfSSL_RSA_meth_free(NULL);
  49492. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  49493. AssertNotNull(rsa_meth =
  49494. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  49495. #ifndef NO_WOLFSSL_STUB
  49496. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  49497. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  49498. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  49499. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  49500. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  49501. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  49502. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  49503. #endif
  49504. AssertIntEQ(RSA_flags(NULL), 0);
  49505. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49506. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  49507. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  49508. AssertNotNull(rsa = RSA_new());
  49509. /* No method set. */
  49510. AssertIntEQ(RSA_flags(rsa), 0);
  49511. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49512. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49513. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49514. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  49515. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  49516. AssertNull(RSA_get_method(NULL));
  49517. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  49518. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  49519. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49520. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49521. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  49522. RSA_METHOD_FLAG_NO_CHECK);
  49523. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  49524. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  49525. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  49526. /* rsa_meth is freed here */
  49527. RSA_free(rsa);
  49528. res = TEST_RES_CHECK(1);
  49529. #endif
  49530. return res;
  49531. }
  49532. static int test_wolfSSL_RSA_verify(void)
  49533. {
  49534. int res = TEST_SKIPPED;
  49535. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  49536. !defined(NO_FILESYSTEM)
  49537. #ifndef NO_BIO
  49538. XFILE fp;
  49539. RSA *pKey, *pubKey;
  49540. X509 *cert;
  49541. const char *text = "Hello wolfSSL !";
  49542. unsigned char hash[SHA256_DIGEST_LENGTH];
  49543. unsigned char signature[2048/8];
  49544. unsigned int signatureLength;
  49545. byte *buf;
  49546. BIO *bio;
  49547. SHA256_CTX c;
  49548. EVP_PKEY *evpPkey, *evpPubkey;
  49549. size_t sz;
  49550. /* generate hash */
  49551. SHA256_Init(&c);
  49552. SHA256_Update(&c, text, strlen(text));
  49553. SHA256_Final(hash, &c);
  49554. #ifdef WOLFSSL_SMALL_STACK_CACHE
  49555. /* workaround for small stack cache case */
  49556. wc_Sha256Free((wc_Sha256*)&c);
  49557. #endif
  49558. /* read privete key file */
  49559. fp = XFOPEN(svrKeyFile, "rb");
  49560. AssertTrue((fp != XBADFILE));
  49561. AssertIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  49562. sz = XFTELL(fp);
  49563. AssertIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  49564. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  49565. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  49566. XFCLOSE(fp);
  49567. /* read private key and sign hash data */
  49568. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  49569. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  49570. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  49571. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  49572. signature, &signatureLength, pKey), SSL_SUCCESS);
  49573. /* read public key and verify signed data */
  49574. fp = XFOPEN(svrCertFile,"rb");
  49575. AssertTrue((fp != XBADFILE));
  49576. cert = PEM_read_X509(fp, 0, 0, 0 );
  49577. XFCLOSE(fp);
  49578. evpPubkey = X509_get_pubkey(cert);
  49579. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  49580. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49581. signatureLength, pubKey), SSL_SUCCESS);
  49582. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  49583. signatureLength, NULL), SSL_FAILURE);
  49584. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  49585. signatureLength, pubKey), SSL_FAILURE);
  49586. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  49587. signatureLength, pubKey), SSL_FAILURE);
  49588. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  49589. signatureLength, NULL), SSL_FAILURE);
  49590. RSA_free(pKey);
  49591. EVP_PKEY_free(evpPkey);
  49592. RSA_free(pubKey);
  49593. EVP_PKEY_free(evpPubkey);
  49594. X509_free(cert);
  49595. BIO_free(bio);
  49596. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  49597. res = TEST_RES_CHECK(1);
  49598. #endif
  49599. #endif
  49600. return res;
  49601. }
  49602. static int test_wolfSSL_RSA_sign(void)
  49603. {
  49604. int res = TEST_SKIPPED;
  49605. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49606. RSA *rsa;
  49607. unsigned char hash[SHA256_DIGEST_LENGTH];
  49608. #ifdef USE_CERT_BUFFERS_1024
  49609. const unsigned char* privDer = client_key_der_1024;
  49610. size_t privDerSz = sizeof_client_key_der_1024;
  49611. const unsigned char* pubDer = client_keypub_der_1024;
  49612. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49613. unsigned char signature[1024/8];
  49614. #else
  49615. const unsigned char* privDer = client_key_der_2048;
  49616. size_t privDerSz = sizeof_client_key_der_2048;
  49617. const unsigned char* pubDer = client_keypub_der_2048;
  49618. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49619. unsigned char signature[2048/8];
  49620. #endif
  49621. unsigned int signatureLen;
  49622. const unsigned char* der;
  49623. XMEMSET(hash, 0, sizeof(hash));
  49624. der = privDer;
  49625. rsa = NULL;
  49626. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49627. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  49628. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  49629. &signatureLen, rsa), 0);
  49630. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  49631. &signatureLen, rsa), 0);
  49632. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  49633. &signatureLen, rsa), 0);
  49634. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49635. NULL, rsa), 0);
  49636. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49637. &signatureLen, NULL), 0);
  49638. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49639. &signatureLen, rsa), 1);
  49640. RSA_free(rsa);
  49641. der = pubDer;
  49642. rsa = NULL;
  49643. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49644. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49645. signatureLen, rsa), 1);
  49646. RSA_free(rsa);
  49647. res = TEST_RES_CHECK(1);
  49648. #endif
  49649. return res;
  49650. }
  49651. static int test_wolfSSL_RSA_sign_ex(void)
  49652. {
  49653. int res = TEST_SKIPPED;
  49654. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49655. RSA *rsa;
  49656. unsigned char hash[SHA256_DIGEST_LENGTH];
  49657. #ifdef USE_CERT_BUFFERS_1024
  49658. const unsigned char* privDer = client_key_der_1024;
  49659. size_t privDerSz = sizeof_client_key_der_1024;
  49660. const unsigned char* pubDer = client_keypub_der_1024;
  49661. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49662. unsigned char signature[1024/8];
  49663. #else
  49664. const unsigned char* privDer = client_key_der_2048;
  49665. size_t privDerSz = sizeof_client_key_der_2048;
  49666. const unsigned char* pubDer = client_keypub_der_2048;
  49667. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49668. unsigned char signature[2048/8];
  49669. #endif
  49670. unsigned int signatureLen;
  49671. const unsigned char* der;
  49672. unsigned char encodedHash[51];
  49673. unsigned int encodedHashLen;
  49674. const unsigned char expEncHash[] = {
  49675. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  49676. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  49677. 0x00, 0x04, 0x20,
  49678. /* Hash data */
  49679. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49680. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49681. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49682. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49683. };
  49684. XMEMSET(hash, 0, sizeof(hash));
  49685. AssertNotNull(rsa = wolfSSL_RSA_new());
  49686. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49687. &signatureLen, rsa, 1), 0);
  49688. wolfSSL_RSA_free(rsa);
  49689. der = privDer;
  49690. rsa = NULL;
  49691. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49692. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  49693. -1), 0);
  49694. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  49695. signature, &signatureLen, rsa, 1), 0);
  49696. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49697. &signatureLen, rsa, 1), 0);
  49698. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49699. &signatureLen, rsa, 1), 0);
  49700. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49701. NULL, rsa, 1), 0);
  49702. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49703. &signatureLen, NULL, 1), 0);
  49704. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49705. &signatureLen, rsa, -1), 0);
  49706. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49707. &signatureLen, rsa, 0), 0);
  49708. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49709. &signatureLen, rsa, 0), 0);
  49710. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49711. NULL, rsa, 0), 0);
  49712. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49713. &signatureLen, rsa, 1), 1);
  49714. /* Test returning encoded hash. */
  49715. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  49716. &encodedHashLen, rsa, 0), 1);
  49717. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  49718. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  49719. RSA_free(rsa);
  49720. der = pubDer;
  49721. rsa = NULL;
  49722. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49723. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49724. signatureLen, rsa), 1);
  49725. RSA_free(rsa);
  49726. res = TEST_RES_CHECK(1);
  49727. #endif
  49728. return res;
  49729. }
  49730. static int test_wolfSSL_RSA_public_decrypt(void)
  49731. {
  49732. int res = TEST_SKIPPED;
  49733. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49734. RSA *rsa;
  49735. unsigned char msg[SHA256_DIGEST_LENGTH];
  49736. #ifdef USE_CERT_BUFFERS_1024
  49737. const unsigned char* pubDer = client_keypub_der_1024;
  49738. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49739. unsigned char decMsg[1024/8];
  49740. const unsigned char encMsg[] = {
  49741. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49742. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49743. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49744. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49745. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49746. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49747. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49748. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49749. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49750. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49751. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49752. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49753. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49754. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49755. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49756. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49757. };
  49758. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49759. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49760. defined(WC_RSA_NO_PADDING)
  49761. const unsigned char encMsgNoPad[] = {
  49762. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49763. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49764. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49765. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49766. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49767. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49768. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49769. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49770. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49771. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49772. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49773. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49774. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49775. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49776. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49777. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49778. };
  49779. #endif
  49780. #else
  49781. const unsigned char* pubDer = client_keypub_der_2048;
  49782. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49783. unsigned char decMsg[2048/8];
  49784. const unsigned char encMsg[] = {
  49785. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49786. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49787. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49788. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49789. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49790. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49791. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49792. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49793. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49794. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49795. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49796. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49797. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49798. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49799. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49800. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49801. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49802. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49803. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49804. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49805. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49806. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49807. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49808. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49809. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49810. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49811. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49812. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49813. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49814. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49815. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49816. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49817. };
  49818. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49819. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49820. defined(WC_RSA_NO_PADDING)
  49821. const unsigned char encMsgNoPad[] = {
  49822. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49823. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49824. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49825. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49826. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49827. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49828. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49829. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49830. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49831. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49832. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49833. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49834. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49835. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49836. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49837. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49838. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49839. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49840. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49841. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49842. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49843. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49844. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  49845. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  49846. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  49847. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  49848. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  49849. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  49850. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  49851. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  49852. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  49853. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  49854. };
  49855. #endif
  49856. #endif
  49857. const unsigned char* der;
  49858. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49859. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49860. defined(WC_RSA_NO_PADDING)
  49861. int i;
  49862. #endif
  49863. XMEMSET(msg, 0, sizeof(msg));
  49864. der = pubDer;
  49865. rsa = NULL;
  49866. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49867. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  49868. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  49869. RSA_PKCS1_PADDING), -1);
  49870. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  49871. RSA_PKCS1_PADDING), -1);
  49872. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  49873. RSA_PKCS1_PADDING), -1);
  49874. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  49875. RSA_PKCS1_PADDING), -1);
  49876. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49877. RSA_PKCS1_PSS_PADDING), -1);
  49878. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49879. RSA_PKCS1_PADDING), 32);
  49880. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  49881. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49882. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49883. defined(WC_RSA_NO_PADDING)
  49884. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  49885. rsa, RSA_NO_PADDING), sizeof(decMsg));
  49886. /* Zeros before actual data. */
  49887. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  49888. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49889. }
  49890. /* Check actual data. */
  49891. XMEMSET(msg, 0x01, sizeof(msg));
  49892. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49893. #endif
  49894. RSA_free(rsa);
  49895. res = TEST_RES_CHECK(1);
  49896. #endif
  49897. return res;
  49898. }
  49899. static int test_wolfSSL_RSA_private_encrypt(void)
  49900. {
  49901. int res = TEST_SKIPPED;
  49902. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49903. RSA *rsa;
  49904. unsigned char msg[SHA256_DIGEST_LENGTH];
  49905. #ifdef USE_CERT_BUFFERS_1024
  49906. const unsigned char* privDer = client_key_der_1024;
  49907. size_t privDerSz = sizeof_client_key_der_1024;
  49908. unsigned char encMsg[1024/8];
  49909. const unsigned char expEncMsg[] = {
  49910. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49911. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49912. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49913. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49914. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49915. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49916. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49917. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49918. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49919. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49920. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49921. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49922. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49923. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49924. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49925. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49926. };
  49927. #ifdef WC_RSA_NO_PADDING
  49928. const unsigned char expEncMsgNoPad[] = {
  49929. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49930. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49931. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49932. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49933. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49934. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49935. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49936. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49937. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49938. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49939. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49940. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49941. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49942. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49943. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49944. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49945. };
  49946. #endif
  49947. #else
  49948. const unsigned char* privDer = client_key_der_2048;
  49949. size_t privDerSz = sizeof_client_key_der_2048;
  49950. unsigned char encMsg[2048/8];
  49951. const unsigned char expEncMsg[] = {
  49952. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49953. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49954. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49955. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49956. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49957. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49958. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49959. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49960. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49961. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49962. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49963. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49964. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49965. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49966. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49967. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49968. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49969. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49970. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49971. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49972. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49973. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49974. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49975. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49976. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49977. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49978. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49979. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49980. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49981. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49982. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49983. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49984. };
  49985. #ifdef WC_RSA_NO_PADDING
  49986. const unsigned char expEncMsgNoPad[] = {
  49987. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49988. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49989. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49990. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49991. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49992. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49993. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49994. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49995. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49996. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49997. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49998. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49999. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50000. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50001. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50002. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50003. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50004. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50005. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50006. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50007. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50008. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50009. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50010. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50011. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50012. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50013. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50014. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50015. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50016. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50017. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50018. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50019. };
  50020. #endif
  50021. #endif
  50022. const unsigned char* der;
  50023. XMEMSET(msg, 0x00, sizeof(msg));
  50024. der = privDer;
  50025. rsa = NULL;
  50026. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50027. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50028. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50029. -1);
  50030. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50031. RSA_PKCS1_PADDING), -1);
  50032. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50033. RSA_PKCS1_PADDING), -1);
  50034. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50035. RSA_PKCS1_PADDING), -1);
  50036. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50037. RSA_PKCS1_PSS_PADDING), -1);
  50038. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50039. RSA_PKCS1_PADDING), sizeof(encMsg));
  50040. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50041. #ifdef WC_RSA_NO_PADDING
  50042. /* Non-zero message. */
  50043. XMEMSET(msg, 0x01, sizeof(msg));
  50044. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50045. RSA_NO_PADDING), sizeof(encMsg));
  50046. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50047. #endif
  50048. RSA_free(rsa);
  50049. res = TEST_RES_CHECK(1);
  50050. #endif
  50051. return res;
  50052. }
  50053. static int test_wolfSSL_RSA_public_encrypt(void)
  50054. {
  50055. int res = TEST_SKIPPED;
  50056. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50057. RSA* rsa;
  50058. const unsigned char msg[2048/8] = { 0 };
  50059. unsigned char encMsg[2048/8];
  50060. AssertNotNull(rsa = RSA_new());
  50061. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  50062. RSA_PKCS1_PADDING), -1);
  50063. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50064. RSA_PKCS1_PADDING), -1);
  50065. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  50066. RSA_PKCS1_PADDING), -1);
  50067. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  50068. RSA_PKCS1_PADDING), -1);
  50069. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50070. RSA_PKCS1_PSS_PADDING), -1);
  50071. /* Empty RSA key. */
  50072. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50073. RSA_PKCS1_PADDING), -1);
  50074. RSA_free(rsa);
  50075. res = TEST_RES_CHECK(1);
  50076. #endif
  50077. return res;
  50078. }
  50079. static int test_wolfSSL_RSA_private_decrypt(void)
  50080. {
  50081. int res = TEST_SKIPPED;
  50082. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50083. RSA* rsa;
  50084. unsigned char msg[2048/8];
  50085. const unsigned char encMsg[2048/8] = { 0 };
  50086. AssertNotNull(rsa = RSA_new());
  50087. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  50088. RSA_PKCS1_PADDING), -1);
  50089. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  50090. RSA_PKCS1_PADDING), -1);
  50091. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50092. RSA_PKCS1_PADDING), -1);
  50093. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  50094. RSA_PKCS1_PADDING), -1);
  50095. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50096. RSA_PKCS1_PSS_PADDING), -1);
  50097. /* Empty RSA key. */
  50098. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50099. RSA_PKCS1_PADDING), -1);
  50100. RSA_free(rsa);
  50101. res = TEST_RES_CHECK(1);
  50102. #endif
  50103. return res;
  50104. }
  50105. static int test_wolfSSL_RSA_GenAdd(void)
  50106. {
  50107. int res = TEST_SKIPPED;
  50108. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50109. RSA *rsa;
  50110. #ifdef USE_CERT_BUFFERS_1024
  50111. const unsigned char* privDer = client_key_der_1024;
  50112. size_t privDerSz = sizeof_client_key_der_1024;
  50113. const unsigned char* pubDer = client_keypub_der_1024;
  50114. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50115. #else
  50116. const unsigned char* privDer = client_key_der_2048;
  50117. size_t privDerSz = sizeof_client_key_der_2048;
  50118. const unsigned char* pubDer = client_keypub_der_2048;
  50119. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50120. #endif
  50121. const unsigned char* der;
  50122. der = privDer;
  50123. rsa = NULL;
  50124. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50125. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  50126. #ifndef RSA_LOW_MEM
  50127. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  50128. #else
  50129. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  50130. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50131. #endif
  50132. RSA_free(rsa);
  50133. der = pubDer;
  50134. rsa = NULL;
  50135. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50136. /* Need private values. */
  50137. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50138. RSA_free(rsa);
  50139. res = TEST_RES_CHECK(1);
  50140. #endif
  50141. return res;
  50142. }
  50143. static int test_wolfSSL_RSA_blinding_on(void)
  50144. {
  50145. int res = TEST_SKIPPED;
  50146. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  50147. RSA *rsa;
  50148. WOLFSSL_BN_CTX *bnCtx;
  50149. #ifdef USE_CERT_BUFFERS_1024
  50150. const unsigned char* privDer = client_key_der_1024;
  50151. size_t privDerSz = sizeof_client_key_der_1024;
  50152. #else
  50153. const unsigned char* privDer = client_key_der_2048;
  50154. size_t privDerSz = sizeof_client_key_der_2048;
  50155. #endif
  50156. const unsigned char* der;
  50157. der = privDer;
  50158. rsa = NULL;
  50159. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50160. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  50161. /* Does nothing so all parameters are valid. */
  50162. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  50163. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  50164. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  50165. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  50166. wolfSSL_BN_CTX_free(bnCtx);
  50167. RSA_free(rsa);
  50168. res = TEST_RES_CHECK(1);
  50169. #endif
  50170. return res;
  50171. }
  50172. static int test_wolfSSL_RSA_ex_data(void)
  50173. {
  50174. int res = TEST_SKIPPED;
  50175. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50176. RSA* rsa;
  50177. unsigned char data[1];
  50178. rsa = RSA_new();
  50179. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  50180. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50181. #ifdef MAX_EX_DATA
  50182. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  50183. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  50184. #endif
  50185. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  50186. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  50187. #ifdef HAVE_EX_DATA
  50188. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  50189. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  50190. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  50191. #else
  50192. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  50193. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  50194. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50195. #endif
  50196. RSA_free(rsa);
  50197. res = TEST_RES_CHECK(1);
  50198. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50199. return res;
  50200. }
  50201. static int test_wolfSSL_RSA_LoadDer(void)
  50202. {
  50203. int res = TEST_SKIPPED;
  50204. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  50205. defined(OPENSSL_EXTRA_X509_SMALL))
  50206. RSA *rsa;
  50207. #ifdef USE_CERT_BUFFERS_1024
  50208. const unsigned char* privDer = client_key_der_1024;
  50209. size_t privDerSz = sizeof_client_key_der_1024;
  50210. #else
  50211. const unsigned char* privDer = client_key_der_2048;
  50212. size_t privDerSz = sizeof_client_key_der_2048;
  50213. #endif
  50214. AssertNotNull(rsa = RSA_new());
  50215. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  50216. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  50217. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  50218. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  50219. RSA_free(rsa);
  50220. res = TEST_RES_CHECK(1);
  50221. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50222. return res;
  50223. }
  50224. /* Local API. */
  50225. static int test_wolfSSL_RSA_To_Der(void)
  50226. {
  50227. int res = TEST_SKIPPED;
  50228. #ifdef WOLFSSL_TEST_STATIC_BUILD
  50229. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  50230. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50231. RSA* rsa;
  50232. #ifdef USE_CERT_BUFFERS_1024
  50233. const unsigned char* privDer = client_key_der_1024;
  50234. size_t privDerSz = sizeof_client_key_der_1024;
  50235. const unsigned char* pubDer = client_keypub_der_1024;
  50236. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50237. unsigned char out[sizeof(client_key_der_1024)];
  50238. #else
  50239. const unsigned char* privDer = client_key_der_2048;
  50240. size_t privDerSz = sizeof_client_key_der_2048;
  50241. const unsigned char* pubDer = client_keypub_der_2048;
  50242. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50243. unsigned char out[sizeof(client_key_der_2048)];
  50244. #endif
  50245. const unsigned char* der;
  50246. unsigned char* outDer = NULL;
  50247. der = privDer;
  50248. rsa = NULL;
  50249. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50250. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50251. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  50252. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  50253. outDer = out;
  50254. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50255. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  50256. outDer = NULL;
  50257. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50258. AssertNotNull(outDer);
  50259. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  50260. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  50261. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  50262. outDer = out;
  50263. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  50264. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  50265. RSA_free(rsa);
  50266. AssertNotNull(rsa = RSA_new());
  50267. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50268. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  50269. RSA_free(rsa);
  50270. der = pubDer;
  50271. rsa = NULL;
  50272. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50273. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50274. RSA_free(rsa);
  50275. res = TEST_RES_CHECK(1);
  50276. #endif
  50277. #endif
  50278. return res;
  50279. }
  50280. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50281. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  50282. {
  50283. int res = TEST_SKIPPED;
  50284. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  50285. XFILE file;
  50286. const char* fname = "./certs/server-keyPub.pem";
  50287. RSA *rsa;
  50288. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  50289. file = XFOPEN(fname, "rb");
  50290. AssertTrue((file != XBADFILE));
  50291. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  50292. AssertIntEQ(RSA_size(rsa), 256);
  50293. RSA_free(rsa);
  50294. XFCLOSE(file);
  50295. res = TEST_RES_CHECK(1);
  50296. #endif
  50297. return res;
  50298. }
  50299. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50300. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  50301. {
  50302. int res = TEST_SKIPPED;
  50303. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50304. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  50305. RSA* rsa = NULL;
  50306. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  50307. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  50308. /* Valid but stub so returns 0. */
  50309. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  50310. res = TEST_RES_CHECK(1);
  50311. #endif
  50312. return res;
  50313. }
  50314. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  50315. {
  50316. int res = TEST_SKIPPED;
  50317. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50318. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50319. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  50320. RSA* rsa;
  50321. #ifdef USE_CERT_BUFFERS_1024
  50322. const unsigned char* privDer = client_key_der_1024;
  50323. size_t privDerSz = sizeof_client_key_der_1024;
  50324. #else
  50325. const unsigned char* privDer = client_key_der_2048;
  50326. size_t privDerSz = sizeof_client_key_der_2048;
  50327. #endif
  50328. const unsigned char* der;
  50329. #ifndef NO_AES
  50330. unsigned char passwd[] = "password";
  50331. #endif
  50332. AssertNotNull(rsa = RSA_new());
  50333. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50334. NULL, NULL), 0);
  50335. RSA_free(rsa);
  50336. der = privDer;
  50337. rsa = NULL;
  50338. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50339. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  50340. NULL, NULL), 0);
  50341. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  50342. NULL, NULL), 0);
  50343. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50344. NULL, NULL), 1);
  50345. #ifndef NO_AES
  50346. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50347. NULL, 0, NULL, NULL), 1);
  50348. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50349. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  50350. #endif
  50351. RSA_free(rsa);
  50352. res = TEST_RES_CHECK(1);
  50353. #endif
  50354. return res;
  50355. }
  50356. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  50357. {
  50358. int res = TEST_SKIPPED;
  50359. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50360. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50361. defined(WOLFSSL_DER_TO_PEM))
  50362. RSA* rsa;
  50363. #ifdef USE_CERT_BUFFERS_1024
  50364. const unsigned char* privDer = client_key_der_1024;
  50365. size_t privDerSz = sizeof_client_key_der_1024;
  50366. #else
  50367. const unsigned char* privDer = client_key_der_2048;
  50368. size_t privDerSz = sizeof_client_key_der_2048;
  50369. #endif
  50370. const unsigned char* der;
  50371. #ifndef NO_AES
  50372. unsigned char passwd[] = "password";
  50373. #endif
  50374. unsigned char* pem;
  50375. int plen;
  50376. AssertNotNull(rsa = RSA_new());
  50377. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50378. &plen), 0);
  50379. RSA_free(rsa);
  50380. der = privDer;
  50381. rsa = NULL;
  50382. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50383. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  50384. &plen), 0);
  50385. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  50386. &plen), 0);
  50387. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50388. NULL), 0);
  50389. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50390. &plen), 1);
  50391. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50392. #ifndef NO_AES
  50393. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50394. NULL, 0, &pem, &plen), 1);
  50395. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50396. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50397. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  50398. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50399. #endif
  50400. RSA_free(rsa);
  50401. res = TEST_RES_CHECK(1);
  50402. #endif
  50403. return res;
  50404. }
  50405. static int test_wolfSSL_DH(void)
  50406. {
  50407. int res = TEST_SKIPPED;
  50408. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50409. DH *dh = NULL;
  50410. BIGNUM* p;
  50411. BIGNUM* q;
  50412. BIGNUM* g;
  50413. BIGNUM* pub;
  50414. BIGNUM* priv;
  50415. #if defined(OPENSSL_ALL)
  50416. #if !defined(HAVE_FIPS) || \
  50417. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  50418. FILE* f = NULL;
  50419. unsigned char buf[268];
  50420. const unsigned char* pt = buf;
  50421. long len = 0;
  50422. dh = NULL;
  50423. XMEMSET(buf, 0, sizeof(buf));
  50424. /* Test 2048 bit parameters */
  50425. f = XFOPEN("./certs/dh2048.der", "rb");
  50426. AssertTrue(f != XBADFILE);
  50427. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  50428. XFCLOSE(f);
  50429. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  50430. AssertNotNull(dh->p);
  50431. AssertNotNull(dh->g);
  50432. AssertTrue(pt == buf);
  50433. AssertIntEQ(DH_generate_key(dh), 1);
  50434. AssertIntEQ(DH_generate_key(dh), 1);
  50435. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  50436. AssertNotNull(pub = BN_new());
  50437. AssertIntEQ(BN_set_word(pub, 1), 1);
  50438. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  50439. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  50440. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  50441. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  50442. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  50443. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  50444. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  50445. BN_free(pub);
  50446. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50447. (const BIGNUM**)&q,
  50448. (const BIGNUM**)&g);
  50449. AssertPtrEq(p, dh->p);
  50450. AssertPtrEq(q, dh->q);
  50451. AssertPtrEq(g, dh->g);
  50452. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50453. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  50454. AssertPtrEq(pub, dh->pub_key);
  50455. AssertPtrEq(priv, dh->priv_key);
  50456. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  50457. AssertPtrEq(pub, dh->pub_key);
  50458. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  50459. AssertPtrEq(priv, dh->priv_key);
  50460. AssertNotNull(pub = BN_new());
  50461. AssertNotNull(priv = BN_new());
  50462. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  50463. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  50464. AssertNotNull(pub = BN_new());
  50465. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  50466. AssertNotNull(priv = BN_new());
  50467. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  50468. AssertPtrEq(pub, dh->pub_key);
  50469. AssertPtrEq(priv, dh->priv_key);
  50470. DH_free(dh);
  50471. AssertNotNull(dh = DH_new());
  50472. AssertNotNull(p = BN_new());
  50473. AssertIntEQ(BN_set_word(p, 1), 1);
  50474. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  50475. AssertNotNull(pub = BN_new());
  50476. AssertNotNull(priv = BN_new());
  50477. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  50478. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  50479. BN_free(p);
  50480. DH_free(dh);
  50481. #ifdef WOLFSSL_KEY_GEN
  50482. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  50483. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  50484. DH_free(dh);
  50485. #endif
  50486. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  50487. #endif /* OPENSSL_ALL */
  50488. (void)dh;
  50489. (void)p;
  50490. (void)q;
  50491. (void)g;
  50492. (void)pub;
  50493. (void)priv;
  50494. dh = wolfSSL_DH_new();
  50495. AssertNotNull(dh);
  50496. /* invalid parameters test */
  50497. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  50498. (const BIGNUM**)&q,
  50499. (const BIGNUM**)&g);
  50500. DH_get0_pqg(dh, NULL,
  50501. (const BIGNUM**)&q,
  50502. (const BIGNUM**)&g);
  50503. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  50504. DH_get0_pqg(dh, NULL, NULL, NULL);
  50505. AssertTrue(1);
  50506. DH_get0_pqg(dh, (const BIGNUM**)&p,
  50507. (const BIGNUM**)&q,
  50508. (const BIGNUM**)&g);
  50509. AssertPtrEq(p, NULL);
  50510. AssertPtrEq(q, NULL);
  50511. AssertPtrEq(g, NULL);
  50512. DH_free(dh);
  50513. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  50514. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  50515. #if defined(OPENSSL_ALL) || \
  50516. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50517. dh = wolfSSL_DH_new();
  50518. AssertNotNull(dh);
  50519. p = wolfSSL_BN_new();
  50520. AssertNotNull(p);
  50521. AssertIntEQ(BN_set_word(p, 11), 1);
  50522. g = wolfSSL_BN_new();
  50523. AssertNotNull(g);
  50524. AssertIntEQ(BN_set_word(g, 2), 1);
  50525. q = wolfSSL_BN_new();
  50526. AssertNotNull(q);
  50527. AssertIntEQ(BN_set_word(q, 5), 1);
  50528. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  50529. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  50530. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  50531. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  50532. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  50533. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  50534. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  50535. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  50536. /* Don't need q. */
  50537. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50538. /* Setting again will free the p and g. */
  50539. wolfSSL_BN_free(q);
  50540. DH_free(dh);
  50541. dh = wolfSSL_DH_new();
  50542. AssertNotNull(dh);
  50543. p = wolfSSL_BN_new();
  50544. AssertNotNull(p);
  50545. AssertIntEQ(BN_set_word(p, 11), 1);
  50546. g = wolfSSL_BN_new();
  50547. AssertNotNull(g);
  50548. AssertIntEQ(BN_set_word(g, 2), 1);
  50549. q = wolfSSL_BN_new();
  50550. AssertNotNull(q);
  50551. AssertIntEQ(BN_set_word(q, 5), 1);
  50552. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  50553. /* p, q and g are now owned by dh - don't free. */
  50554. p = wolfSSL_BN_new();
  50555. AssertNotNull(p);
  50556. AssertIntEQ(BN_set_word(p, 11), 1);
  50557. g = wolfSSL_BN_new();
  50558. AssertNotNull(g);
  50559. AssertIntEQ(BN_set_word(g, 2), 1);
  50560. q = wolfSSL_BN_new();
  50561. AssertNotNull(q);
  50562. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  50563. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  50564. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  50565. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  50566. /* p, q and g are now owned by dh - don't free. */
  50567. DH_free(dh);
  50568. AssertIntEQ(DH_generate_key(NULL), 0);
  50569. AssertNotNull(dh = DH_new());
  50570. AssertIntEQ(DH_generate_key(dh), 0);
  50571. p = wolfSSL_BN_new();
  50572. AssertNotNull(p);
  50573. AssertIntEQ(BN_set_word(p, 0), 1);
  50574. g = wolfSSL_BN_new();
  50575. AssertNotNull(g);
  50576. AssertIntEQ(BN_set_word(g, 2), 1);
  50577. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50578. AssertIntEQ(DH_generate_key(dh), 0);
  50579. DH_free(dh);
  50580. #endif
  50581. #endif
  50582. /* Test DH_up_ref() */
  50583. dh = wolfSSL_DH_new();
  50584. AssertNotNull(dh);
  50585. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  50586. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  50587. DH_free(dh); /* decrease ref count */
  50588. DH_free(dh); /* free WOLFSSL_DH */
  50589. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  50590. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  50591. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  50592. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  50593. #ifdef HAVE_FFDHE_2048
  50594. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50595. DH_free(dh);
  50596. #endif
  50597. #ifdef HAVE_FFDHE_3072
  50598. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  50599. DH_free(dh);
  50600. #endif
  50601. #ifdef HAVE_FFDHE_4096
  50602. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  50603. DH_free(dh);
  50604. #endif
  50605. #else
  50606. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50607. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  50608. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  50609. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  50610. res = TEST_RES_CHECK(1);
  50611. #endif /* OPENSSL_EXTRA && !NO_DH */
  50612. return res;
  50613. }
  50614. static int test_wolfSSL_DH_dup(void)
  50615. {
  50616. int res = TEST_SKIPPED;
  50617. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  50618. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  50619. defined(OPENSSL_EXTRA)
  50620. DH *dh;
  50621. DH *dhDup;
  50622. WOLFSSL_BIGNUM* p;
  50623. WOLFSSL_BIGNUM* g;
  50624. AssertNotNull(p = wolfSSL_BN_new());
  50625. AssertNotNull(g = wolfSSL_BN_new());
  50626. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  50627. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  50628. dhDup = wolfSSL_DH_dup(NULL);
  50629. AssertNull(dhDup);
  50630. dh = wolfSSL_DH_new();
  50631. AssertNotNull(dh);
  50632. dhDup = wolfSSL_DH_dup(dh);
  50633. AssertNull(dhDup);
  50634. #if defined(OPENSSL_ALL) || \
  50635. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50636. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50637. dhDup = wolfSSL_DH_dup(dh);
  50638. AssertNotNull(dhDup);
  50639. wolfSSL_DH_free(dhDup);
  50640. #else
  50641. wolfSSL_BN_free(p);
  50642. wolfSSL_BN_free(g);
  50643. #endif
  50644. wolfSSL_DH_free(dh);
  50645. res = TEST_RES_CHECK(1);
  50646. #endif
  50647. #endif
  50648. return res;
  50649. }
  50650. static int test_wolfSSL_DH_check(void)
  50651. {
  50652. int res = TEST_SKIPPED;
  50653. #ifdef OPENSSL_ALL
  50654. #ifndef NO_DH
  50655. #ifndef NO_BIO
  50656. #ifndef NO_DSA
  50657. byte buf[6000];
  50658. char file[] = "./certs/dsaparams.pem";
  50659. XFILE f;
  50660. int bytes;
  50661. BIO* bio;
  50662. DSA* dsa;
  50663. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50664. static const byte dh2048[] = {
  50665. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50666. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50667. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50668. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50669. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50670. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50671. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50672. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50673. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50674. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50675. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50676. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50677. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50678. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50679. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50680. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50681. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50682. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50683. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50684. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50685. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50686. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50687. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50688. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50689. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50690. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50691. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50692. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50693. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50694. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50695. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50696. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50697. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50698. 0x93, 0x02, 0x01, 0x02
  50699. };
  50700. const byte* params;
  50701. #endif
  50702. DH* dh = NULL;
  50703. WOLFSSL_BIGNUM* p;
  50704. WOLFSSL_BIGNUM* g;
  50705. WOLFSSL_BIGNUM* pTmp = NULL;
  50706. WOLFSSL_BIGNUM* gTmp = NULL;
  50707. int codes = -1;
  50708. #ifndef NO_DSA
  50709. /* Initialize DH */
  50710. f = XFOPEN(file, "rb");
  50711. AssertTrue((f != XBADFILE));
  50712. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  50713. XFCLOSE(f);
  50714. bio = BIO_new_mem_buf((void*)buf, bytes);
  50715. AssertNotNull(bio);
  50716. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  50717. AssertNotNull(dsa);
  50718. dh = wolfSSL_DSA_dup_DH(dsa);
  50719. AssertNotNull(dh);
  50720. BIO_free(bio);
  50721. DSA_free(dsa);
  50722. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50723. params = dh2048;
  50724. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  50725. AssertNotNull(dh);
  50726. #else
  50727. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  50728. AssertNotNull(dh);
  50729. #endif
  50730. /* Test assumed to be valid dh.
  50731. * Should return WOLFSSL_SUCCESS
  50732. * codes should be 0
  50733. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  50734. */
  50735. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50736. AssertIntEQ(codes, 0);
  50737. /* Test NULL dh: expected BAD_FUNC_ARG */
  50738. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  50739. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  50740. pTmp = dh->p;
  50741. dh->p = NULL;
  50742. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50743. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50744. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50745. /* set dh->p back to normal so it wont fail on next tests */
  50746. dh->p = pTmp;
  50747. pTmp = NULL;
  50748. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  50749. gTmp = dh->g;
  50750. dh->g = NULL;
  50751. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50752. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50753. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  50754. dh->g = gTmp;
  50755. gTmp = NULL;
  50756. /* Cleanup */
  50757. DH_free(dh);
  50758. dh = DH_new();
  50759. AssertNotNull(dh);
  50760. /* Check empty DH. */
  50761. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50762. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50763. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  50764. /* Check non-prime valued p. */
  50765. AssertNotNull(p = BN_new());
  50766. AssertIntEQ(BN_set_word(p, 4), 1);
  50767. AssertNotNull(g = BN_new());
  50768. AssertIntEQ(BN_set_word(g, 2), 1);
  50769. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  50770. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50771. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50772. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50773. DH_free(dh);
  50774. res = TEST_RES_CHECK(1);
  50775. #endif
  50776. #endif /* !NO_DH && !NO_DSA */
  50777. #endif
  50778. return res;
  50779. }
  50780. static int test_wolfSSL_DH_prime(void)
  50781. {
  50782. int res = TEST_SKIPPED;
  50783. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50784. WOLFSSL_BIGNUM* bn;
  50785. #if WOLFSSL_MAX_BN_BITS >= 768
  50786. WOLFSSL_BIGNUM* bn2;
  50787. #endif
  50788. bn = wolfSSL_DH_768_prime(NULL);
  50789. #if WOLFSSL_MAX_BN_BITS >= 768
  50790. AssertNotNull(bn);
  50791. bn2 = wolfSSL_DH_768_prime(bn);
  50792. AssertNotNull(bn2);
  50793. AssertTrue(bn == bn2);
  50794. wolfSSL_BN_free(bn);
  50795. #else
  50796. AssertNull(bn);
  50797. #endif
  50798. bn = wolfSSL_DH_1024_prime(NULL);
  50799. #if WOLFSSL_MAX_BN_BITS >= 1024
  50800. AssertNotNull(bn);
  50801. wolfSSL_BN_free(bn);
  50802. #else
  50803. AssertNull(bn);
  50804. #endif
  50805. bn = wolfSSL_DH_2048_prime(NULL);
  50806. #if WOLFSSL_MAX_BN_BITS >= 2048
  50807. AssertNotNull(bn);
  50808. wolfSSL_BN_free(bn);
  50809. #else
  50810. AssertNull(bn);
  50811. #endif
  50812. bn = wolfSSL_DH_3072_prime(NULL);
  50813. #if WOLFSSL_MAX_BN_BITS >= 3072
  50814. AssertNotNull(bn);
  50815. wolfSSL_BN_free(bn);
  50816. #else
  50817. AssertNull(bn);
  50818. #endif
  50819. bn = wolfSSL_DH_4096_prime(NULL);
  50820. #if WOLFSSL_MAX_BN_BITS >= 4096
  50821. AssertNotNull(bn);
  50822. wolfSSL_BN_free(bn);
  50823. #else
  50824. AssertNull(bn);
  50825. #endif
  50826. bn = wolfSSL_DH_6144_prime(NULL);
  50827. #if WOLFSSL_MAX_BN_BITS >= 6144
  50828. AssertNotNull(bn);
  50829. wolfSSL_BN_free(bn);
  50830. #else
  50831. AssertNull(bn);
  50832. #endif
  50833. bn = wolfSSL_DH_8192_prime(NULL);
  50834. #if WOLFSSL_MAX_BN_BITS >= 8192
  50835. AssertNotNull(bn);
  50836. wolfSSL_BN_free(bn);
  50837. #else
  50838. AssertNull(bn);
  50839. #endif
  50840. res = TEST_RES_CHECK(1);
  50841. #endif
  50842. return res;
  50843. }
  50844. static int test_wolfSSL_DH_1536_prime(void)
  50845. {
  50846. int res = TEST_SKIPPED;
  50847. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50848. BIGNUM* bn;
  50849. unsigned char bits[200];
  50850. int sz = 192; /* known binary size */
  50851. const byte expected[] = {
  50852. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50853. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  50854. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  50855. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  50856. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  50857. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  50858. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  50859. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  50860. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  50861. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  50862. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  50863. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  50864. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  50865. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  50866. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  50867. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  50868. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  50869. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  50870. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  50871. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  50872. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  50873. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  50874. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  50875. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50876. };
  50877. bn = get_rfc3526_prime_1536(NULL);
  50878. AssertNotNull(bn);
  50879. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  50880. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  50881. BN_free(bn);
  50882. res = TEST_RES_CHECK(1);
  50883. #endif
  50884. return res;
  50885. }
  50886. static int test_wolfSSL_DH_get_2048_256(void)
  50887. {
  50888. int res = TEST_SKIPPED;
  50889. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50890. WOLFSSL_DH* dh;
  50891. const WOLFSSL_BIGNUM* pBn;
  50892. const WOLFSSL_BIGNUM* gBn;
  50893. const WOLFSSL_BIGNUM* qBn;
  50894. const byte pExpected[] = {
  50895. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  50896. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  50897. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  50898. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  50899. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  50900. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  50901. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  50902. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  50903. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  50904. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  50905. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  50906. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  50907. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  50908. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  50909. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  50910. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  50911. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  50912. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  50913. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  50914. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  50915. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  50916. 0x1E, 0x1A, 0x15, 0x97
  50917. };
  50918. const byte gExpected[] = {
  50919. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  50920. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  50921. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  50922. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  50923. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  50924. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  50925. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  50926. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  50927. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  50928. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  50929. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  50930. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  50931. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  50932. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  50933. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  50934. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  50935. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  50936. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  50937. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  50938. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  50939. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  50940. 0x6C, 0xC4, 0x16, 0x59
  50941. };
  50942. const byte qExpected[] = {
  50943. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  50944. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  50945. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  50946. };
  50947. int pSz;
  50948. int qSz;
  50949. int gSz;
  50950. byte* pReturned;
  50951. byte* qReturned;
  50952. byte* gReturned;
  50953. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  50954. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  50955. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  50956. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50957. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  50958. AssertIntEQ(pSz, sizeof(pExpected));
  50959. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  50960. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  50961. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50962. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  50963. AssertIntEQ(qSz, sizeof(qExpected));
  50964. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  50965. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  50966. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50967. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  50968. AssertIntEQ(gSz, sizeof(gExpected));
  50969. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  50970. wolfSSL_DH_free(dh);
  50971. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50972. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50973. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50974. res = TEST_RES_CHECK(1);
  50975. #endif
  50976. return res;
  50977. }
  50978. static int test_wolfSSL_PEM_write_DHparams(void)
  50979. {
  50980. int res = TEST_SKIPPED;
  50981. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  50982. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  50983. DH* dh;
  50984. BIO* bio;
  50985. XFILE fp;
  50986. byte pem[2048];
  50987. int pemSz;
  50988. const char expected[] =
  50989. "-----BEGIN DH PARAMETERS-----\n"
  50990. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  50991. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  50992. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  50993. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  50994. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  50995. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  50996. "-----END DH PARAMETERS-----\n";
  50997. const char badPem[] =
  50998. "-----BEGIN DH PARAMETERS-----\n"
  50999. "-----END DH PARAMETERS-----\n";
  51000. const char emptySeqPem[] =
  51001. "-----BEGIN DH PARAMETERS-----\n"
  51002. "MAA=\n"
  51003. "-----END DH PARAMETERS-----\n";
  51004. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  51005. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51006. XFCLOSE(fp);
  51007. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  51008. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51009. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51010. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51011. (int)sizeof(badPem));
  51012. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51013. BIO_free(bio);
  51014. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51015. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51016. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51017. (int)sizeof(emptySeqPem));
  51018. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51019. BIO_free(bio);
  51020. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  51021. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51022. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51023. BIO_free(bio);
  51024. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51025. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51026. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51027. DH_free(dh);
  51028. dh = wolfSSL_DH_new();
  51029. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51030. XFCLOSE(fp);
  51031. wolfSSL_DH_free(dh);
  51032. /* check results */
  51033. XMEMSET(pem, 0, sizeof(pem));
  51034. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  51035. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51036. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51037. XFCLOSE(fp);
  51038. res = TEST_RES_CHECK(1);
  51039. #endif
  51040. return res;
  51041. }
  51042. static int test_wolfSSL_d2i_DHparams(void)
  51043. {
  51044. int res = TEST_SKIPPED;
  51045. #ifdef OPENSSL_ALL
  51046. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51047. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51048. FILE* f = NULL;
  51049. unsigned char buf[4096];
  51050. const unsigned char* pt = buf;
  51051. #ifdef HAVE_FFDHE_2048
  51052. const char* params1 = "./certs/dh2048.der";
  51053. #endif
  51054. #ifdef HAVE_FFDHE_3072
  51055. const char* params2 = "./certs/dh3072.der";
  51056. #endif
  51057. long len = 0;
  51058. WOLFSSL_DH* dh = NULL;
  51059. XMEMSET(buf, 0, sizeof(buf));
  51060. /* Test 2048 bit parameters */
  51061. #ifdef HAVE_FFDHE_2048
  51062. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  51063. f = XFOPEN(params1, "rb");
  51064. AssertTrue(f != XBADFILE);
  51065. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51066. XFCLOSE(f);
  51067. /* Valid case */
  51068. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51069. AssertNotNull(dh->p);
  51070. AssertNotNull(dh->g);
  51071. AssertTrue(pt == buf);
  51072. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  51073. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  51074. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  51075. /* Invalid cases */
  51076. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51077. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51078. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  51079. DH_free(dh);
  51080. *buf = 0;
  51081. pt = buf;
  51082. res = TEST_RES_CHECK(1);
  51083. }
  51084. #endif /* HAVE_FFDHE_2048 */
  51085. /* Test 3072 bit parameters */
  51086. #ifdef HAVE_FFDHE_3072
  51087. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  51088. f = XFOPEN(params2, "rb");
  51089. AssertTrue(f != XBADFILE);
  51090. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51091. XFCLOSE(f);
  51092. /* Valid case */
  51093. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  51094. AssertNotNull(dh->p);
  51095. AssertNotNull(dh->g);
  51096. AssertTrue(pt != buf);
  51097. AssertIntEQ(DH_generate_key(dh), 1);
  51098. /* Invalid cases */
  51099. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51100. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51101. DH_free(dh);
  51102. res = TEST_RES_CHECK(1);
  51103. }
  51104. #endif /* HAVE_FFDHE_3072 */
  51105. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51106. #endif /* !NO_DH */
  51107. #endif
  51108. return res;
  51109. }
  51110. static int test_wolfSSL_DH_LoadDer(void)
  51111. {
  51112. int res = TEST_SKIPPED;
  51113. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  51114. defined(OPENSSL_EXTRA)
  51115. static const byte dh2048[] = {
  51116. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51117. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51118. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51119. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51120. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51121. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51122. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51123. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51124. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51125. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51126. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51127. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51128. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51129. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51130. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51131. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51132. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51133. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51134. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51135. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51136. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51137. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51138. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51139. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51140. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51141. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51142. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51143. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51144. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51145. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51146. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51147. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51148. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51149. 0x93, 0x02, 0x01, 0x02
  51150. };
  51151. WOLFSSL_DH* dh;
  51152. dh = wolfSSL_DH_new();
  51153. AssertNotNull(dh);
  51154. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  51155. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  51156. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  51157. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  51158. wolfSSL_DH_free(dh);
  51159. res = TEST_RES_CHECK(1);
  51160. #endif
  51161. return res;
  51162. }
  51163. static int test_wolfSSL_i2d_DHparams(void)
  51164. {
  51165. int res = TEST_SKIPPED;
  51166. #ifdef OPENSSL_ALL
  51167. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51168. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51169. FILE* f;
  51170. unsigned char buf[4096];
  51171. const unsigned char* pt;
  51172. unsigned char* pt2;
  51173. #ifdef HAVE_FFDHE_2048
  51174. const char* params1 = "./certs/dh2048.der";
  51175. #endif
  51176. #ifdef HAVE_FFDHE_3072
  51177. const char* params2 = "./certs/dh3072.der";
  51178. #endif
  51179. long len;
  51180. WOLFSSL_DH* dh;
  51181. /* Test 2048 bit parameters */
  51182. #ifdef HAVE_FFDHE_2048
  51183. pt = buf;
  51184. pt2 = buf;
  51185. f = XFOPEN(params1, "rb");
  51186. AssertTrue(f != XBADFILE);
  51187. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51188. XFCLOSE(f);
  51189. /* Valid case */
  51190. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51191. AssertTrue(pt == buf);
  51192. AssertIntEQ(DH_generate_key(dh), 1);
  51193. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  51194. /* Invalid case */
  51195. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51196. /* Return length only */
  51197. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  51198. DH_free(dh);
  51199. *buf = 0;
  51200. #endif
  51201. /* Test 3072 bit parameters */
  51202. #ifdef HAVE_FFDHE_3072
  51203. pt = buf;
  51204. pt2 = buf;
  51205. f = XFOPEN(params2, "rb");
  51206. AssertTrue(f != XBADFILE);
  51207. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  51208. XFCLOSE(f);
  51209. /* Valid case */
  51210. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51211. AssertTrue(pt == buf);
  51212. AssertIntEQ(DH_generate_key(dh), 1);
  51213. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  51214. /* Invalid case */
  51215. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51216. /* Return length only */
  51217. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  51218. DH_free(dh);
  51219. #endif
  51220. dh = DH_new();
  51221. AssertNotNull(dh);
  51222. pt2 = buf;
  51223. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  51224. DH_free(dh);
  51225. res = TEST_RES_CHECK(1);
  51226. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51227. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  51228. #endif
  51229. return res;
  51230. }
  51231. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  51232. /*----------------------------------------------------------------------------*
  51233. | EC
  51234. *----------------------------------------------------------------------------*/
  51235. static int test_wolfSSL_EC_GROUP(void)
  51236. {
  51237. int res = TEST_SKIPPED;
  51238. #ifdef OPENSSL_EXTRA
  51239. EC_GROUP *group;
  51240. EC_GROUP *group2;
  51241. EC_GROUP *group3;
  51242. #ifndef HAVE_ECC_BRAINPOOL
  51243. EC_GROUP *group4;
  51244. #endif
  51245. WOLFSSL_BIGNUM* order;
  51246. int group_bits;
  51247. int i;
  51248. static const int knownEccNids[] = {
  51249. NID_X9_62_prime192v1,
  51250. NID_X9_62_prime192v2,
  51251. NID_X9_62_prime192v3,
  51252. NID_X9_62_prime239v1,
  51253. NID_X9_62_prime239v2,
  51254. NID_X9_62_prime239v3,
  51255. NID_X9_62_prime256v1,
  51256. NID_secp112r1,
  51257. NID_secp112r2,
  51258. NID_secp128r1,
  51259. NID_secp128r2,
  51260. NID_secp160r1,
  51261. NID_secp160r2,
  51262. NID_secp224r1,
  51263. NID_secp384r1,
  51264. NID_secp521r1,
  51265. NID_secp160k1,
  51266. NID_secp192k1,
  51267. NID_secp224k1,
  51268. NID_secp256k1,
  51269. NID_brainpoolP160r1,
  51270. NID_brainpoolP192r1,
  51271. NID_brainpoolP224r1,
  51272. NID_brainpoolP256r1,
  51273. NID_brainpoolP320r1,
  51274. NID_brainpoolP384r1,
  51275. NID_brainpoolP512r1,
  51276. };
  51277. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  51278. static const int knownEccEnums[] = {
  51279. ECC_SECP192R1,
  51280. ECC_PRIME192V2,
  51281. ECC_PRIME192V3,
  51282. ECC_PRIME239V1,
  51283. ECC_PRIME239V2,
  51284. ECC_PRIME239V3,
  51285. ECC_SECP256R1,
  51286. ECC_SECP112R1,
  51287. ECC_SECP112R2,
  51288. ECC_SECP128R1,
  51289. ECC_SECP128R2,
  51290. ECC_SECP160R1,
  51291. ECC_SECP160R2,
  51292. ECC_SECP224R1,
  51293. ECC_SECP384R1,
  51294. ECC_SECP521R1,
  51295. ECC_SECP160K1,
  51296. ECC_SECP192K1,
  51297. ECC_SECP224K1,
  51298. ECC_SECP256K1,
  51299. ECC_BRAINPOOLP160R1,
  51300. ECC_BRAINPOOLP192R1,
  51301. ECC_BRAINPOOLP224R1,
  51302. ECC_BRAINPOOLP256R1,
  51303. ECC_BRAINPOOLP320R1,
  51304. ECC_BRAINPOOLP384R1,
  51305. ECC_BRAINPOOLP512R1,
  51306. };
  51307. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  51308. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51309. AssertNotNull(group2 = EC_GROUP_dup(group));
  51310. AssertNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  51311. #ifndef HAVE_ECC_BRAINPOOL
  51312. AssertNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  51313. NID_brainpoolP256r1));
  51314. #endif
  51315. AssertNull(EC_GROUP_dup(NULL));
  51316. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  51317. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51318. AssertIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  51319. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  51320. #ifndef HAVE_ECC_BRAINPOOL
  51321. AssertIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  51322. #endif
  51323. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  51324. AssertIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  51325. AssertNotNull(order = BN_new());
  51326. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  51327. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  51328. AssertIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  51329. AssertIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  51330. wolfSSL_BN_free(order);
  51331. AssertNotNull(EC_GROUP_method_of(group));
  51332. AssertIntEQ(EC_METHOD_get_field_type(NULL), 0);
  51333. AssertIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  51334. NID_X9_62_prime_field);
  51335. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  51336. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  51337. AssertIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  51338. AssertIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  51339. #ifndef NO_WOLFSSL_STUB
  51340. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  51341. #endif
  51342. #ifndef HAVE_ECC_BRAINPOOL
  51343. EC_GROUP_free(group4);
  51344. #endif
  51345. EC_GROUP_free(group3);
  51346. EC_GROUP_free(group2);
  51347. EC_GROUP_free(group);
  51348. for (i = 0; i < knowEccNidsLen; i++) {
  51349. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  51350. AssertIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  51351. EC_GROUP_free(group);
  51352. }
  51353. for (i = 0; i < knowEccEnumsLen; i++) {
  51354. AssertNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  51355. AssertIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  51356. EC_GROUP_free(group);
  51357. }
  51358. res = TEST_RES_CHECK(1);
  51359. #endif
  51360. return res;
  51361. }
  51362. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  51363. {
  51364. int res = TEST_SKIPPED;
  51365. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51366. EC_GROUP *group;
  51367. BIO* bio;
  51368. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51369. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51370. EC_GROUP *ret;
  51371. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  51372. "BgUrgQQAIg==\n"
  51373. "-----END EC PARAMETERS-----";
  51374. #endif
  51375. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  51376. "MAA=\n"
  51377. "-----END EC PARAMETERS-----";
  51378. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  51379. "BgA=\n"
  51380. "-----END EC PARAMETERS-----";
  51381. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  51382. "BgE=\n"
  51383. "-----END EC PARAMETERS-----";
  51384. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  51385. "BgE*\n"
  51386. "-----END EC PARAMETERS-----";
  51387. /* Test that first parameter, bio, being NULL fails. */
  51388. AssertNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  51389. /* Test that reading named parameters works. */
  51390. AssertNotNull(bio = BIO_new(BIO_s_file()));
  51391. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  51392. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51393. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  51394. BIO_free(bio);
  51395. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  51396. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  51397. /* Test that reusing group works. */
  51398. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51399. sizeof(ec_nc_p384)));
  51400. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51401. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51402. BIO_free(bio);
  51403. EC_GROUP_free(group);
  51404. group = NULL;
  51405. /* Test that returning through group works. */
  51406. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  51407. sizeof(ec_nc_p384)));
  51408. AssertNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  51409. AssertIntEQ(group == ret, 1);
  51410. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  51411. BIO_free(bio);
  51412. #endif
  51413. EC_GROUP_free(group);
  51414. /* Test 0x30, 0x00 (not and object id) fails. */
  51415. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  51416. sizeof(ec_nc_bad_1)));
  51417. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51418. BIO_free(bio);
  51419. /* Test 0x06, 0x00 (empty object id) fails. */
  51420. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  51421. sizeof(ec_nc_bad_2)));
  51422. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51423. BIO_free(bio);
  51424. /* Test 0x06, 0x01 (badly formed object id) fails. */
  51425. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  51426. sizeof(ec_nc_bad_3)));
  51427. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51428. BIO_free(bio);
  51429. /* Test invalid PEM encoding - invalid character. */
  51430. AssertNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  51431. sizeof(ec_nc_bad_4)));
  51432. AssertNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  51433. BIO_free(bio);
  51434. res = TEST_RES_CHECK(1);
  51435. #endif
  51436. return res;
  51437. }
  51438. static int test_wolfSSL_EC_POINT(void)
  51439. {
  51440. int res = TEST_SKIPPED;
  51441. #if !defined(WOLFSSL_SP_MATH) && \
  51442. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  51443. #ifdef OPENSSL_EXTRA
  51444. BN_CTX* ctx;
  51445. EC_GROUP* group;
  51446. #ifndef HAVE_ECC_BRAINPOOL
  51447. EC_GROUP* group2;
  51448. #endif
  51449. EC_POINT* Gxy;
  51450. EC_POINT* new_point;
  51451. EC_POINT* set_point;
  51452. EC_POINT* infinity;
  51453. BIGNUM* k = NULL;
  51454. BIGNUM* Gx = NULL;
  51455. BIGNUM* Gy = NULL;
  51456. BIGNUM* Gz = NULL;
  51457. BIGNUM* X;
  51458. BIGNUM* Y;
  51459. BIGNUM* set_point_bn;
  51460. char* hexStr;
  51461. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  51462. "17AF381913FF7A96314EA47055EA0FD0";
  51463. /* NISTP256R1 Gx/Gy */
  51464. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  51465. "77037D812DEB33A0F4A13945D898C296";
  51466. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51467. "2BCE33576B315ECECBB6406837BF51F5";
  51468. #ifndef HAVE_SELFTEST
  51469. EC_POINT *tmp;
  51470. size_t bin_len;
  51471. unsigned int blen;
  51472. unsigned char* buf = NULL;
  51473. unsigned char bufInf[1] = { 0x00 };
  51474. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  51475. "77037D812DEB33A0F4A13945D898C296"
  51476. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  51477. "2BCE33576B315ECECBB6406837BF51F5";
  51478. const unsigned char binUncompG[] = {
  51479. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51480. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51481. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51482. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51483. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51484. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51485. };
  51486. const unsigned char binUncompGBad[] = {
  51487. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51488. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51489. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51490. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51491. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51492. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51493. };
  51494. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  51495. "77037D812DEB33A0F4A13945D898C296";
  51496. #ifdef HAVE_COMP_KEY
  51497. const unsigned char binCompG[] = {
  51498. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51499. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51500. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51501. };
  51502. #endif
  51503. #endif
  51504. AssertNotNull(ctx = BN_CTX_new());
  51505. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51506. #ifndef HAVE_ECC_BRAINPOOL
  51507. /* Used to make groups curve_idx == -1. */
  51508. AssertNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  51509. #endif
  51510. AssertNull(EC_POINT_new(NULL));
  51511. AssertNotNull(Gxy = EC_POINT_new(group));
  51512. AssertNotNull(new_point = EC_POINT_new(group));
  51513. AssertNotNull(set_point = EC_POINT_new(group));
  51514. AssertNotNull(X = BN_new());
  51515. AssertNotNull(Y = BN_new());
  51516. AssertNotNull(set_point_bn = BN_new());
  51517. AssertNotNull(infinity = EC_POINT_new(group));
  51518. /* load test values */
  51519. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  51520. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  51521. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  51522. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  51523. /* populate coordinates for input point */
  51524. Gxy->X = Gx;
  51525. Gxy->Y = Gy;
  51526. Gxy->Z = Gz;
  51527. /* Test handling of NULL point. */
  51528. EC_POINT_clear_free(NULL);
  51529. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51530. NULL, NULL, ctx), 0);
  51531. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51532. NULL, NULL, ctx), 0);
  51533. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51534. NULL, NULL, ctx), 0);
  51535. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51536. X, NULL, ctx), 0);
  51537. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51538. NULL, Y, ctx), 0);
  51539. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51540. X, Y, ctx), 0);
  51541. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51542. X, Y, ctx), 0);
  51543. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51544. NULL, Y, ctx), 0);
  51545. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51546. X, NULL, ctx), 0);
  51547. /* Getting point at infinity returns an error. */
  51548. AssertIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  51549. X, Y, ctx), 0);
  51550. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51551. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51552. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51553. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  51554. AssertIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  51555. AssertIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  51556. AssertIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  51557. AssertIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  51558. AssertIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  51559. AssertIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  51560. AssertIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  51561. AssertIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  51562. AssertIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  51563. AssertIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  51564. AssertIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  51565. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  51566. /* perform point multiplication */
  51567. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  51568. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51569. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51570. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51571. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  51572. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51573. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51574. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51575. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  51576. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51577. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51578. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51579. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  51580. AssertIntEQ(BN_is_zero(new_point->X), 1);
  51581. AssertIntEQ(BN_is_zero(new_point->Y), 1);
  51582. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  51583. /* Set point to something. */
  51584. AssertIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  51585. #else
  51586. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  51587. ctx), 1);
  51588. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51589. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  51590. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  51591. #endif
  51592. /* check if point X coordinate is zero */
  51593. AssertIntEQ(BN_is_zero(new_point->X), 0);
  51594. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  51595. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  51596. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  51597. #endif
  51598. /* extract the coordinates from point */
  51599. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51600. ctx), WOLFSSL_SUCCESS);
  51601. /* check if point X coordinate is zero */
  51602. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  51603. /* set the same X and Y points in another object */
  51604. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  51605. ctx), WOLFSSL_SUCCESS);
  51606. /* compare points as they should be the same */
  51607. AssertIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  51608. AssertIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  51609. AssertIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  51610. AssertIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  51611. AssertIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  51612. AssertIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  51613. AssertIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  51614. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  51615. /* Test copying */
  51616. AssertIntEQ(EC_POINT_copy(NULL, NULL), 0);
  51617. AssertIntEQ(EC_POINT_copy(NULL, set_point), 0);
  51618. AssertIntEQ(EC_POINT_copy(new_point, NULL), 0);
  51619. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  51620. /* Test inverting */
  51621. AssertIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  51622. AssertIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  51623. AssertIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  51624. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  51625. /* Test getting affine converts from projective. */
  51626. AssertIntEQ(EC_POINT_copy(set_point, new_point), 1);
  51627. /* Force non-affine coordinates */
  51628. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51629. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  51630. new_point->inSet = 0;
  51631. /* extract the coordinates from point */
  51632. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51633. ctx), WOLFSSL_SUCCESS);
  51634. /* check if point ordinates have changed. */
  51635. AssertIntNE(BN_cmp(X, set_point->X), 0);
  51636. AssertIntNE(BN_cmp(Y, set_point->Y), 0);
  51637. /* Test check for infinity */
  51638. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  51639. AssertIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  51640. AssertIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  51641. AssertIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  51642. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  51643. AssertIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  51644. #else
  51645. AssertIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  51646. #endif
  51647. AssertPtrEq(EC_POINT_point2bn(group, set_point,
  51648. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  51649. /* check bn2hex */
  51650. hexStr = BN_bn2hex(k);
  51651. AssertStrEQ(hexStr, kTest);
  51652. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51653. BN_print_fp(stderr, k);
  51654. fprintf(stderr, "\n");
  51655. #endif
  51656. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51657. hexStr = BN_bn2hex(Gx);
  51658. AssertStrEQ(hexStr, kGx);
  51659. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51660. BN_print_fp(stderr, Gx);
  51661. fprintf(stderr, "\n");
  51662. #endif
  51663. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51664. hexStr = BN_bn2hex(Gy);
  51665. AssertStrEQ(hexStr, kGy);
  51666. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51667. BN_print_fp(stderr, Gy);
  51668. fprintf(stderr, "\n");
  51669. #endif
  51670. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51671. #ifndef HAVE_SELFTEST
  51672. /* Test point to hex */
  51673. AssertNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51674. ctx));
  51675. AssertNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51676. ctx));
  51677. AssertNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51678. ctx));
  51679. #ifndef HAVE_ECC_BRAINPOOL
  51680. /* Group not supported in wolfCrypt. */
  51681. AssertNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51682. ctx));
  51683. #endif
  51684. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  51685. AssertStrEQ(hexStr, uncompG);
  51686. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51687. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  51688. AssertStrEQ(hexStr, compG);
  51689. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51690. /* Test point to oct */
  51691. AssertIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51692. NULL, 0, ctx), 0);
  51693. AssertIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51694. NULL, 0, ctx), 0);
  51695. AssertIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51696. NULL, 0, ctx), 0);
  51697. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51698. NULL, 0, ctx);
  51699. AssertIntEQ(bin_len, sizeof(binUncompG));
  51700. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51701. DYNAMIC_TYPE_ECC));
  51702. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51703. buf, bin_len, ctx), bin_len);
  51704. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51705. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51706. /* Infinity (x=0, y=0) encodes as '0x00'. */
  51707. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51708. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  51709. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51710. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  51711. AssertIntEQ(EC_POINT_point2oct(group, infinity,
  51712. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  51713. AssertIntEQ(bufInf[0], 0);
  51714. wolfSSL_EC_POINT_dump(NULL, NULL);
  51715. /* Test point i2d */
  51716. AssertIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  51717. AssertIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  51718. AssertIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  51719. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  51720. AssertIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  51721. AssertIntEQ(blen, sizeof(binUncompG));
  51722. AssertNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  51723. blen -= 1;
  51724. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  51725. blen += 1;
  51726. AssertIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  51727. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51728. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51729. #ifdef HAVE_COMP_KEY
  51730. /* Test point to oct compressed */
  51731. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  51732. 0, ctx);
  51733. AssertIntEQ(bin_len, sizeof(binCompG));
  51734. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51735. DYNAMIC_TYPE_ECC));
  51736. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  51737. bin_len, ctx), bin_len);
  51738. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  51739. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51740. #endif
  51741. /* Test point BN */
  51742. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  51743. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51744. AssertNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  51745. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51746. AssertNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  51747. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51748. AssertNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  51749. /* Test oct to point */
  51750. AssertNotNull(tmp = EC_POINT_new(group));
  51751. AssertIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  51752. ctx), 0);
  51753. AssertIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  51754. ctx), 0);
  51755. AssertIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  51756. ctx), 0);
  51757. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  51758. sizeof(binUncompGBad), ctx), 0);
  51759. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  51760. ctx), 1);
  51761. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51762. EC_POINT_free(tmp);
  51763. /* Test setting BN ordinates. */
  51764. AssertNotNull(tmp = EC_POINT_new(group));
  51765. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51766. NULL, ctx), 0);
  51767. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  51768. NULL, ctx), 0);
  51769. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  51770. NULL, ctx), 0);
  51771. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  51772. NULL, ctx), 0);
  51773. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51774. Gy, ctx), 0);
  51775. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  51776. ctx), 0);
  51777. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  51778. ctx), 0);
  51779. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  51780. Gy, ctx), 0);
  51781. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  51782. NULL, ctx), 0);
  51783. AssertIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  51784. ctx), 1);
  51785. EC_POINT_free(tmp);
  51786. /* Test point d2i */
  51787. AssertNotNull(tmp = EC_POINT_new(group));
  51788. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  51789. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  51790. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  51791. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  51792. AssertIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  51793. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  51794. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  51795. AssertIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  51796. AssertIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  51797. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51798. EC_POINT_free(tmp);
  51799. #ifdef HAVE_COMP_KEY
  51800. /* Test oct compressed to point */
  51801. AssertNotNull(tmp = EC_POINT_new(group));
  51802. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  51803. 1);
  51804. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51805. EC_POINT_free(tmp);
  51806. /* Test point d2i - compressed */
  51807. AssertNotNull(tmp = EC_POINT_new(group));
  51808. AssertIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  51809. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51810. EC_POINT_free(tmp);
  51811. #endif
  51812. #endif
  51813. /* test BN_mod_add */
  51814. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51815. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  51816. 1);
  51817. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  51818. /* cleanup */
  51819. BN_free(X);
  51820. BN_free(Y);
  51821. BN_free(k);
  51822. BN_free(set_point_bn);
  51823. EC_POINT_free(infinity);
  51824. EC_POINT_free(new_point);
  51825. EC_POINT_free(set_point);
  51826. EC_POINT_clear_free(Gxy);
  51827. #ifndef HAVE_ECC_BRAINPOOL
  51828. EC_GROUP_free(group2);
  51829. #endif
  51830. EC_GROUP_free(group);
  51831. BN_CTX_free(ctx);
  51832. res = TEST_RES_CHECK(1);
  51833. #endif
  51834. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  51835. return res;
  51836. }
  51837. static int test_wolfSSL_EC_KEY_generate(void)
  51838. {
  51839. int res = TEST_SKIPPED;
  51840. #ifdef OPENSSL_EXTRA
  51841. WOLFSSL_EC_KEY* key;
  51842. #ifndef HAVE_ECC_BRAINPOOL
  51843. WOLFSSL_EC_GROUP* group;
  51844. #endif
  51845. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51846. AssertIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  51847. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  51848. wolfSSL_EC_KEY_free(key);
  51849. #ifndef HAVE_ECC_BRAINPOOL
  51850. AssertNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  51851. NID_brainpoolP256r1));
  51852. AssertNotNull(key = wolfSSL_EC_KEY_new());
  51853. AssertIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  51854. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  51855. wolfSSL_EC_KEY_free(key);
  51856. wolfSSL_EC_GROUP_free(group);
  51857. #endif
  51858. res = TEST_RES_CHECK(1);
  51859. #endif
  51860. return res;
  51861. }
  51862. static int test_EC_i2d(void)
  51863. {
  51864. int res = TEST_SKIPPED;
  51865. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  51866. EC_KEY *key;
  51867. EC_KEY *copy = NULL;
  51868. int len;
  51869. unsigned char *buf = NULL;
  51870. unsigned char *p;
  51871. const unsigned char *tmp = NULL;
  51872. const unsigned char octBad[] = {
  51873. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51874. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51875. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51876. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51877. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51878. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51879. };
  51880. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51881. AssertIntEQ(EC_KEY_generate_key(key), 1);
  51882. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  51883. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51884. DYNAMIC_TYPE_TMP_BUFFER));
  51885. p = buf;
  51886. AssertIntEQ(i2d_EC_PUBKEY(key, &p), len);
  51887. AssertNull(o2i_ECPublicKey(NULL, NULL, -1));
  51888. AssertNull(o2i_ECPublicKey(&copy, NULL, -1));
  51889. AssertNull(o2i_ECPublicKey(&key, NULL, -1));
  51890. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51891. AssertNull(o2i_ECPublicKey(NULL, NULL, 0));
  51892. AssertNull(o2i_ECPublicKey(&key, NULL, 0));
  51893. AssertNull(o2i_ECPublicKey(&key, &tmp, 0));
  51894. tmp = buf;
  51895. AssertNull(o2i_ECPublicKey(NULL, &tmp, 0));
  51896. AssertNull(o2i_ECPublicKey(&copy, &tmp, 0));
  51897. AssertNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51898. AssertNull(o2i_ECPublicKey(&key, &tmp, -1));
  51899. AssertIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  51900. AssertIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  51901. tmp = buf;
  51902. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  51903. AssertNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  51904. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  51905. AssertNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  51906. AssertNull(d2i_ECPrivateKey(&key, &tmp, 0));
  51907. AssertIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  51908. AssertIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  51909. AssertIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  51910. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  51911. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  51912. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  51913. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  51914. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  51915. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51916. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  51917. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51918. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  51919. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51920. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  51921. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51922. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  51923. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  51924. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51925. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51926. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51927. AssertIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51928. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51929. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51930. buf = NULL;
  51931. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  51932. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51933. DYNAMIC_TYPE_TMP_BUFFER));
  51934. p = buf;
  51935. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  51936. p = NULL;
  51937. AssertIntEQ(i2d_ECPrivateKey(key, &p), len);
  51938. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51939. /* Bad point is also an invalid private key. */
  51940. tmp = octBad;
  51941. AssertNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  51942. tmp = buf;
  51943. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  51944. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51945. buf = NULL;
  51946. AssertIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  51947. AssertNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51948. DYNAMIC_TYPE_TMP_BUFFER));
  51949. p = buf;
  51950. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51951. p = NULL;
  51952. AssertIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51953. tmp = buf;
  51954. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  51955. tmp = octBad;
  51956. AssertNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  51957. AssertIntEQ(EC_KEY_check_key(NULL), 0);
  51958. AssertIntEQ(EC_KEY_check_key(key), 1);
  51959. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  51960. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  51961. EC_KEY_free(key);
  51962. EC_KEY_free(copy);
  51963. res = TEST_RES_CHECK(1);
  51964. #endif
  51965. return res;
  51966. }
  51967. static int test_wolfSSL_EC_curve(void)
  51968. {
  51969. int res = TEST_SKIPPED;
  51970. #if defined(OPENSSL_EXTRA)
  51971. int nid = NID_secp160k1;
  51972. const char* nid_name;
  51973. AssertNull(EC_curve_nid2nist(NID_sha256));
  51974. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  51975. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  51976. AssertIntEQ(EC_curve_nist2nid("INVALID"), 0);
  51977. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  51978. res = TEST_RES_CHECK(1);
  51979. #endif
  51980. return res;
  51981. }
  51982. static int test_wolfSSL_EC_KEY_dup(void)
  51983. {
  51984. int res = TEST_SKIPPED;
  51985. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  51986. WOLFSSL_EC_KEY* ecKey;
  51987. WOLFSSL_EC_KEY* dupKey;
  51988. ecc_key* srcKey;
  51989. ecc_key* destKey;
  51990. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  51991. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51992. /* Valid cases */
  51993. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51994. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  51995. /* Compare pubkey */
  51996. srcKey = (ecc_key*)ecKey->internal;
  51997. destKey = (ecc_key*)dupKey->internal;
  51998. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  51999. /* compare EC_GROUP */
  52000. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  52001. /* compare EC_POINT */
  52002. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  52003. dupKey->pub_key, NULL), MP_EQ);
  52004. /* compare BIGNUM */
  52005. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  52006. wolfSSL_EC_KEY_free(dupKey);
  52007. /* Invalid cases */
  52008. /* NULL key */
  52009. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52010. /* NULL ecc_key */
  52011. wc_ecc_free((ecc_key*)ecKey->internal);
  52012. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52013. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52014. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52015. wolfSSL_EC_KEY_free(ecKey);
  52016. wolfSSL_EC_KEY_free(dupKey);
  52017. /* NULL Group */
  52018. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52019. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52020. wolfSSL_EC_GROUP_free(ecKey->group);
  52021. ecKey->group = NULL; /* Set group to NULL */
  52022. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52023. wolfSSL_EC_KEY_free(ecKey);
  52024. wolfSSL_EC_KEY_free(dupKey);
  52025. /* NULL public key */
  52026. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52027. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52028. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52029. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52030. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52031. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52032. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52033. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52034. wolfSSL_EC_KEY_free(ecKey);
  52035. wolfSSL_EC_KEY_free(dupKey);
  52036. /* NULL private key */
  52037. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52038. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52039. wolfSSL_BN_free(ecKey->priv_key);
  52040. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52041. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52042. wolfSSL_EC_KEY_free(ecKey);
  52043. wolfSSL_EC_KEY_free(dupKey);
  52044. /* Test EC_KEY_up_ref */
  52045. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  52046. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52047. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52048. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52049. /* reference count doesn't follow duplicate */
  52050. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52051. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52052. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52053. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52054. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52055. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52056. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  52057. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  52058. res = TEST_RES_CHECK(1);
  52059. #endif
  52060. return res;
  52061. }
  52062. static int test_wolfSSL_EC_KEY_set_group(void)
  52063. {
  52064. int res = TEST_SKIPPED;
  52065. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52066. defined(OPENSSL_EXTRA)
  52067. EC_KEY *key = NULL;
  52068. EC_GROUP *group = NULL;
  52069. const EC_GROUP *group2 = NULL;
  52070. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52071. AssertNotNull(key = EC_KEY_new());
  52072. AssertNull(EC_KEY_get0_group(NULL));
  52073. AssertIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  52074. AssertIntEQ(EC_KEY_set_group(key, NULL), 0);
  52075. AssertIntEQ(EC_KEY_set_group(NULL, group), 0);
  52076. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  52077. AssertNotNull(group2 = EC_KEY_get0_group(key));
  52078. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  52079. EC_GROUP_free(group);
  52080. EC_KEY_free(key);
  52081. res = TEST_RES_CHECK(1);
  52082. #endif
  52083. return res;
  52084. }
  52085. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  52086. {
  52087. int res = TEST_SKIPPED;
  52088. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52089. BIO* bio;
  52090. EC_KEY* key;
  52091. /* Error condition: NULL key. */
  52092. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  52093. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  52094. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  52095. /* Conversion form defaults to uncompressed. */
  52096. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52097. #ifdef HAVE_COMP_KEY
  52098. /* Explicitly set to compressed. */
  52099. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52100. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  52101. #else
  52102. /* Will still work just won't change anything. */
  52103. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52104. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52105. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  52106. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52107. #endif
  52108. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  52109. BIO_free(bio);
  52110. EC_KEY_free(key);
  52111. res = TEST_RES_CHECK(1);
  52112. #endif
  52113. return res;
  52114. }
  52115. static int test_wolfSSL_EC_KEY_private_key(void)
  52116. {
  52117. int res = TEST_SKIPPED;
  52118. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52119. WOLFSSL_EC_KEY* key;
  52120. WOLFSSL_BIGNUM* priv = NULL;
  52121. WOLFSSL_BIGNUM* priv2 = NULL;
  52122. WOLFSSL_BIGNUM* bn;
  52123. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52124. AssertNotNull(priv = wolfSSL_BN_new());
  52125. AssertNotNull(priv2 = wolfSSL_BN_new());
  52126. AssertIntNE(BN_set_word(priv, 2), 0);
  52127. AssertIntNE(BN_set_word(priv2, 2), 0);
  52128. AssertNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  52129. /* No private key set. */
  52130. AssertNull(wolfSSL_EC_KEY_get0_private_key(key));
  52131. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  52132. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  52133. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  52134. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  52135. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52136. AssertPtrNE(bn, priv);
  52137. AssertIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  52138. AssertNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52139. AssertPtrNE(bn, priv2);
  52140. wolfSSL_BN_free(priv2);
  52141. wolfSSL_BN_free(priv);
  52142. wolfSSL_EC_KEY_free(key);
  52143. res = TEST_RES_CHECK(1);
  52144. #endif
  52145. return res;
  52146. }
  52147. static int test_wolfSSL_EC_KEY_public_key(void)
  52148. {
  52149. int res = TEST_SKIPPED;
  52150. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52151. WOLFSSL_EC_KEY* key;
  52152. WOLFSSL_EC_POINT* pub;
  52153. WOLFSSL_EC_POINT* point;
  52154. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52155. AssertNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  52156. AssertNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  52157. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52158. AssertNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  52159. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  52160. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  52161. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  52162. AssertIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  52163. AssertNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  52164. AssertPtrEq(point, pub);
  52165. wolfSSL_EC_KEY_free(key);
  52166. res = TEST_RES_CHECK(1);
  52167. #endif
  52168. return res;
  52169. }
  52170. static int test_wolfSSL_EC_KEY_print_fp(void)
  52171. {
  52172. int res = TEST_SKIPPED;
  52173. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  52174. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  52175. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  52176. !defined(NO_STDIO_FILESYSTEM)
  52177. EC_KEY* key = NULL;
  52178. /* Bad file pointer. */
  52179. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  52180. /* NULL key. */
  52181. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  52182. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  52183. /* Negative indent. */
  52184. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  52185. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52186. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52187. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52188. wolfSSL_EC_KEY_free(key);
  52189. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  52190. NID_X9_62_prime256v1)));
  52191. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  52192. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  52193. wolfSSL_EC_KEY_free(key);
  52194. res = TEST_RES_CHECK(1);
  52195. #endif
  52196. return res;
  52197. }
  52198. static int test_wolfSSL_EC_get_builtin_curves(void)
  52199. {
  52200. int res = TEST_SKIPPED;
  52201. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  52202. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52203. EC_builtin_curve* curves = NULL;
  52204. size_t crv_len = 0;
  52205. size_t i = 0;
  52206. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  52207. AssertNotNull(curves = (EC_builtin_curve*)XMALLOC(
  52208. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  52209. AssertIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  52210. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  52211. for (i = 0; i < crv_len; i++) {
  52212. if (curves[i].comment != NULL) {
  52213. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  52214. }
  52215. }
  52216. if (crv_len > 1) {
  52217. AssertIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  52218. }
  52219. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52220. res = TEST_RES_CHECK(1);
  52221. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52222. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  52223. return res;
  52224. }
  52225. static int test_wolfSSL_ECDSA_SIG(void)
  52226. {
  52227. int res = TEST_SKIPPED;
  52228. #ifdef OPENSSL_EXTRA
  52229. WOLFSSL_ECDSA_SIG* sig = NULL;
  52230. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  52231. WOLFSSL_BIGNUM* r;
  52232. WOLFSSL_BIGNUM* s;
  52233. const WOLFSSL_BIGNUM* r2;
  52234. const WOLFSSL_BIGNUM* s2;
  52235. const unsigned char* cp;
  52236. unsigned char* p;
  52237. unsigned char outSig[8];
  52238. unsigned char sigData[8] =
  52239. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52240. unsigned char sigDataBad[8] =
  52241. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  52242. wolfSSL_ECDSA_SIG_free(NULL);
  52243. AssertNotNull(sig = wolfSSL_ECDSA_SIG_new());
  52244. AssertNotNull(r = wolfSSL_BN_new());
  52245. AssertNotNull(s = wolfSSL_BN_new());
  52246. AssertIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  52247. AssertIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  52248. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  52249. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  52250. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  52251. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52252. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  52253. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  52254. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  52255. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  52256. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  52257. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  52258. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  52259. r2 = NULL;
  52260. s2 = NULL;
  52261. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  52262. AssertNull(r2);
  52263. AssertNull(s2);
  52264. AssertIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  52265. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  52266. AssertPtrEq(r2, r);
  52267. AssertPtrEq(s2, s);
  52268. r2 = NULL;
  52269. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  52270. AssertPtrEq(r2, r);
  52271. s2 = NULL;
  52272. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  52273. AssertPtrEq(s2, s);
  52274. /* r and s are freed when sig is freed. */
  52275. wolfSSL_ECDSA_SIG_free(sig);
  52276. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  52277. cp = sigDataBad;
  52278. AssertNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  52279. cp = sigData;
  52280. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  52281. AssertIntEQ((cp == sigData + 8), 1);
  52282. cp = sigData;
  52283. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  52284. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  52285. AssertIntEQ((sig == sig2), 1);
  52286. cp = outSig;
  52287. p = outSig;
  52288. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  52289. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  52290. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  52291. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  52292. AssertIntEQ((p == outSig + 8), 1);
  52293. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  52294. wolfSSL_ECDSA_SIG_free(sig);
  52295. res = TEST_RES_CHECK(1);
  52296. #endif
  52297. return res;
  52298. }
  52299. static int test_ECDSA_size_sign(void)
  52300. {
  52301. int res = TEST_SKIPPED;
  52302. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  52303. EC_KEY* key;
  52304. ECDSA_SIG* ecdsaSig;
  52305. int id;
  52306. byte hash[WC_MAX_DIGEST_SIZE];
  52307. byte hash2[WC_MAX_DIGEST_SIZE];
  52308. byte sig[ECC_MAX_SIG_SIZE];
  52309. unsigned int sigSz = sizeof(sig);
  52310. XMEMSET(hash, 123, sizeof(hash));
  52311. XMEMSET(hash2, 234, sizeof(hash2));
  52312. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  52313. AssertIntEQ(id, ECC_SECP256R1);
  52314. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52315. AssertIntEQ(EC_KEY_generate_key(key), 1);
  52316. AssertIntGE(ECDSA_size(NULL), 0);
  52317. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  52318. AssertIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  52319. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  52320. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  52321. AssertIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  52322. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  52323. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  52324. AssertIntGE(ECDSA_size(key), sigSz);
  52325. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  52326. AssertIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  52327. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  52328. AssertNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  52329. AssertNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  52330. AssertNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  52331. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  52332. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  52333. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  52334. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  52335. AssertIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  52336. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  52337. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  52338. AssertIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  52339. AssertIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  52340. ECDSA_SIG_free(ecdsaSig);
  52341. EC_KEY_free(key);
  52342. res = TEST_RES_CHECK(1);
  52343. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  52344. return res;
  52345. }
  52346. static int test_ECDH_compute_key(void)
  52347. {
  52348. int res = TEST_SKIPPED;
  52349. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52350. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52351. EC_KEY* key1;
  52352. EC_KEY* key2;
  52353. EC_POINT* pub1;
  52354. EC_POINT* pub2;
  52355. byte secret1[32];
  52356. byte secret2[32];
  52357. int i;
  52358. AssertNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52359. AssertIntEQ(EC_KEY_generate_key(key1), 1);
  52360. AssertNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  52361. AssertNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52362. AssertIntEQ(EC_KEY_generate_key(key2), 1);
  52363. AssertNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  52364. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  52365. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  52366. 0);
  52367. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  52368. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  52369. AssertIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  52370. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  52371. 0);
  52372. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  52373. 0);
  52374. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  52375. NULL), 0);
  52376. AssertIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  52377. sizeof(secret1));
  52378. AssertIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  52379. sizeof(secret2));
  52380. for (i = 0; i < (int)sizeof(secret1); i++) {
  52381. AssertIntEQ(secret1[i], secret2[i]);
  52382. }
  52383. EC_KEY_free(key2);
  52384. EC_KEY_free(key1);
  52385. res = TEST_RES_CHECK(1);
  52386. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  52387. * !WOLF_CRYPTO_CB_ONLY_ECC */
  52388. return res;
  52389. }
  52390. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  52391. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  52392. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52393. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  52394. {
  52395. X509* x509 = NULL;
  52396. BIGNUM* serial_number = NULL;
  52397. X509_NAME* name = NULL;
  52398. time_t epoch_off = 0;
  52399. ASN1_INTEGER* asn1_serial_number;
  52400. long not_before, not_after;
  52401. AssertNotNull(x509 = X509_new());
  52402. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  52403. AssertNotNull(serial_number = BN_new());
  52404. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  52405. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  52406. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  52407. /* version 3 */
  52408. AssertIntNE(X509_set_version(x509, 2L), 0);
  52409. AssertNotNull(name = X509_NAME_new());
  52410. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  52411. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  52412. AssertIntNE(X509_set_subject_name(x509, name), 0);
  52413. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  52414. not_before = (long)wc_Time(NULL);
  52415. not_after = not_before + (365 * 24 * 60 * 60);
  52416. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  52417. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  52418. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  52419. BN_free(serial_number);
  52420. X509_NAME_free(name);
  52421. X509_free(x509);
  52422. return 0;
  52423. }
  52424. #endif
  52425. static int test_openssl_generate_key_and_cert(void)
  52426. {
  52427. int res = TEST_SKIPPED;
  52428. #if defined(OPENSSL_EXTRA)
  52429. #if !defined(NO_RSA)
  52430. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52431. EVP_PKEY* pkey = EVP_PKEY_new();
  52432. int key_length = 2048;
  52433. BIGNUM* exponent = BN_new();
  52434. RSA* rsa = RSA_new();
  52435. AssertNotNull(pkey);
  52436. AssertNotNull(exponent);
  52437. AssertNotNull(rsa);
  52438. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  52439. #ifndef WOLFSSL_KEY_GEN
  52440. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52441. #if defined(USE_CERT_BUFFERS_1024)
  52442. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  52443. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52444. key_length = 1024;
  52445. #elif defined(USE_CERT_BUFFERS_2048)
  52446. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  52447. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52448. #else
  52449. RSA_free(rsa);
  52450. rsa = NULL;
  52451. #endif
  52452. #else
  52453. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  52454. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  52455. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  52456. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52457. #endif
  52458. if (rsa) {
  52459. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  52460. BN_free(exponent);
  52461. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52462. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52463. test_openssl_make_self_signed_certificate(pkey);
  52464. #endif
  52465. }
  52466. EVP_PKEY_free(pkey);
  52467. res = TEST_RES_CHECK(1);
  52468. }
  52469. #endif /* !NO_RSA */
  52470. #ifdef HAVE_ECC
  52471. if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
  52472. EVP_PKEY* pkey = EVP_PKEY_new();
  52473. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  52474. AssertNotNull(pkey);
  52475. AssertNotNull(ec_key);
  52476. #ifndef NO_WOLFSSL_STUB
  52477. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  52478. #endif
  52479. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  52480. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  52481. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52482. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52483. test_openssl_make_self_signed_certificate(pkey);
  52484. #endif
  52485. EVP_PKEY_free(pkey);
  52486. res = TEST_RES_CHECK(1);
  52487. }
  52488. #endif /* HAVE_ECC */
  52489. #endif /* OPENSSL_EXTRA */
  52490. return res;
  52491. }
  52492. static int test_stubs_are_stubs(void)
  52493. {
  52494. int res = TEST_SKIPPED;
  52495. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  52496. WOLFSSL_CTX* ctx = NULL;
  52497. WOLFSSL_CTX* ctxN = NULL;
  52498. #ifndef NO_WOLFSSL_CLIENT
  52499. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  52500. AssertNotNull(ctx);
  52501. #elif !defined(NO_WOLFSSL_SERVER)
  52502. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  52503. AssertNotNull(ctx);
  52504. #else
  52505. return res;
  52506. #endif
  52507. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  52508. AssertIntEQ((int) x(z), 0)
  52509. /* test logic, all stubs return same result regardless of ctx being NULL
  52510. * as there are no sanity checks, it's just a stub! If at some
  52511. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  52512. * if invalid inputs are supplied. Test calling both
  52513. * with and without valid inputs, if a stub functionality remains unchanged.
  52514. */
  52515. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  52516. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  52517. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  52518. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  52519. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  52520. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  52521. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  52522. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  52523. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  52524. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  52525. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  52526. wolfSSL_CTX_free(ctx);
  52527. ctx = NULL;
  52528. res = TEST_RES_CHECK(1);
  52529. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  52530. return res;
  52531. }
  52532. static int test_CONF_modules_xxx(void)
  52533. {
  52534. int res = TEST_SKIPPED;
  52535. #if defined(OPENSSL_EXTRA)
  52536. CONF_modules_free();
  52537. AssertTrue(1); /* to confirm previous call gives no harm */
  52538. CONF_modules_unload(0);
  52539. AssertTrue(1);
  52540. CONF_modules_unload(1);
  52541. AssertTrue(1);
  52542. CONF_modules_unload(-1);
  52543. AssertTrue(1);
  52544. res = TEST_RES_CHECK(1);
  52545. #endif /* OPENSSL_EXTRA */
  52546. return res;
  52547. }
  52548. static int test_CRYPTO_set_dynlock_xxx(void)
  52549. {
  52550. int res = TEST_SKIPPED;
  52551. #if defined(OPENSSL_EXTRA)
  52552. CRYPTO_set_dynlock_create_callback(
  52553. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  52554. CRYPTO_set_dynlock_create_callback(
  52555. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  52556. CRYPTO_set_dynlock_destroy_callback(
  52557. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  52558. CRYPTO_set_dynlock_destroy_callback(
  52559. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  52560. CRYPTO_set_dynlock_lock_callback(
  52561. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  52562. CRYPTO_set_dynlock_lock_callback(
  52563. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  52564. AssertTrue(1); /* to confirm previous call gives no harm */
  52565. res = TEST_RES_CHECK(1);
  52566. #endif /* OPENSSL_EXTRA */
  52567. return res;
  52568. }
  52569. static int test_CRYPTO_THREADID_xxx(void)
  52570. {
  52571. int res = TEST_SKIPPED;
  52572. #if defined(OPENSSL_EXTRA)
  52573. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  52574. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  52575. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  52576. res = TEST_RES_CHECK(1);
  52577. #endif /* OPENSSL_EXTRA */
  52578. return res;
  52579. }
  52580. static int test_ENGINE_cleanup(void)
  52581. {
  52582. int res = TEST_SKIPPED;
  52583. #if defined(OPENSSL_EXTRA)
  52584. ENGINE_cleanup();
  52585. AssertTrue(1); /* to confirm previous call gives no harm */
  52586. res = TEST_RES_CHECK(1);
  52587. #endif /* OPENSSL_EXTRA */
  52588. return res;
  52589. }
  52590. static int test_wolfSSL_CTX_LoadCRL(void)
  52591. {
  52592. int res = TEST_SKIPPED;
  52593. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  52594. WOLFSSL_CTX* ctx = NULL;
  52595. WOLFSSL* ssl = NULL;
  52596. const char* badPath = "dummypath";
  52597. const char* validPath = "./certs/crl";
  52598. const char* validFilePath = "./certs/crl/cliCrl.pem";
  52599. const char* issuerCert = "./certs/client-cert.pem";
  52600. int derType = WOLFSSL_FILETYPE_ASN1;
  52601. int pemType = WOLFSSL_FILETYPE_PEM;
  52602. int monitor = WOLFSSL_CRL_MONITOR;
  52603. WOLFSSL_CERT_MANAGER* cm = NULL;
  52604. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  52605. BAD_FUNC_ARG)
  52606. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  52607. WOLFSSL_SUCCESS)
  52608. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52609. #ifndef NO_WOLFSSL_CLIENT
  52610. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52611. #elif !defined(NO_WOLFSSL_SERVER)
  52612. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52613. #else
  52614. return;
  52615. #endif
  52616. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52617. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  52618. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  52619. wolfSSL_CTX_free(ctx);
  52620. #ifndef NO_WOLFSSL_CLIENT
  52621. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52622. #elif !defined(NO_WOLFSSL_SERVER)
  52623. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52624. #else
  52625. return;
  52626. #endif
  52627. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52628. WOLFSSL_SUCCESS);
  52629. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  52630. wolfSSL_CTX_free(ctx);
  52631. #ifndef NO_WOLFSSL_CLIENT
  52632. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52633. #elif !defined(NO_WOLFSSL_SERVER)
  52634. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52635. #else
  52636. return;
  52637. #endif
  52638. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52639. WOLFSSL_SUCCESS);
  52640. AssertNotNull(ssl = wolfSSL_new(ctx));
  52641. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  52642. wolfSSL_free(ssl);
  52643. wolfSSL_CTX_free(ctx);
  52644. AssertNotNull(cm = wolfSSL_CertManagerNew());
  52645. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  52646. WOLFSSL_SUCCESS);
  52647. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  52648. wolfSSL_CertManagerFree(cm);
  52649. res = TEST_RES_CHECK(1);
  52650. #endif
  52651. return res;
  52652. }
  52653. static int test_SetTmpEC_DHE_Sz(void)
  52654. {
  52655. int res = TEST_SKIPPED;
  52656. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  52657. WOLFSSL_CTX *ctx;
  52658. WOLFSSL *ssl;
  52659. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  52660. AssertNotNull(ctx);
  52661. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  52662. ssl = wolfSSL_new(ctx);
  52663. AssertNotNull(ssl);
  52664. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  52665. wolfSSL_free(ssl);
  52666. wolfSSL_CTX_free(ctx);
  52667. res = TEST_RES_CHECK(1);
  52668. #endif
  52669. return res;
  52670. }
  52671. static int test_wolfSSL_CTX_get0_privatekey(void)
  52672. {
  52673. int res = TEST_SKIPPED;
  52674. #ifdef OPENSSL_ALL
  52675. WOLFSSL_CTX* ctx = NULL;
  52676. #ifndef NO_RSA
  52677. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52678. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52679. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  52680. WOLFSSL_FILETYPE_PEM));
  52681. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52682. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  52683. WOLFSSL_FILETYPE_PEM));
  52684. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  52685. wolfSSL_CTX_free(ctx);
  52686. #endif
  52687. #ifdef HAVE_ECC
  52688. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52689. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52690. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  52691. WOLFSSL_FILETYPE_PEM));
  52692. AssertNull(SSL_CTX_get0_privatekey(ctx));
  52693. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  52694. WOLFSSL_FILETYPE_PEM));
  52695. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  52696. wolfSSL_CTX_free(ctx);
  52697. #endif
  52698. res = TEST_RES_CHECK(1);
  52699. #endif
  52700. return res;
  52701. }
  52702. static int test_wolfSSL_dtls_set_mtu(void)
  52703. {
  52704. int res = TEST_SKIPPED;
  52705. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  52706. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  52707. WOLFSSL_CTX* ctx = NULL;
  52708. WOLFSSL* ssl = NULL;
  52709. const char* testCertFile;
  52710. const char* testKeyFile;
  52711. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  52712. #ifndef NO_RSA
  52713. testCertFile = svrCertFile;
  52714. testKeyFile = svrKeyFile;
  52715. #elif defined(HAVE_ECC)
  52716. testCertFile = eccCertFile;
  52717. testKeyFile = eccKeyFile;
  52718. #endif
  52719. if (testCertFile != NULL && testKeyFile != NULL) {
  52720. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  52721. WOLFSSL_FILETYPE_PEM));
  52722. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52723. WOLFSSL_FILETYPE_PEM));
  52724. }
  52725. AssertNotNull(ssl = wolfSSL_new(ctx));
  52726. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52727. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52728. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  52729. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  52730. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  52731. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  52732. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  52733. wolfSSL_free(ssl);
  52734. wolfSSL_CTX_free(ctx);
  52735. res = TEST_RES_CHECK(1);
  52736. #endif
  52737. return res;
  52738. }
  52739. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52740. defined(WOLFSSL_DTLS)
  52741. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  52742. enum HandShakeType hsType, word16 length)
  52743. {
  52744. size_t idx = 0;
  52745. byte* l;
  52746. /* record layer */
  52747. /* handshake type */
  52748. out[idx++] = handshake;
  52749. /* protocol version */
  52750. out[idx++] = 0xfe;
  52751. out[idx++] = 0xfd; /* DTLS 1.2 */
  52752. /* epoch 0 */
  52753. XMEMSET(out + idx, 0, 2);
  52754. idx += 2;
  52755. /* sequence number */
  52756. XMEMSET(out + idx, 0, 6);
  52757. c32toa(seq, out + idx + 2);
  52758. idx += 6;
  52759. /* length in BE */
  52760. if (length)
  52761. c16toa(length, out + idx);
  52762. else
  52763. c16toa(outSz - idx - 2, out + idx);
  52764. idx += 2;
  52765. /* handshake layer */
  52766. /* handshake type */
  52767. out[idx++] = (byte)hsType;
  52768. /* length */
  52769. l = out + idx;
  52770. idx += 3;
  52771. /* message seq */
  52772. c16toa(0, out + idx);
  52773. idx += 2;
  52774. /* frag offset */
  52775. c32to24(0, out + idx);
  52776. idx += 3;
  52777. /* frag length */
  52778. c32to24((word32)outSz - (word32)idx - 3, l);
  52779. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  52780. idx += 3;
  52781. XMEMSET(out + idx, 0, outSz - idx);
  52782. }
  52783. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  52784. {
  52785. byte msg[] = "This is a msg for the client";
  52786. byte reply[40];
  52787. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52788. reply[sizeof(reply) - 1] = '\0';
  52789. fprintf(stderr, "Client message: %s\n", reply);
  52790. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52791. }
  52792. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  52793. {
  52794. byte ch[50];
  52795. int fd = wolfSSL_get_fd(ssl);
  52796. byte msg[] = "This is a msg for the server";
  52797. byte reply[40];
  52798. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  52799. /* Server should ignore this datagram */
  52800. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52801. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  52802. /* Server should ignore this datagram */
  52803. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52804. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52805. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52806. reply[sizeof(reply) - 1] = '\0';
  52807. fprintf(stderr, "Server response: %s\n", reply);
  52808. }
  52809. static int test_wolfSSL_dtls_plaintext(void)
  52810. {
  52811. callback_functions func_cb_client;
  52812. callback_functions func_cb_server;
  52813. size_t i;
  52814. struct test_params {
  52815. method_provider client_meth;
  52816. method_provider server_meth;
  52817. ssl_callback on_result_server;
  52818. ssl_callback on_result_client;
  52819. } params[] = {
  52820. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52821. test_wolfSSL_dtls_plaintext_server,
  52822. test_wolfSSL_dtls_plaintext_client},
  52823. };
  52824. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52825. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52826. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52827. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52828. func_cb_server.method = params[i].server_meth;
  52829. func_cb_client.method = params[i].client_meth;
  52830. func_cb_client.on_result = params[i].on_result_client;
  52831. func_cb_server.on_result = params[i].on_result_server;
  52832. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52833. if (!func_cb_client.return_code)
  52834. return TEST_FAIL;
  52835. if (!func_cb_server.return_code)
  52836. return TEST_FAIL;
  52837. }
  52838. return TEST_RES_CHECK(1);
  52839. }
  52840. #else
  52841. static int test_wolfSSL_dtls_plaintext(void) {
  52842. return TEST_SKIPPED;
  52843. }
  52844. #endif
  52845. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52846. defined(WOLFSSL_DTLS)
  52847. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  52848. {
  52849. byte b[1100]; /* buffer for the messages to send */
  52850. size_t idx = 0;
  52851. size_t seq_offset = 0;
  52852. size_t msg_offset = 0;
  52853. int i;
  52854. int fd = wolfSSL_get_fd(ssl);
  52855. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52856. word32 seq_number = 100; /* start high so server definitely reads this */
  52857. word16 msg_number = 50; /* start high so server has to buffer this */
  52858. AssertIntEQ(ret, 1);
  52859. /* Now let's start spamming the peer with fragments it needs to store */
  52860. XMEMSET(b, -1, sizeof(b));
  52861. /* record layer */
  52862. /* handshake type */
  52863. b[idx++] = 22;
  52864. /* protocol version */
  52865. b[idx++] = 0xfe;
  52866. b[idx++] = 0xfd; /* DTLS 1.2 */
  52867. /* epoch 0 */
  52868. XMEMSET(b + idx, 0, 2);
  52869. idx += 2;
  52870. /* sequence number */
  52871. XMEMSET(b + idx, 0, 6);
  52872. seq_offset = idx + 2; /* increment only the low 32 bits */
  52873. idx += 6;
  52874. /* static length in BE */
  52875. c16toa(42, b + idx);
  52876. idx += 2;
  52877. /* handshake layer */
  52878. /* cert type */
  52879. b[idx++] = 11;
  52880. /* length */
  52881. c32to24(1000, b + idx);
  52882. idx += 3;
  52883. /* message seq */
  52884. c16toa(0, b + idx);
  52885. msg_offset = idx;
  52886. idx += 2;
  52887. /* frag offset */
  52888. c32to24(500, b + idx);
  52889. idx += 3;
  52890. /* frag length */
  52891. c32to24(30, b + idx);
  52892. idx += 3;
  52893. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  52894. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  52895. seq_number++, msg_number++, i++) {
  52896. struct timespec delay;
  52897. XMEMSET(&delay, 0, sizeof(delay));
  52898. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52899. c32toa(seq_number, b + seq_offset);
  52900. c16toa(msg_number, b + msg_offset);
  52901. ret = (int)send(fd, b, 55, 0);
  52902. nanosleep(&delay, NULL);
  52903. }
  52904. }
  52905. #ifdef WOLFSSL_DTLS13
  52906. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  52907. {
  52908. const word16 sendCountMax = 100;
  52909. byte b[150]; /* buffer for the messages to send */
  52910. size_t idx = 0;
  52911. size_t msg_offset = 0;
  52912. int fd = wolfSSL_get_fd(ssl);
  52913. word16 msg_number = 10; /* start high so server has to buffer this */
  52914. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52915. AssertIntEQ(ret, 1);
  52916. /* Now let's start spamming the peer with fragments it needs to store */
  52917. XMEMSET(b, -1, sizeof(b));
  52918. /* handshake type */
  52919. b[idx++] = 11;
  52920. /* length */
  52921. c32to24(10000, b + idx);
  52922. idx += 3;
  52923. /* message_seq */
  52924. msg_offset = idx;
  52925. idx += 2;
  52926. /* fragment_offset */
  52927. c32to24(5000, b + idx);
  52928. idx += 3;
  52929. /* fragment_length */
  52930. c32to24(100, b + idx);
  52931. idx += 3;
  52932. /* fragment contents */
  52933. idx += 100;
  52934. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  52935. byte sendBuf[150];
  52936. int sendSz = sizeof(sendBuf);
  52937. struct timespec delay;
  52938. XMEMSET(&delay, 0, sizeof(delay));
  52939. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52940. c16toa(msg_number, b + msg_offset);
  52941. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  52942. (int)idx, handshake, 0, 0, 0);
  52943. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  52944. nanosleep(&delay, NULL);
  52945. }
  52946. }
  52947. #endif
  52948. static int test_wolfSSL_dtls_fragments(void)
  52949. {
  52950. callback_functions func_cb_client;
  52951. callback_functions func_cb_server;
  52952. size_t i;
  52953. struct test_params {
  52954. method_provider client_meth;
  52955. method_provider server_meth;
  52956. ssl_callback spammer;
  52957. } params[] = {
  52958. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52959. test_wolfSSL_dtls12_fragments_spammer},
  52960. #ifdef WOLFSSL_DTLS13
  52961. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  52962. test_wolfSSL_dtls13_fragments_spammer},
  52963. #endif
  52964. };
  52965. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52966. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52967. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52968. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52969. func_cb_server.method = params[i].server_meth;
  52970. func_cb_client.method = params[i].client_meth;
  52971. func_cb_client.ssl_ready = params[i].spammer;
  52972. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52973. AssertFalse(func_cb_client.return_code);
  52974. AssertFalse(func_cb_server.return_code);
  52975. /* The socket should be closed by the server resulting in a
  52976. * socket error, fatal error or reading a close notify alert */
  52977. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  52978. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  52979. func_cb_client.last_err != FATAL_ERROR) {
  52980. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  52981. }
  52982. /* Check the server returned an error indicating the msg buffer
  52983. * was full */
  52984. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  52985. }
  52986. return TEST_RES_CHECK(1);
  52987. }
  52988. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  52989. {
  52990. int fd, ret;
  52991. byte alert_msg[] = {
  52992. 0x15, /* alert type */
  52993. 0xfe, 0xfd, /* version */
  52994. 0x00, 0x00, /* epoch */
  52995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  52996. 0x00, 0x02, /* length */
  52997. 0x02, /* level: fatal */
  52998. 0x46 /* protocol version */
  52999. };
  53000. fd = wolfSSL_get_fd(ssl);
  53001. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  53002. AssertIntGT(ret, 0);
  53003. }
  53004. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  53005. {
  53006. callback_functions client_cbs, server_cbs;
  53007. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53008. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53009. client_cbs.doUdp = server_cbs.doUdp = 1;
  53010. if (version12) {
  53011. client_cbs.method = wolfDTLSv1_2_client_method;
  53012. server_cbs.method = wolfDTLSv1_2_server_method;
  53013. }
  53014. else {
  53015. #ifdef WOLFSSL_DTLS13
  53016. client_cbs.method = wolfDTLSv1_3_client_method;
  53017. server_cbs.method = wolfDTLSv1_3_server_method;
  53018. #else
  53019. return TEST_SKIPPED;
  53020. #endif /* WOLFSSL_DTLS13 */
  53021. }
  53022. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53023. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53024. if (!client_cbs.return_code)
  53025. return TEST_FAIL;
  53026. if (!server_cbs.return_code)
  53027. return TEST_FAIL;
  53028. return TEST_SUCCESS;
  53029. }
  53030. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53031. {
  53032. int ret;
  53033. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53034. if (ret != 0)
  53035. return ret;
  53036. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53037. if (ret != 0)
  53038. return ret;
  53039. return 0;
  53040. }
  53041. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53042. {
  53043. int ret;
  53044. int fd;
  53045. byte bad_msg[] = {
  53046. 0x17, /* app data */
  53047. 0xaa, 0xfd, /* bad version */
  53048. 0x00, 0x01, /* epoch 1 */
  53049. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53050. 0x00, 0x26, /* length: 38 bytes */
  53051. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53052. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53053. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  53054. 0x03, 0x18, 0x72
  53055. };
  53056. fd = wolfSSL_get_fd(ssl);
  53057. AssertIntGE(fd, 0);
  53058. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  53059. AssertIntEQ(ret, sizeof(bad_msg));
  53060. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  53061. AssertIntEQ(ret, sizeof("badrecordtest"));
  53062. }
  53063. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  53064. {
  53065. byte buf[100];
  53066. int ret;
  53067. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  53068. AssertIntGT(ret, 0);
  53069. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  53070. }
  53071. static int _test_wolfSSL_dtls_bad_record(
  53072. method_provider client_method, method_provider server_method)
  53073. {
  53074. callback_functions client_cbs, server_cbs;
  53075. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53076. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53077. client_cbs.doUdp = server_cbs.doUdp = 1;
  53078. client_cbs.method = client_method;
  53079. server_cbs.method = server_method;
  53080. client_cbs.on_result = test_wolfSSL_send_bad_record;
  53081. server_cbs.on_result = test_wolfSSL_read_string;
  53082. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53083. if (!client_cbs.return_code)
  53084. return TEST_FAIL;
  53085. if (!server_cbs.return_code)
  53086. return TEST_FAIL;
  53087. return TEST_SUCCESS;
  53088. }
  53089. static int test_wolfSSL_dtls_bad_record(void)
  53090. {
  53091. int ret;
  53092. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  53093. wolfDTLSv1_2_server_method);
  53094. #ifdef WOLFSSL_DTLS13
  53095. if (ret != TEST_SUCCESS)
  53096. return ret;
  53097. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  53098. wolfDTLSv1_3_server_method);
  53099. #else
  53100. return ret;
  53101. #endif /* WOLFSSL_DTLS13 */
  53102. }
  53103. #else
  53104. static int test_wolfSSL_dtls_fragments(void) {
  53105. return TEST_SKIPPED;
  53106. }
  53107. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  53108. return TEST_SKIPPED;
  53109. }
  53110. static int test_wolfSSL_dtls_bad_record(void) {
  53111. return TEST_SKIPPED;
  53112. }
  53113. #endif
  53114. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  53115. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53116. defined(HAVE_IO_TESTS_DEPENDENCIES)
  53117. static byte test_AEAD_fail_decryption = 0;
  53118. static byte test_AEAD_seq_num = 0;
  53119. static byte test_AEAD_done = 0;
  53120. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  53121. {
  53122. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  53123. if (ret > 0) {
  53124. if (test_AEAD_fail_decryption) {
  53125. /* Modify the packet to trigger a decryption failure */
  53126. buf[ret/2] ^= 0xFF;
  53127. if (test_AEAD_fail_decryption == 1)
  53128. test_AEAD_fail_decryption = 0;
  53129. }
  53130. }
  53131. (void)ctx;
  53132. return ret;
  53133. }
  53134. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  53135. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  53136. {
  53137. if (sendLimit)
  53138. w64Zero(sendLimit);
  53139. switch (ssl->specs.bulk_cipher_algorithm) {
  53140. case wolfssl_aes_gcm:
  53141. if (sendLimit)
  53142. *sendLimit = AEAD_AES_LIMIT;
  53143. FALL_THROUGH;
  53144. case wolfssl_chacha:
  53145. if (hardLimit)
  53146. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  53147. if (keyUpdateLimit)
  53148. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  53149. break;
  53150. case wolfssl_aes_ccm:
  53151. if (sendLimit)
  53152. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  53153. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  53154. if (hardLimit)
  53155. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  53156. if (keyUpdateLimit)
  53157. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  53158. }
  53159. else {
  53160. if (hardLimit)
  53161. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  53162. if (keyUpdateLimit)
  53163. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  53164. }
  53165. break;
  53166. default:
  53167. fprintf(stderr, "Unrecognized bulk cipher");
  53168. AssertFalse(1);
  53169. break;
  53170. }
  53171. }
  53172. static void test_AEAD_limit_client(WOLFSSL* ssl)
  53173. {
  53174. int ret;
  53175. int i;
  53176. int didReKey = 0;
  53177. char msgBuf[20];
  53178. w64wrapper hardLimit;
  53179. w64wrapper keyUpdateLimit;
  53180. w64wrapper counter;
  53181. w64wrapper sendLimit;
  53182. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  53183. w64Zero(&counter);
  53184. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  53185. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  53186. for (i = 0; i < 10; i++) {
  53187. /* Test some failed decryptions */
  53188. test_AEAD_fail_decryption = 1;
  53189. w64Increment(&counter);
  53190. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53191. /* Should succeed since decryption failures are dropped */
  53192. AssertIntGT(ret, 0);
  53193. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  53194. }
  53195. test_AEAD_fail_decryption = 1;
  53196. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  53197. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53198. /* 100 read calls should be enough to complete the key update */
  53199. w64Zero(&counter);
  53200. for (i = 0; i < 100; i++) {
  53201. /* Key update should be sent and negotiated */
  53202. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53203. AssertIntGT(ret, 0);
  53204. /* Epoch after one key update is 4 */
  53205. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  53206. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  53207. didReKey = 1;
  53208. break;
  53209. }
  53210. }
  53211. AssertTrue(didReKey);
  53212. if (!w64IsZero(sendLimit)) {
  53213. /* Test the sending limit for AEAD ciphers */
  53214. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  53215. test_AEAD_seq_num = 1;
  53216. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53217. AssertIntGT(ret, 0);
  53218. didReKey = 0;
  53219. w64Zero(&counter);
  53220. /* 100 read calls should be enough to complete the key update */
  53221. for (i = 0; i < 100; i++) {
  53222. /* Key update should be sent and negotiated */
  53223. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53224. AssertIntGT(ret, 0);
  53225. /* Epoch after another key update is 5 */
  53226. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  53227. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  53228. didReKey = 1;
  53229. break;
  53230. }
  53231. }
  53232. AssertTrue(didReKey);
  53233. }
  53234. test_AEAD_fail_decryption = 2;
  53235. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  53236. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  53237. /* Connection should fail with a DECRYPT_ERROR */
  53238. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53239. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  53240. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  53241. test_AEAD_done = 1;
  53242. }
  53243. int counter = 0;
  53244. static void test_AEAD_limit_server(WOLFSSL* ssl)
  53245. {
  53246. char msgBuf[] = "Sending data";
  53247. int ret = WOLFSSL_SUCCESS;
  53248. w64wrapper sendLimit;
  53249. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53250. struct timespec delay;
  53251. XMEMSET(&delay, 0, sizeof(delay));
  53252. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  53253. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  53254. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53255. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  53256. while (!test_AEAD_done && ret > 0) {
  53257. counter++;
  53258. if (test_AEAD_seq_num) {
  53259. /* We need to update the seq number so that we can understand the
  53260. * peer. Otherwise we will incorrectly interpret the seq number. */
  53261. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  53262. AssertNotNull(e);
  53263. e->nextPeerSeqNumber = sendLimit;
  53264. test_AEAD_seq_num = 0;
  53265. }
  53266. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  53267. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  53268. nanosleep(&delay, NULL);
  53269. }
  53270. }
  53271. static int test_wolfSSL_dtls_AEAD_limit(void)
  53272. {
  53273. callback_functions func_cb_client;
  53274. callback_functions func_cb_server;
  53275. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53276. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53277. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53278. func_cb_server.method = wolfDTLSv1_3_server_method;
  53279. func_cb_client.method = wolfDTLSv1_3_client_method;
  53280. func_cb_server.on_result = test_AEAD_limit_server;
  53281. func_cb_client.on_result = test_AEAD_limit_client;
  53282. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53283. if (!func_cb_client.return_code)
  53284. return TEST_FAIL;
  53285. if (!func_cb_server.return_code)
  53286. return TEST_FAIL;
  53287. return TEST_SUCCESS;
  53288. }
  53289. #else
  53290. static int test_wolfSSL_dtls_AEAD_limit(void)
  53291. {
  53292. return TEST_SKIPPED;
  53293. }
  53294. #endif
  53295. #if defined(WOLFSSL_DTLS) && \
  53296. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  53297. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  53298. {
  53299. int fd, ret;
  53300. byte ch_msg[] = {
  53301. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53302. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  53303. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  53304. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  53305. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  53306. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53307. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53308. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53309. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53310. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53311. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53312. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53313. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53314. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  53315. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  53316. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  53317. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  53318. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  53319. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  53320. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  53321. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  53322. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  53323. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  53324. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  53325. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  53326. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  53327. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  53328. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  53329. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  53330. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  53331. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  53332. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  53333. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  53334. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  53335. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  53336. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  53337. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  53338. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  53339. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  53340. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  53341. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  53342. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  53343. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  53344. 0x31, 0x68, 0x4c
  53345. };
  53346. fd = wolfSSL_get_fd(ssl);
  53347. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  53348. AssertIntGT(ret, 0);
  53349. /* consume the HRR otherwise handshake will fail */
  53350. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  53351. AssertIntGT(ret, 0);
  53352. }
  53353. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53354. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  53355. {
  53356. int fd, ret;
  53357. byte ch_msh_invalid_cookie[] = {
  53358. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  53359. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  53360. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  53361. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  53362. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  53363. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53364. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53365. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53366. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53367. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53368. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53369. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53370. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53371. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  53372. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53373. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53374. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53375. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53376. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53377. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  53378. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  53379. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  53380. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  53381. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  53382. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  53383. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  53384. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  53385. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  53386. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  53387. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  53388. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  53389. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  53390. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  53391. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  53392. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  53393. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  53394. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  53395. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  53396. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  53397. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  53398. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  53399. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  53400. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  53401. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  53402. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  53403. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  53404. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  53405. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  53406. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  53407. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  53408. 0x23, 0x00, 0x00
  53409. };
  53410. byte alert_reply[50];
  53411. byte expected_alert_reply[] = {
  53412. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  53413. 0x02, 0x02, 0x2f
  53414. };
  53415. fd = wolfSSL_get_fd(ssl);
  53416. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  53417. AssertIntGT(ret, 0);
  53418. /* should reply with an illegal_parameter reply */
  53419. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  53420. AssertIntEQ(ret, sizeof(expected_alert_reply));
  53421. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  53422. }
  53423. #endif
  53424. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  53425. {
  53426. #ifndef NO_MD5
  53427. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  53428. #elif !defined(NO_SHA)
  53429. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  53430. #elif !defined(NO_SHA256)
  53431. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  53432. #else
  53433. #error "We need a digest to hash the WOLFSSL object"
  53434. #endif
  53435. byte hashBuf[WC_MAX_DIGEST_SIZE];
  53436. wc_HashAlg hash;
  53437. const TLSX* exts = ssl->extensions;
  53438. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  53439. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  53440. * InitHandshakeHashes */
  53441. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  53442. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  53443. /* Following fields are not important to compare */
  53444. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53445. sslCopy.buffers.inputBuffer.buffer = NULL;
  53446. sslCopy.buffers.inputBuffer.bufferSize = 0;
  53447. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  53448. sslCopy.buffers.inputBuffer.offset = 0;
  53449. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53450. sslCopy.buffers.outputBuffer.buffer = NULL;
  53451. sslCopy.buffers.outputBuffer.bufferSize = 0;
  53452. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  53453. sslCopy.buffers.outputBuffer.offset = 0;
  53454. sslCopy.error = 0;
  53455. sslCopy.curSize = 0;
  53456. sslCopy.keys.curSeq_lo = 0;
  53457. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  53458. #ifdef WOLFSSL_DTLS13
  53459. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  53460. sslCopy.dtls13FastTimeout = 0;
  53461. #endif
  53462. sslCopy.keys.dtls_peer_handshake_number = 0;
  53463. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  53464. sslCopy.hsHashes = NULL;
  53465. #ifdef WOLFSSL_ASYNC_IO
  53466. #ifdef WOLFSSL_ASYNC_CRYPT
  53467. sslCopy.asyncDev = NULL;
  53468. #endif
  53469. sslCopy.async = NULL;
  53470. #endif
  53471. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  53472. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  53473. /* hash extension list */
  53474. while (exts != NULL) {
  53475. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  53476. exts = exts->next;
  53477. }
  53478. /* Hash suites */
  53479. if (sslCopy.suites != NULL) {
  53480. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  53481. sizeof(struct Suites)), 0);
  53482. }
  53483. /* Hash hsHashes */
  53484. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  53485. sizeof(*hsHashes)), 0);
  53486. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  53487. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  53488. return MakeWordFromHash(hashBuf);
  53489. }
  53490. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  53491. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  53492. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  53493. char *buf, int sz, void *ctx)
  53494. {
  53495. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  53496. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  53497. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  53498. }
  53499. else {
  53500. return WOLFSSL_CBIO_ERR_WANT_READ;
  53501. }
  53502. }
  53503. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  53504. {
  53505. /* Compare the ssl object before and after one ClientHello msg */
  53506. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53507. int res;
  53508. int err;
  53509. word32 initHash;
  53510. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  53511. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  53512. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53513. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  53514. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  53515. (void)initHash;
  53516. res = tcp_select(fd, 5);
  53517. /* We are expecting a msg. A timeout indicates failure. */
  53518. AssertIntEQ(res, TEST_RECV_READY);
  53519. res = wolfSSL_accept(ssl);
  53520. err = wolfSSL_get_error(ssl, res);
  53521. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  53522. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  53523. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  53524. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  53525. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  53526. }
  53527. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53528. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  53529. {
  53530. int ret;
  53531. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  53532. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  53533. test_wolfSSL_dtls_compare_stateless(ssl);
  53534. }
  53535. #endif
  53536. static int test_wolfSSL_dtls_stateless(void)
  53537. {
  53538. callback_functions client_cbs, server_cbs;
  53539. size_t i;
  53540. struct {
  53541. method_provider client_meth;
  53542. method_provider server_meth;
  53543. ssl_callback client_ssl_ready;
  53544. ssl_callback server_ssl_ready;
  53545. } test_params[] = {
  53546. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53547. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  53548. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53549. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53550. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  53551. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53552. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  53553. #endif
  53554. };
  53555. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  53556. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53557. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53558. client_cbs.doUdp = server_cbs.doUdp = 1;
  53559. client_cbs.method = test_params[i].client_meth;
  53560. server_cbs.method = test_params[i].server_meth;
  53561. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  53562. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  53563. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53564. if (!client_cbs.return_code)
  53565. return TEST_FAIL;
  53566. if (!server_cbs.return_code)
  53567. return TEST_FAIL;
  53568. }
  53569. return TEST_SUCCESS;
  53570. }
  53571. #else
  53572. static int test_wolfSSL_dtls_stateless(void)
  53573. {
  53574. return TEST_SKIPPED;
  53575. }
  53576. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  53577. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  53578. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  53579. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  53580. !defined(WOLFSSL_NO_CLIENT_AUTH))
  53581. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53582. {
  53583. int ret;
  53584. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  53585. fprintf(stderr, "loading cert %s failed\n", certA);
  53586. fprintf(stderr, "Error: (%d): %s\n", ret,
  53587. wolfSSL_ERR_reason_error_string(ret));
  53588. return -1;
  53589. }
  53590. return 0;
  53591. }
  53592. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53593. {
  53594. int ret;
  53595. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  53596. != WOLFSSL_SUCCESS) {
  53597. fprintf(stderr, "could not verify the cert: %s\n", certA);
  53598. fprintf(stderr, "Error: (%d): %s\n", ret,
  53599. wolfSSL_ERR_reason_error_string(ret));
  53600. return -1;
  53601. }
  53602. else {
  53603. fprintf(stderr, "successfully verified: %s\n", certA);
  53604. }
  53605. return 0;
  53606. }
  53607. #define LOAD_ONE_CA(a, b, c, d) \
  53608. do { \
  53609. (a) = load_ca_into_cm(c, d); \
  53610. if ((a) != 0) \
  53611. return (b); \
  53612. else \
  53613. (b)--; \
  53614. } while(0)
  53615. #define VERIFY_ONE_CERT(a, b, c, d) \
  53616. do { \
  53617. (a) = verify_cert_with_cm(c, d); \
  53618. if ((a) != 0) \
  53619. return (b); \
  53620. else \
  53621. (b)--; \
  53622. } while(0)
  53623. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  53624. {
  53625. int ret;
  53626. int i = -1;
  53627. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  53628. char chainGArr[9][50] = {"certs/ca-cert.pem",
  53629. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  53630. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  53631. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  53632. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  53633. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  53634. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  53635. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  53636. "certs/test-pathlen/chainG-entity.pem"};
  53637. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  53638. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  53639. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  53640. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  53641. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  53642. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  53643. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  53644. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  53645. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  53646. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  53647. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  53648. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  53649. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  53650. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  53651. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  53652. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  53653. /* test validating the entity twice, should have no effect on pathLen since
  53654. * entity/leaf cert */
  53655. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  53656. return ret;
  53657. }
  53658. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  53659. {
  53660. int ret;
  53661. int i = -1;
  53662. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  53663. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  53664. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  53665. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  53666. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  53667. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53668. */
  53669. char chainHArr[6][50] = {"certs/ca-cert.pem",
  53670. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  53671. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  53672. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  53673. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  53674. "certs/test-pathlen/chainH-entity.pem"};
  53675. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  53676. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  53677. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  53678. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  53679. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  53680. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  53681. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  53682. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  53683. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  53684. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  53685. return ret;
  53686. }
  53687. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  53688. {
  53689. int ret;
  53690. int i = -1;
  53691. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  53692. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  53693. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  53694. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  53695. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53696. */
  53697. char chainIArr[5][50] = {"certs/ca-cert.pem",
  53698. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  53699. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  53700. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  53701. "certs/test-pathlen/chainI-entity.pem"};
  53702. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  53703. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  53704. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  53705. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  53706. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  53707. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  53708. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  53709. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  53710. return ret;
  53711. }
  53712. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  53713. {
  53714. int ret;
  53715. int i = -1;
  53716. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  53717. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  53718. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  53719. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  53720. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  53721. */
  53722. char chainJArr[6][50] = {"certs/ca-cert.pem",
  53723. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  53724. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  53725. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  53726. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  53727. "certs/test-pathlen/chainJ-entity.pem"};
  53728. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  53729. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  53730. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  53731. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  53732. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  53733. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  53734. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  53735. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  53736. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  53737. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  53738. return ret;
  53739. }
  53740. static int test_various_pathlen_chains(void)
  53741. {
  53742. int ret;
  53743. WOLFSSL_CERT_MANAGER* cm;
  53744. /* Test chain G (large chain with varying pathLens) */
  53745. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53746. fprintf(stderr, "cert manager new failed\n");
  53747. return -1;
  53748. }
  53749. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53750. AssertIntEQ(test_chainG(cm), -1);
  53751. #else
  53752. AssertIntEQ(test_chainG(cm), 0);
  53753. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53754. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53755. if (ret != WOLFSSL_SUCCESS)
  53756. return -1;
  53757. wolfSSL_CertManagerFree(cm);
  53758. /* end test chain G */
  53759. /* Test chain H (5 chain with same pathLens) */
  53760. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53761. fprintf(stderr, "cert manager new failed\n");
  53762. return -1;
  53763. }
  53764. AssertIntLT(test_chainH(cm), 0);
  53765. wolfSSL_CertManagerUnloadCAs(cm);
  53766. wolfSSL_CertManagerFree(cm);
  53767. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53768. fprintf(stderr, "cert manager new failed\n");
  53769. return -1;
  53770. }
  53771. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53772. if (ret != WOLFSSL_SUCCESS)
  53773. return -1;
  53774. wolfSSL_CertManagerFree(cm);
  53775. /* end test chain H */
  53776. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  53777. * followed by 2 ICA's, should pass) */
  53778. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53779. fprintf(stderr, "cert manager new failed\n");
  53780. return -1;
  53781. }
  53782. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53783. AssertIntEQ(test_chainI(cm), -1);
  53784. #else
  53785. AssertIntEQ(test_chainI(cm), 0);
  53786. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53787. wolfSSL_CertManagerUnloadCAs(cm);
  53788. wolfSSL_CertManagerFree(cm);
  53789. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53790. fprintf(stderr, "cert manager new failed\n");
  53791. return -1;
  53792. }
  53793. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53794. if (ret != WOLFSSL_SUCCESS)
  53795. return -1;
  53796. wolfSSL_CertManagerFree(cm);
  53797. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  53798. * this time followed by 3 ICA's, should fail */
  53799. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53800. fprintf(stderr, "cert manager new failed\n");
  53801. return -1;
  53802. }
  53803. AssertIntLT(test_chainJ(cm), 0);
  53804. wolfSSL_CertManagerUnloadCAs(cm);
  53805. wolfSSL_CertManagerFree(cm);
  53806. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  53807. fprintf(stderr, "cert manager new failed\n");
  53808. return -1;
  53809. }
  53810. ret = wolfSSL_CertManagerUnloadCAs(cm);
  53811. wolfSSL_CertManagerFree(cm);
  53812. return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
  53813. }
  53814. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  53815. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53816. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  53817. {
  53818. byte ekm[100] = {0};
  53819. (void)ctx;
  53820. /* Succes Cases */
  53821. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53822. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  53823. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53824. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  53825. /* Use some random context */
  53826. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53827. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  53828. /* Failure cases */
  53829. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53830. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  53831. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53832. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  53833. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53834. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  53835. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53836. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  53837. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53838. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  53839. return TEST_RES_CHECK(1);
  53840. }
  53841. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  53842. {
  53843. wolfSSL_KeepArrays(ssl);
  53844. }
  53845. static int test_export_keying_material(void)
  53846. {
  53847. int res = TEST_SKIPPED;
  53848. #ifndef SINGLE_THREADED
  53849. tcp_ready ready;
  53850. callback_functions clientCb;
  53851. func_args client_args;
  53852. func_args server_args;
  53853. THREAD_TYPE serverThread;
  53854. XMEMSET(&client_args, 0, sizeof(func_args));
  53855. XMEMSET(&server_args, 0, sizeof(func_args));
  53856. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  53857. #ifdef WOLFSSL_TIRTOS
  53858. fdOpenSession(Task_self());
  53859. #endif
  53860. StartTCP();
  53861. InitTcpReady(&ready);
  53862. #if defined(USE_WINDOWS_API)
  53863. /* use RNG to get random port if using windows */
  53864. ready.port = GetRandomPort();
  53865. #endif
  53866. server_args.signal = &ready;
  53867. client_args.signal = &ready;
  53868. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  53869. client_args.callbacks = &clientCb;
  53870. start_thread(test_server_nofail, &server_args, &serverThread);
  53871. wait_tcp_ready(&server_args);
  53872. test_client_nofail(&client_args, test_export_keying_material_cb);
  53873. join_thread(serverThread);
  53874. AssertTrue(client_args.return_code);
  53875. AssertTrue(server_args.return_code);
  53876. FreeTcpReady(&ready);
  53877. #ifdef WOLFSSL_TIRTOS
  53878. fdOpenSession(Task_self());
  53879. #endif
  53880. res = TEST_RES_CHECK(1);
  53881. #endif /* !SINGLE_THREADED */
  53882. return res;
  53883. }
  53884. #endif /* HAVE_KEYING_MATERIAL */
  53885. static int test_wolfSSL_THREADID_hash(void)
  53886. {
  53887. int result = TEST_SKIPPED;
  53888. #if defined(OPENSSL_EXTRA)
  53889. unsigned long res;
  53890. CRYPTO_THREADID id;
  53891. CRYPTO_THREADID_current(NULL);
  53892. AssertTrue(1);
  53893. res = CRYPTO_THREADID_hash(NULL);
  53894. AssertTrue( res == 0UL);
  53895. XMEMSET(&id, 0, sizeof(id));
  53896. res = CRYPTO_THREADID_hash(&id);
  53897. AssertTrue( res == 0UL);
  53898. result = TEST_RES_CHECK(1);
  53899. #endif /* OPENSSL_EXTRA */
  53900. return result;
  53901. }
  53902. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  53903. {
  53904. int res = TEST_SKIPPED;
  53905. #if defined(OPENSSL_EXTRA)
  53906. WOLFSSL_CTX* ctx = NULL;
  53907. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  53908. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  53909. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  53910. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  53911. res = TEST_RES_CHECK(1);
  53912. #endif /* OPENSSL_EXTRA */
  53913. return res;
  53914. }
  53915. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  53916. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  53917. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  53918. {
  53919. callback_functions* callbacks = NULL;
  53920. WOLFSSL_CTX* ctx = NULL;
  53921. WOLFSSL* ssl = NULL;
  53922. SOCKET_T sfd = 0;
  53923. SOCKET_T cfd = 0;
  53924. word16 port;
  53925. char msg[] = "I hear you fa shizzle!";
  53926. int len = (int) XSTRLEN(msg);
  53927. char input[1024];
  53928. int ret, err;
  53929. if (!args)
  53930. return 0;
  53931. ((func_args*)args)->return_code = TEST_FAIL;
  53932. callbacks = ((func_args*)args)->callbacks;
  53933. ctx = wolfSSL_CTX_new(callbacks->method());
  53934. #if defined(USE_WINDOWS_API)
  53935. port = ((func_args*)args)->signal->port;
  53936. #else
  53937. /* Let tcp_listen assign port */
  53938. port = 0;
  53939. #endif
  53940. #ifdef WOLFSSL_TIRTOS
  53941. fdOpenSession(Task_self());
  53942. #endif
  53943. AssertIntEQ(WOLFSSL_SUCCESS,
  53944. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  53945. AssertIntEQ(WOLFSSL_SUCCESS,
  53946. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53947. WOLFSSL_FILETYPE_PEM));
  53948. AssertIntEQ(WOLFSSL_SUCCESS,
  53949. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53950. WOLFSSL_FILETYPE_PEM));
  53951. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  53952. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  53953. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53954. #elif !defined(NO_DH)
  53955. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  53956. #endif
  53957. if (callbacks->ctx_ready)
  53958. callbacks->ctx_ready(ctx);
  53959. ssl = wolfSSL_new(ctx);
  53960. AssertNotNull(ssl);
  53961. /* listen and accept */
  53962. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  53963. CloseSocket(sfd);
  53964. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  53965. if (callbacks->ssl_ready)
  53966. callbacks->ssl_ready(ssl);
  53967. do {
  53968. err = 0; /* Reset error */
  53969. ret = wolfSSL_accept(ssl);
  53970. if (ret != WOLFSSL_SUCCESS) {
  53971. err = wolfSSL_get_error(ssl, 0);
  53972. }
  53973. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  53974. if (ret != WOLFSSL_SUCCESS) {
  53975. wolfSSL_free(ssl);
  53976. wolfSSL_CTX_free(ctx);
  53977. CloseSocket(cfd);
  53978. ((func_args*)args)->return_code = TEST_FAIL;
  53979. return 0;
  53980. }
  53981. /* read and write data */
  53982. XMEMSET( input, 0, sizeof(input));
  53983. while (1) {
  53984. ret = wolfSSL_read(ssl, input, sizeof(input));
  53985. if (ret > 0) {
  53986. break;
  53987. }
  53988. else {
  53989. err = wolfSSL_get_error(ssl,ret);
  53990. if (err == WOLFSSL_ERROR_WANT_READ) {
  53991. continue;
  53992. }
  53993. break;
  53994. }
  53995. }
  53996. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  53997. do {
  53998. ret = wolfSSL_write(ssl, msg, len);
  53999. if (ret > 0) {
  54000. break;
  54001. }
  54002. } while (ret < 0);
  54003. }
  54004. /* bidirectional shutdown */
  54005. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  54006. continue;
  54007. }
  54008. /* wait for the peer to disconnect the tcp connection */
  54009. do {
  54010. ret = wolfSSL_read(ssl, input, sizeof(input));
  54011. err = wolfSSL_get_error(ssl, ret);
  54012. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54013. /* detect TCP disconnect */
  54014. AssertIntLE(ret,WOLFSSL_FAILURE);
  54015. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54016. ((func_args*)args)->return_code = TEST_SUCCESS;
  54017. wolfSSL_free(ssl);
  54018. wolfSSL_CTX_free(ctx);
  54019. CloseSocket(cfd);
  54020. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54021. wc_ecc_fp_free(); /* free per thread cache */
  54022. #endif
  54023. return 0;
  54024. }
  54025. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54026. {
  54027. callback_functions* callbacks = NULL;
  54028. WOLFSSL_CTX* ctx = NULL;
  54029. WOLFSSL* ssl = NULL;
  54030. SOCKET_T sfd = 0;
  54031. char msg[] = "hello wolfssl server!";
  54032. int len = (int) XSTRLEN(msg);
  54033. char input[1024];
  54034. int idx;
  54035. int ret, err;
  54036. if (!args)
  54037. return 0;
  54038. ((func_args*)args)->return_code = TEST_FAIL;
  54039. callbacks = ((func_args*)args)->callbacks;
  54040. ctx = wolfSSL_CTX_new(callbacks->method());
  54041. #ifdef WOLFSSL_TIRTOS
  54042. fdOpenSession(Task_self());
  54043. #endif
  54044. AssertIntEQ(WOLFSSL_SUCCESS,
  54045. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  54046. AssertIntEQ(WOLFSSL_SUCCESS,
  54047. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  54048. WOLFSSL_FILETYPE_PEM));
  54049. AssertIntEQ(WOLFSSL_SUCCESS,
  54050. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  54051. WOLFSSL_FILETYPE_PEM));
  54052. AssertNotNull((ssl = wolfSSL_new(ctx)));
  54053. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54054. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54055. do {
  54056. err = 0; /* Reset error */
  54057. ret = wolfSSL_connect(ssl);
  54058. if (ret != WOLFSSL_SUCCESS) {
  54059. err = wolfSSL_get_error(ssl, 0);
  54060. }
  54061. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54062. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  54063. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54064. input[idx] = 0;
  54065. }
  54066. ret = wolfSSL_shutdown(ssl);
  54067. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54068. ret = wolfSSL_shutdown(ssl);
  54069. }
  54070. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54071. ((func_args*)args)->return_code = TEST_SUCCESS;
  54072. wolfSSL_free(ssl);
  54073. wolfSSL_CTX_free(ctx);
  54074. CloseSocket(sfd);
  54075. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54076. wc_ecc_fp_free(); /* free per thread cache */
  54077. #endif
  54078. return 0;
  54079. }
  54080. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  54081. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  54082. /* This test is to check wolfSSL_read behaves as same as
  54083. * openSSL when it is called after SSL_shutdown completes.
  54084. */
  54085. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  54086. {
  54087. int res = TEST_SKIPPED;
  54088. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54089. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54090. tcp_ready ready;
  54091. func_args client_args;
  54092. func_args server_args;
  54093. THREAD_TYPE serverThread;
  54094. THREAD_TYPE clientThread;
  54095. callback_functions server_cbf;
  54096. callback_functions client_cbf;
  54097. #ifdef WOLFSSL_TIRTOS
  54098. fdOpenSession(Task_self());
  54099. #endif
  54100. StartTCP();
  54101. InitTcpReady(&ready);
  54102. #if defined(USE_WINDOWS_API)
  54103. /* use RNG to get random port if using windows */
  54104. ready.port = GetRandomPort();
  54105. #endif
  54106. XMEMSET(&client_args, 0, sizeof(func_args));
  54107. XMEMSET(&server_args, 0, sizeof(func_args));
  54108. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  54109. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  54110. server_cbf.method = wolfTLSv1_2_server_method;
  54111. client_cbf.method = wolfTLSv1_2_client_method;
  54112. server_args.callbacks = &server_cbf;
  54113. client_args.callbacks = &client_cbf;
  54114. server_args.signal = &ready;
  54115. client_args.signal = &ready;
  54116. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  54117. wait_tcp_ready(&server_args);
  54118. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  54119. join_thread(clientThread);
  54120. join_thread(serverThread);
  54121. AssertTrue(client_args.return_code);
  54122. AssertTrue(server_args.return_code);
  54123. FreeTcpReady(&ready);
  54124. res = TEST_RES_CHECK(1);
  54125. #endif
  54126. return res;
  54127. }
  54128. static int test_wolfSSL_CTX_get_min_proto_version(void)
  54129. {
  54130. int res = TEST_SKIPPED;
  54131. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54132. WOLFSSL_CTX *ctx;
  54133. (void)ctx;
  54134. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54135. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  54136. #ifdef WOLFSSL_ALLOW_SSLV3
  54137. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54138. #else
  54139. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  54140. #endif
  54141. wolfSSL_CTX_free(ctx);
  54142. #ifdef WOLFSSL_ALLOW_TLSV10
  54143. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  54144. #else
  54145. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54146. #endif
  54147. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  54148. #ifdef WOLFSSL_ALLOW_TLSV10
  54149. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54150. #else
  54151. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  54152. #endif
  54153. wolfSSL_CTX_free(ctx);
  54154. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54155. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  54156. #ifndef NO_OLD_TLS
  54157. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54158. #else
  54159. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  54160. #endif
  54161. wolfSSL_CTX_free(ctx);
  54162. #ifndef WOLFSSL_NO_TLS12
  54163. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  54164. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  54165. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  54166. wolfSSL_CTX_free(ctx);
  54167. #endif
  54168. #ifdef WOLFSSL_TLS13
  54169. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  54170. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  54171. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  54172. wolfSSL_CTX_free(ctx);
  54173. #endif
  54174. res = TEST_RES_CHECK(1);
  54175. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54176. return res;
  54177. }
  54178. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54179. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54180. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54181. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  54182. static int test_wolfSSL_set_SSL_CTX(void)
  54183. {
  54184. int res = TEST_SKIPPED;
  54185. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  54186. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  54187. !defined(NO_RSA)
  54188. WOLFSSL_CTX *ctx1, *ctx2;
  54189. WOLFSSL *ssl;
  54190. const byte *session_id1 = (const byte *)"CTX1";
  54191. const byte *session_id2 = (const byte *)"CTX2";
  54192. AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54193. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  54194. WOLFSSL_FILETYPE_PEM));
  54195. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  54196. WOLFSSL_FILETYPE_PEM));
  54197. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  54198. WOLFSSL_SUCCESS);
  54199. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  54200. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  54201. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  54202. WOLFSSL_SUCCESS);
  54203. AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  54204. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  54205. WOLFSSL_FILETYPE_PEM));
  54206. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  54207. WOLFSSL_FILETYPE_PEM));
  54208. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  54209. WOLFSSL_SUCCESS);
  54210. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  54211. WOLFSSL_SUCCESS);
  54212. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  54213. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  54214. AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  54215. WOLFSSL_SUCCESS);
  54216. #ifdef HAVE_SESSION_TICKET
  54217. AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  54218. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  54219. AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  54220. #endif
  54221. AssertNotNull(ssl = wolfSSL_new(ctx2));
  54222. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54223. #ifdef WOLFSSL_INT_H
  54224. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  54225. AssertTrue(ssl->buffers.certificate == ctx2->certificate);
  54226. AssertTrue(ssl->buffers.certChain == ctx2->certChain);
  54227. #endif
  54228. #ifdef HAVE_SESSION_TICKET
  54229. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54230. #endif
  54231. /* Set the ctx1 that has TLSv1.3 as max proto version */
  54232. AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  54233. /* MUST not change proto versions of ssl */
  54234. AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  54235. #ifdef HAVE_SESSION_TICKET
  54236. /* MUST not change */
  54237. AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  54238. #endif
  54239. /* MUST change */
  54240. #ifdef WOLFSSL_INT_H
  54241. AssertTrue(ssl->buffers.certificate == ctx1->certificate);
  54242. AssertTrue(ssl->buffers.certChain == ctx1->certChain);
  54243. AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  54244. #endif
  54245. wolfSSL_free(ssl);
  54246. wolfSSL_CTX_free(ctx1);
  54247. wolfSSL_CTX_free(ctx2);
  54248. res = TEST_RES_CHECK(1);
  54249. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  54250. return res;
  54251. }
  54252. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54253. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54254. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54255. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  54256. static int test_wolfSSL_security_level(void)
  54257. {
  54258. int res = TEST_SKIPPED;
  54259. #if defined(OPENSSL_EXTRA)
  54260. SSL_CTX *ctx;
  54261. #ifdef WOLFSSL_TLS13
  54262. #ifdef NO_WOLFSSL_SERVER
  54263. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54264. #else
  54265. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54266. #endif
  54267. SSL_CTX_set_security_level(ctx, 1);
  54268. AssertTrue(1);
  54269. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  54270. SSL_CTX_free(ctx);
  54271. #else
  54272. (void)ctx;
  54273. #endif
  54274. res = TEST_RES_CHECK(1);
  54275. #endif
  54276. return res;
  54277. }
  54278. static int test_wolfSSL_SSL_in_init(void)
  54279. {
  54280. int res = TEST_SKIPPED;
  54281. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  54282. SSL_CTX* ctx;
  54283. SSL* ssl;
  54284. const char* testCertFile;
  54285. const char* testKeyFile;
  54286. #ifdef WOLFSSL_TLS13
  54287. #ifdef NO_WOLFSSL_SERVER
  54288. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54289. #else
  54290. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54291. #endif
  54292. #ifndef NO_RSA
  54293. testCertFile = svrCertFile;
  54294. testKeyFile = svrKeyFile;
  54295. #elif defined(HAVE_ECC)
  54296. testCertFile = eccCertFile;
  54297. testKeyFile = eccKeyFile;
  54298. #else
  54299. testCertFile = NULL;
  54300. testKeyFile = NULL;
  54301. #endif
  54302. if (testCertFile != NULL && testKeyFile != NULL) {
  54303. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54304. SSL_FILETYPE_PEM));
  54305. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54306. SSL_FILETYPE_PEM));
  54307. }
  54308. ssl = SSL_new(ctx);
  54309. AssertNotNull(ssl);
  54310. AssertIntEQ(SSL_in_init(ssl), 1);
  54311. SSL_CTX_free(ctx);
  54312. SSL_free(ssl);
  54313. #else
  54314. (void)ctx;
  54315. (void)ssl;
  54316. (void)testCertFile;
  54317. (void)testKeyFile;
  54318. #endif
  54319. res = TEST_RES_CHECK(1);
  54320. #endif
  54321. return res;
  54322. }
  54323. static int test_wolfSSL_CTX_set_timeout(void)
  54324. {
  54325. int res = TEST_SKIPPED;
  54326. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  54327. int timeout;
  54328. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  54329. (void)timeout;
  54330. AssertNotNull(ctx);
  54331. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  54332. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  54333. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  54334. */
  54335. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54336. /* giving 0 as timeout value sets default timeout */
  54337. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  54338. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  54339. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  54340. #else
  54341. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  54342. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  54343. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  54344. #endif
  54345. wolfSSL_CTX_free(ctx);
  54346. res = TEST_RES_CHECK(1);
  54347. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  54348. return res;
  54349. }
  54350. static int test_wolfSSL_OpenSSL_version(void)
  54351. {
  54352. int res = TEST_SKIPPED;
  54353. #if defined(OPENSSL_EXTRA)
  54354. const char* ver;
  54355. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  54356. AssertNotNull(ver = OpenSSL_version(0));
  54357. #else
  54358. AssertNotNull(ver = OpenSSL_version());
  54359. #endif
  54360. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  54361. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  54362. res = TEST_RES_CHECK(1);
  54363. #endif
  54364. return res;
  54365. }
  54366. static int test_CONF_CTX_CMDLINE(void)
  54367. {
  54368. int res = TEST_SKIPPED;
  54369. #if defined(OPENSSL_ALL)
  54370. SSL_CTX* ctx = NULL;
  54371. SSL_CONF_CTX* cctx = NULL;
  54372. AssertNotNull(cctx = SSL_CONF_CTX_new());
  54373. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54374. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54375. AssertTrue(1);
  54376. /* set flags */
  54377. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  54378. WOLFSSL_CONF_FLAG_CMDLINE);
  54379. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54380. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54381. /* cmd invalid command */
  54382. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54383. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54384. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54385. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54386. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54387. /* cmd Certificate and Private Key*/
  54388. {
  54389. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54390. const char* ourCert = svrCertFile;
  54391. const char* ourKey = svrKeyFile;
  54392. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  54393. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  54394. WOLFSSL_SUCCESS);
  54395. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  54396. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  54397. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54398. #endif
  54399. }
  54400. /* cmd curves */
  54401. {
  54402. #if defined(HAVE_ECC)
  54403. const char* curve = "secp256r1";
  54404. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  54405. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  54406. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54407. #endif
  54408. }
  54409. /* cmd CipherString */
  54410. {
  54411. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54412. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  54413. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  54414. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54415. }
  54416. /* cmd DH parameter */
  54417. {
  54418. #if !defined(NO_DH) && !defined(NO_BIO)
  54419. const char* ourdhcert = "./certs/dh2048.pem";
  54420. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  54421. -3);
  54422. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  54423. WOLFSSL_SUCCESS);
  54424. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54425. #endif
  54426. }
  54427. SSL_CTX_free(ctx);
  54428. SSL_CONF_CTX_free(cctx);
  54429. res = TEST_RES_CHECK(1);
  54430. #endif /* OPENSSL_EXTRA */
  54431. return res;
  54432. }
  54433. static int test_CONF_CTX_FILE(void)
  54434. {
  54435. int res = TEST_SKIPPED;
  54436. #if defined(OPENSSL_ALL)
  54437. SSL_CTX* ctx = NULL;
  54438. SSL_CONF_CTX* cctx = NULL;
  54439. AssertNotNull(cctx = SSL_CONF_CTX_new());
  54440. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54441. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54442. AssertTrue(1);
  54443. /* set flags */
  54444. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  54445. WOLFSSL_CONF_FLAG_FILE);
  54446. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54447. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54448. /* sanity check */
  54449. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54450. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54451. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54452. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54453. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54454. /* cmd Certificate and Private Key*/
  54455. {
  54456. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54457. const char* ourCert = svrCertFile;
  54458. const char* ourKey = svrKeyFile;
  54459. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  54460. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  54461. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  54462. WOLFSSL_SUCCESS);
  54463. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  54464. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54465. #endif
  54466. }
  54467. /* cmd curves */
  54468. {
  54469. #if defined(HAVE_ECC)
  54470. const char* curve = "secp256r1";
  54471. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  54472. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  54473. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54474. #endif
  54475. }
  54476. /* cmd CipherString */
  54477. {
  54478. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54479. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  54480. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  54481. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54482. }
  54483. /* cmd DH parameter */
  54484. {
  54485. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  54486. const char* ourdhcert = "./certs/dh3072.pem";
  54487. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  54488. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  54489. WOLFSSL_SUCCESS);
  54490. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54491. #endif
  54492. }
  54493. SSL_CTX_free(ctx);
  54494. SSL_CONF_CTX_free(cctx);
  54495. res = TEST_RES_CHECK(1);
  54496. #endif /* OPENSSL_EXTRA */
  54497. return res;
  54498. }
  54499. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  54500. {
  54501. int res = TEST_SKIPPED;
  54502. #ifdef HAVE_EX_DATA
  54503. int idx1, idx2;
  54504. /* test for unsupported class index */
  54505. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  54506. 0,NULL, NULL, NULL, NULL ), -1);
  54507. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  54508. 0,NULL, NULL, NULL, NULL ), -1);
  54509. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  54510. 0,NULL, NULL, NULL, NULL ), -1);
  54511. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  54512. 0,NULL, NULL, NULL, NULL ), -1);
  54513. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  54514. 0,NULL, NULL, NULL, NULL ), -1);
  54515. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  54516. 0,NULL, NULL, NULL, NULL ), -1);
  54517. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  54518. 0,NULL, NULL, NULL, NULL ), -1);
  54519. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  54520. 0,NULL, NULL, NULL, NULL ), -1);
  54521. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  54522. 0,NULL, NULL, NULL, NULL ), -1);
  54523. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  54524. 0,NULL, NULL, NULL, NULL ), -1);
  54525. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  54526. 0,NULL, NULL, NULL, NULL ), -1);
  54527. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  54528. 0,NULL, NULL, NULL, NULL ), -1);
  54529. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  54530. /* test for supported class index */
  54531. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54532. 0,NULL, NULL, NULL, NULL );
  54533. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54534. 0,NULL, NULL, NULL, NULL );
  54535. AssertIntNE(idx1, -1);
  54536. AssertIntNE(idx2, -1);
  54537. AssertIntNE(idx1, idx2);
  54538. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54539. 0,NULL, NULL, NULL, NULL );
  54540. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54541. 0,NULL, NULL, NULL, NULL );
  54542. AssertIntNE(idx1, -1);
  54543. AssertIntNE(idx2, -1);
  54544. AssertIntNE(idx1, idx2);
  54545. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54546. 0,NULL, NULL, NULL, NULL );
  54547. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54548. 0,NULL, NULL, NULL, NULL );
  54549. AssertIntNE(idx1, -1);
  54550. AssertIntNE(idx2, -1);
  54551. AssertIntNE(idx1, idx2);
  54552. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54553. 0,NULL, NULL, NULL, NULL );
  54554. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54555. 0,NULL, NULL, NULL, NULL );
  54556. AssertIntNE(idx1, -1);
  54557. AssertIntNE(idx2, -1);
  54558. AssertIntNE(idx1, idx2);
  54559. res = TEST_RES_CHECK(1);
  54560. #endif /* HAVE_EX_DATA */
  54561. return res;
  54562. }
  54563. #if defined(HAVE_EX_DATA) && \
  54564. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54565. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54566. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54567. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  54568. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  54569. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  54570. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  54571. #define SESSION_NEW_IDX_PTR "Testing"
  54572. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  54573. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54574. {
  54575. AssertNotNull(p);
  54576. AssertNull(ptr);
  54577. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  54578. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54579. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54580. }
  54581. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  54582. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  54583. void* arg)
  54584. {
  54585. AssertNotNull(out);
  54586. AssertNotNull(in);
  54587. AssertPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  54588. AssertPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  54589. AssertPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  54590. AssertIntEQ(argV, SESSION_NEW_IDX_LONG);
  54591. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54592. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  54593. return SSL_SUCCESS;
  54594. }
  54595. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  54596. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  54597. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54598. {
  54599. AssertNotNull(p);
  54600. AssertNull(ptr);
  54601. AssertPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  54602. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54603. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54604. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  54605. }
  54606. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54607. {
  54608. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  54609. (void*)SESSION_NEW_IDX_PTR,
  54610. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  54611. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  54612. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  54613. SSL_SESSION* s = SSL_SESSION_new();
  54614. SSL_SESSION* d = NULL;
  54615. AssertNotNull(s);
  54616. AssertPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  54617. AssertNotNull(d = SSL_SESSION_dup(s));
  54618. AssertPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  54619. SSL_SESSION_free(s);
  54620. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  54621. SSL_SESSION_free(d);
  54622. AssertIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  54623. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  54624. crypto_ex_cb_ctx_session = NULL;
  54625. return TEST_RES_CHECK(1);
  54626. }
  54627. #else
  54628. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54629. {
  54630. return TEST_SKIPPED;
  54631. }
  54632. #endif
  54633. static int test_wolfSSL_set_psk_use_session_callback(void)
  54634. {
  54635. int res = TEST_SKIPPED;
  54636. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  54637. SSL_CTX* ctx;
  54638. SSL* ssl;
  54639. const char* testCertFile;
  54640. const char* testKeyFile;
  54641. #ifdef WOLFSSL_TLS13
  54642. #ifdef NO_WOLFSSL_SERVER
  54643. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54644. #else
  54645. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54646. #endif
  54647. #ifndef NO_RSA
  54648. testCertFile = svrCertFile;
  54649. testKeyFile = svrKeyFile;
  54650. #elif defined(HAVE_ECC)
  54651. testCertFile = eccCertFile;
  54652. testKeyFile = eccKeyFile;
  54653. #else
  54654. testCertFile = NULL;
  54655. testKeyFile = NULL;
  54656. #endif
  54657. if (testCertFile != NULL && testKeyFile != NULL) {
  54658. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54659. SSL_FILETYPE_PEM));
  54660. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54661. SSL_FILETYPE_PEM));
  54662. }
  54663. ssl = SSL_new(ctx);
  54664. AssertNotNull(ssl);
  54665. SSL_set_psk_use_session_callback(ssl,
  54666. my_psk_use_session_cb);
  54667. AssertTrue(1);
  54668. SSL_CTX_free(ctx);
  54669. SSL_free(ssl);
  54670. #else
  54671. (void)ctx;
  54672. (void)ssl;
  54673. (void)testCertFile;
  54674. (void)testKeyFile;
  54675. #endif
  54676. res = TEST_RES_CHECK(1);
  54677. #endif
  54678. return res;
  54679. }
  54680. static int test_wolfSSL_ERR_strings(void)
  54681. {
  54682. int res = TEST_SKIPPED;
  54683. #if !defined(NO_ERROR_STRINGS)
  54684. const char* err1 = "unsupported cipher suite";
  54685. const char* err2 = "wolfSSL PEM routines";
  54686. const char* err = NULL;
  54687. (void)err;
  54688. (void)err1;
  54689. (void)err2;
  54690. #if defined(OPENSSL_EXTRA)
  54691. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  54692. AssertTrue(err != NULL);
  54693. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54694. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  54695. AssertTrue(err != NULL);
  54696. AssertIntEQ((*err == '\0'), 1);
  54697. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  54698. AssertTrue(err != NULL);
  54699. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  54700. #else
  54701. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  54702. AssertTrue(err != NULL);
  54703. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54704. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  54705. AssertTrue(err != NULL);
  54706. AssertIntEQ((*err == '\0'), 1);
  54707. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  54708. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  54709. AssertTrue(err != NULL);
  54710. AssertIntEQ((*err == '\0'), 1);
  54711. #endif
  54712. res = TEST_RES_CHECK(1);
  54713. #endif
  54714. return res;
  54715. }
  54716. static int test_wolfSSL_EVP_shake128(void)
  54717. {
  54718. int res = TEST_SKIPPED;
  54719. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54720. defined(WOLFSSL_SHAKE128)
  54721. const EVP_MD* md = NULL;
  54722. md = EVP_shake128();
  54723. AssertTrue(md != NULL);
  54724. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  54725. res = TEST_RES_CHECK(1);
  54726. #endif
  54727. return res;
  54728. }
  54729. static int test_wolfSSL_EVP_shake256(void)
  54730. {
  54731. int res = TEST_SKIPPED;
  54732. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54733. defined(WOLFSSL_SHAKE256)
  54734. const EVP_MD* md = NULL;
  54735. md = EVP_shake256();
  54736. AssertTrue(md != NULL);
  54737. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  54738. res = TEST_RES_CHECK(1);
  54739. #endif
  54740. return res;
  54741. }
  54742. static int test_EVP_blake2(void)
  54743. {
  54744. int res = TEST_SKIPPED;
  54745. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  54746. const EVP_MD* md = NULL;
  54747. (void)md;
  54748. #if defined(HAVE_BLAKE2)
  54749. md = EVP_blake2b512();
  54750. AssertTrue(md != NULL);
  54751. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  54752. #endif
  54753. #if defined(HAVE_BLAKE2S)
  54754. md = EVP_blake2s256();
  54755. AssertTrue(md != NULL);
  54756. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  54757. #endif
  54758. res = TEST_RES_CHECK(1);
  54759. #endif
  54760. return res;
  54761. }
  54762. #if defined(OPENSSL_EXTRA)
  54763. static void list_md_fn(const EVP_MD* m, const char* from,
  54764. const char* to, void* arg)
  54765. {
  54766. const char* mn;
  54767. BIO *bio;
  54768. (void) from;
  54769. (void) to;
  54770. (void) arg;
  54771. (void) mn;
  54772. (void) bio;
  54773. if (!m) {
  54774. /* alias */
  54775. AssertNull(m);
  54776. AssertNotNull(to);
  54777. }
  54778. else {
  54779. AssertNotNull(m);
  54780. AssertNull(to);
  54781. }
  54782. AssertNotNull(from);
  54783. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54784. mn = EVP_get_digestbyname(from);
  54785. /* print to stderr */
  54786. AssertNotNull(arg);
  54787. bio = BIO_new(BIO_s_file());
  54788. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54789. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  54790. BIO_free(bio);
  54791. #endif
  54792. }
  54793. #endif
  54794. static int test_EVP_MD_do_all(void)
  54795. {
  54796. int res = TEST_SKIPPED;
  54797. #if defined(OPENSSL_EXTRA)
  54798. EVP_MD_do_all(NULL, stderr);
  54799. /* to confirm previous call gives no harm */
  54800. AssertTrue(1);
  54801. EVP_MD_do_all(list_md_fn, stderr);
  54802. /* to confirm previous call gives no harm */
  54803. AssertTrue(1);
  54804. res = TEST_RES_CHECK(1);
  54805. #endif
  54806. return res;
  54807. }
  54808. #if defined(OPENSSL_EXTRA)
  54809. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  54810. {
  54811. (void)arg;
  54812. (void)nm;
  54813. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  54814. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54815. /* print to stderr */
  54816. AssertNotNull(arg);
  54817. bio = BIO_new(BIO_s_file());
  54818. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54819. BIO_printf(bio, "%s\n", mn);
  54820. BIO_free(bio);
  54821. #endif
  54822. }
  54823. #endif
  54824. static int test_OBJ_NAME_do_all(void)
  54825. {
  54826. int res = TEST_SKIPPED;
  54827. #if defined(OPENSSL_EXTRA)
  54828. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  54829. /* to confirm previous call gives no harm */
  54830. AssertTrue(1);
  54831. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  54832. /* to confirm previous call gives no harm */
  54833. AssertTrue(1);
  54834. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  54835. AssertTrue(1);
  54836. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  54837. AssertTrue(1);
  54838. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  54839. AssertTrue(1);
  54840. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  54841. AssertTrue(1);
  54842. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  54843. AssertTrue(1);
  54844. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  54845. AssertTrue(1);
  54846. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  54847. AssertTrue(1);
  54848. res = TEST_RES_CHECK(1);
  54849. #endif
  54850. return res;
  54851. }
  54852. static int test_SSL_CIPHER_get_xxx(void)
  54853. {
  54854. int res = TEST_SKIPPED;
  54855. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  54856. !defined(NO_FILESYSTEM)
  54857. const SSL_CIPHER* cipher = NULL;
  54858. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  54859. int i, numCiphers = 0;
  54860. SSL_CTX* ctx = NULL;
  54861. SSL* ssl = NULL;
  54862. const char* testCertFile;
  54863. const char* testKeyFile;
  54864. char buf[256] = {0};
  54865. const char* cipher_id = NULL;
  54866. int expect_nid1 = NID_undef;
  54867. int expect_nid2 = NID_undef;
  54868. int expect_nid3 = NID_undef;
  54869. int expect_nid4 = NID_undef;
  54870. int expect_nid5 = 0;
  54871. const char* cipher_id2 = NULL;
  54872. int expect_nid21 = NID_undef;
  54873. int expect_nid22 = NID_undef;
  54874. int expect_nid23 = NID_undef;
  54875. int expect_nid24 = NID_undef;
  54876. int expect_nid25 = 0;
  54877. (void)cipher;
  54878. (void)supportedCiphers;
  54879. (void)i;
  54880. (void)numCiphers;
  54881. (void)ctx;
  54882. (void)ssl;
  54883. (void)testCertFile;
  54884. (void)testKeyFile;
  54885. #if defined(WOLFSSL_TLS13)
  54886. cipher_id = "TLS13-AES128-GCM-SHA256";
  54887. expect_nid1 = NID_auth_rsa;
  54888. expect_nid2 = NID_aes_128_gcm;
  54889. expect_nid3 = NID_sha256;
  54890. expect_nid4 = NID_kx_any;
  54891. expect_nid5 = 1;
  54892. #if !defined(WOLFSSL_NO_TLS12)
  54893. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  54894. expect_nid21 = NID_auth_rsa;
  54895. expect_nid22 = NID_aes_256_gcm;
  54896. expect_nid23 = NID_sha384;
  54897. expect_nid24 = NID_kx_ecdhe;
  54898. expect_nid25 = 1;
  54899. #endif
  54900. #endif
  54901. #ifdef NO_WOLFSSL_SERVER
  54902. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54903. #else
  54904. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54905. #endif
  54906. if (cipher_id) {
  54907. #ifndef NO_RSA
  54908. testCertFile = svrCertFile;
  54909. testKeyFile = svrKeyFile;
  54910. #elif defined(HAVE_ECC)
  54911. testCertFile = eccCertFile;
  54912. testKeyFile = eccKeyFile;
  54913. #else
  54914. testCertFile = NULL;
  54915. testKeyFile = NULL;
  54916. #endif
  54917. if (testCertFile != NULL && testKeyFile != NULL) {
  54918. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54919. SSL_FILETYPE_PEM));
  54920. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54921. SSL_FILETYPE_PEM));
  54922. }
  54923. ssl = SSL_new(ctx);
  54924. AssertNotNull(ssl);
  54925. AssertIntEQ(SSL_in_init(ssl), 1);
  54926. supportedCiphers = SSL_get_ciphers(ssl);
  54927. numCiphers = sk_num(supportedCiphers);
  54928. for (i = 0; i < numCiphers; ++i) {
  54929. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54930. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54931. }
  54932. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  54933. break;
  54934. }
  54935. }
  54936. /* test case for */
  54937. if (i != numCiphers) {
  54938. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  54939. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  54940. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  54941. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  54942. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  54943. }
  54944. if (cipher_id2) {
  54945. for (i = 0; i < numCiphers; ++i) {
  54946. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54947. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54948. }
  54949. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  54950. break;
  54951. }
  54952. }
  54953. /* test case for */
  54954. if (i != numCiphers) {
  54955. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  54956. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  54957. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  54958. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  54959. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  54960. }
  54961. }
  54962. }
  54963. if (ctx)
  54964. SSL_CTX_free(ctx);
  54965. if (ssl)
  54966. SSL_free(ssl);
  54967. res = TEST_RES_CHECK(1);
  54968. #endif
  54969. return res;
  54970. }
  54971. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  54972. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  54973. int* keyFormat)
  54974. {
  54975. int ret;
  54976. byte* key_buf = NULL;
  54977. size_t key_sz = 0;
  54978. EncryptedInfo encInfo;
  54979. XMEMSET(&encInfo, 0, sizeof(encInfo));
  54980. ret = load_file(privKeyFile, &key_buf, &key_sz);
  54981. if (ret == 0) {
  54982. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  54983. NULL, &encInfo, keyFormat);
  54984. }
  54985. if (key_buf != NULL) {
  54986. free(key_buf); key_buf = NULL;
  54987. }
  54988. (void)encInfo; /* not used in this test */
  54989. #ifdef DEBUG_WOLFSSL
  54990. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  54991. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  54992. (*pDer)->length);
  54993. #endif
  54994. return ret;
  54995. }
  54996. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  54997. {
  54998. int ret = CRYPTOCB_UNAVAILABLE;
  54999. const char* privKeyFile = (const char*)ctx;
  55000. DerBuffer* pDer = NULL;
  55001. int keyFormat = 0;
  55002. if (info->algo_type == WC_ALGO_TYPE_PK) {
  55003. #ifdef DEBUG_WOLFSSL
  55004. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  55005. #endif
  55006. #ifndef NO_RSA
  55007. if (info->pk.type == WC_PK_TYPE_RSA) {
  55008. switch (info->pk.rsa.type) {
  55009. case RSA_PUBLIC_ENCRYPT:
  55010. case RSA_PUBLIC_DECRYPT:
  55011. /* perform software based RSA public op */
  55012. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55013. break;
  55014. case RSA_PRIVATE_ENCRYPT:
  55015. case RSA_PRIVATE_DECRYPT:
  55016. {
  55017. RsaKey key;
  55018. /* perform software based RSA private op */
  55019. #ifdef DEBUG_WOLFSSL
  55020. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55021. #endif
  55022. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55023. &keyFormat);
  55024. if (ret != 0) {
  55025. return ret;
  55026. }
  55027. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55028. if (ret == 0) {
  55029. word32 keyIdx = 0;
  55030. /* load RSA private key and perform private transform */
  55031. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55032. &key, pDer->length);
  55033. if (ret == 0) {
  55034. ret = wc_RsaFunction(
  55035. info->pk.rsa.in, info->pk.rsa.inLen,
  55036. info->pk.rsa.out, info->pk.rsa.outLen,
  55037. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55038. }
  55039. else {
  55040. /* if decode fails, then fall-back to software based crypto */
  55041. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55042. "key decode failed %d, falling back to "
  55043. "software\n", ret);
  55044. ret = CRYPTOCB_UNAVAILABLE;
  55045. }
  55046. wc_FreeRsaKey(&key);
  55047. }
  55048. wc_FreeDer(&pDer); pDer = NULL;
  55049. break;
  55050. }
  55051. }
  55052. #ifdef DEBUG_WOLFSSL
  55053. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55054. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55055. #endif
  55056. }
  55057. #endif /* !NO_RSA */
  55058. #ifdef HAVE_ECC
  55059. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55060. /* mark this key as ephemeral */
  55061. if (info->pk.eckg.key != NULL) {
  55062. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  55063. sizeof(info->pk.eckg.key->label));
  55064. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  55065. }
  55066. }
  55067. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  55068. ecc_key key;
  55069. /* perform software based ECC sign */
  55070. #ifdef DEBUG_WOLFSSL
  55071. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  55072. #endif
  55073. if (info->pk.eccsign.key != NULL &&
  55074. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  55075. /* this is an empheral key */
  55076. #ifdef DEBUG_WOLFSSL
  55077. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  55078. "ephemeral key\n");
  55079. #endif
  55080. return CRYPTOCB_UNAVAILABLE;
  55081. }
  55082. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55083. if (ret != 0) {
  55084. return ret;
  55085. }
  55086. ret = wc_ecc_init(&key);
  55087. if (ret == 0) {
  55088. word32 keyIdx = 0;
  55089. /* load ECC private key and perform private transform */
  55090. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  55091. &key, pDer->length);
  55092. if (ret == 0) {
  55093. ret = wc_ecc_sign_hash(
  55094. info->pk.eccsign.in, info->pk.eccsign.inlen,
  55095. info->pk.eccsign.out, info->pk.eccsign.outlen,
  55096. info->pk.eccsign.rng, &key);
  55097. }
  55098. else {
  55099. /* if decode fails, then fall-back to software based crypto */
  55100. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  55101. "decode failed %d, falling back to software\n", ret);
  55102. ret = CRYPTOCB_UNAVAILABLE;
  55103. }
  55104. wc_ecc_free(&key);
  55105. }
  55106. wc_FreeDer(&pDer); pDer = NULL;
  55107. #ifdef DEBUG_WOLFSSL
  55108. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  55109. ret, *info->pk.eccsign.outlen);
  55110. #endif
  55111. }
  55112. #endif /* HAVE_ECC */
  55113. #ifdef HAVE_ED25519
  55114. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  55115. ed25519_key key;
  55116. /* perform software based ED25519 sign */
  55117. #ifdef DEBUG_WOLFSSL
  55118. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  55119. #endif
  55120. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  55121. if (ret != 0) {
  55122. return ret;
  55123. }
  55124. ret = wc_ed25519_init(&key);
  55125. if (ret == 0) {
  55126. word32 keyIdx = 0;
  55127. /* load ED25519 private key and perform private transform */
  55128. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  55129. &key, pDer->length);
  55130. if (ret == 0) {
  55131. /* calculate public key */
  55132. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  55133. if (ret == 0) {
  55134. key.pubKeySet = 1;
  55135. ret = wc_ed25519_sign_msg_ex(
  55136. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  55137. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  55138. &key, info->pk.ed25519sign.type,
  55139. info->pk.ed25519sign.context,
  55140. info->pk.ed25519sign.contextLen);
  55141. }
  55142. }
  55143. else {
  55144. /* if decode fails, then fall-back to software based crypto */
  55145. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  55146. "decode failed %d, falling back to software\n", ret);
  55147. ret = CRYPTOCB_UNAVAILABLE;
  55148. }
  55149. wc_ed25519_free(&key);
  55150. }
  55151. wc_FreeDer(&pDer); pDer = NULL;
  55152. #ifdef DEBUG_WOLFSSL
  55153. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  55154. ret, *info->pk.ed25519sign.outLen);
  55155. #endif
  55156. }
  55157. #endif /* HAVE_ED25519 */
  55158. }
  55159. (void)thisDevId;
  55160. (void)keyFormat;
  55161. return ret;
  55162. }
  55163. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55164. static void test_wc_CryptoCb_TLS(int tlsVer,
  55165. const char* cliCaPemFile, const char* cliCertPemFile,
  55166. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  55167. const char* svrCaPemFile, const char* svrCertPemFile,
  55168. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  55169. {
  55170. callback_functions client_cbf;
  55171. callback_functions server_cbf;
  55172. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55173. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55174. if (tlsVer == WOLFSSL_TLSV1_3) {
  55175. #ifdef WOLFSSL_TLS13
  55176. server_cbf.method = wolfTLSv1_3_server_method;
  55177. client_cbf.method = wolfTLSv1_3_client_method;
  55178. #endif
  55179. }
  55180. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55181. #ifndef WOLFSSL_NO_TLS12
  55182. server_cbf.method = wolfTLSv1_2_server_method;
  55183. client_cbf.method = wolfTLSv1_2_client_method;
  55184. #endif
  55185. }
  55186. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55187. #ifndef NO_OLD_TLS
  55188. server_cbf.method = wolfTLSv1_1_server_method;
  55189. client_cbf.method = wolfTLSv1_1_client_method;
  55190. #endif
  55191. }
  55192. else if (tlsVer == WOLFSSL_TLSV1) {
  55193. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55194. server_cbf.method = wolfTLSv1_server_method;
  55195. client_cbf.method = wolfTLSv1_client_method;
  55196. #endif
  55197. }
  55198. else if (tlsVer == WOLFSSL_SSLV3) {
  55199. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55200. defined(WOLFSSL_STATIC_RSA)
  55201. server_cbf.method = wolfSSLv3_server_method;
  55202. client_cbf.method = wolfSSLv3_client_method;
  55203. #endif
  55204. }
  55205. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55206. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55207. server_cbf.method = wolfDTLSv1_2_server_method;
  55208. client_cbf.method = wolfDTLSv1_2_client_method;
  55209. #endif
  55210. }
  55211. else if (tlsVer == WOLFSSL_DTLSV1) {
  55212. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55213. server_cbf.method = wolfDTLSv1_server_method;
  55214. client_cbf.method = wolfDTLSv1_client_method;
  55215. #endif
  55216. }
  55217. if (server_cbf.method == NULL) {
  55218. /* not enabled */
  55219. return;
  55220. }
  55221. /* Setup the keys for the TLS test */
  55222. client_cbf.certPemFile = cliCertPemFile;
  55223. client_cbf.keyPemFile = cliPubKeyPemFile;
  55224. client_cbf.caPemFile = cliCaPemFile;
  55225. server_cbf.certPemFile = svrCertPemFile;
  55226. server_cbf.keyPemFile = svrPubKeyPemFile;
  55227. server_cbf.caPemFile = svrCaPemFile;
  55228. /* Setup a crypto callback with pointer to private key file for testing */
  55229. client_cbf.devId = 1;
  55230. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  55231. (void*)cliPrivKeyPemFile);
  55232. server_cbf.devId = 2;
  55233. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  55234. (void*)svrPrivKeyPemFile);
  55235. /* Perform TLS server and client test */
  55236. /* First test is at WOLFSSL_CTX level */
  55237. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55238. /* Check for success */
  55239. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55240. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55241. /* Second test is a WOLFSSL object level */
  55242. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55243. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55244. /* Check for success */
  55245. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55246. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55247. /* Un register the devId's */
  55248. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  55249. client_cbf.devId = INVALID_DEVID;
  55250. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  55251. server_cbf.devId = INVALID_DEVID;
  55252. }
  55253. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  55254. static int test_wc_CryptoCb(void)
  55255. {
  55256. int res = TEST_SKIPPED;
  55257. #ifdef WOLF_CRYPTO_CB
  55258. /* TODO: Add crypto callback API tests */
  55259. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55260. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55261. int tlsVer;
  55262. #endif
  55263. #ifndef NO_RSA
  55264. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55265. test_wc_CryptoCb_TLS(tlsVer,
  55266. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  55267. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  55268. }
  55269. #endif
  55270. #ifdef HAVE_ECC
  55271. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55272. test_wc_CryptoCb_TLS(tlsVer,
  55273. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  55274. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  55275. }
  55276. #endif
  55277. #ifdef HAVE_ED25519
  55278. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55279. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55280. test_wc_CryptoCb_TLS(tlsVer,
  55281. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  55282. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  55283. }
  55284. #endif
  55285. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55286. res = TEST_RES_CHECK(1);
  55287. #endif /* WOLF_CRYPTO_CB */
  55288. return res;
  55289. }
  55290. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55291. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  55292. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  55293. const char* cliCaPemFile, const char* cliCertPemFile,
  55294. const char* cliPrivKeyPemFile,
  55295. const char* svrCaPemFile, const char* svrCertPemFile,
  55296. const char* svrPrivKeyPemFile,
  55297. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  55298. {
  55299. callback_functions client_cbf;
  55300. callback_functions server_cbf;
  55301. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55302. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55303. if (tlsVer == WOLFSSL_TLSV1_3) {
  55304. #ifdef WOLFSSL_TLS13
  55305. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  55306. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  55307. #endif
  55308. }
  55309. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55310. #ifndef WOLFSSL_NO_TLS12
  55311. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  55312. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  55313. #endif
  55314. }
  55315. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55316. #ifndef NO_OLD_TLS
  55317. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  55318. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  55319. #endif
  55320. }
  55321. else if (tlsVer == WOLFSSL_TLSV1) {
  55322. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55323. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  55324. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  55325. #endif
  55326. }
  55327. else if (tlsVer == WOLFSSL_SSLV3) {
  55328. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55329. defined(WOLFSSL_STATIC_RSA)
  55330. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  55331. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  55332. #endif
  55333. }
  55334. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55335. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55336. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  55337. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  55338. #endif
  55339. }
  55340. else if (tlsVer == WOLFSSL_DTLSV1) {
  55341. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55342. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  55343. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  55344. #endif
  55345. }
  55346. if (server_cbf.method_ex == NULL) {
  55347. /* not enabled */
  55348. return;
  55349. }
  55350. /* Setup the keys for the TLS test */
  55351. client_cbf.certPemFile = cliCertPemFile;
  55352. client_cbf.keyPemFile = cliPrivKeyPemFile;
  55353. client_cbf.caPemFile = cliCaPemFile;
  55354. server_cbf.certPemFile = svrCertPemFile;
  55355. server_cbf.keyPemFile = svrPrivKeyPemFile;
  55356. server_cbf.caPemFile = svrCaPemFile;
  55357. client_cbf.mem = cliMem;
  55358. client_cbf.memSz = cliMemSz;
  55359. server_cbf.mem = svrMem;
  55360. server_cbf.memSz = svrMemSz;
  55361. client_cbf.devId = INVALID_DEVID;
  55362. server_cbf.devId = INVALID_DEVID;
  55363. /* Perform TLS server and client test */
  55364. /* First test is at WOLFSSL_CTX level */
  55365. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55366. /* Check for success */
  55367. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55368. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55369. /* Second test is a WOLFSSL object level */
  55370. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55371. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55372. /* Check for success */
  55373. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55374. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55375. }
  55376. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  55377. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55378. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  55379. defined(SESSION_CERTS)
  55380. #ifdef OPENSSL_EXTRA
  55381. #define TEST_TLS_STATIC_MEMSZ (400000)
  55382. #else
  55383. #define TEST_TLS_STATIC_MEMSZ (320000)
  55384. #endif
  55385. #else
  55386. #define TEST_TLS_STATIC_MEMSZ (80000)
  55387. #endif
  55388. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  55389. {
  55390. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  55391. WOLFSSL_MEM_STATS mem_stats;
  55392. WOLFSSL_MEM_CONN_STATS ssl_stats;
  55393. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  55394. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55395. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55396. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55397. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55398. #endif
  55399. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  55400. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  55401. /* this should fail because kMaxCtxClients == 2 */
  55402. AssertNull((ssl3 = wolfSSL_new(ctx)));
  55403. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  55404. #ifdef DEBUG_WOLFSSL
  55405. wolfSSL_PrintStatsConn(&ssl_stats);
  55406. #endif
  55407. (void)ssl_stats;
  55408. }
  55409. /* display collected statistics */
  55410. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  55411. #ifdef DEBUG_WOLFSSL
  55412. wolfSSL_PrintStats(&mem_stats);
  55413. #endif
  55414. (void)mem_stats;
  55415. }
  55416. wolfSSL_free(ssl1);
  55417. wolfSSL_free(ssl2);
  55418. return TEST_RES_CHECK(1);
  55419. }
  55420. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55421. static int test_wolfSSL_CTX_StaticMemory(void)
  55422. {
  55423. int res = TEST_SKIPPED;
  55424. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55425. wolfSSL_method_func method_func;
  55426. WOLFSSL_CTX* ctx;
  55427. const int kMaxCtxClients = 2;
  55428. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55429. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55430. int tlsVer;
  55431. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  55432. #endif
  55433. #endif
  55434. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  55435. #ifndef NO_WOLFSSL_SERVER
  55436. #ifndef WOLFSSL_NO_TLS12
  55437. method_func = wolfTLSv1_2_server_method_ex;
  55438. #else
  55439. method_func = wolfTLSv1_3_server_method_ex;
  55440. #endif
  55441. #else
  55442. #ifndef WOLFSSL_NO_TLS12
  55443. method_func = wolfTLSv1_2_client_method_ex;
  55444. #else
  55445. method_func = wolfTLSv1_3_client_method_ex;
  55446. #endif
  55447. #endif
  55448. /* Test creating CTX directly from static memory pool */
  55449. ctx = NULL;
  55450. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  55451. &ctx, method_func, svrMem, sizeof(svrMem),
  55452. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55453. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  55454. wolfSSL_CTX_free(ctx);
  55455. ctx = NULL;
  55456. /* Test for heap allocated CTX, then assigning static pool to it */
  55457. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  55458. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  55459. NULL, svrMem, sizeof(svrMem),
  55460. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55461. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  55462. wolfSSL_CTX_free(ctx);
  55463. /* TLS Level Tests using static memory */
  55464. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55465. #ifndef NO_RSA
  55466. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55467. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55468. svrCertFile, cliCertFile, cliKeyFile,
  55469. cliCertFile, svrCertFile, svrKeyFile,
  55470. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55471. }
  55472. #endif
  55473. #ifdef HAVE_ECC
  55474. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55475. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55476. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  55477. cliEccCertFile, eccCertFile, eccKeyFile,
  55478. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55479. }
  55480. #endif
  55481. #ifdef HAVE_ED25519
  55482. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55483. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55484. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55485. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  55486. cliEdCertFile, edCertFile, edKeyFile,
  55487. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  55488. }
  55489. #endif
  55490. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55491. res = TEST_RES_CHECK(1);
  55492. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55493. return res;
  55494. }
  55495. static int test_openssl_FIPS_drbg(void)
  55496. {
  55497. int res = TEST_SKIPPED;
  55498. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  55499. DRBG_CTX* dctx;
  55500. byte data1[32], data2[32], zeroData[32];
  55501. byte testSeed[16];
  55502. size_t dlen = sizeof(data1);
  55503. int i;
  55504. XMEMSET(data1, 0, dlen);
  55505. XMEMSET(data2, 0, dlen);
  55506. XMEMSET(zeroData, 0, sizeof(zeroData));
  55507. for (i=0; i<(int)sizeof(testSeed); i++) {
  55508. testSeed[i] = (byte)i;
  55509. }
  55510. AssertNotNull(dctx = FIPS_get_default_drbg());
  55511. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  55512. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  55513. WOLFSSL_SUCCESS);
  55514. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  55515. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  55516. WOLFSSL_SUCCESS);
  55517. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55518. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  55519. WOLFSSL_SUCCESS);
  55520. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  55521. WOLFSSL_SUCCESS);
  55522. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55523. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  55524. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  55525. #ifndef HAVE_GLOBAL_RNG
  55526. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  55527. wolfSSL_FIPS_drbg_free(dctx);
  55528. #endif
  55529. res = TEST_RES_CHECK(1);
  55530. #endif
  55531. return res;
  55532. }
  55533. static int test_wolfSSL_FIPS_mode(void)
  55534. {
  55535. int res = TEST_SKIPPED;
  55536. #if defined(OPENSSL_ALL)
  55537. #ifdef HAVE_FIPS
  55538. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  55539. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  55540. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  55541. #else
  55542. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  55543. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  55544. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  55545. #endif
  55546. res = TEST_RES_CHECK(1);
  55547. #endif
  55548. return res;
  55549. }
  55550. #ifdef WOLFSSL_DTLS
  55551. /* Prints out the current window */
  55552. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  55553. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55554. int i;
  55555. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  55556. word32 b = w[i];
  55557. int j;
  55558. /* Prints out a 32 bit binary number in big endian order */
  55559. for (j = 0; j < 32; j++, b <<= 1) {
  55560. if (b & (((word32)1) << 31))
  55561. fprintf(stderr, "1");
  55562. else
  55563. fprintf(stderr, "0");
  55564. }
  55565. fprintf(stderr, " ");
  55566. }
  55567. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  55568. #else
  55569. (void)h;
  55570. (void)l;
  55571. (void)w;
  55572. #endif
  55573. }
  55574. /* a - cur_hi
  55575. * b - cur_lo
  55576. * c - next_hi
  55577. * d - next_lo
  55578. * e - window
  55579. * f - expected next_hi
  55580. * g - expected next_lo
  55581. * h - expected window[1]
  55582. * i - expected window[0]
  55583. */
  55584. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  55585. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  55586. DUW_TEST_print_window_binary((a), (b), (e)); \
  55587. AssertIntEQ((c), (f)); \
  55588. AssertIntEQ((d), (g)); \
  55589. AssertIntEQ((e)[1], (h)); \
  55590. AssertIntEQ((e)[0], (i)); \
  55591. } while (0)
  55592. static int test_wolfSSL_DtlsUpdateWindow(void)
  55593. {
  55594. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  55595. word32 next_lo = 0;
  55596. word16 next_hi = 0;
  55597. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55598. fprintf(stderr, "\n");
  55599. #endif
  55600. XMEMSET(window, 0, sizeof window);
  55601. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  55602. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  55603. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  55604. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  55605. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  55606. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  55607. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  55608. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  55609. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  55610. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  55611. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  55612. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  55613. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  55614. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  55615. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  55616. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  55617. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  55618. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  55619. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  55620. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  55621. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  55622. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55623. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55624. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  55625. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  55626. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55627. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55628. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55629. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55630. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55631. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  55632. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  55633. return TEST_RES_CHECK(1);
  55634. }
  55635. #endif /* WOLFSSL_DTLS */
  55636. #ifdef WOLFSSL_DTLS
  55637. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  55638. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  55639. {
  55640. DtlsMsg* cur;
  55641. static byte msg[100];
  55642. static byte msgInit = 0;
  55643. if (!msgInit) {
  55644. int i;
  55645. for (i = 0; i < 100; i++)
  55646. msg[i] = i + 1;
  55647. msgInit = 1;
  55648. }
  55649. /* Sanitize test parameters */
  55650. if (len > sizeof(msg))
  55651. return -1;
  55652. if (f_offset + f_len > sizeof(msg))
  55653. return -1;
  55654. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  55655. if (ssl->dtls_rx_msg_list == NULL)
  55656. return -100;
  55657. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  55658. return -200;
  55659. if (cur->fragBucketListCount != f_count)
  55660. return -300;
  55661. if (cur->ready != ready)
  55662. return -400;
  55663. if (cur->bytesReceived != bytesReceived)
  55664. return -500;
  55665. if (ready) {
  55666. if (cur->fragBucketList != NULL)
  55667. return -600;
  55668. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  55669. return -700;
  55670. }
  55671. else {
  55672. DtlsFragBucket* fb;
  55673. if (cur->fragBucketList == NULL)
  55674. return -800;
  55675. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  55676. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  55677. return -900;
  55678. }
  55679. }
  55680. return 0;
  55681. }
  55682. static void DFB_TEST_RESET(WOLFSSL* ssl)
  55683. {
  55684. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  55685. ssl->dtls_rx_msg_list = NULL;
  55686. ssl->dtls_rx_msg_list_sz = 0;
  55687. }
  55688. static int test_wolfSSL_DTLS_fragment_buckets(void)
  55689. {
  55690. WOLFSSL ssl[1];
  55691. XMEMSET(ssl, 0, sizeof(*ssl));
  55692. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  55693. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55694. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  55695. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  55696. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  55697. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  55698. /* Test all permutations of 3 regions */
  55699. /* 1 2 3 */
  55700. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55701. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  55702. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55703. /* 1 3 2 */
  55704. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55705. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  55706. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55707. /* 2 1 3 */
  55708. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55709. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  55710. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55711. /* 2 3 1 */
  55712. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55713. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  55714. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55715. /* 3 1 2 */
  55716. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55717. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  55718. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55719. /* 3 2 1 */
  55720. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55721. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  55722. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55723. /* Test overlapping regions */
  55724. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55725. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  55726. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  55727. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  55728. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  55729. /* Test overlapping multiple regions */
  55730. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55731. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  55732. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  55733. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  55734. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  55735. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  55736. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  55737. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  55738. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  55739. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  55740. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55741. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  55742. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  55743. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  55744. DFB_TEST_RESET(ssl);
  55745. return TEST_RES_CHECK(1);
  55746. }
  55747. #endif
  55748. #if !defined(NO_FILESYSTEM) && \
  55749. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55750. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  55751. static int test_wolfSSL_dtls_stateless2(void)
  55752. {
  55753. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55754. struct test_memio_ctx test_ctx;
  55755. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55756. int ret;
  55757. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55758. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55759. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55760. if (ret != 0)
  55761. return -1;
  55762. ssl_c2 = wolfSSL_new(ctx_c);
  55763. if (ssl_c2 == NULL)
  55764. return -2;
  55765. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55766. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55767. /* send CH */
  55768. ret = wolfSSL_connect(ssl_c2);
  55769. if (ret == 0 || ssl_c2->error != WANT_READ)
  55770. return -3;
  55771. ret = wolfSSL_accept(ssl_s);
  55772. if (ret == 0 || ssl_s->error != WANT_READ)
  55773. return -4;
  55774. if (test_ctx.c_len == 0)
  55775. return -5;
  55776. /* consume HRR */
  55777. test_ctx.c_len = 0;
  55778. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55779. if (ret != 0)
  55780. return -6;
  55781. wolfSSL_free(ssl_c2);
  55782. wolfSSL_free(ssl_c);
  55783. wolfSSL_free(ssl_s);
  55784. wolfSSL_CTX_free(ctx_c);
  55785. wolfSSL_CTX_free(ctx_s);
  55786. return TEST_SUCCESS;
  55787. }
  55788. #ifdef HAVE_MAX_FRAGMENT
  55789. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  55790. {
  55791. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55792. struct test_memio_ctx test_ctx;
  55793. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55794. word16 max_fragment;
  55795. int ret;
  55796. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55797. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55798. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55799. if (ret != 0)
  55800. return -1;
  55801. ssl_c2 = wolfSSL_new(ctx_c);
  55802. if (ssl_c2 == NULL)
  55803. return -2;
  55804. ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
  55805. if (ret != WOLFSSL_SUCCESS)
  55806. return -3;
  55807. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55808. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55809. max_fragment = ssl_s->max_fragment;
  55810. /* send CH */
  55811. ret = wolfSSL_connect(ssl_c2);
  55812. if (ret == 0 || ssl_c2->error != WANT_READ)
  55813. return -4;
  55814. ret = wolfSSL_accept(ssl_s);
  55815. if (ret == 0 || ssl_s->error != WANT_READ)
  55816. return -5;
  55817. /* CH without cookie shouldn't change state */
  55818. if (ssl_s->max_fragment != max_fragment)
  55819. return -6;
  55820. if (test_ctx.c_len == 0)
  55821. return -7;
  55822. /* consume HRR from buffer */
  55823. test_ctx.c_len = 0;
  55824. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55825. if (ret != 0)
  55826. return -8;
  55827. wolfSSL_free(ssl_c2);
  55828. wolfSSL_free(ssl_c);
  55829. wolfSSL_free(ssl_s);
  55830. wolfSSL_CTX_free(ctx_c);
  55831. wolfSSL_CTX_free(ctx_s);
  55832. return TEST_SUCCESS;
  55833. }
  55834. #endif /* HAVE_MAX_FRAGMENT */
  55835. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  55836. #define ROUNDS_WITH_HVR 4
  55837. #define ROUNDS_WITHOUT_HVR 2
  55838. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  55839. static int buf_is_hvr(const byte *data, int len)
  55840. {
  55841. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  55842. return 0;
  55843. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  55844. }
  55845. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  55846. {
  55847. struct test_memio_ctx test_ctx;
  55848. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55849. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  55850. WOLFSSL_SESSION *sess;
  55851. int ret, round_trips;
  55852. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55853. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55854. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55855. if (ret != 0)
  55856. return -1;
  55857. #ifdef HAVE_SESSION_TICKET
  55858. if (useticket) {
  55859. ret = wolfSSL_UseSessionTicket(ssl_c);
  55860. if (ret != WOLFSSL_SUCCESS)
  55861. return -2;
  55862. }
  55863. #endif
  55864. round_trips = ROUNDS_WITH_HVR;
  55865. ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
  55866. if (ret != 0)
  55867. return -3;
  55868. if (round_trips != ROUNDS_WITH_HVR)
  55869. return -4;
  55870. sess = wolfSSL_get1_session(ssl_c);
  55871. if (sess == NULL)
  55872. return -5;
  55873. wolfSSL_shutdown(ssl_c);
  55874. wolfSSL_shutdown(ssl_s);
  55875. wolfSSL_free(ssl_c);
  55876. wolfSSL_free(ssl_s);
  55877. test_ctx.c_len = test_ctx.s_len = 0;
  55878. /* make resumption invalid */
  55879. if (bad) {
  55880. if (useticket) {
  55881. #ifdef HAVE_SESSION_TICKET
  55882. sess->ticket[0] = !sess->ticket[0];
  55883. #endif /* HAVE_SESSION_TICKET */
  55884. }
  55885. else {
  55886. sess->sessionID[0] = !sess->sessionID[0];
  55887. }
  55888. }
  55889. ssl_c = wolfSSL_new(ctx_c);
  55890. ssl_s = wolfSSL_new(ctx_s);
  55891. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  55892. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  55893. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  55894. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  55895. ret = wolfSSL_set_session(ssl_c, sess);
  55896. if (ret != WOLFSSL_SUCCESS)
  55897. return -6;
  55898. ret = wolfSSL_connect(ssl_c);
  55899. if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
  55900. return -7;
  55901. ret = wolfSSL_accept(ssl_s);
  55902. if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
  55903. return -8;
  55904. if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  55905. return -9;
  55906. if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
  55907. return -10;
  55908. if (!useticket) {
  55909. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
  55910. if (ret != 0)
  55911. return -11;
  55912. if (bad && round_trips != ROUNDS_WITH_HVR - 1)
  55913. return -12;
  55914. if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
  55915. return -13;
  55916. }
  55917. wolfSSL_SESSION_free(sess);
  55918. wolfSSL_free(ssl_c);
  55919. wolfSSL_free(ssl_s);
  55920. wolfSSL_CTX_free(ctx_c);
  55921. wolfSSL_CTX_free(ctx_s);
  55922. return 0;
  55923. }
  55924. static int test_wolfSSL_dtls_stateless_resume(void)
  55925. {
  55926. int ret;
  55927. #ifdef HAVE_SESSION_TICKET
  55928. ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
  55929. if (ret != 0)
  55930. return TEST_RES_CHECK(ret);
  55931. ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
  55932. if (ret != 0)
  55933. return TEST_RES_CHECK(ret - 100);
  55934. #endif /* HAVE_SESION_TICKET */
  55935. ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
  55936. if (ret != 0)
  55937. return TEST_RES_CHECK(ret - 200);
  55938. ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
  55939. if (ret != 0)
  55940. return TEST_RES_CHECK(ret - 300);
  55941. return TEST_RES_CHECK(TEST_SUCCESS);
  55942. }
  55943. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  55944. #if !defined(NO_OLD_TLS)
  55945. static int test_wolfSSL_dtls_stateless_downgrade(void)
  55946. {
  55947. WOLFSSL_CTX *ctx_c = NULL, *ctx_c2 = NULL, *ctx_s = NULL;
  55948. WOLFSSL *ssl_c = NULL, *ssl_c2 = NULL, *ssl_s = NULL;
  55949. struct test_memio_ctx test_ctx;
  55950. int ret;
  55951. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55952. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55953. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
  55954. if (ret != 0)
  55955. return -1;
  55956. ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
  55957. if (ret != WOLFSSL_SUCCESS)
  55958. return -2;
  55959. ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
  55960. if (ctx_c2 == NULL)
  55961. return -3;
  55962. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  55963. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  55964. ssl_c2 = wolfSSL_new(ctx_c2);
  55965. if (ssl_c2 == NULL)
  55966. return -4;
  55967. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55968. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55969. /* send CH */
  55970. ret = wolfSSL_connect(ssl_c2);
  55971. if (ret == 0 || ssl_c2->error != WANT_READ)
  55972. return -5;
  55973. ret = wolfSSL_accept(ssl_s);
  55974. if (ret == 0 || ssl_s->error != WANT_READ)
  55975. return -6;
  55976. if (test_ctx.c_len == 0)
  55977. return -7;
  55978. /* consume HRR */
  55979. test_ctx.c_len = 0;
  55980. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55981. if (ret != 0)
  55982. return -8;
  55983. wolfSSL_free(ssl_c2);
  55984. wolfSSL_free(ssl_c);
  55985. wolfSSL_free(ssl_s);
  55986. wolfSSL_CTX_free(ctx_c);
  55987. wolfSSL_CTX_free(ctx_c2);
  55988. wolfSSL_CTX_free(ctx_s);
  55989. return TEST_SUCCESS;
  55990. }
  55991. #endif /* !defined(NO_OLD_TLS) */
  55992. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55993. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  55994. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55995. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55996. !defined(NO_OLD_TLS)
  55997. static int test_WOLFSSL_dtls_version_alert(void)
  55998. {
  55999. struct test_memio_ctx test_ctx;
  56000. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56001. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56002. int ret;
  56003. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56004. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56005. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  56006. if (ret != 0)
  56007. return -1;
  56008. /* client hello */
  56009. ret = wolfSSL_connect(ssl_c);
  56010. if (ret == 0 || ssl_c->error != WANT_READ )
  56011. return -2;
  56012. /* hrr */
  56013. ret = wolfSSL_accept(ssl_s);
  56014. if (ret == 0 || ssl_s->error != WANT_READ )
  56015. return -3;
  56016. /* client hello 1 */
  56017. ret = wolfSSL_connect(ssl_c);
  56018. if (ret == 0 || ssl_c->error != WANT_READ )
  56019. return -4;
  56020. /* server hello */
  56021. ret = wolfSSL_accept(ssl_s);
  56022. if (ret == 0 || ssl_s->error != WANT_READ )
  56023. return -5;
  56024. /* should fail */
  56025. ret = wolfSSL_connect(ssl_c);
  56026. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  56027. return -6;
  56028. /* shuould fail */
  56029. ret = wolfSSL_accept(ssl_s);
  56030. if (ret == 0 ||
  56031. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  56032. return -7;
  56033. wolfSSL_free(ssl_c);
  56034. wolfSSL_free(ssl_s);
  56035. wolfSSL_CTX_free(ctx_c);
  56036. wolfSSL_CTX_free(ctx_s);
  56037. return TEST_RES_CHECK(1);
  56038. }
  56039. #else
  56040. static int test_WOLFSSL_dtls_version_alert(void)
  56041. {
  56042. return TEST_SKIPPED;
  56043. }
  56044. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56045. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56046. * !defined(NO_OLD_TLS)
  56047. */
  56048. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56049. && defined(WOLFSSL_TLS13) && \
  56050. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  56051. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56052. {
  56053. struct test_memio_ctx *test_ctx;
  56054. byte buf[2048];
  56055. int idx, sz;
  56056. word32 tmp;
  56057. int ret;
  56058. idx = 5; /* space for record header */
  56059. buf[idx] = session_ticket; /* type */
  56060. idx++;
  56061. tmp = OPAQUE32_LEN +
  56062. OPAQUE32_LEN +
  56063. OPAQUE8_LEN + nonceLength +
  56064. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56065. c32to24(tmp, buf + idx);
  56066. idx += OPAQUE24_LEN;
  56067. c32toa((word32)12345, buf+idx); /* lifetime */
  56068. idx += OPAQUE32_LEN;
  56069. c32toa((word32)12345, buf+idx); /* add */
  56070. idx += OPAQUE32_LEN;
  56071. buf[idx] = nonceLength; /* nonce length */
  56072. idx++;
  56073. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56074. idx += nonceLength;
  56075. tmp = 1; /* ticket len */
  56076. c16toa((word16)tmp, buf+idx);
  56077. idx += 2;
  56078. buf[idx] = 0xFF; /* ticket */
  56079. idx++;
  56080. tmp = 0; /* ext len */
  56081. c16toa((word16)tmp, buf+idx);
  56082. idx += 2;
  56083. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56084. handshake, 0, 0, 0);
  56085. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56086. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56087. return !(ret == sz);
  56088. }
  56089. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56090. {
  56091. int i;
  56092. if (sess == NULL)
  56093. return -1;
  56094. if (sess->ticketNonce.len != len)
  56095. return -1;
  56096. for (i = 0; i < len; i++)
  56097. if (sess->ticketNonce.data[i] != len)
  56098. return -1;
  56099. return 0;
  56100. }
  56101. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56102. {
  56103. char *buf[1024];
  56104. int ret;
  56105. ret = send_new_session_ticket(ssl_s, len, len);
  56106. if (ret != 0)
  56107. return -1;
  56108. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  56109. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  56110. return -1;
  56111. return test_ticket_nonce_check(ssl_c->session, len);
  56112. }
  56113. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56114. {
  56115. WOLFSSL_SESSION *sess, *cached;
  56116. WOLFSSL_CTX *ctx;
  56117. int ret;
  56118. ctx = ssl_c->ctx;
  56119. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  56120. if (ret != 0)
  56121. return -1;
  56122. sess = wolfSSL_get1_session(ssl_c);
  56123. if (sess == NULL)
  56124. return -1;
  56125. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  56126. NULL, ssl_c->options.side, 1,NULL);
  56127. if (ret != 0)
  56128. return -1;
  56129. cached = wolfSSL_SESSION_new();
  56130. if (cached == NULL)
  56131. return -1;
  56132. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  56133. if (ret != WOLFSSL_SUCCESS)
  56134. return -1;
  56135. ret = test_ticket_nonce_check(cached, len);
  56136. if (ret != 0)
  56137. return -1;
  56138. wolfSSL_SESSION_free(cached);
  56139. wolfSSL_SESSION_free(sess);
  56140. return 0;
  56141. }
  56142. static int test_ticket_nonce_malloc(void)
  56143. {
  56144. struct test_memio_ctx test_ctx;
  56145. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56146. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56147. byte small, medium, big;
  56148. int ret;
  56149. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56150. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56151. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  56152. if (ret != 0)
  56153. return -1;
  56154. /* will send ticket manually */
  56155. wolfSSL_no_ticket_TLSv13(ssl_s);
  56156. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56157. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56158. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  56159. ret = wolfSSL_connect(ssl_c);
  56160. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  56161. return -2;
  56162. ret = wolfSSL_accept(ssl_s);
  56163. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  56164. return -3;
  56165. }
  56166. small = TLS13_TICKET_NONCE_STATIC_SZ;
  56167. medium = small + 20 <= 255 ? small + 20 : 255;
  56168. big = medium + 20 <= 255 ? small + 20 : 255;
  56169. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  56170. return -1;
  56171. if (ssl_c->session->ticketNonce.data !=
  56172. ssl_c->session->ticketNonce.dataStatic)
  56173. return -1;
  56174. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  56175. return -1;
  56176. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  56177. return -1;
  56178. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  56179. return -5;
  56180. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  56181. return -6;
  56182. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  56183. return -1;
  56184. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  56185. return -1;
  56186. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  56187. return -1;
  56188. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  56189. return -1;
  56190. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  56191. return -1;
  56192. wolfSSL_free(ssl_c);
  56193. wolfSSL_free(ssl_s);
  56194. wolfSSL_CTX_free(ctx_c);
  56195. wolfSSL_CTX_free(ctx_s);
  56196. return 0;
  56197. }
  56198. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  56199. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  56200. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  56201. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56202. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  56203. defined(HAVE_ECC)
  56204. static int test_ticket_ret_create(void)
  56205. {
  56206. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56207. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56208. byte ticket[SESSION_TICKET_LEN];
  56209. struct test_memio_ctx test_ctx;
  56210. WOLFSSL_SESSION *sess = NULL;
  56211. word16 ticketLen;
  56212. int ret;
  56213. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56214. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56215. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56216. if (ret != 0)
  56217. return TEST_FAIL;
  56218. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  56219. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  56220. wolfSSL_CTX_UseSessionTicket(ctx_c);
  56221. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56222. if (ret != 0)
  56223. return TEST_FAIL;
  56224. sess = wolfSSL_get1_session(ssl_c);
  56225. if (sess->ticketLen > SESSION_TICKET_LEN)
  56226. return TEST_FAIL;
  56227. ticketLen = sess->ticketLen;
  56228. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  56229. wolfSSL_free(ssl_c);
  56230. wolfSSL_free(ssl_s);
  56231. ssl_s = wolfSSL_new(ctx_s);
  56232. if (ssl_s == NULL)
  56233. return TEST_FAIL;
  56234. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56235. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56236. ssl_c = wolfSSL_new(ctx_c);
  56237. if (ssl_c == NULL)
  56238. return TEST_FAIL;
  56239. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56240. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56241. wolfSSL_set_session(ssl_c, sess);
  56242. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  56243. if (ret != 0)
  56244. return TEST_FAIL;
  56245. if (ssl_c->session->ticketLen > SESSION_TICKET_LEN)
  56246. return TEST_FAIL;
  56247. if (ssl_c->session->ticketLen != ticketLen)
  56248. return TEST_FAIL;
  56249. if (XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) == 0)
  56250. return TEST_FAIL;
  56251. wolfSSL_SESSION_free(sess);
  56252. wolfSSL_free(ssl_c);
  56253. wolfSSL_free(ssl_s);
  56254. wolfSSL_CTX_free(ctx_c);
  56255. wolfSSL_CTX_free(ctx_s);
  56256. return TEST_SUCCESS;
  56257. }
  56258. #else
  56259. static int test_ticket_ret_create(void)
  56260. {
  56261. return TEST_SKIPPED;
  56262. }
  56263. #endif
  56264. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56265. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56266. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56267. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56268. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  56269. {
  56270. int ret;
  56271. WOLFSSL_SESSION* session = NULL;
  56272. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56273. if (!wolfSSL_is_server(ssl)) {
  56274. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56275. AssertNotNull(session);
  56276. }
  56277. do {
  56278. ret = wolfSSL_shutdown(ssl);
  56279. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56280. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56281. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56282. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56283. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56284. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56285. #endif
  56286. if (!wolfSSL_is_server(ssl)) {
  56287. /* client */
  56288. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56289. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56290. wolfSSL_set_session(ssl, session);
  56291. wolfSSL_SESSION_free(session);
  56292. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56293. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56294. }
  56295. else {
  56296. /* server */
  56297. /* Different ciphersuite so that the ticket will be invalidated based on
  56298. * the ciphersuite */
  56299. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  56300. WOLFSSL_SUCCESS);
  56301. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  56302. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56303. }
  56304. }
  56305. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  56306. {
  56307. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  56308. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56309. WOLFSSL_SUCCESS);
  56310. }
  56311. static int test_ticket_and_psk_mixing(void)
  56312. {
  56313. /* Test mixing tickets and regular PSK */
  56314. callback_functions client_cbs, server_cbs;
  56315. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56316. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56317. client_cbs.method = wolfTLSv1_3_client_method;
  56318. server_cbs.method = wolfTLSv1_3_server_method;
  56319. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  56320. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56321. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  56322. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56323. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56324. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56325. return TEST_RES_CHECK(1);
  56326. }
  56327. #else
  56328. static int test_ticket_and_psk_mixing(void)
  56329. {
  56330. return TEST_SKIPPED;
  56331. }
  56332. #endif
  56333. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  56334. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56335. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56336. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  56337. static int test_prioritize_psk_cb_called = FALSE;
  56338. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  56339. const char* identity, unsigned char* key, unsigned int key_max_len,
  56340. const char** ciphersuite)
  56341. {
  56342. test_prioritize_psk_cb_called = TRUE;
  56343. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  56344. }
  56345. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  56346. {
  56347. int ret;
  56348. WOLFSSL_SESSION* session = NULL;
  56349. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  56350. if (!wolfSSL_is_server(ssl)) {
  56351. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  56352. AssertNotNull(session);
  56353. }
  56354. do {
  56355. ret = wolfSSL_shutdown(ssl);
  56356. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  56357. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  56358. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  56359. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  56360. * important. */
  56361. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56362. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56363. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  56364. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  56365. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  56366. #endif
  56367. if (!wolfSSL_is_server(ssl)) {
  56368. /* client */
  56369. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  56370. wolfSSL_set_session(ssl, session);
  56371. wolfSSL_SESSION_free(session);
  56372. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  56373. }
  56374. else {
  56375. /* server */
  56376. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  56377. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56378. #ifdef WOLFSSL_PRIORITIZE_PSK
  56379. /* The ticket should be first tried with all ciphersuites and chosen */
  56380. AssertFalse(test_prioritize_psk_cb_called);
  56381. #else
  56382. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  56383. * callback that sets this var. */
  56384. AssertTrue(test_prioritize_psk_cb_called);
  56385. #endif
  56386. }
  56387. }
  56388. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  56389. {
  56390. if (!wolfSSL_is_server(ssl))
  56391. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56392. WOLFSSL_SUCCESS);
  56393. else
  56394. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56395. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56396. }
  56397. static int test_prioritize_psk(void)
  56398. {
  56399. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  56400. * of the PSK's will be followed instead of the ciphersuite. */
  56401. callback_functions client_cbs, server_cbs;
  56402. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56403. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56404. client_cbs.method = wolfTLSv1_3_client_method;
  56405. server_cbs.method = wolfTLSv1_3_server_method;
  56406. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56407. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56408. client_cbs.on_result = test_prioritize_psk_on_result;
  56409. server_cbs.on_result = test_prioritize_psk_on_result;
  56410. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56411. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56412. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56413. return TEST_RES_CHECK(1);
  56414. }
  56415. #else
  56416. static int test_prioritize_psk(void)
  56417. {
  56418. return TEST_SKIPPED;
  56419. }
  56420. #endif
  56421. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  56422. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56423. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56424. !defined(WOLFSSL_NO_TLS12)
  56425. static void test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  56426. {
  56427. AssertTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  56428. /* Set TLS 1.3 specific suite */
  56429. AssertTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  56430. }
  56431. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56432. {
  56433. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  56434. * do a 1.2 connection. */
  56435. callback_functions client_cbs, server_cbs;
  56436. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56437. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56438. client_cbs.method = wolfTLSv1_2_client_method;
  56439. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  56440. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  56441. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56442. AssertIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56443. AssertIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56444. return TEST_RES_CHECK(1);
  56445. }
  56446. #else
  56447. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56448. {
  56449. return TEST_SKIPPED;
  56450. }
  56451. #endif
  56452. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  56453. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56454. static void test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  56455. {
  56456. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56457. }
  56458. static void test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  56459. {
  56460. WOLFSSL_ALERT_HISTORY h;
  56461. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56462. AssertIntEQ(h.last_rx.level, alert_fatal);
  56463. AssertIntEQ(h.last_rx.code, certificate_revoked);
  56464. }
  56465. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56466. {
  56467. callback_functions client_cbs, server_cbs;
  56468. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56469. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56470. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  56471. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  56472. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  56473. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  56474. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  56475. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56476. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  56477. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  56478. return TEST_RES_CHECK(1);
  56479. }
  56480. #else
  56481. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56482. {
  56483. return TEST_SKIPPED;
  56484. }
  56485. #endif
  56486. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  56487. && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56488. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56489. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56490. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56491. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56492. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  56493. static int test_TLS_13_ticket_different_ciphers_run = 0;
  56494. static void test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  56495. {
  56496. switch (test_TLS_13_ticket_different_ciphers_run) {
  56497. case 0:
  56498. /* First run */
  56499. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56500. WOLFSSL_SUCCESS);
  56501. if (wolfSSL_is_server(ssl)) {
  56502. AssertNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  56503. wolfSSL_get_SSL_CTX(ssl));
  56504. AssertIntEQ(WOLFSSL_SUCCESS,
  56505. wolfSSL_CTX_up_ref(test_TLS_13_ticket_different_ciphers_ctx));
  56506. }
  56507. break;
  56508. case 1:
  56509. /* Second run */
  56510. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56511. "TLS13-AES128-GCM-SHA256"),
  56512. WOLFSSL_SUCCESS);
  56513. if (!wolfSSL_is_server(ssl)) {
  56514. AssertIntEQ(wolfSSL_set_session(ssl,
  56515. test_TLS_13_ticket_different_ciphers_session),
  56516. WOLFSSL_SUCCESS);
  56517. }
  56518. break;
  56519. default:
  56520. /* Bad state? */
  56521. Fail(("Should not enter here"), ("Should not enter here"));
  56522. }
  56523. }
  56524. static void test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  56525. {
  56526. switch (test_TLS_13_ticket_different_ciphers_run) {
  56527. case 0:
  56528. /* First run */
  56529. AssertNotNull(test_TLS_13_ticket_different_ciphers_session =
  56530. wolfSSL_get1_session(ssl));
  56531. break;
  56532. case 1:
  56533. /* Second run */
  56534. AssertTrue(wolfSSL_session_reused(ssl));
  56535. break;
  56536. default:
  56537. /* Bad state? */
  56538. Fail(("Should not enter here"), ("Should not enter here"));
  56539. }
  56540. }
  56541. static int test_TLS_13_ticket_different_ciphers(void)
  56542. {
  56543. /* Check that we handle the connection when the ticket doesn't match
  56544. * the first ciphersuite. */
  56545. callback_functions client_cbs, server_cbs;
  56546. struct test_params {
  56547. method_provider client_meth;
  56548. method_provider server_meth;
  56549. int doUdp;
  56550. } params[] = {
  56551. #ifdef WOLFSSL_DTLS13
  56552. /* Test that the stateless code handles sessions correctly */
  56553. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  56554. #endif
  56555. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  56556. };
  56557. size_t i;
  56558. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56559. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56560. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56561. test_TLS_13_ticket_different_ciphers_run = 0;
  56562. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  56563. client_cbs.method = params[i].client_meth;
  56564. server_cbs.method = params[i].server_meth;
  56565. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56566. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56567. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  56568. server_cbs.ticNoInit = 1;
  56569. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56570. AssertTrue(client_cbs.return_code);
  56571. AssertTrue(server_cbs.return_code);
  56572. test_TLS_13_ticket_different_ciphers_run++;
  56573. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  56574. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56575. AssertTrue(client_cbs.return_code);
  56576. AssertTrue(server_cbs.return_code);
  56577. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  56578. test_TLS_13_ticket_different_ciphers_session = NULL;
  56579. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  56580. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56581. }
  56582. return TEST_RES_CHECK(1);
  56583. }
  56584. #else
  56585. static int test_TLS_13_ticket_different_ciphers(void)
  56586. {
  56587. return TEST_SKIPPED;
  56588. }
  56589. #endif
  56590. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  56591. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56592. #define TEST_WRONG_CS_CLIENT "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"
  56593. byte test_extra_alerts_wrong_cs_sh[] = {
  56594. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  56595. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  56596. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  56597. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  56598. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  56599. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  56600. 0xbb, 0x07, 0x54, 0x1c,
  56601. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  56602. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  56603. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  56604. };
  56605. static int test_extra_alerts_wrong_cs(void)
  56606. {
  56607. struct test_memio_ctx test_ctx;
  56608. WOLFSSL_CTX *ctx_c = NULL;
  56609. WOLFSSL_ALERT_HISTORY h;
  56610. WOLFSSL *ssl_c = NULL;
  56611. int ret, err;
  56612. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56613. ret = test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  56614. wolfTLSv1_2_client_method, NULL);
  56615. if (ret != 0)
  56616. return TEST_FAIL;
  56617. ret = wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT);
  56618. if (ret != WOLFSSL_SUCCESS) {
  56619. wolfSSL_free(ssl_c);
  56620. wolfSSL_CTX_free(ctx_c);
  56621. return TEST_SKIPPED;
  56622. }
  56623. /* CH */
  56624. ret = wolfSSL_connect(ssl_c);
  56625. err = wolfSSL_get_error(ssl_c, ret);
  56626. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56627. return TEST_FAIL;
  56628. /* consume CH */
  56629. test_ctx.s_len = 0;
  56630. /* inject SH */
  56631. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  56632. sizeof(test_extra_alerts_wrong_cs_sh));
  56633. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  56634. ret = wolfSSL_connect(ssl_c);
  56635. err = wolfSSL_get_error(ssl_c, ret);
  56636. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  56637. return TEST_FAIL;
  56638. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56639. if (ret != WOLFSSL_SUCCESS)
  56640. return TEST_FAIL;
  56641. if (h.last_tx.code != illegal_parameter)
  56642. return TEST_FAIL;
  56643. if (h.last_tx.level != alert_fatal)
  56644. return TEST_FAIL;
  56645. wolfSSL_free(ssl_c);
  56646. wolfSSL_CTX_free(ctx_c);
  56647. return TEST_SUCCESS;
  56648. }
  56649. #else
  56650. static int test_extra_alerts_wrong_cs(void)
  56651. {
  56652. return TEST_SKIPPED;
  56653. }
  56654. #endif
  56655. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  56656. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  56657. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  56658. {
  56659. tail_len -= msg_length;
  56660. XMEMMOVE(msg, msg + msg_length, tail_len);
  56661. *len = *len - msg_length;
  56662. }
  56663. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  56664. byte *found)
  56665. {
  56666. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  56667. const unsigned int _RECORD_HEADER_SZ = 5;
  56668. const int _change_cipher_hs = 55;
  56669. const int _change_cipher = 20;
  56670. const int _handshake = 22;
  56671. unsigned int tail_len;
  56672. byte *idx, *curr;
  56673. word8 currType;
  56674. word16 rLength;
  56675. word32 hLength;
  56676. idx = buf;
  56677. tail_len = *len;
  56678. *found = 0;
  56679. while (tail_len > _RECORD_HEADER_SZ) {
  56680. curr = idx;
  56681. currType = *idx;
  56682. ato16(idx + 3, &rLength);
  56683. idx += _RECORD_HEADER_SZ;
  56684. tail_len -= _RECORD_HEADER_SZ;
  56685. if (tail_len < rLength)
  56686. return -1;
  56687. if (type == _change_cipher_hs && currType == _change_cipher) {
  56688. if (rLength != 1)
  56689. return -1;
  56690. /* match */
  56691. test_remove_msg(curr, *len - (int)(curr - buf),
  56692. len, _RECORD_HEADER_SZ + 1);
  56693. *found = 1;
  56694. return 0;
  56695. }
  56696. if (currType != _handshake) {
  56697. idx += rLength;
  56698. tail_len -= rLength;
  56699. continue;
  56700. }
  56701. if (rLength < _HANDSHAKE_HEADER_SZ)
  56702. return -1;
  56703. currType = *idx;
  56704. ato24(idx+1, &hLength);
  56705. hLength += _HANDSHAKE_HEADER_SZ;
  56706. if (tail_len < hLength)
  56707. return -1;
  56708. if (currType != type) {
  56709. idx += hLength;
  56710. tail_len -= hLength;
  56711. continue;
  56712. }
  56713. /* match */
  56714. test_remove_msg(curr, *len - (int)(curr - buf), len,
  56715. hLength + _RECORD_HEADER_SZ);
  56716. *found = 1;
  56717. return 0;
  56718. }
  56719. /* not found */
  56720. return 0;
  56721. }
  56722. static int test_remove_hs_message(byte hs_message_type,
  56723. int extra_round, byte alert_type)
  56724. {
  56725. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56726. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56727. struct test_memio_ctx test_ctx;
  56728. WOLFSSL_ALERT_HISTORY h;
  56729. int ret, err;
  56730. byte found;
  56731. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56732. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56733. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56734. AssertIntEQ(ret, 0);
  56735. ret = wolfSSL_connect(ssl_c);
  56736. err = wolfSSL_get_error(ssl_c, ret);
  56737. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56738. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56739. ret = wolfSSL_accept(ssl_s);
  56740. err = wolfSSL_get_error(ssl_s, ret);
  56741. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56742. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56743. if (extra_round) {
  56744. ret = wolfSSL_connect(ssl_c);
  56745. err = wolfSSL_get_error(ssl_c, ret);
  56746. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56747. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56748. /* this will complete handshake from server side */
  56749. ret = wolfSSL_accept(ssl_s);
  56750. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56751. }
  56752. ret = test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  56753. &test_ctx.c_len, hs_message_type, &found);
  56754. AssertIntEQ(ret, 0);
  56755. if (!found) {
  56756. wolfSSL_free(ssl_c);
  56757. wolfSSL_CTX_free(ctx_c);
  56758. wolfSSL_free(ssl_s);
  56759. wolfSSL_CTX_free(ctx_s);
  56760. return TEST_SKIPPED;
  56761. }
  56762. ret = wolfSSL_connect(ssl_c);
  56763. err = wolfSSL_get_error(ssl_c, ret);
  56764. AssertIntNE(ret, WOLFSSL_SUCCESS);
  56765. AssertIntNE(err, WOLFSSL_ERROR_WANT_READ);
  56766. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56767. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56768. if (alert_type != 0xff && h.last_tx.code != alert_type)
  56769. return TEST_FAIL;
  56770. if (h.last_tx.level != alert_fatal)
  56771. return TEST_FAIL;
  56772. wolfSSL_free(ssl_c);
  56773. wolfSSL_CTX_free(ctx_c);
  56774. wolfSSL_free(ssl_s);
  56775. wolfSSL_CTX_free(ctx_s);
  56776. return TEST_SUCCESS;
  56777. }
  56778. static int test_extra_alerts_skip_hs(void)
  56779. {
  56780. const byte _server_key_exchange = 12;
  56781. const byte _server_hello = 2;
  56782. const byte _certificate = 11;
  56783. int ret;
  56784. /* server_hello */
  56785. ret = test_remove_hs_message(_server_hello, 0,
  56786. unexpected_message);
  56787. AssertIntNE(ret, TEST_FAIL);
  56788. ret = test_remove_hs_message(_certificate, 0,
  56789. 0xff);
  56790. AssertIntNE(ret, TEST_FAIL);
  56791. ret = test_remove_hs_message(_server_key_exchange, 0,
  56792. unexpected_message);
  56793. AssertIntNE(ret, TEST_FAIL);
  56794. return TEST_SUCCESS;
  56795. }
  56796. #else
  56797. static int test_extra_alerts_skip_hs(void)
  56798. {
  56799. return TEST_SKIPPED;
  56800. }
  56801. #endif
  56802. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  56803. defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  56804. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  56805. unsigned char* key, unsigned int key_max_len)
  56806. {
  56807. (void)ssl;
  56808. (void)id;
  56809. (void)key_max_len;
  56810. /* zero means error */
  56811. key[0] = 0x10;
  56812. return 1;
  56813. }
  56814. static int test_extra_alerts_bad_psk(void)
  56815. {
  56816. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56817. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56818. struct test_memio_ctx test_ctx;
  56819. WOLFSSL_ALERT_HISTORY h;
  56820. int ret, err;
  56821. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56822. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56823. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method);
  56824. if (ret != 0)
  56825. return TEST_FAIL;
  56826. ret = wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256");
  56827. if (ret != WOLFSSL_SUCCESS)
  56828. return TEST_FAIL;
  56829. ret = wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256");
  56830. if (ret != WOLFSSL_SUCCESS)
  56831. return TEST_FAIL;
  56832. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  56833. ret = wolfSSL_connect(ssl_c);
  56834. err = wolfSSL_get_error(ssl_c, ret);
  56835. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56836. return TEST_FAIL;
  56837. ret = wolfSSL_accept(ssl_s);
  56838. err = wolfSSL_get_error(ssl_s, ret);
  56839. if (ret == WOLFSSL_SUCCESS || err != WOLFSSL_ERROR_WANT_READ)
  56840. return TEST_FAIL;
  56841. ret = wolfSSL_connect(ssl_c);
  56842. err = wolfSSL_get_error(ssl_c, ret);
  56843. if (ret == WOLFSSL_SUCCESS || err == WOLFSSL_ERROR_WANT_READ)
  56844. return TEST_FAIL;
  56845. ret = wolfSSL_get_alert_history(ssl_c, &h);
  56846. if (ret != WOLFSSL_SUCCESS)
  56847. return TEST_FAIL;
  56848. if (h.last_tx.code != handshake_failure)
  56849. return TEST_FAIL;
  56850. if (h.last_tx.level != alert_fatal)
  56851. return TEST_FAIL;
  56852. wolfSSL_free(ssl_c);
  56853. wolfSSL_CTX_free(ctx_c);
  56854. wolfSSL_free(ssl_s);
  56855. wolfSSL_CTX_free(ctx_s);
  56856. return TEST_SUCCESS;
  56857. }
  56858. #else
  56859. static int test_extra_alerts_bad_psk(void)
  56860. {
  56861. return TEST_SKIPPED;
  56862. }
  56863. #endif
  56864. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  56865. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56866. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  56867. int sz, void *ctx)
  56868. {
  56869. static int sentServerHello = FALSE;
  56870. if (!sentServerHello) {
  56871. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  56872. size_t i;
  56873. if (sz < (int)sizeof(renegExt))
  56874. return WOLFSSL_CBIO_ERR_GENERAL;
  56875. /* Remove SCR from ServerHello */
  56876. for (i = 0; i < sz - sizeof(renegExt); i++) {
  56877. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  56878. /* Found the extension. Change it to something unrecognized. */
  56879. buf[i+1] = 0x11;
  56880. break;
  56881. }
  56882. }
  56883. sentServerHello = TRUE;
  56884. }
  56885. return EmbedSend(ssl, buf, sz, ctx);
  56886. }
  56887. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  56888. {
  56889. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  56890. }
  56891. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  56892. {
  56893. WOLFSSL_ALERT_HISTORY h;
  56894. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56895. AssertIntEQ(h.last_rx.code, handshake_failure);
  56896. AssertIntEQ(h.last_rx.level, alert_fatal);
  56897. }
  56898. static int test_harden_no_secure_renegotiation(void)
  56899. {
  56900. callback_functions client_cbs, server_cbs;
  56901. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56902. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56903. client_cbs.method = wolfTLSv1_2_client_method;
  56904. server_cbs.method = wolfTLSv1_2_server_method;
  56905. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  56906. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  56907. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56908. AssertIntEQ(client_cbs.return_code, TEST_FAIL);
  56909. AssertIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  56910. AssertIntEQ(server_cbs.return_code, TEST_FAIL);
  56911. AssertIntEQ(server_cbs.last_err, SOCKET_ERROR_E);
  56912. return TEST_RES_CHECK(1);
  56913. }
  56914. #else
  56915. static int test_harden_no_secure_renegotiation(void)
  56916. {
  56917. return TEST_SKIPPED;
  56918. }
  56919. #endif
  56920. #if defined(HAVE_OCSP) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56921. static int test_override_alt_cert_chain_cert_cb(int preverify,
  56922. WOLFSSL_X509_STORE_CTX* store)
  56923. {
  56924. printf("preverify: %d\n", preverify);
  56925. printf("store->error: %d\n", store->error);
  56926. printf("error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  56927. if (store->error == OCSP_INVALID_STATUS) {
  56928. printf("Overriding OCSP error\n");
  56929. return 1;
  56930. }
  56931. #ifndef WOLFSSL_ALT_CERT_CHAINS
  56932. else if ((store->error == ASN_NO_SIGNER_E ||
  56933. store->error == ASN_SELF_SIGNED_E
  56934. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  56935. defined(HAVE_WEBSERVER)
  56936. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  56937. #endif
  56938. ) && store->error_depth == store->totalCerts - 1) {
  56939. printf("Overriding no signer error only for root cert\n");
  56940. return 1;
  56941. }
  56942. #endif
  56943. else
  56944. return preverify;
  56945. }
  56946. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  56947. int urlSz, unsigned char* request, int requestSz,
  56948. unsigned char** response)
  56949. {
  56950. (void)ioCtx;
  56951. (void)url;
  56952. (void)urlSz;
  56953. (void)request;
  56954. (void)requestSz;
  56955. (void)response;
  56956. return -1;
  56957. }
  56958. static void test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  56959. {
  56960. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  56961. test_override_alt_cert_chain_cert_cb);
  56962. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56963. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56964. AssertIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56965. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56966. AssertIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56967. WOLFSSL_SUCCESS);
  56968. }
  56969. static void test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  56970. {
  56971. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56972. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56973. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56974. AssertIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56975. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56976. AssertIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56977. WOLFSSL_SUCCESS);
  56978. }
  56979. static void test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  56980. {
  56981. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  56982. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  56983. }
  56984. static int test_override_alt_cert_chain(void)
  56985. {
  56986. size_t i;
  56987. struct test_params {
  56988. ctx_callback client_ctx_cb;
  56989. ctx_callback server_ctx_cb;
  56990. int result;
  56991. } params[] = {
  56992. {test_override_alt_cert_chain_client_ctx_ready,
  56993. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  56994. {test_override_alt_cert_chain_client_ctx_ready2,
  56995. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  56996. };
  56997. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56998. callback_functions client_cbs, server_cbs;
  56999. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57000. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57001. printf("test config: %d\n", (int)i);
  57002. client_cbs.ctx_ready = params[i].client_ctx_cb;
  57003. server_cbs.ctx_ready = params[i].server_ctx_cb;
  57004. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57005. AssertIntEQ(client_cbs.return_code, params[i].result);
  57006. AssertIntEQ(server_cbs.return_code, params[i].result);
  57007. }
  57008. return TEST_RES_CHECK(1);
  57009. }
  57010. #else
  57011. static int test_override_alt_cert_chain(void)
  57012. {
  57013. return TEST_SKIPPED;
  57014. }
  57015. #endif
  57016. /*----------------------------------------------------------------------------*
  57017. | Main
  57018. *----------------------------------------------------------------------------*/
  57019. typedef int (*TEST_FUNC)(void);
  57020. typedef struct {
  57021. const char *name;
  57022. TEST_FUNC func;
  57023. byte run:1;
  57024. } TEST_CASE;
  57025. #define TEST_DECL(func) { #func, func, 0 }
  57026. int testAll = 1;
  57027. TEST_CASE testCases[] = {
  57028. TEST_DECL(test_fileAccess),
  57029. TEST_DECL(test_wolfSSL_Init),
  57030. TEST_DECL(test_wolfSSL_Method_Allocators),
  57031. #ifndef NO_WOLFSSL_SERVER
  57032. TEST_DECL(test_wolfSSL_CTX_new),
  57033. #endif
  57034. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  57035. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  57036. TEST_DECL(test_for_double_Free),
  57037. #endif
  57038. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57039. TEST_DECL(test_wolfSSL_get_finished),
  57040. TEST_DECL(test_wolfSSL_CTX_add_session),
  57041. TEST_DECL(test_wolfSSL_CTX_add_session_ext),
  57042. #endif
  57043. TEST_DECL(test_SSL_CIPHER_get_xxx),
  57044. TEST_DECL(test_wolfSSL_ERR_strings),
  57045. TEST_DECL(test_wolfSSL_EVP_shake128),
  57046. TEST_DECL(test_wolfSSL_EVP_shake256),
  57047. TEST_DECL(test_EVP_blake2),
  57048. TEST_DECL(test_EVP_MD_do_all),
  57049. TEST_DECL(test_OBJ_NAME_do_all),
  57050. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  57051. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  57052. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  57053. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  57054. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  57055. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  57056. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  57057. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  57058. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  57059. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  57060. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  57061. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  57062. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  57063. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  57064. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  57065. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  57066. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  57067. TEST_DECL(test_wolfSSL_FPKI),
  57068. TEST_DECL(test_wolfSSL_OtherName),
  57069. TEST_DECL(test_wolfSSL_CertRsaPss),
  57070. TEST_DECL(test_wolfSSL_CertManagerCRL),
  57071. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  57072. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  57073. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  57074. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  57075. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  57076. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  57077. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  57078. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  57079. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  57080. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  57081. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  57082. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  57083. TEST_DECL(test_server_wolfSSL_new),
  57084. TEST_DECL(test_client_wolfSSL_new),
  57085. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  57086. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  57087. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  57088. TEST_DECL(test_SetTmpEC_DHE_Sz),
  57089. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  57090. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  57091. TEST_DECL(test_wolfSSL_dtls_plaintext),
  57092. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57093. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57094. TEST_DECL(test_wolfSSL_read_write),
  57095. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  57096. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  57097. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  57098. TEST_DECL(test_wolfSSL_dtls_export),
  57099. TEST_DECL(test_wolfSSL_tls_export),
  57100. #endif
  57101. TEST_DECL(test_wolfSSL_SetMinVersion),
  57102. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  57103. /* TLS extensions tests */
  57104. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57105. #ifdef HAVE_SNI
  57106. TEST_DECL(test_wolfSSL_UseSNI_params),
  57107. TEST_DECL(test_wolfSSL_UseSNI_connection),
  57108. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  57109. #endif /* HAVE_SNI */
  57110. #endif
  57111. TEST_DECL(test_wolfSSL_UseTrustedCA),
  57112. TEST_DECL(test_wolfSSL_UseMaxFragment),
  57113. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  57114. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  57115. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57116. TEST_DECL(test_wolfSSL_UseALPN_connection),
  57117. TEST_DECL(test_wolfSSL_UseALPN_params),
  57118. #endif
  57119. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  57120. TEST_DECL(test_wolfSSL_set_alpn_protos),
  57121. #endif
  57122. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  57123. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  57124. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  57125. TEST_DECL(test_tls_ext_duplicate),
  57126. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  57127. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  57128. TEST_DECL(test_wolfSSL_Tls13_ECH),
  57129. #endif
  57130. /* X509 tests */
  57131. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  57132. TEST_DECL(test_wolfSSL_PKCS12),
  57133. TEST_DECL(test_wolfSSL_no_password_cb),
  57134. TEST_DECL(test_wolfSSL_PKCS8),
  57135. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  57136. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  57137. TEST_DECL(test_wolfSSL_PKCS5),
  57138. TEST_DECL(test_wolfSSL_URI),
  57139. TEST_DECL(test_wolfSSL_TBS),
  57140. TEST_DECL(test_wolfSSL_X509_verify),
  57141. TEST_DECL(test_wolfSSL_X509_TLS_version),
  57142. TEST_DECL(test_wc_PemToDer),
  57143. TEST_DECL(test_wc_AllocDer),
  57144. TEST_DECL(test_wc_CertPemToDer),
  57145. TEST_DECL(test_wc_KeyPemToDer),
  57146. TEST_DECL(test_wc_PubKeyPemToDer),
  57147. TEST_DECL(test_wc_PemPubKeyToDer),
  57148. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  57149. TEST_DECL(test_wc_CheckCertSigPubKey),
  57150. /* OCSP Stapling */
  57151. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  57152. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  57153. /* Multicast */
  57154. TEST_DECL(test_wolfSSL_mcast),
  57155. /* ASN.1 compatibility API tests */
  57156. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  57157. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  57158. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  57159. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  57160. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  57161. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  57162. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  57163. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  57164. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  57165. TEST_DECL(test_wolfSSL_ASN1_get_object),
  57166. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  57167. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  57168. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  57169. TEST_DECL(test_wolfSSL_ASN1_STRING),
  57170. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  57171. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  57172. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  57173. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  57174. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  57175. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  57176. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  57177. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  57178. TEST_DECL(test_wolfSSL_ASN1_TIME),
  57179. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  57180. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  57181. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  57182. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  57183. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  57184. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  57185. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  57186. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  57187. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  57188. /* compatibility tests */
  57189. TEST_DECL(test_wolfSSL_lhash),
  57190. TEST_DECL(test_wolfSSL_X509_NAME),
  57191. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  57192. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  57193. #ifndef NO_BIO
  57194. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  57195. TEST_DECL(test_wolfSSL_X509_INFO),
  57196. #endif
  57197. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  57198. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  57199. TEST_DECL(test_wolfSSL_X509_check_host),
  57200. TEST_DECL(test_wolfSSL_X509_check_email),
  57201. TEST_DECL(test_wolfSSL_DES),
  57202. TEST_DECL(test_wolfSSL_certs),
  57203. TEST_DECL(test_wolfSSL_X509_check_private_key),
  57204. TEST_DECL(test_wolfSSL_private_keys),
  57205. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  57206. #ifndef NO_BIO
  57207. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  57208. #endif
  57209. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  57210. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  57211. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  57212. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  57213. #ifndef NO_BIO
  57214. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  57215. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  57216. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  57217. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  57218. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  57219. #endif
  57220. TEST_DECL(test_DSA_do_sign_verify),
  57221. TEST_DECL(test_wolfSSL_tmp_dh),
  57222. TEST_DECL(test_wolfSSL_ctrl),
  57223. TEST_DECL(test_wolfSSL_EVP_MD_size),
  57224. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  57225. TEST_DECL(test_wolfSSL_EVP_Digest),
  57226. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  57227. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  57228. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  57229. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  57230. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  57231. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  57232. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  57233. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  57234. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  57235. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  57236. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  57237. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  57238. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  57239. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  57240. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  57241. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  57242. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57243. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  57244. #endif
  57245. #ifndef NO_BIO
  57246. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  57247. TEST_DECL(test_wolfSSL_GetLoggingCb),
  57248. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  57249. TEST_DECL(test_wc_ERR_remove_state),
  57250. TEST_DECL(test_wc_ERR_print_errors_fp),
  57251. #endif
  57252. TEST_DECL(test_wolfSSL_set_options),
  57253. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  57254. TEST_DECL(test_wolfSSL_set1_curves_list),
  57255. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  57256. TEST_DECL(test_wolfSSL_PKCS7_certs),
  57257. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  57258. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  57259. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  57260. TEST_DECL(test_wolfSSL_msgCb),
  57261. TEST_DECL(test_wolfSSL_either_side),
  57262. TEST_DECL(test_wolfSSL_DTLS_either_side),
  57263. TEST_DECL(test_wolfSSL_dtls_fragments),
  57264. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  57265. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  57266. TEST_DECL(test_wolfSSL_dtls_bad_record),
  57267. TEST_DECL(test_wolfSSL_dtls_stateless),
  57268. TEST_DECL(test_generate_cookie),
  57269. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  57270. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  57271. TEST_DECL(test_wolfSSL_X509_Name_canon),
  57272. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  57273. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  57274. TEST_DECL(test_wolfSSL_X509_NID),
  57275. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  57276. TEST_DECL(test_wolfSSL_get0_param),
  57277. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  57278. TEST_DECL(test_wolfSSL_set1_host),
  57279. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  57280. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  57281. TEST_DECL(test_wolfSSL_X509_STORE),
  57282. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  57283. TEST_DECL(test_X509_STORE_get0_objects),
  57284. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  57285. /* RAND compatability API */
  57286. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  57287. TEST_DECL(test_wolfSSL_RAND_bytes),
  57288. /* BN compatability API */
  57289. TEST_DECL(test_wolfSSL_BN_CTX),
  57290. TEST_DECL(test_wolfSSL_BN),
  57291. TEST_DECL(test_wolfSSL_BN_init),
  57292. TEST_DECL(test_wolfSSL_BN_enc_dec),
  57293. TEST_DECL(test_wolfSSL_BN_word),
  57294. TEST_DECL(test_wolfSSL_BN_bits),
  57295. TEST_DECL(test_wolfSSL_BN_shift),
  57296. TEST_DECL(test_wolfSSL_BN_math),
  57297. TEST_DECL(test_wolfSSL_BN_math_mod),
  57298. TEST_DECL(test_wolfSSL_BN_math_other),
  57299. TEST_DECL(test_wolfSSL_BN_rand),
  57300. TEST_DECL(test_wolfSSL_BN_prime),
  57301. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  57302. #ifndef NO_BIO
  57303. TEST_DECL(test_wolfSSL_PEM_read_bio),
  57304. TEST_DECL(test_wolfSSL_BIO),
  57305. #endif
  57306. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  57307. TEST_DECL(test_wolfSSL_X509),
  57308. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  57309. TEST_DECL(test_wolfSSL_X509_sign),
  57310. TEST_DECL(test_wolfSSL_X509_sign2),
  57311. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  57312. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  57313. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57314. TEST_DECL(test_wolfSSL_check_domain),
  57315. #endif
  57316. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  57317. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  57318. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  57319. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  57320. TEST_DECL(test_wolfSSL_RAND),
  57321. TEST_DECL(test_wolfSSL_BUF),
  57322. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  57323. TEST_DECL(test_wolfSSL_X509_cmp_time),
  57324. TEST_DECL(test_wolfSSL_X509_time_adj),
  57325. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  57326. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  57327. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  57328. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  57329. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  57330. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  57331. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  57332. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  57333. TEST_DECL(test_wolfSSL_Tls13_postauth),
  57334. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  57335. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  57336. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  57337. TEST_DECL(test_wolfSSL_THREADID_hash),
  57338. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  57339. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  57340. TEST_DECL(test_error_queue_per_thread),
  57341. TEST_DECL(test_wolfSSL_ERR_put_error),
  57342. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  57343. #ifndef NO_BIO
  57344. TEST_DECL(test_wolfSSL_ERR_print_errors),
  57345. #endif
  57346. TEST_DECL(test_wolfSSL_HMAC),
  57347. TEST_DECL(test_wolfSSL_CMAC),
  57348. TEST_DECL(test_wolfSSL_OBJ),
  57349. TEST_DECL(test_wolfSSL_OBJ_cmp),
  57350. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  57351. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  57352. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  57353. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  57354. TEST_DECL(test_GENERAL_NAME_set0_othername),
  57355. TEST_DECL(test_othername_and_SID_ext),
  57356. TEST_DECL(test_wolfSSL_X509_set_name),
  57357. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  57358. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  57359. TEST_DECL(test_wolfSSL_X509_set_version),
  57360. #ifndef NO_BIO
  57361. TEST_DECL(test_wolfSSL_BIO_gets),
  57362. TEST_DECL(test_wolfSSL_BIO_puts),
  57363. TEST_DECL(test_wolfSSL_BIO_dump),
  57364. TEST_DECL(test_wolfSSL_BIO_should_retry),
  57365. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  57366. TEST_DECL(test_wolfSSL_BIO_write),
  57367. TEST_DECL(test_wolfSSL_BIO_connect),
  57368. TEST_DECL(test_wolfSSL_BIO_accept),
  57369. TEST_DECL(test_wolfSSL_BIO_printf),
  57370. TEST_DECL(test_wolfSSL_BIO_f_md),
  57371. TEST_DECL(test_wolfSSL_BIO_up_ref),
  57372. TEST_DECL(test_wolfSSL_BIO_tls),
  57373. TEST_DECL(test_wolfSSL_BIO_reset),
  57374. #endif
  57375. TEST_DECL(test_wolfSSL_cert_cb),
  57376. TEST_DECL(test_wolfSSL_SESSION),
  57377. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  57378. TEST_DECL(test_wolfSSL_ticket_keys),
  57379. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  57380. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  57381. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  57382. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  57383. TEST_DECL(test_wolfSSL_MD4),
  57384. TEST_DECL(test_wolfSSL_verify_mode),
  57385. TEST_DECL(test_wolfSSL_verify_depth),
  57386. TEST_DECL(test_wolfSSL_HMAC_CTX),
  57387. TEST_DECL(test_wolfSSL_msg_callback),
  57388. TEST_DECL(test_wolfSSL_SHA),
  57389. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  57390. TEST_DECL(test_wolfSSL_MD5),
  57391. TEST_DECL(test_wolfSSL_MD5_Transform),
  57392. TEST_DECL(test_wolfSSL_SHA_Transform),
  57393. TEST_DECL(test_wolfSSL_SHA256),
  57394. TEST_DECL(test_wolfSSL_SHA256_Transform),
  57395. TEST_DECL(test_wolfSSL_SHA224),
  57396. TEST_DECL(test_wolfSSL_SHA512_Transform),
  57397. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  57398. TEST_DECL(test_wolfSSL_X509_CRL),
  57399. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  57400. TEST_DECL(test_wolfSSL_PEM_read_X509),
  57401. TEST_DECL(test_wolfSSL_PEM_read),
  57402. #ifndef NO_BIO
  57403. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  57404. #endif
  57405. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  57406. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  57407. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  57408. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  57409. TEST_DECL(test_wolfSSL_X509_check_ca),
  57410. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  57411. TEST_DECL(test_wolfSSL_make_cert),
  57412. TEST_DECL(test_wolfSSL_DES_ncbc),
  57413. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  57414. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  57415. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  57416. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  57417. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  57418. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  57419. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  57420. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  57421. TEST_DECL(test_wolfssl_EVP_chacha20),
  57422. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  57423. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  57424. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  57425. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  57426. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  57427. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  57428. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  57429. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  57430. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  57431. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  57432. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  57433. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  57434. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  57435. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  57436. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  57437. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  57438. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  57439. TEST_DECL(test_CONF_modules_xxx),
  57440. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  57441. TEST_DECL(test_CRYPTO_THREADID_xxx),
  57442. TEST_DECL(test_ENGINE_cleanup),
  57443. #ifdef OPENSSL_ALL
  57444. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  57445. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  57446. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  57447. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  57448. TEST_DECL(test_wolfSSL_DSA_SIG),
  57449. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  57450. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  57451. TEST_DECL(test_wolfSSL_CTX_ctrl),
  57452. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  57453. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  57454. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  57455. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  57456. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  57457. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  57458. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  57459. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  57460. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  57461. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  57462. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  57463. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  57464. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  57465. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  57466. TEST_DECL(test_wolfSSL_EVP_rc4),
  57467. TEST_DECL(test_wolfSSL_EVP_enc_null),
  57468. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  57469. TEST_DECL(test_wolfSSL_EVP_mdc2),
  57470. TEST_DECL(test_wolfSSL_EVP_md4),
  57471. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  57472. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  57473. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  57474. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  57475. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  57476. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  57477. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  57478. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  57479. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  57480. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  57481. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  57482. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  57483. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  57484. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  57485. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  57486. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  57487. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  57488. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  57489. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  57490. TEST_DECL(test_evp_cipher_aes_gcm),
  57491. TEST_DECL(test_wolfSSL_OBJ_ln),
  57492. TEST_DECL(test_wolfSSL_OBJ_sn),
  57493. TEST_DECL(test_wolfSSL_TXT_DB),
  57494. TEST_DECL(test_wolfSSL_NCONF),
  57495. #endif /* OPENSSL_ALL */
  57496. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  57497. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  57498. #ifndef NO_BIO
  57499. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  57500. #endif /* !NO_BIO */
  57501. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  57502. TEST_DECL(test_wolfSSL_X509_CA_num),
  57503. TEST_DECL(test_wolfSSL_X509_get_version),
  57504. #ifndef NO_BIO
  57505. TEST_DECL(test_wolfSSL_X509_print),
  57506. TEST_DECL(test_wolfSSL_X509_CRL_print),
  57507. TEST_DECL(test_wolfSSL_BIO_get_len),
  57508. #endif
  57509. TEST_DECL(test_wolfSSL_RSA),
  57510. TEST_DECL(test_wolfSSL_RSA_DER),
  57511. TEST_DECL(test_wolfSSL_RSA_print),
  57512. #ifndef NO_RSA
  57513. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  57514. #endif
  57515. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  57516. TEST_DECL(test_wolfSSL_RSA_get0_key),
  57517. TEST_DECL(test_wolfSSL_RSA_meth),
  57518. TEST_DECL(test_wolfSSL_RSA_verify),
  57519. TEST_DECL(test_wolfSSL_RSA_sign),
  57520. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  57521. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  57522. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  57523. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  57524. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  57525. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  57526. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  57527. TEST_DECL(test_wolfSSL_RSA_ex_data),
  57528. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  57529. TEST_DECL(test_wolfSSL_RSA_To_Der),
  57530. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  57531. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  57532. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  57533. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  57534. TEST_DECL(test_wolfSSL_DH),
  57535. TEST_DECL(test_wolfSSL_DH_dup),
  57536. TEST_DECL(test_wolfSSL_DH_check),
  57537. TEST_DECL(test_wolfSSL_DH_prime),
  57538. TEST_DECL(test_wolfSSL_DH_1536_prime),
  57539. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  57540. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  57541. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  57542. TEST_DECL(test_wolfSSL_d2i_DHparams),
  57543. TEST_DECL(test_wolfSSL_DH_LoadDer),
  57544. TEST_DECL(test_wolfSSL_i2d_DHparams),
  57545. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  57546. TEST_DECL(test_wolfSSL_EC_GROUP),
  57547. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  57548. TEST_DECL(test_wolfSSL_EC_POINT),
  57549. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  57550. TEST_DECL(test_EC_i2d),
  57551. TEST_DECL(test_wolfSSL_EC_curve),
  57552. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  57553. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  57554. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  57555. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  57556. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  57557. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  57558. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  57559. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  57560. TEST_DECL(test_ECDSA_size_sign),
  57561. TEST_DECL(test_ECDH_compute_key),
  57562. #endif
  57563. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  57564. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  57565. TEST_DECL(test_wolfSSL_X509V3_EXT),
  57566. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  57567. TEST_DECL(test_wolfSSL_X509_get_ext),
  57568. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  57569. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  57570. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  57571. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  57572. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  57573. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  57574. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  57575. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  57576. TEST_DECL(test_wolfSSL_X509_cmp),
  57577. TEST_DECL(test_openssl_generate_key_and_cert),
  57578. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  57579. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  57580. /* test the no op functions for compatibility */
  57581. TEST_DECL(test_no_op_functions),
  57582. /* OpenSSL EVP_PKEY API tests */
  57583. TEST_DECL(test_EVP_PKEY_rsa),
  57584. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  57585. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  57586. TEST_DECL(test_EVP_PKEY_ec),
  57587. TEST_DECL(test_EVP_PKEY_cmp),
  57588. /* OpenSSL error API tests */
  57589. TEST_DECL(test_ERR_load_crypto_strings),
  57590. /* OpenSSL sk_X509 API test */
  57591. TEST_DECL(test_sk_X509),
  57592. /* OpenSSL sk_X509_CRL API test */
  57593. TEST_DECL(test_sk_X509_CRL),
  57594. /* OpenSSL X509 API test */
  57595. TEST_DECL(test_X509_get_signature_nid),
  57596. /* OpenSSL X509 REQ API test */
  57597. TEST_DECL(test_X509_REQ),
  57598. /* OpenSSL PKCS7 API test */
  57599. TEST_DECL(test_wolfssl_PKCS7),
  57600. TEST_DECL(test_wolfSSL_PKCS7_sign),
  57601. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  57602. #ifndef NO_BIO
  57603. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  57604. #ifdef HAVE_SMIME
  57605. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  57606. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  57607. #endif /* HAVE_SMIME */
  57608. #endif /* !NO_BIO */
  57609. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  57610. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  57611. TEST_DECL(test_X509_LOOKUP_add_dir),
  57612. /* wolfCrypt ASN tests */
  57613. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  57614. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  57615. TEST_DECL(test_wc_SetSubjectRaw),
  57616. TEST_DECL(test_wc_GetSubjectRaw),
  57617. TEST_DECL(test_wc_SetIssuerRaw),
  57618. TEST_DECL(test_wc_SetIssueBuffer),
  57619. TEST_DECL(test_wc_SetSubjectKeyId),
  57620. TEST_DECL(test_wc_SetSubject),
  57621. TEST_DECL(test_CheckCertSignature),
  57622. TEST_DECL(test_wc_ParseCert),
  57623. TEST_DECL(test_wc_ParseCert_Error),
  57624. TEST_DECL(test_MakeCertWithPathLen),
  57625. /* wolfCrypt ECC tests */
  57626. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  57627. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  57628. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  57629. #ifdef WOLFSSL_TLS13
  57630. /* TLS v1.3 API tests */
  57631. TEST_DECL(test_tls13_apis),
  57632. TEST_DECL(test_tls13_cipher_suites),
  57633. #endif
  57634. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57635. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  57636. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  57637. /* Bad certificate signature tests */
  57638. TEST_DECL(test_EccSigFailure_cm),
  57639. TEST_DECL(test_RsaSigFailure_cm),
  57640. #endif /* NO_CERTS */
  57641. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  57642. !defined(NO_OLD_TLS))
  57643. TEST_DECL(test_DhCallbacks),
  57644. #endif
  57645. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57646. TEST_DECL(test_export_keying_material),
  57647. #endif
  57648. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  57649. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57650. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57651. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57652. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57653. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  57654. #endif
  57655. TEST_DECL(test_wolfSSL_security_level),
  57656. TEST_DECL(test_wolfSSL_SSL_in_init),
  57657. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  57658. TEST_DECL(test_wolfSSL_OpenSSL_version),
  57659. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  57660. TEST_DECL(test_ticket_and_psk_mixing),
  57661. TEST_DECL(test_prioritize_psk),
  57662. TEST_DECL(test_CONF_CTX_FILE),
  57663. TEST_DECL(test_CONF_CTX_CMDLINE),
  57664. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  57665. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  57666. /* wolfcrypt */
  57667. TEST_DECL(test_wolfCrypt_Init),
  57668. TEST_DECL(test_wc_InitMd5),
  57669. TEST_DECL(test_wc_Md5Update),
  57670. TEST_DECL(test_wc_Md5Final),
  57671. TEST_DECL(test_wc_InitSha),
  57672. TEST_DECL(test_wc_ShaUpdate),
  57673. TEST_DECL(test_wc_ShaFinal),
  57674. TEST_DECL(test_wc_InitSha256),
  57675. TEST_DECL(test_wc_Sha256Update),
  57676. TEST_DECL(test_wc_Sha256Final),
  57677. TEST_DECL(test_wc_Sha256FinalRaw),
  57678. TEST_DECL(test_wc_Sha256GetFlags),
  57679. TEST_DECL(test_wc_Sha256Free),
  57680. TEST_DECL(test_wc_Sha256GetHash),
  57681. TEST_DECL(test_wc_Sha256Copy),
  57682. TEST_DECL(test_wc_InitSha512),
  57683. TEST_DECL(test_wc_Sha512Update),
  57684. TEST_DECL(test_wc_Sha512Final),
  57685. TEST_DECL(test_wc_Sha512GetFlags),
  57686. TEST_DECL(test_wc_Sha512FinalRaw),
  57687. TEST_DECL(test_wc_Sha512Free),
  57688. TEST_DECL(test_wc_Sha512GetHash),
  57689. TEST_DECL(test_wc_Sha512Copy),
  57690. TEST_DECL(test_wc_InitSha512_224),
  57691. TEST_DECL(test_wc_Sha512_224Update),
  57692. TEST_DECL(test_wc_Sha512_224Final),
  57693. TEST_DECL(test_wc_Sha512_224GetFlags),
  57694. TEST_DECL(test_wc_Sha512_224FinalRaw),
  57695. TEST_DECL(test_wc_Sha512_224Free),
  57696. TEST_DECL(test_wc_Sha512_224GetHash),
  57697. TEST_DECL(test_wc_Sha512_224Copy),
  57698. TEST_DECL(test_wc_InitSha512_256),
  57699. TEST_DECL(test_wc_Sha512_256Update),
  57700. TEST_DECL(test_wc_Sha512_256Final),
  57701. TEST_DECL(test_wc_Sha512_256GetFlags),
  57702. TEST_DECL(test_wc_Sha512_256FinalRaw),
  57703. TEST_DECL(test_wc_Sha512_256Free),
  57704. TEST_DECL(test_wc_Sha512_256GetHash),
  57705. TEST_DECL(test_wc_Sha512_256Copy),
  57706. TEST_DECL(test_wc_InitSha384),
  57707. TEST_DECL(test_wc_Sha384Update),
  57708. TEST_DECL(test_wc_Sha384Final),
  57709. TEST_DECL(test_wc_Sha384GetFlags),
  57710. TEST_DECL(test_wc_Sha384FinalRaw),
  57711. TEST_DECL(test_wc_Sha384Free),
  57712. TEST_DECL(test_wc_Sha384GetHash),
  57713. TEST_DECL(test_wc_Sha384Copy),
  57714. TEST_DECL(test_wc_InitSha224),
  57715. TEST_DECL(test_wc_Sha224Update),
  57716. TEST_DECL(test_wc_Sha224Final),
  57717. TEST_DECL(test_wc_Sha224SetFlags),
  57718. TEST_DECL(test_wc_Sha224GetFlags),
  57719. TEST_DECL(test_wc_Sha224Free),
  57720. TEST_DECL(test_wc_Sha224GetHash),
  57721. TEST_DECL(test_wc_Sha224Copy),
  57722. TEST_DECL(test_wc_InitBlake2b),
  57723. TEST_DECL(test_wc_InitBlake2b_WithKey),
  57724. TEST_DECL(test_wc_InitBlake2s_WithKey),
  57725. TEST_DECL(test_wc_InitRipeMd),
  57726. TEST_DECL(test_wc_RipeMdUpdate),
  57727. TEST_DECL(test_wc_RipeMdFinal),
  57728. TEST_DECL(test_wc_InitSha3),
  57729. TEST_DECL(testing_wc_Sha3_Update),
  57730. TEST_DECL(test_wc_Sha3_224_Final),
  57731. TEST_DECL(test_wc_Sha3_256_Final),
  57732. TEST_DECL(test_wc_Sha3_384_Final),
  57733. TEST_DECL(test_wc_Sha3_512_Final),
  57734. TEST_DECL(test_wc_Sha3_224_Copy),
  57735. TEST_DECL(test_wc_Sha3_256_Copy),
  57736. TEST_DECL(test_wc_Sha3_384_Copy),
  57737. TEST_DECL(test_wc_Sha3_512_Copy),
  57738. TEST_DECL(test_wc_Sha3_GetFlags),
  57739. TEST_DECL(test_wc_InitShake256),
  57740. TEST_DECL(testing_wc_Shake256_Update),
  57741. TEST_DECL(test_wc_Shake256_Final),
  57742. TEST_DECL(test_wc_Shake256_Copy),
  57743. TEST_DECL(test_wc_Shake256Hash),
  57744. TEST_DECL(test_wc_Md5HmacSetKey),
  57745. TEST_DECL(test_wc_Md5HmacUpdate),
  57746. TEST_DECL(test_wc_Md5HmacFinal),
  57747. TEST_DECL(test_wc_ShaHmacSetKey),
  57748. TEST_DECL(test_wc_ShaHmacUpdate),
  57749. TEST_DECL(test_wc_ShaHmacFinal),
  57750. TEST_DECL(test_wc_Sha224HmacSetKey),
  57751. TEST_DECL(test_wc_Sha224HmacUpdate),
  57752. TEST_DECL(test_wc_Sha224HmacFinal),
  57753. TEST_DECL(test_wc_Sha256HmacSetKey),
  57754. TEST_DECL(test_wc_Sha256HmacUpdate),
  57755. TEST_DECL(test_wc_Sha256HmacFinal),
  57756. TEST_DECL(test_wc_Sha384HmacSetKey),
  57757. TEST_DECL(test_wc_Sha384HmacUpdate),
  57758. TEST_DECL(test_wc_Sha384HmacFinal),
  57759. TEST_DECL(test_wc_HashInit),
  57760. TEST_DECL(test_wc_HashSetFlags),
  57761. TEST_DECL(test_wc_HashGetFlags),
  57762. TEST_DECL(test_wc_InitCmac),
  57763. TEST_DECL(test_wc_CmacUpdate),
  57764. TEST_DECL(test_wc_CmacFinal),
  57765. TEST_DECL(test_wc_AesCmacGenerate),
  57766. TEST_DECL(test_wc_AesGcmStream),
  57767. TEST_DECL(test_wc_Des3_SetIV),
  57768. TEST_DECL(test_wc_Des3_SetKey),
  57769. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  57770. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  57771. TEST_DECL(test_wc_Des3_EcbEncrypt),
  57772. TEST_DECL(test_wc_Chacha_SetKey),
  57773. TEST_DECL(test_wc_Chacha_Process),
  57774. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  57775. TEST_DECL(test_wc_Poly1305SetKey),
  57776. TEST_DECL(test_wc_CamelliaSetKey),
  57777. TEST_DECL(test_wc_CamelliaSetIV),
  57778. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  57779. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  57780. TEST_DECL(test_wc_Arc4SetKey),
  57781. TEST_DECL(test_wc_Arc4Process),
  57782. TEST_DECL(test_wc_Rc2SetKey),
  57783. TEST_DECL(test_wc_Rc2SetIV),
  57784. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  57785. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  57786. TEST_DECL(test_wc_AesSetKey),
  57787. TEST_DECL(test_wc_AesSetIV),
  57788. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  57789. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  57790. TEST_DECL(test_wc_AesGcmSetKey),
  57791. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  57792. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  57793. TEST_DECL(test_wc_GmacSetKey),
  57794. TEST_DECL(test_wc_GmacUpdate),
  57795. TEST_DECL(test_wc_InitRsaKey),
  57796. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  57797. TEST_DECL(test_wc_RsaPublicKeyDecode),
  57798. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  57799. TEST_DECL(test_wc_MakeRsaKey),
  57800. TEST_DECL(test_wc_SetKeyUsage),
  57801. TEST_DECL(test_wc_CheckProbablePrime),
  57802. TEST_DECL(test_wc_RsaPSS_Verify),
  57803. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  57804. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  57805. TEST_DECL(test_wc_SetMutexCb),
  57806. TEST_DECL(test_wc_LockMutex_ex),
  57807. TEST_DECL(test_wc_RsaKeyToDer),
  57808. TEST_DECL(test_wc_RsaKeyToPublicDer),
  57809. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  57810. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  57811. TEST_DECL(test_wc_RsaEncryptSize),
  57812. TEST_DECL(test_wc_RsaSSL_SignVerify),
  57813. TEST_DECL(test_wc_RsaFlattenPublicKey),
  57814. TEST_DECL(test_RsaDecryptBoundsCheck),
  57815. TEST_DECL(test_wc_AesCcmSetKey),
  57816. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  57817. TEST_DECL(test_wc_InitDsaKey),
  57818. TEST_DECL(test_wc_DsaSignVerify),
  57819. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  57820. TEST_DECL(test_wc_MakeDsaKey),
  57821. TEST_DECL(test_wc_DsaKeyToDer),
  57822. TEST_DECL(test_wc_DsaKeyToPublicDer),
  57823. TEST_DECL(test_wc_DsaImportParamsRaw),
  57824. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  57825. TEST_DECL(test_wc_DsaExportParamsRaw),
  57826. TEST_DECL(test_wc_DsaExportKeyRaw),
  57827. TEST_DECL(test_wc_SignatureGetSize_ecc),
  57828. TEST_DECL(test_wc_SignatureGetSize_rsa),
  57829. /*
  57830. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  57831. * avoid memory leaks.
  57832. */
  57833. TEST_DECL(test_wolfCrypt_Cleanup),
  57834. #ifdef OPENSSL_EXTRA
  57835. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  57836. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  57837. TEST_DECL(test_ED25519),
  57838. TEST_DECL(test_ED448),
  57839. #endif
  57840. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  57841. !defined(HAVE_SELFTEST) && \
  57842. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  57843. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  57844. #endif
  57845. #ifdef HAVE_HASHDRBG
  57846. #ifdef TEST_RESEED_INTERVAL
  57847. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  57848. #endif
  57849. TEST_DECL(test_wc_RNG_GenerateBlock),
  57850. #endif
  57851. TEST_DECL(test_get_rand_digit),
  57852. TEST_DECL(test_get_digit_count),
  57853. TEST_DECL(test_mp_cond_copy),
  57854. TEST_DECL(test_mp_rand),
  57855. TEST_DECL(test_get_digit),
  57856. TEST_DECL(test_wc_export_int),
  57857. TEST_DECL(test_wc_InitRngNonce),
  57858. TEST_DECL(test_wc_InitRngNonce_ex),
  57859. TEST_DECL(test_wc_ed25519_make_key),
  57860. TEST_DECL(test_wc_ed25519_init),
  57861. TEST_DECL(test_wc_ed25519_sign_msg),
  57862. TEST_DECL(test_wc_ed25519_import_public),
  57863. TEST_DECL(test_wc_ed25519_import_private_key),
  57864. TEST_DECL(test_wc_ed25519_export),
  57865. TEST_DECL(test_wc_ed25519_size),
  57866. TEST_DECL(test_wc_ed25519_exportKey),
  57867. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  57868. TEST_DECL(test_wc_curve25519_init),
  57869. TEST_DECL(test_wc_curve25519_size),
  57870. TEST_DECL(test_wc_curve25519_export_key_raw),
  57871. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  57872. TEST_DECL(test_wc_curve25519_make_key),
  57873. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  57874. TEST_DECL(test_wc_curve25519_make_pub),
  57875. TEST_DECL(test_wc_curve25519_export_public_ex),
  57876. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  57877. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  57878. TEST_DECL(test_wc_curve25519_import_private),
  57879. TEST_DECL(test_wc_ed448_make_key),
  57880. TEST_DECL(test_wc_ed448_init),
  57881. TEST_DECL(test_wc_ed448_sign_msg),
  57882. TEST_DECL(test_wc_ed448_import_public),
  57883. TEST_DECL(test_wc_ed448_import_private_key),
  57884. TEST_DECL(test_wc_ed448_export),
  57885. TEST_DECL(test_wc_ed448_size),
  57886. TEST_DECL(test_wc_ed448_exportKey),
  57887. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  57888. TEST_DECL(test_wc_curve448_make_key),
  57889. TEST_DECL(test_wc_curve448_shared_secret_ex),
  57890. TEST_DECL(test_wc_curve448_export_public_ex),
  57891. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  57892. TEST_DECL(test_wc_curve448_export_key_raw),
  57893. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  57894. TEST_DECL(test_wc_curve448_import_private),
  57895. TEST_DECL(test_wc_curve448_init),
  57896. TEST_DECL(test_wc_curve448_size),
  57897. TEST_DECL(test_wc_ecc_make_key),
  57898. TEST_DECL(test_wc_ecc_init),
  57899. TEST_DECL(test_wc_ecc_check_key),
  57900. TEST_DECL(test_wc_ecc_get_generator),
  57901. TEST_DECL(test_wc_ecc_size),
  57902. TEST_DECL(test_wc_ecc_params),
  57903. TEST_DECL(test_wc_ecc_signVerify_hash),
  57904. TEST_DECL(test_wc_ecc_shared_secret),
  57905. TEST_DECL(test_wc_ecc_export_x963),
  57906. TEST_DECL(test_wc_ecc_export_x963_ex),
  57907. TEST_DECL(test_wc_ecc_import_x963),
  57908. TEST_DECL(ecc_import_private_key),
  57909. TEST_DECL(test_wc_ecc_export_private_only),
  57910. TEST_DECL(test_wc_ecc_rs_to_sig),
  57911. TEST_DECL(test_wc_ecc_import_raw),
  57912. TEST_DECL(test_wc_ecc_import_unsigned),
  57913. TEST_DECL(test_wc_ecc_sig_size),
  57914. TEST_DECL(test_wc_ecc_ctx_new),
  57915. TEST_DECL(test_wc_ecc_ctx_reset),
  57916. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  57917. TEST_DECL(test_wc_ecc_ctx_set_info),
  57918. TEST_DECL(test_wc_ecc_encryptDecrypt),
  57919. TEST_DECL(test_wc_ecc_del_point),
  57920. TEST_DECL(test_wc_ecc_pointFns),
  57921. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  57922. TEST_DECL(test_wc_ecc_verify_hash_ex),
  57923. TEST_DECL(test_wc_ecc_mulmod),
  57924. TEST_DECL(test_wc_ecc_is_valid_idx),
  57925. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  57926. TEST_DECL(test_wc_ecc_sig_size_calc),
  57927. TEST_DECL(test_ToTraditional),
  57928. TEST_DECL(test_wc_EccPrivateKeyToDer),
  57929. TEST_DECL(test_wc_DhPublicKeyDecode),
  57930. TEST_DECL(test_wc_Ed25519KeyToDer),
  57931. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  57932. TEST_DECL(test_wc_Ed448KeyToDer),
  57933. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  57934. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  57935. TEST_DECL(test_wc_SetSubjectBuffer),
  57936. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  57937. TEST_DECL(test_wc_PKCS7_New),
  57938. TEST_DECL(test_wc_PKCS7_Init),
  57939. TEST_DECL(test_wc_PKCS7_InitWithCert),
  57940. TEST_DECL(test_wc_PKCS7_EncodeData),
  57941. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  57942. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  57943. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  57944. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  57945. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  57946. TEST_DECL(test_wc_PKCS7_Degenerate),
  57947. TEST_DECL(test_wc_PKCS7_BER),
  57948. TEST_DECL(test_PKCS7_signed_enveloped),
  57949. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  57950. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  57951. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  57952. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  57953. TEST_DECL(test_wc_i2d_PKCS12),
  57954. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  57955. TEST_DECL(test_openssl_FIPS_drbg),
  57956. TEST_DECL(test_wc_CryptoCb),
  57957. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  57958. TEST_DECL(test_wolfSSL_FIPS_mode),
  57959. #ifdef WOLFSSL_DTLS
  57960. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  57961. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  57962. #endif
  57963. #if !defined(NO_FILESYSTEM) && \
  57964. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57965. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57966. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  57967. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  57968. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57969. #ifdef HAVE_MAX_FRAGMENT
  57970. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  57971. #endif /* HAVE_MAX_FRAGMENT */
  57972. TEST_DECL(test_wolfSSL_dtls_stateless2),
  57973. #if !defined(NO_OLD_TLS)
  57974. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  57975. #endif /* !defined(NO_OLD_TLS) */
  57976. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57977. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  57978. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  57979. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  57980. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  57981. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  57982. TEST_DECL(test_ForceZero),
  57983. TEST_DECL(test_wolfSSL_Cleanup),
  57984. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57985. && defined(WOLFSSL_TLS13) && \
  57986. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  57987. TEST_DECL(test_ticket_nonce_malloc),
  57988. #endif
  57989. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  57990. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57991. !defined(WOLFSSL_NO_CLIENT_AUTH))
  57992. TEST_DECL(test_various_pathlen_chains),
  57993. #endif
  57994. TEST_DECL(test_ticket_ret_create),
  57995. TEST_DECL(test_extra_alerts_wrong_cs),
  57996. TEST_DECL(test_extra_alerts_skip_hs),
  57997. TEST_DECL(test_extra_alerts_bad_psk),
  57998. TEST_DECL(test_harden_no_secure_renegotiation),
  57999. TEST_DECL(test_override_alt_cert_chain),
  58000. /* If at some point a stub get implemented this test should fail indicating
  58001. * a need to implement a new test case
  58002. */
  58003. TEST_DECL(test_stubs_are_stubs)
  58004. };
  58005. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  58006. static void TestSetup(void)
  58007. {
  58008. /* Stub, for now. Add common test setup code here. */
  58009. }
  58010. static void TestCleanup(void)
  58011. {
  58012. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  58013. /* Clear any errors added to the error queue during the test run. */
  58014. wolfSSL_ERR_clear_error();
  58015. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  58016. }
  58017. /* Print out all API test cases with numeric identifier.
  58018. */
  58019. void ApiTest_PrintTestCases(void)
  58020. {
  58021. int i;
  58022. printf("All Test Cases:\n");
  58023. for (i = 0; i < TEST_CASE_CNT; i++) {
  58024. printf("%3d: %s\n", i + 1, testCases[i].name);
  58025. }
  58026. }
  58027. /* Add test case with index to the list to run.
  58028. *
  58029. * @param [in] idx Index of test case to run starting at 1.
  58030. * @return 0 on success.
  58031. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  58032. */
  58033. int ApiTest_RunIdx(int idx)
  58034. {
  58035. if (idx < 1 || idx > TEST_CASE_CNT) {
  58036. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  58037. return BAD_FUNC_ARG;
  58038. }
  58039. testAll = 0;
  58040. testCases[idx-1].run = 1;
  58041. return 0;
  58042. }
  58043. /* Add test case with name to the list to run.
  58044. *
  58045. * @param [in] name Name of test case to run.
  58046. * @return 0 on success.
  58047. * @return BAD_FUNC_ARG when name is not a known test case name.
  58048. */
  58049. int ApiTest_RunName(char* name)
  58050. {
  58051. int i;
  58052. for (i = 0; i < TEST_CASE_CNT; i++) {
  58053. if (XSTRCMP(testCases[i].name, name) == 0) {
  58054. testAll = 0;
  58055. testCases[i].run = 1;
  58056. return 0;
  58057. }
  58058. }
  58059. printf("Test case name not found: %s\n", name);
  58060. printf("Use --list to see all test case names.\n");
  58061. return BAD_FUNC_ARG;
  58062. }
  58063. /* Converts the result code to a string.
  58064. *
  58065. * @param [in] res Test result code.
  58066. * @return String describing test result.
  58067. */
  58068. static const char* apitest_res_string(int res)
  58069. {
  58070. const char* str = "invalid result";
  58071. switch (res) {
  58072. case TEST_SUCCESS:
  58073. str = "passed";
  58074. break;
  58075. case TEST_FAIL:
  58076. str = "failed";
  58077. break;
  58078. case TEST_SKIPPED:
  58079. str = "skipped";
  58080. break;
  58081. }
  58082. return str;
  58083. }
  58084. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58085. static double gettime_secs(void)
  58086. {
  58087. struct timeval tv;
  58088. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  58089. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  58090. }
  58091. #endif
  58092. void ApiTest(void)
  58093. {
  58094. int i;
  58095. int ret;
  58096. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58097. double timeDiff;
  58098. #endif
  58099. printf(" Begin API Tests\n");
  58100. fflush(stdout);
  58101. /* we must perform init and cleanup if not all tests are running */
  58102. if (!testAll) {
  58103. #ifdef WOLFCRYPT_ONLY
  58104. wolfCrypt_Init();
  58105. #else
  58106. wolfSSL_Init();
  58107. #endif
  58108. }
  58109. for (i = 0; i < TEST_CASE_CNT; ++i) {
  58110. /* When not testing all cases then skip if not marked for running. */
  58111. if (!testAll && !testCases[i].run) {
  58112. continue;
  58113. }
  58114. TestSetup();
  58115. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  58116. fflush(stdout);
  58117. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58118. timeDiff = gettime_secs();
  58119. #endif
  58120. ret = testCases[i].func();
  58121. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58122. timeDiff = gettime_secs() - timeDiff;
  58123. #endif
  58124. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58125. if (ret != TEST_SKIPPED) {
  58126. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  58127. }
  58128. else
  58129. #endif
  58130. {
  58131. printf(" %s\n", apitest_res_string(ret));
  58132. }
  58133. fflush(stdout);
  58134. /* if return code is < 0 and not skipped then assert error */
  58135. Assert((ret > 0 || ret == TEST_SKIPPED),
  58136. ("Test failed\n"),
  58137. ("ret %d", ret));
  58138. TestCleanup();
  58139. }
  58140. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  58141. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  58142. wc_ecc_fp_free(); /* free per thread cache */
  58143. #endif
  58144. if (!testAll) {
  58145. #ifdef WOLFCRYPT_ONLY
  58146. wolfCrypt_Cleanup();
  58147. #else
  58148. wolfSSL_Cleanup();
  58149. #endif
  58150. }
  58151. (void)testDevId;
  58152. printf(" End API Tests\n");
  58153. fflush(stdout);
  58154. }